Recently you need to display the curve in a WPF program, feeling SPARROW.CHART.WPF control is good (http://sparrowtoolkit.codeplex.com/), a fully open source control support, can be downloaded through NuGet. The examples in the source code are fully implemented in MVVM, and the requirement in my project is to create the control entirely through code, binding the data through the code, so it's simple to implement a basic example:
First, top a CPU class (from Sparrow source code):
Public classCPU {PrivateDateTime time; PublicDateTime Time {Get{returnTime ;} Set{time =value;} } Private Doublepercentage; Public DoublePercentage {Get{returnpercentage;} Set{percentage =value;} } Private DoubleMemoryusage; Public DoubleMemoryusage {Get{returnMemoryusage;} Set{memoryusage =value;} } PublicCPU () {} PublicCPU (DateTime time,DoublePercentageDoublememoryusage) { This. Time =Time ; This. Percentage =percentage; This. Memoryusage =Memoryusage; }
The random data is then generated by a function:
Private voidCreatesomedata () {DateTime dt=DateTime.Now; System.Random rad=NewRandom (System.DateTime.Now.Millisecond); for(intn=0;n< -; n++) {DT=dt. AddSeconds (1); CPU CPU=NewCPU (Dt,rad. Next ( -),Wuyi); M_listcpu.add (CPU); } }
Finally, the chart control is dynamically created by code:
Createsomedata (); //Form Load EventsSparrowchart chart =NewSparrowchart (); Datetimexaxis Xaxis=NewDatetimexaxis (); Xaxis. Interval="0:0:1"; Chart. Xaxis=Xaxis; Xaxis. Visibility=visibility.collapsed; Linearyaxis YAxis=NewLinearyaxis (); Chart. YAxis=YAxis; YAxis. MinValue=0; YAxis. MaxValue= -; Lineseries ser=Newlineseries (); Chart. Series.add (Ser); Ser. XPath=" Time"; Ser. Ypath="Percentage"; Ser. Pointssource=m_listcpu; ROOTLAYOUT.CHILDREN.ADD (chart);
The program runs as follows:
Dynamic invocation of the SPARROW.CHART.WPF control