After modifying the code provided on the website's documentation, I have had trouble displaying the SeriesCollection. Let me show you what I have done.
Firstly, the XAML:
<Grid>
<lvc:CartesianChart Series="{Binding GraphData.SeriesCollection}"
LegendLocation="Top">
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="VIs"></lvc:Axis>
</lvc:CartesianChart.AxisY>
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Players"></lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
In my ViewModel, I have a custom object storing information I wish to bind to this chart as:
public GraphModel GraphData { get; set; } = new GraphModel();
I have a button to test my code. Clicking it executes the following code:
private void UpdateGraphStatistics()
{
var compdata = new List<double>();
var dqdata = new List<double>();
foreach (var item in Competitors)
{
if (!item.DQ)
{
compdata.Add(item.VIs);
}
else if (item.DQ)
{
dqdata.Add(item.VIs);
}
}
GraphData = new GraphModel(compdata, dqdata);
}
For all intents and purposes, this above code adds to two lists a series of doubles, which I have determined is working (so this isn't the issue; the SeriesCollection isn't empty!)
Next, the GraphModel. This is the big one, where the issue likely lies, but I cannot determine where:
class GraphModel
{
public ChartValues<ObservablePoint> CompetitionData = new ChartValues<ObservablePoint>();
public ChartValues<ObservablePoint> DQData = new ChartValues<ObservablePoint>();
public SeriesCollection SeriesCollection { get; set; }
public GraphModel()
{
CreateSeriesCollection();
}
public GraphModel(List<double> competitionData, List<double> dqData)
{
ParseData(competitionData, dqData);
CreateSeriesCollection();
}
private void CreateSeriesCollection()
{
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Competition Data (VIs/Player Number)",
Values = CompetitionData,
LineSmoothness = 0.6,
PointForeground = Brushes.Blue
},
new LineSeries
{
Title = "DQ Data (VIs/Player Number)",
Values = DQData,
LineSmoothness = 1,
PointForeground = Brushes.Red
}
};
}
private void ParseData(List<double> compData, List<double> dqData)
{
// Convert competitionData into observable points
int count = 1;
foreach (var item in compData)
{
CompetitionData.Add(new ObservablePoint(count++, item));
}
// Convert dqdata into observable points
int offsetX = CompetitionData.Count;
foreach (var item in dqData)
{
DQData.Add(new ObservablePoint(offsetX++, item));
}
}
}
The method ParseData()
has been used before in non-WPF code, so I know that is unlikely the cause for issue.
I still haven't solved what is going wrong. Am I not binding my data correctly?
Edit
For context, I know the SeriesCollection within the GraphModel
is definitely containing the information I want it too, as it should since this code is more or less the same from a non-WPF version of the software I initially trialled in.