学习之【C#】Canvas折线统计图

image.png
    List<string> XList = new List<string>
            {
                "2017-01",
                "2017-02",
                "2017-03",
                "2017-04",
                "2017-05",
                "2017-06",
                "2017-07",
                "2017-08",
                "2017-09",
                "2017-10",
                "2017-11",
                "2017-12",
                "2017-13"
            };


            List<double> XValues = new List<double>
            {
                1,
                21,
                32,
                44,
                51,
                62,
                73,
                84,
                91,
                101,
                111,
                121,
                113
            };
            List<double> XValues2 = new List<double>
            {
                5,
                9,
                20,
                60,
                12,
                60,
                20,
                10,
                100,
                52,
                22,
                35,
                44
            };
            List<List<double>> XValueList = new List<List<double>>();
            XValueList.Add(XValues);
            XValueList.Add(XValues2);
         string XText = "時間";
         string YText = "人數";
         int XCount = XList.Count + 1;
         int YCount = 6;
         Canvas chartCanvas = (Canvas)clientReportWindow.FindName("chartCanvas");
         DrawArrow(chartCanvas, XCount, XText,YText);
         DrawScale(chartCanvas, XCount, YCount);
         DrawScaleLabel(chartCanvas, XList, XValueList , YCount);
        /// <summary>
        /// 生成横纵坐标及箭头
        /// </summary>
        private void DrawArrow(Canvas chartCanvas,int XCount,string XText,string YText)
        {
            double XPoint = 25 + 45 * XCount;
          
            Line x_axis = new Line();//x轴
            Line y_axis = new Line();//y轴
            x_axis.Stroke =Brushes.Black;
            y_axis.Stroke = Brushes.Black;
            x_axis.StrokeThickness = 3;
            y_axis.StrokeThickness = 3;
            x_axis.X1 = 40;
            x_axis.Y1 = 320;
            x_axis.X2 = XPoint;
            x_axis.Y2 = 320;

            y_axis.X1 = 40;
            y_axis.Y1 = 320;
            y_axis.X2 = 40;
            y_axis.Y2 = 30;
            chartCanvas.Children.Add(x_axis);
            chartCanvas.Children.Add(y_axis);

            Line y_scale1 = new Line(); //坐标原点直角
            y_scale1.Stroke = Brushes.Black;
            y_scale1.StrokeThickness = 1;
            y_scale1.X1 = 40;
            y_scale1.Y1 = 310;
            y_scale1.X2 = 44;
            y_scale1.Y2 = 310;
            y_scale1.StrokeStartLineCap = PenLineCap.Triangle;
            chartCanvas.Children.Add(y_scale1);

            Path x_axisArrow = new Path();//x轴箭头
            Path y_axisArrow = new Path();//y轴箭头
            x_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            y_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            PathFigure x_axisFigure = new PathFigure();
            x_axisFigure.IsClosed = true;
            x_axisFigure.StartPoint = new Point(XPoint, 316);                          //路径的起点
            x_axisFigure.Segments.Add(new LineSegment(new Point(XPoint, 324), false)); //第2个点
            x_axisFigure.Segments.Add(new LineSegment(new Point(XPoint+10, 320), false)); //第3个点
            PathFigure y_axisFigure = new PathFigure();
            y_axisFigure.IsClosed = true;
            y_axisFigure.StartPoint = new Point(36, 30);                          //路径的起点
            y_axisFigure.Segments.Add(new LineSegment(new Point(44, 30), false)); //第2个点
            y_axisFigure.Segments.Add(new LineSegment(new Point(40, 20), false)); //第3个点
            PathGeometry x_axisGeometry = new PathGeometry();
            PathGeometry y_axisGeometry = new PathGeometry();
            x_axisGeometry.Figures.Add(x_axisFigure);
            y_axisGeometry.Figures.Add(y_axisFigure);
            x_axisArrow.Data = x_axisGeometry;
            y_axisArrow.Data = y_axisGeometry;
            chartCanvas.Children.Add(x_axisArrow);
            chartCanvas.Children.Add(y_axisArrow);

            TextBlock x_label = new TextBlock();
            TextBlock y_label = new TextBlock();
            TextBlock o_label = new TextBlock();
            x_label.Text = XText;
            y_label.Text = YText;
            o_label.Text = "0";
            Canvas.SetLeft(x_label, XPoint+10);
            Canvas.SetLeft(y_label, 20);
            Canvas.SetLeft(o_label, 20);
            Canvas.SetTop(x_label, 317);
            Canvas.SetTop(y_label, 4);
            Canvas.SetTop(o_label, 312);
            x_label.FontSize = 14;
            y_label.FontSize = 14;
            o_label.FontSize = 14;
            chartCanvas.Children.Add(x_label);
            chartCanvas.Children.Add(y_label);
            chartCanvas.Children.Add(o_label);

        }
        /// <summary>
        /// 作出x轴和y轴的标尺
        /// </summary>
        private void DrawScale(Canvas chartCanvas,int XCount,int YCount)
        {
            double XPoint = 45 * XCount;

            for (int i = 1; i < XCount; i++)//作12个刻度
            {
                //原点 O=(40,320)
                Line x_scale = new Line(); //主x轴标尺
                x_scale.StrokeEndLineCap = PenLineCap.Triangle;
                x_scale.StrokeThickness = 1;
                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.X1 = 40 + i * 45;
                x_scale.X2 = x_scale.X1;
                x_scale.Y1 = 320;
                x_scale.StrokeThickness = 3;
                x_scale.Y2 = x_scale.Y1 - 8;
                chartCanvas.Children.Add(x_scale);

                Line x_in = new Line();//x轴轴辅助标尺
                x_in.Stroke = Brushes.LightGray;
                x_in.StrokeThickness = 0.5;
                x_in.X1 = 40 + i * 45;
                x_in.Y1 = 320;
                x_in.X2 = 40 + i * 45;
                x_in.Y2 = 30;
                chartCanvas.Children.Add(x_in);
            }
            for (int j = 0; j < 30; j++)
            {
                Line y_scale = new Line(); //主Y轴标尺
                y_scale.StrokeEndLineCap = PenLineCap.Triangle;
                y_scale.StrokeThickness = 1;
                y_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));

                y_scale.X1 = 40;            //原点x=40
                if (j % 5 == 0)
                {
                    y_scale.StrokeThickness = 3;
                    y_scale.X2 = y_scale.X1 + 8;//大刻度线
                }
                else
                {
                    y_scale.X2 = y_scale.X1 + 4;//小刻度线
                }

                y_scale.Y1 = 320 - j * 10;  //每10px作一个刻度 
                y_scale.Y2 = y_scale.Y1;
                chartCanvas.Children.Add(y_scale);
            }
            for (int i = 1; i < YCount; i++)
            {
                Line y_in = new Line();//y轴辅助标尺
                y_in.Stroke = Brushes.LightGray;
                y_in.StrokeThickness = 0.5;
                y_in.X1 = 40;
                y_in.Y1 = 320 - i * 50;
                y_in.X2 = XPoint;
                y_in.Y2 = 320 - i * 50;
                chartCanvas.Children.Add(y_in);
            }

        }
        /// <summary>
        /// 添加刻度标签
        /// </summary>
        private void DrawScaleLabel(Canvas chartCanvas,List<string> XList, List<List<double>> XValueList, int YCount)
        {
            // 最大值
            double Max = 0;
            for (int i = 0; i < XValueList.Count; i++)
            {
                if(Max< XValueList[i].Max()) Max = XValueList[i].Max();
            }
            double y_Max = Math.Ceiling((Max / 5));

            // X坐标值
            for (int i = 1; i <= XList.Count; i++)
            {
                TextBlock x_ScaleLabel = new TextBlock();
                x_ScaleLabel.Text = XList[i-1];
                x_ScaleLabel.LayoutTransform = new RotateTransform(90);
                Canvas.SetLeft(x_ScaleLabel, 25 + 45 * i);
                Canvas.SetTop(x_ScaleLabel, 320 + 5);
                chartCanvas.Children.Add(x_ScaleLabel);
            }

            // Y坐标值
            for (int i = 1; i < YCount; i++)
            {
                TextBlock y_ScaleLabel = new TextBlock();
                y_ScaleLabel.Text = (i * y_Max).ToString();
                Canvas.SetLeft(y_ScaleLabel, 0);
                Canvas.SetTop(y_ScaleLabel, 320 - 50 * i);
                chartCanvas.Children.Add(y_ScaleLabel);
            }

            // 多条线
            for(int k = 0; k < XValueList.Count; k++)
            {
                // 折线图坐标点
                PointCollection coordinatePoints = new PointCollection();

                List<double> XValues = XValueList[k];
                
                SolidColorBrush solidColor = new SolidColorBrush();
                switch (k)
                {
                    case 0:
                        solidColor = Brushes.Red;
                        break;
                    case 1:
                        solidColor = Brushes.Green;
                        break;
                    case 2:
                        solidColor = Brushes.Orange;
                        break;
                    case 3:
                        solidColor = Brushes.Blue;
                        break;
                    default:
                        solidColor = Brushes.Black;
                        break;
                }

                // 计算数据点
                for (int i = 1; i <= XValues.Count; i++)
                {
                    double xPoint = 40 + 45 * i;
                    double yPoint = 320 - 50 * (XValues[i - 1] / y_Max);
                    // 点显示
                    Ellipse Ellipse = new Ellipse();
                    Ellipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                    Ellipse.Width = 8;
                    Ellipse.Height = 8;
                    Canvas.SetLeft(Ellipse, xPoint-4);
                    Canvas.SetTop(Ellipse, yPoint);
                    chartCanvas.Children.Add(Ellipse);

                    // 线显示
                    coordinatePoints.Add(new Point(xPoint+5, yPoint+2));

                    // 值显示
                    TextBlock EP_Label = new TextBlock();
                    EP_Label.Foreground = solidColor;
                    EP_Label.Text = XValues[i - 1].ToString();
                    Canvas.SetLeft(EP_Label, xPoint);
                    Canvas.SetTop(EP_Label, yPoint);
                    chartCanvas.Children.Add(EP_Label);
                }

                // 绘制连接折线
                Polyline curvePolyline = new Polyline();
                curvePolyline.Stroke = solidColor;
                curvePolyline.StrokeThickness = 2;
                curvePolyline.Points = coordinatePoints;
                chartCanvas.Children.Add(curvePolyline);
            }
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343