1、基礎介紹編程
Silverlight ToolKit是微軟發佈的基於Microsoft-Public License(MS-PL)許可協議的控件集。MS-PL許可協議容許商業或非商業的發佈,因此咱們能夠很方便地將該ToolKit應用於Silverlight 項目。要使用Silverlight ToolKit:首先,您須要從http://www.codeplex.com/Silverlight下載最新的Dll文件或者源代碼;而後在您的silverlight項目中添加引用;最後,您就能夠建立ToolKit中提供的控件了this
2、使用Chart控件spa
採用XAML語言或者XAML+代碼方式使用Chart控件的示例較爲廣泛,本文將簡單介紹如何採用代碼編程方式使用Chart.Chart是Silverlight ToolKit中用於圖表化展示數據的控件,位於Microsoft.Windows.Controls.DataVisualzation.Chartingassembly。code
使用Chart控件時:ip
首先,添加對Microsoft.Windows.Controls.DataVisualzation.Charting名稱空間的引用。get
using Microsoft.Windows.Controls.DataVisualization.Charting;
其次,建立Chart控件,並設置外觀屬性;it
Chart chart = new Chart();
chart.SetValue(Grid.RowProperty, 1);
this.LayoutRoot.Children.Add(chart);
io
第 三步、建立DynamicSeries數據,Silverlight ToolKit中的Chart能夠表現棒圖(BarSeries)、柱狀圖(ColumnSeries)、點圖(ScatterSeries)和折線圖 (LineSeries),在使用這些圖時必須首先建立相對應的Series。class
PieSeries ps = new PieSeries();
ps.ItemsSource = new int[] { 1, 2, 30, 50 };
基礎
最後、將建立的DynamicSeries添加入Chart的ItemSource。
chart.Series.Add(ps);
在項目中使用時,數據這塊應該會稍微複雜一點,咱們首先須要明白的兩個概念:
IndependentValue 和 DependentValue
IndependentValue 和 DependentValue分別經過IndependentValueBinding和DependentValueBinding屬性綁定。 IndependentValue表示您須要考察的量的名稱,而DependentValue表示每一個IndependentValue的數量,例如上面 圖例中,{1,2,3,4}就是IndependentValue,而對應的DependentValue爲{1,2,30,50}。
使用Chart中還須要注意的是:
LineChart和ScaterChart對應的數據LineSeries.IndepdenValue 和ScatterSeries.IndepdenValue必須是能夠比較的量.
LineSeries lineSeries = new LineSeries();
System.Windows.Data.Binding keyBinding = new System.Windows.Data.Binding();
keyBinding.Path = new PropertyPath("Key");
lineSeries.IndependentValueBinding = keyBinding;
System.Windows.Data.Binding valueBinding = new System.Windows.Data.Binding();
valueBinding.Path = new PropertyPath("Value");
lineSeries.DependentValueBinding = valueBinding;
lineSeries.ItemsSource = new KeyValuePair<DateTime, int>[] {
new KeyValuePair<DateTime, int>(DateTime.Now, 9),
new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(1), 8),
new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(3), 6),
new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(2), 9),
new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(4), 8) }; chart.Series.Add(lineSeries);