c# - WPF Graph change when calendar date changes -
i have made wpf graph pulls data database. need data automatically change on date change.
here code:
xaml code:
<dvc:chart name="calllogs" background="steelblue" margin="136,0,0,0" title="calls per hour" borderbrush="transparent"> <dvc:chart.legendstyle> <style targettype="control"> <setter property="width" value="0"/> <setter property="height" value="0"/> </style> </dvc:chart.legendstyle> <dvc:chart.axes> <dvc:linearaxis orientation="y" title="ammount of calls" showgridlines="true"/> </dvc:chart.axes> <dvc:chart.series> <dvc:columnseries title="calls per hour" independentvaluebinding="{binding path=key}" dependentvaluebinding="{binding path=value}"> </dvc:columnseries> </dvc:chart.series> </dvc:chart> <datepicker x:name="datepicker1" horizontalalignment="left" margin="10,21,0,0" verticalalignment="top" width="126" borderbrush="transparent" selecteddateformat="short"/>
c# code:
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.shapes; using system.data; using system.data.sqlclient; using system.xml; using system.windows.controls.datavisualization; using system.windows.controls.primitives; using system.windows.controls.datavisualization.charting; namespace mainwindow { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); loadcolumnchartdata(); } private void loadcolumnchartdata() { datepicker1.selecteddate = datetime.today; string pickdate = datepicker1.selecteddate.value.tostring("yyyy-mm-dd"); int[] callcounter = new int[100]; int[] callhour = new int[100]; int counter = 0; sqlconnection sqlconnection1 = new sqlconnection("server=nl-reportserver;database=realconnect;user id=sa;password=<password>"); sqlcommand cmd = new sqlcommand(); sqldatareader reader; cmd.parameters.add("@pickdate", pickdate); cmd.commandtext = "select * rc_callsperhour convert(date,call_logdate,120) = convert(date,@pickdate,120) order callhour"; cmd.commandtype = commandtype.text; cmd.connection = sqlconnection1; sqlconnection1.open(); reader = cmd.executereader(); if (reader.hasrows) { while (reader.read()) { callhour[counter] = reader.getint32(2); callcounter[counter] = reader.getint32(3); counter++; } } else { messagebox.show("no rows found."); } reader.close(); sqlconnection1.close(); ((columnseries)calllogs.series[0]).itemssource = enumerable.range(0, counter).select(i => new keyvaluepair<int, int>(callhour[i], callcounter[i])).toarray(); } } }
please note have removed password sql connection security purposes.
how go doing this?
you can use wpf binding getting data. first need create observablecollection<yourdatamodel>
date changed set model class property:
public class yourdatamodel { public property int callhour { get;set; } public property int callcounter { get;set; } public yourdatamodel(callhour int, callcounter int) { callhour = callhour; callcounter = callcounter; } } public partial class mainwindow : window { private observablecollection<yourdatamodel> datamodel; public mainwindow() { initializecomponent(); datamodel = new observablecollection<yourdatamodel>(); //set itemsource property or can bind xaml //which name columnseries x:name=callsperhour callsperhour.itemssource = datamodel; } //when date change running function private void gettingdatafromsql() { ... while (reader.read()) { yourdatamodel data = new yourdatamodel( reader.getint32(2), reader.getint32(3)); datamodel.add(data); } } }
xaml:
<dvc:chart.series> <dvc:columnseries title="calls per hour" independentvaluebinding="{binding callhour, mode=oneway}" dependentvaluebinding="{binding callcounter, mode=oneway}"> </dvc:columnseries> </dvc:chart.series>
Comments
Post a Comment