2017-08-28 102 views
2

我想用LiveChart绘制一个简单的LineSeries。因为默认情况下计算机/数组索引从0开始,并且人类(非程序员)从1开始计数,所以我喜欢显示从1开始的值索引(即索引+ 1),但无法弄清楚如何执行此操作。在LiveChart中自定义起始索引

我读了LiveChart文档上Types and Configurations,并试图让指数+ 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 
}; 

enter image description here

+2

该文档对于特定问题是相当无用的,但由于我已经使用LiveCharts进行了很多修改,因此我可以提供一些建议。你可以试试'sc = new SeriesCollection(mapper1){...}'来设置映射器,而不是你的代码。 –

+1

@KeyurPATEL,耶稣基督它的作品。如果它告诉我要通过映射器来重载构造函数,这会节省我数小时的试用时间......或者我可能只是忽略了文档。请回答,以便我可以选择你的答案。 – KMC

+0

“SeriesCollection”中还有一个名为'Configuration'的属性,可用于设置映射器。这可以随时设置,并且将使用映射器。 –

回答

3

我可以回答这个问题,只是因为我曾与LiveCharts鼓捣自己,不是因为我是从他们的文档(虽然我没有找到它的嵌入式here

如果要专门设置一个映射为一个系列,您可以在如下添加到delcaration:

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