Silverlight动态创建Chart

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

} //结束

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值