2017-08-28 135 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