Silverlight动态创建Chart

来源:互联网 发布:ubuntu fedora 比较 编辑:程序博客网 时间:2024/04/28 08:21

1. Home.xaml文件中,加入Chart控件

<chartingToolkit:Chart x:Name="TestChart" Background= "AliceBlue"  Height="600">

2. Home.xaml.cs中,写如下代码:(以AreaChart为例,前提必须先有一个List类型的数据源,怎样添加数据源,参看前一篇Blog

//绑定数据源到图标

private void BindListToChart(List<ProductionData> list)

{

                //设置图名称,TestChart是前台控件的X:Name属性设置的,必须一致

                TestChart.Title = "石油天然气产量、销量、消耗图";
                //
创建X轴,这个方法是从网上Copy的,其实直接写成创建Y轴的方式就行了,更简单易懂

                //X轴可以创建成DataTimeAxis, CategoryAxis等等类型,显示的形式不同,可参考自带Document
                Action<Chart> chartModifier = (chart) =>
                {
                    DateTimeAxis XAxis = new DateTimeAxis { Orientation = AxisOrientation.X, Title = "
月份", FontStyle = FontStyles.Normal, FontSize = 8 };
                    XAxis.IntervalType = DateTimeIntervalType.Months;
                    XAxis.Interval = 1;
                    XAxis.ShowGridLines = true;

                    TestChart.Axes.Add(XAxis);                 
                };

                chartModifier(TestChart);      

                       
                //
创建Y1
                LinearAxis YAxis1 = new LinearAxis { Orientation = AxisOrientation.Y, Location = AxisLocation.Right, Title = "
天然气香港销量", ShowGridLines = false };
                YAxis1.Orientation = AxisOrientation.Y;                
                TestChart.Axes.Add(YAxis1);


                //
创建数据系列,并绑定数据源
                AreaSeries areaSeries1 = new AreaSeries();
                areaSeries1.Title = "
香港";  //图例显示的文字
                areaSeries1.ItemsSource = list;  //
数据源绑定
                areaSeries1.IndependentValueBinding = new System.Windows.Data.Binding("DATE"); //
绑定字段
                areaSeries1.DependentValueBinding = new System.Windows.Data.Binding("GAS_HKT_SALES");
                
                areaSeries1.DependentRangeAxis = YAxis1;

                TestChart.Series.Add(areaSeries1);

} //结束

 

原创粉丝点击