3

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

我阅读了有关Types and Configurations的 LiveChart 文档,并尝试将索引 + 1 的映射器放入 SeriesCollection,但出现无效参数错误:无法从 'LiveCharts.Configurations.CartesianMapper' 转换为 '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
};

在此处输入图像描述

4

1 回答 1

4

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

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

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);

这个例子来自这里

于 2017-08-28T06:07:49.440 回答