微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

} //结束

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