I have a line series chart called “lineSeries1”.
I would like that chart to be filled and updated during runtime, depending of two variables. “currentPos” would be the X axis, and “budget” would be the Y axis. Also, “budget” is possible to change value during runtime.
Basically, I would just like to know how to set values in the chart, programmatically. For example, whenever a new currentPos/budget is created, or when an existing budget is modified.
How can I pull that off in Silverlight C#? there is very little documentation on that online…
EDIT: after some research, I came upon this tutorial: http://www.a2zdotnet.com/View.aspx?Id=136. With some inspiration from that site, I came up with my own code, which doesn’t work…
Here’s my XAML:
<toolkit:Chart x:Name="theChart">
<toolkit:LineSeries x:Name="lineSeries1" DependentValuePath="CurrentPos" IndependentValuePath="Budget"></toolkit:LineSeries>
</toolkit:Chart>
Here’s my class:
public class oneEvent
{
private int _CurrentPos;
private int _Budget;
public oneEvent(int currentPos, int budget)
{
_CurrentPos = currentPos;
_Budget = budget;
}
public int CurrentPos
{
get { return _CurrentPos; }
set { _CurrentPos = value; }
}
public int Budget
{
get { return _Budget; }
set { _Budget = value; }
}
}
and here’s my code which populates the lines chart:
public MainPage()
{
InitializeComponent();
List<oneEvent> list = new List<oneEvent>();
list.Add(new oneEvent(0, 8000));
list.Add(new oneEvent(1, 9000));
list.Add(new oneEvent(2, 10000));
list.Add(new oneEvent(3, 11000));
list.Add(new oneEvent(4, 12000));
list.Add(new oneEvent(5, 9000));
list.Add(new oneEvent(6, 500));
list.Add(new oneEvent(7, 1000));
try
{
lineSeries1.ItemsSource = list;
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
}
When I run this, I get a message box saying “object reference not set to an instance of an object”. What am I doing wrong?
EDIT:
ok, figured out my problem.
Instead of writing lineSeries1.ItemsSource = list;,
I should’ve written LineSeries lineseries = theChart.Series[0] as LineSeries;
lineseries.ItemsSource = list;
I don’t know why, but it works…
ok, figured out my problem. Instead of writing
lineSeries1.ItemsSource = list;, I should’ve written
LineSeries lineseries = theChart.Series[0] as LineSeries;lineseries.ItemsSource = list;
I don’t know why, but it works…