在 LiveChart 中自定义起始索引

Customize starting index in LiveChart

我正在尝试使用 LiveChart 绘制一个简单的 LineSeries。因为 computer/array 索引默认从 0 开始,而人类(非程序员)从 1 开始计数,所以我喜欢显示从 1 开始的值的索引(即索引+1),但不知道如何这样做。

我阅读了关于 Types and Configurations 的 LiveChart 文档,并试图将索引 + 1 的映射器获取到 SeriesCollection 中,但我收到无效参数错误:无法从 [=21= 转换] 到 'LiveCharts.Definitions.Series.ISeriesView'

var mapper1 = new CartesianMapper<double>()
        .X((value, index) => index + 1) 
        .Y((value, index) => value); 

sc = new SeriesCollection
{
    new LineSeries
    {
        Values = new ChartValues<double>()  {1,2,3,4,1,2,3,4,1,2},
    },
    mapper1
};

我能回答这个问题只是因为我不得不自己修改 LiveCharts,而不是因为我从他们的文档中得到它(尽管我确实发现它嵌入了 here

如果你想为一个系列专门设置一个映射器,你可以将它添加到声明中,如下所示:

var mapper1 = new CartesianMapper<double>()
        .X((value, index) => index + 1) 
        .Y((value, index) => value); 

sc = new SeriesCollection(mapper1)
{
    new LineSeries
    {
        Values = new ChartValues<double>()  {1,2,3,4,1,2,3,4,1,2},
    }
};

或者,有一种方法可以为特定数据类型设置全局映射器,例如如果您使用 MeasureModel:

var mapper = Mappers.Xy<MeasureModel>()
            .X(model => model.DateTime.Ticks)   //use DateTime.Ticks as X
            .Y(model => model.Value);           //use the value property as Y

//lets save the mapper globally.
Charting.For<MeasureModel>(mapper);

该示例来自 here.