C# 使用ZedGraph进行自定义绘图

 开源的统计图控件中基本常用的是OpenFlashChar和ZedGraph,今天就先来讲讲ZedGraph的使用。

ZedGraph资源

 

ZedGraph的特点:

  • 第一,可以先生成图片后再显示,这对一些多用户并发有点帮助。(RenderMode.ImageTag)
  • 第二,可以动态生成,不用保存文件,可以减少IO的读写消耗。(RenderMode.RawImage)
  • 第三,比较多的呈现方式,比如曲线图、柱状图、饼图等。 

 

ZedGraph的缺点:

  • 第一,编码的时候,在设置属性时太烂了,一点注释都没有,不要说是中文的啦,就连英文都没有,太不方便了。
  • 第二,图表的显示比较简陋,没有OpenFlashChar来得好看。

 

注意事宜

  • 第一,当前的最新版本是5.1.5,我以前也使用过4.3.4的版本,这两个版本的差别很大,很多属性都已经不存在了,面向对象的感念可能加强了不少,也清晰了不少,因为现在的属性设置都是先实体,再属性了,而以前就比较混乱了。所以在使用不同的版本的时候要注意这点。
  • 第二,在Windows Forms和 ASP.Net Web Form中使用是不同。

 

请自行下载并引用ZedGraph.dll 5.1

using ZedGraph;

