ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#编程方式使用Silverlight toolkit Chart

C#编程方式使用Silverlight toolkit Chart

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-04 15:58:15 0 删除 编辑

一、基础介绍

Silverlight ToolKit是微软发布的基于许可协议的控件集。MS-PL许可协议允许商业或非商业的发布,所以我们可以很方便地将该ToolKit应用于Silverlight 项目。

要使用Silverlight ToolKit:首先,您需要从下载最新的Dll文件或者源代码;然后在您的silverlight项目中添加引用;最后,您就可以创建ToolKit中提供的控件了。

二、使用Chart控件

采用XAML语言或者XAML+代码方式使用Chart控件的示例较为普遍,本文将简单介绍如何采用代码编程方式使用Chart。

Chart是Silverlight ToolKit中用于图表化展现数据的控件,位于Microsoft.Windows.Controls.DataVisualzation.Charting assembly。

使用Chart控件时:

首先,添加对Microsoft.Windows.Controls.DataVisualzation.Charting 名称空间的引用。


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)


-->using Microsoft.Windows.Controls.DataVisualization.Charting;

 

其次,创建Chart控件,并设置外观属性;


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)


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

 

第三步、创建DynamicSeries数据,Silverlight ToolKit中的Chart可以表现棒图(BarSeries)、柱状图(ColumnSeries)、点图(ScatterSeries)和折线图(LineSeries),在使用这些图时必须首先创建相对应的Series。


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)


-->                PieSeries ps = new PieSeries();
                ps.ItemsSource 
= new int[] { 123050 };

 

最后、将创建的DynamicSeries添加入Chart的ItemSource。


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)


-->                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 highlighting produced by Actipro CodeHighlighter (freeware)


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

上面代码的运行结果: 

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-526513/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5603932