Was trying to plot a chart on UWP mobile app using Visual Studio. On the .cs file, the following are the codes;
The class declaration is:
public class Average
{
public string Date { get; set; }
public int DailyAverageForChart { get; set; }
}
public viewChart()
{
this.InitializeComponent();
List<Average> listOfAverage = new List<Average>();
listOfAverage.Add(new Average { Date = "1", DailyAverageForChart = 20 });
listOfAverage.Add(new Average { Date = "2", DailyAverageForChart = 30 });
listOfAverage.Add(new Average { Date = "3", DailyAverageForChart = 10 });
(ColumnChart.Series[0] as ColumnSeries).ItemsSource = listOfAverage;
}
On the .xaml file, has the following codes;
<Grid>
<Charting:Chart x:Name="ColumnChart"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="450"
Width="314"
Margin="0,0,0,0"/>
<Charting:ColumnSeries Title="Average Consumption"
Margin="0"
IndependentValuePath="Date"
DependentValuePath="DailyAverageForChart"
IsSelectionEnabled="True"/>
</Grid>
No compilation error, however when come to this .xaml file during runtime, the following error pointing at the statement (ColumnChart.Series[0] as ColumnSeries).ItemsSource = listOfAverage;
Error message is :
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'
Please advise. Thank you very much.