C#編程方式使用Silverlight toolkit Chart

來源:互聯網
上載者:User

一、基礎介紹

Silverlight ToolKit是微軟發布的基於Microsoft-Public License(MS-PL)許可協議的控制項集。MS-PL許可協議允許商業或非商業的發布,所以我們可以很方便地將該ToolKit應用於Silverlight 項目。

要使用Silverlight ToolKit:首先,您需要從http://www.codeplex.com/Silverlight下載最新的Dll檔案或者原始碼;然後在您的silverlight項目中添加引用;最後,您就可以建立ToolKit中提供的控制項了。

二、使用Chart控制項

採用XAML語言或者XAML+代碼方式使用Chart控制項的樣本較為普遍,本文將簡單介紹如何採用代碼編程方式使用Chart。

Chart是Silverlight ToolKit中用於圖表化展現資料的控制項,位於Microsoft.Windows.Controls.DataVisualzation.Charting assembly。

使用Chart控制項時:

首先,添加對Microsoft.Windows.Controls.DataVisualzation.Charting 名稱空間的引用。

Code
using Microsoft.Windows.Controls.DataVisualization.Charting;

 

其次,建立Chart控制項,並設定外觀屬性;

Code
            Chart chart = new Chart();
            chart.SetValue(Grid.RowProperty, 1);
            this.LayoutRoot.Children.Add(chart);

 

第三步、建立DynamicSeries資料,Silverlight ToolKit中的Chart可以表現棒圖(BarSeries)、柱狀圖(ColumnSeries)、點圖(ScatterSeries)和折線圖(LineSeries),在使用這些圖時必須首先建立相對應的Series。

Code
                PieSeries ps = new PieSeries();
                ps.ItemsSource = new int[] { 1, 2, 30, 50 };

 

最後、將建立的DynamicSeries添加入Chart的ItemSource。

Code
                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必須是可以比較的量。

Code
                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);

上面代碼的運行結果: 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.