添加一个控件到界面,使用下面代码即可实现绘图

        private void btn_DrawChart_Click(object sender, EventArgs e)
        {
            string[] labels = new string[]{"2016Y","2017Y","2018Y","2019Y","Q1","JAN","FEB","MAR",
                "WK01","WK02","WK03","WK04","WK05","WK06","WK07","WK08","WK09","WK10"};


            //不良数/抽检数
            int[] DPPM = new int[] { 4911, 2099, 3386, 1197, 1245, 1510, 1069, 743, 0, 330, 0, 5699, 497, 0, 837, 748, 1105, 972 };

            int[] WarningLimit = new int[] { 2500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500 };
            int[] Target = new int[] { 1500, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };
            DrawChart(labels, DPPM, WarningLimit, Target);

        }

 

 


        void DrawChart(string[] TimeLabels, int[] DPPM, int[] WarningLimit, int[] Target)
        {
            //背景透明设置1:
            zedGraphControl1.GraphPane.Chart.Fill = new Fill(Color.Transparent, Color.Transparent, 45.0f);
            zedGraphControl1.MasterPane.Fill = new Fill(Color.Transparent, Color.Transparent, 45.0f);
            zedGraphControl1.GraphPane.Fill.Color = Color.Transparent;
            //背景透明设置2:
            //zedGraphControl1.GraphPane.Fill.Color = Color.Transparent;
            //zedGraphControl1.MasterPane.Fill.IsVisible = false;
            //zedGraphControl1.GraphPane.Chart.Fill.IsVisible = false;



            //文字及颜色/图表标题
            zedGraphControl1.GraphPane.Title.Text = "OQA DPPM";
            zedGraphControl1.GraphPane.Title.FontSpec.Family = "新宋体";

            //Y轴坐标轴颜色、标题、可见性:
            zedGraphControl1.GraphPane.YAxis.Color = Color.Gray;
            zedGraphControl1.GraphPane.Y2Axis.Color = Color.Gray;

            zedGraphControl1.GraphPane.YAxis.Title.Text = "Pcs"; //Y轴标题
            zedGraphControl1.GraphPane.YAxis.Title.IsVisible = false; //Y轴标题


            //X轴坐标轴颜色、标题、可见性:
            zedGraphControl1.GraphPane.XAxis.Title.Text = "Time"; //X轴标题
            zedGraphControl1.GraphPane.XAxis.Title.IsVisible = false; //X轴标题


            //坐标轴刻度及字体颜色:
            zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Black;
            zedGraphControl1.GraphPane.XAxis.MajorTic.Color = Color.Gray;
            zedGraphControl1.GraphPane.XAxis.MinorTic.Color = Color.Gray;

            zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 10; //X轴文本字体大小
            zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Black;//X轴文本字体颜色


            zedGraphControl1.GraphPane.YAxis.MajorTic.Color = Color.Gray;
            zedGraphControl1.GraphPane.YAxis.MinorTic.Color = Color.Gray;

            zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 10; //X轴文本字体大小
            zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.FontColor = Color.Black;//X轴文本字体颜色


            //网格及颜色:
            //主刻度线显示
            zedGraphControl1.GraphPane.XAxis.MajorGrid.IsVisible = false;
            zedGraphControl1.GraphPane.XAxis.MajorGrid.Color = Color.Gray;
            //主刻度线显示
            zedGraphControl1.GraphPane.YAxis.MajorGrid.IsVisible = true;
            zedGraphControl1.GraphPane.YAxis.MajorGrid.Color = Color.Gray;

            //次刻度线显示
            zedGraphControl1.GraphPane.YAxis.MinorGrid.IsVisible = false;
            zedGraphControl1.GraphPane.YAxis.MinorGrid.Color = Color.Gray;


            //图表边框颜色:内边框******
            zedGraphControl1.GraphPane.Chart.Border.Color = Color.Gray;


            //外边框
            zedGraphControl1.GraphPane.Border = new Border(Color.Gray, 0);
            //隐藏外边框:
            zedGraphControl1.BorderStyle = BorderStyle.None;
            zedGraphControl1.GraphPane.Border.IsVisible = false;
            zedGraphControl1.MasterPane.Border.IsVisible = false;


            //曲线标签位置
            zedGraphControl1.GraphPane.Legend.Position = LegendPos.Top;
            zedGraphControl1.GraphPane.Legend.Border.IsVisible = false;
            zedGraphControl1.GraphPane.Legend.Fill = new Fill(Color.Transparent, Color.Transparent, 45.0f);

            PointPairList PPL_PPM = new PointPairList();
            PointPairList PPL_Warning = new PointPairList();
            PointPairList PPL_Target = new PointPairList();




            //以上生成的图标X轴为数字,下面将转换为文本
            zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Text;


            zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = TimeLabels; //X轴文本取值

            int max = 0;
            for (int i = 0; i < DPPM.Length; i++)
            {
                if (max < DPPM[i])
                    max = DPPM[i];
                PPL_PPM.Add(i + 1, DPPM[i]);
            }

            for (int i = 0; i < WarningLimit.Length; i++)
            {
                if (max < WarningLimit[i])
                    max = WarningLimit[i];
                PPL_Warning.Add(i + 1, WarningLimit[i]);
            }


            for (int i = 0; i < Target.Length; i++)
            {
                if (max < Target[i])
                    max = Target[i];
                PPL_Target.Add(i + 1, Target[i]);
            }


            GraphPane myPane = zedGraphControl1.GraphPane;
            myPane.CurveList.Clear();//清空面板图像
            myPane.GraphObjList.Clear();//清空面板数值内容

            BarItem myBarb = myPane.AddBar("DPPM(Overall)", PPL_PPM, Color.CadetBlue);
            myBarb.Bar.Fill = new Fill(Color.CadetBlue, Color.CadetBlue, 45.0f);//柱状图填充模式纯色
            myBarb.Bar.Border.IsVisible = false;//使柱形图边框不可见

            //BarItem.CreateBarLabels(myPane, false, "");//显示柱状图顶部数字
            //BarItem.CreateBarLabels(myPane, true, "");//显示柱状图内部数字


            LineItem myLinec = myPane.AddCurve("Warning Limit", PPL_Warning, Color.SandyBrown, SymbolType.None);
            myLinec.Line.Width = 2;//使当前线条变粗
            LineItem myLined = myPane.AddCurve("Target", PPL_Target, Color.YellowGreen, SymbolType.None);
            myLined.Line.Width = 2;


            #region 在柱状图顶部添加数值显示

            double offset = max * 0.05;
            // Loop to add text labels to the points
            for (int i = 0; i < DPPM.Length; i++)
            {
                // Get the pointpair
                PointPair pt = PPL_PPM[i];

                string label = pt.Y.ToString("f0");
                // Create a text label from the Y data value
                TextObj text = new TextObj(label, pt.X - label.Length / 15.0, pt.Y + offset,
                    CoordType.AxisXYScale, AlignH.Left, AlignV.Center);
                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                //text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) );
                text.FontSpec.Size = 10;  //字体倾斜度
                // Rotate the text to 90 degrees
                text.FontSpec.Angle = 0;  //字体倾斜度
                myPane.GraphObjList.Add(text);
            } 
            #endregion
            
            //自动调整图形到合适比例
            zedGraphControl1.AxisChange();

            //标注调整后Y轴的步长
            string label1 = zedGraphControl1.GraphPane.YAxis.Scale.MajorStep.ToString();
            double max1 = zedGraphControl1.GraphPane.YAxis.Scale.Max + zedGraphControl1.GraphPane.YAxis.Scale.MajorStep * 0.3;
            TextObj text1 = new TextObj(label1, 0, max1,
                   CoordType.AxisXYScale, AlignH.Center, AlignV.Center);
            text1.FontSpec.Border.IsVisible = true;
            text1.FontSpec.Fill.IsVisible = false;
            //text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) );
            text1.FontSpec.Size = 9;  //字体倾斜度
            // Rotate the text to 90 degrees
            text1.FontSpec.Angle = 0;  //字体倾斜度/300
            myPane.GraphObjList.Add(text1);

            //zedGraphControl1.GraphPane.Title.Text = "OQA DPPM-" + label1;


            zedGraphControl1.Refresh();//这句话非常重要,否则不会立即显示

        }

绘图结果如图所示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值