C# Chart统计图

this.chart1.Series.Clear();
           
            //chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
            Series s = new Series();
            s.ChartType = SeriesChartType.Column;
            s.Name = "年龄阶段";
            
            this.chart1.Series.Add(s);
            var result = from r in lst group r by r.AgeQuantum into g select new { ageQuantum = g.Key, count = g.Count() };
            s.Points.DataBind(result, "ageQuantum", "count", "Label=#VAL{D},ToolTip=count");
            

            ChartArea area = chart1.ChartAreas[0];
            area.Visible = true;
            area.AxisX.MajorGrid.LineWidth = 0;
            area.AxisX2.Enabled = AxisEnabled.False;
            area.AxisY.MajorGrid.LineWidth = 0;
            area.AxisY2.Enabled = AxisEnabled.False;
            this.chart1.Width = 450;
            //area.AxisX.IsMarginVisible = false;
            area.Area3DStyle.Enable3D = false;

 

 this.chart1.Series.Clear();
            List<ReaderInfo> lst = new List<ReaderInfo>();
            Random ran = new Random();
            for (int i = 0; i < 150; i++)
            {
                lst.Add(new ReaderInfo() { Age = ran.Next(30, 60), Sex = i % 3 == 0 ? "男" : "女" });
            }
            for (int i = 0; i < 150; i++)
            {
                lst.Add(new ReaderInfo() { Age = ran.Next(10, 30), Sex = i % 3 == 0 ? "男" : "女" });
            }
            bool flag = false;
            for (int i = 0; i < 300; i++)
            {
                flag = false;
                for (int j = 0; j < lstAgequantum.Count; j++)
                {
                    if (lstAgequantum[j].min <= lst[i].Age && lst[i].Age <= lstAgequantum[j].max)
                    {
                        lst[i].AgeQuantum = this.lstQuantum.Items[j].ToString();
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    lst[i].AgeQuantum = "其他";
                }
            }
            var result = from r in lst group r by r.AgeQuantum into g select new { ageQuantum = g.Key, count = g.Count() };
            Series s = new Series();
            s.ChartType = SeriesChartType.Pie;
            this.chart1.Series.Add(s);
            foreach (var stu in result)
            {
                int ptIdx = this.chart1.Series[0].Points.AddY(stu.count);
                DataPoint pt = this.chart1.Series[0].Points[ptIdx];
                pt.LegendText = stu.ageQuantum;//右边标签列显示的文字  
                pt.Label = "#PERCENT{P2}" + " [ " + "#VAL{D} 人" + " ]"; //圆饼外显示的信息  
                pt.LabelToolTip = stu.ageQuantum;
            }
            //chart1.Series[0]["PieLabelStyle"] = "Outside";//
            this.chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

 

posted on 2015-07-21 23:28  !无名之辈  阅读(254)  评论(0)    收藏  举报