WPF、Silverlight项目中使用柱状图、饼状图、折线图

来源:互联网 发布:做生意的沟通软件 编辑:程序博客网 时间:2024/05/14 21:00

参考:http://www.cnblogs.com/sunyjie/p/3410851.html

  private List<DateTime> LsTime = new List<DateTime>()            {              new DateTime(2017,1,1,0,1,1),              new DateTime(2017,1,1,1,1,1),             new DateTime(2017,1,1,2,1,1),              new DateTime(2017,1,1,3,1,1),             new DateTime(2017,1,1,4,1,1),              new DateTime(2017,1,1,5,1,1),            new DateTime(2017,1,1,6,1,1),            new DateTime(2017,1,1,7,1,1),            new DateTime(2017,1,1,8,1,1),          new DateTime(2017,1,1,9,1,1),            new DateTime(2017,1,1,10,1,1),            new DateTime(2017,1,1,11,1,1),              new DateTime(2017,1,1,12,1,1),               new DateTime(2017,1,1,13,1,1),             new DateTime(2017,1,1,14,1,1),              new DateTime(2017,1,1,15,1,1),             new DateTime(2017,1,1,16,1,1),              new DateTime(2017,1,1,17,1,1),            new DateTime(2017,1,1,18,1,1),            new DateTime(2017,1,1,19,1,1),             new DateTime(2017,1,1,20,1,1),          new DateTime(2017,1,1,21,1,1),            new DateTime(2017,1,1,22,1,1),            new DateTime(2017,1,1,23,1,1),        };
      private List<string> OnCar = new List<string>() { "0", "1", "1", "3", "5", "6", "10", "8", "30", "12", "15", "6", "9", "13", "5", "21", "3", "7", "7", "9", "4", "1", "0", "0" };        private List<string> OutCar = new List<string>() { "0", "0", "0", "0", "3", "5", "8", "17", "23", "1", "8", "5", "9", "10", "5", "6", "0", "2", "29", "3", "9", "1", "1", "4" };
       #region 今日车流量        public void CreateChartSpline(string name, List<DateTime> lsTime, List<string> cherry, List<string> pineapple)        {            //创建一个图标            Chart chart = new Chart();            //设置图标的宽度和高度            chart.Width = 800;            chart.Height = 300;            chart.Margin = new Thickness(5, 5, 5, 5);            //是否启用打印和保持图片            chart.ToolBarEnabled = false;            //设置图标的属性            chart.ScrollingEnabled = false;//是否启用或禁用滚动            chart.View3D = true;//3D效果显示            //创建一个标题的对象            Title title = new Title();            //设置标题的名称            title.Text = name;            title.Padding = new Thickness(0, 10, 5, 0);            //向图标添加标题            chart.Titles.Add(title);            //初始化一个新的Axis            Axis xaxis = new Axis();            //设置Axis的属性            //图表的X轴坐标按什么来分类,如时分秒            xaxis.IntervalType = IntervalTypes.Hours;            //图表的X轴坐标间隔如2,3,20等,单位为xAxis.IntervalType设置的时分秒。            xaxis.Interval = 1;            //设置X轴的时间显示格式为7-10 11:20                       xaxis.ValueFormatString = "HH点";            //给图标添加Axis                        chart.AxesX.Add(xaxis);            Axis yAxis = new Axis();            //设置图标中Y轴的最小值永远为0                       yAxis.AxisMinimum = 0;            //设置图表中Y轴的后缀                      yAxis.Suffix = "辆";            chart.AxesY.Add(yAxis);            // 创建一个新的数据线。                           DataSeries dataSeries = new DataSeries();            // 设置数据线的格式。                           dataSeries.LegendText = "进入车辆";            dataSeries.RenderAs = RenderAs.Spline;//折线图            dataSeries.XValueType = ChartValueTypes.DateTime;            // 设置数据点                          DataPoint dataPoint;            for (int i = 0; i < lsTime.Count; i++)            {                // 创建一个数据点的实例。                                   dataPoint = new DataPoint();                // 设置X轴点                                    dataPoint.XValue = lsTime[i];                //设置Y轴点                                   dataPoint.YValue = double.Parse(cherry[i]);                dataPoint.MarkerSize = 8;                //dataPoint.Tag = tableName.Split('(')[0];                //设置数据点颜色                                  // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                                   //     dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);                //添加数据点                                   dataSeries.DataPoints.Add(dataPoint);            }            // 添加数据线到数据序列。                            chart.Series.Add(dataSeries);            // 创建一个新的数据线。                           DataSeries dataSeriesPineapple = new DataSeries();            // 设置数据线的格式。                     dataSeriesPineapple.LegendText = "驶出车辆";            dataSeriesPineapple.RenderAs = RenderAs.Spline;//折线图            dataSeriesPineapple.XValueType = ChartValueTypes.DateTime;            // 设置数据点                          DataPoint dataPoint2;            for (int i = 0; i < lsTime.Count; i++)            {                // 创建一个数据点的实例。                                   dataPoint2 = new DataPoint();                // 设置X轴点                                    dataPoint2.XValue = lsTime[i];                //设置Y轴点                                   dataPoint2.YValue = double.Parse(pineapple[i]);                dataPoint2.MarkerSize = 8;                //dataPoint2.Tag = tableName.Split('(')[0];                //设置数据点颜色                                  // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                                   //    dataPoint2.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);                //添加数据点                                   dataSeriesPineapple.DataPoints.Add(dataPoint2);            }            // 添加数据线到数据序列。                            chart.Series.Add(dataSeriesPineapple);            //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.                       Grid gr = new Grid();            gr.Children.Add(chart);            CarCount.Child = gr;        }        #endregion
     void dataPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            DataPoint dp = sender as DataPoint;            MessageBox.Show(dp.YValue.ToString());        }
      private List<string> strListx = new List<string>() { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };        private List<string> strListy = new List<string>() { "70", "75", "60", "55", "58", "52", "12", "8" };        private List<string> strListy2 = new List<string>() { "55", "47", "60", "56", "68", "54", "2", "9" };
      #region 创建本周车流量统计        public void CreateChartColumn(string name, List<string> valuex, List<string> valuey, List<string> valuey2)        {            //创建一个图标            Chart chart = new Chart();            //设置图标的宽度和高度            chart.Width = 800;            chart.Height = 300;            chart.Margin = new Thickness(5, 5, 5, 5);            //是否启用打印和保持图片            chart.ToolBarEnabled = false;            //设置图标的属性            chart.ScrollingEnabled = false;//是否启用或禁用滚动            chart.View3D = true;//3D效果显示            //创建一个标题的对象            Title title = new Title();            //设置标题的名称            title.Text = Name;            title.Padding = new Thickness(0, 10, 5, 0);            //向图标添加标题            chart.Titles.Add(title);            Axis yAxis = new Axis();            //设置图标中Y轴的最小值永远为0                       yAxis.AxisMinimum = 0;            //设置图表中Y轴的后缀                      yAxis.Suffix = "辆";            chart.AxesY.Add(yAxis);            // 创建一个新的数据线。                           DataSeries dataSeries = new DataSeries();            dataSeries.LegendText = "驶入车辆";            // 设置数据线的格式            dataSeries.RenderAs = RenderAs.Bar;//柱状Stacked            // 设置数据点                          DataPoint dataPoint;            for (int i = 0; i < valuex.Count; i++)            {                // 创建一个数据点的实例。                                   dataPoint = new DataPoint();                // 设置X轴点                                    dataPoint.AxisXLabel = valuex[i];                //设置Y轴点                                   dataPoint.YValue = double.Parse(valuey[i]);                //添加一个点击事件                   //     dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);                //添加数据点                                   dataSeries.DataPoints.Add(dataPoint);            }            // 添加数据线到数据序列。                            chart.Series.Add(dataSeries);            // 创建一个新的数据线。                           DataSeries dataSeries2 = new DataSeries();            dataSeries2.LegendText = "驶出车辆";            // 设置数据线的格式            //   dataSeries2.RenderAs = RenderAs.StackedColumn;//柱状Stacked            dataSeries2.RenderAs = RenderAs.Bar;//柱状Stacked            // 设置数据点                          DataPoint dataPoint2;            for (int i = 0; i < valuex.Count; i++)            {                // 创建一个数据点的实例。                                   dataPoint2 = new DataPoint();                // 设置X轴点                                    dataPoint2.AxisXLabel = valuex[i];                //设置Y轴点                                   dataPoint2.YValue = double.Parse(valuey2[i]);                //添加一个点击事件                        //  dataPoint2.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);                //添加数据点                                   dataSeries2.DataPoints.Add(dataPoint2);            }            // 添加数据线到数据序列。                            chart.Series.Add(dataSeries2);            //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.                       Grid gr = new Grid();            gr.Children.Add(chart);            chartItem.Child = gr;        }        #endregion
阅读全文
0 0
原创粉丝点击