How to update the datasource of a Chart
hello.
i have chart on a usercontrol. (call chartcontrol) i have user control on header. ( call header control) have header on usercontrol. (mainuc)
when on mainuc want data on chartcontrol updated.
here i'm trying do...
on chartcontrol have exposed property
observablecollection
<datastuff> dataroot = new observablecollection<datastuff> { new datastuff{ date = datetime
.now, totalvalue=10} };
public observablecollection<datastuff
> bindingdata {
get { return
dataroot; }
set { dataroot = value
; }
}
public smalllinegraph() {
initializecomponent();
//buildsampleset();
datetimeaxis oxaxes = (datetimeaxis)totalchart.axes[0];
oxaxes.minimum = dataroot.orderby(c => c.date).take(1).singleordefault().date;
oxaxes.maximum = dataroot.orderbydescending(c => c.date).take(1).singleordefault().date;
linearaxis oyaxes = (linearaxis)totalchart.axes[1];
oyaxes.minimum = (double?)dataroot.orderby(c => c.totalvalue).take(1).singleordefault().totalvalue;
oyaxes.maximum = (double?)dataroot.orderbydescending(c => c.totalvalue).take(1).singleordefault().totalvalue;
totalchart.datacontext = dataroot;
}
public class datastuff : inotifypropertychanged {
private datetime mdate;
private decimal mtotalvalue;
public datetime date {
{ return mdate; }
set {
mdate = value;
this.notifypropertychanged("date");
}
}
public decimal totalvalue {
{ return mtotalvalue; }
set {
mtotalvalue = value;
this.notifypropertychanged("totalvalue");
}
}
public event propertychangedeventhandler propertychanged;
public void notifypropertychanged(string propertyname) {
if (this.propertychanged != null) {
this.propertychanged(this, new propertychangedeventargs(propertyname));
}
}
}
then on mainuc reference headeruc (salesheadingtotals) , try change data so.
salesheadingtotals.smallgraphtoday.bindingdata = buildsampleset();
observablecollection
<datastuff
> buildsampleset() {
observablecollection<datastuff> dataroot = new observablecollection<datastuff
>();
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 9, 0, 0), totalvalue = 0 });
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 9, 30, 0), totalvalue = 20 });
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 10, 0, 0), totalvalue = 50 });
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 14, 0, 0), totalvalue = 51 });
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 16, 0, 0), totalvalue = 52 });
dataroot.add(
new datastuff { date = new datetime
(2010, 1, 1, 18, 0, 0), totalvalue = 100 });
return
dataroot;
}
this not working. nothing displaying on chart. seems data set when chart initialized displayed.
so bascially want know how "re-bind" chart new datasource?
thanks,
hi,
you can set binding of usercontrol chart in mainuc code behind directly.
here sample code in mainuc
binding binding = new binding();
observablecollection<datastuff> dataroot = new observablecollection<datastuff>();
dataroot.add(new datastuff { date = new datetime(2010, 1, 1, 9, 0, 0), totalvalue = 0 });
dataroot.add(new datastuff { date = new datetime(2010, 1, 1, 9, 30, 0), totalvalue = 20 });
dataroot.add(new datastuff { date = new datetime(2010, 1, 1, 10, 0, 0), totalvalue = 50 });
binding.source = dataroot;
lineseries ls = (lineseries) sc.totalchart.series[0]; //sc is usercontrol name defined in mainuc
ls.setbinding(lineseries.itemssourceproperty,binding);
http://msdn.microsoft.com/en-us/library/cc838207(v=vs.95).aspx
hope helps
Silverlight > Silverlight Controls and Silverlight Toolkit
Comments
Post a Comment