用ms chart 进行绘制图表(包含比较多的应用)

 #region 方法
    private void CreateElevationChart(int isel, string MC,string RQ)
    {



        DataTable dtc = this.bll.GetWarningData(MC, RQ.Substring(0, 7), "SBJXS");//时不均系数
        DataTable dt1 = this.bll.GetWarningData(MC, RQ, "SJ");//管线基础数据
        DataTable dt2 = this.bll.GetWarningData(MC, RQ, "GX");//管存数据
        //if (dt.Rows.Count < 1)
        //{
        //    return;
        //}
        Title title = new Title(MC);
        chart1.Titles.Add(title);
        chart1.Height = 350;
        chart1.Width = 800;
        chart1.BackColor = System.Drawing.Color.White;
        
#region 绘制曲线
        //安全区域上限
           Series series_SX = new Series("SX");
          series_SX.ChartType = SeriesChartType.Spline; //曲线类型
          series_SX.BorderWidth = 2;
          series_SX.Color = System.Drawing.Color.Blue;//线条宽度
          series_SX.IsVisibleInLegend = false;           //是否显示数据说明
          //series_SX.IsValueShownAsLabel = true;        //线条上是否给出数据的显示
          series_SX.MarkerStyle = MarkerStyle.Circle;  //线条上的数据点标志类型
          series_SX.MarkerSize = 2;//
        //安全区域下限
          Series  series_XX=new Series("XX");
         series_XX.ChartType = SeriesChartType.Spline; //曲线类型
          series_XX.BorderWidth = 2;
          series_XX.Color = Color.FromArgb(220, 238, 43, 217);//线条宽度
          series_XX.IsVisibleInLegend = false;           //是否显示数据说明
          //series_XX.IsValueShownAsLabel = true;        //线条上是否给出数据的显示
          series_XX.MarkerStyle = MarkerStyle.Cross;  //线条上的数据点标志类型
          //series_XX.MarkerBorderWidth = 2;
          series_XX.MarkerSize = 2;//
         

        //实际管存

          Series series_GC = new Series("GC");
          series_GC.ChartType = SeriesChartType.Spline; //曲线类型
          series_GC.BorderWidth = 2;
          series_GC.Color =Color.FromArgb(220, 255, 246, 0);//线条宽度
          series_GC.IsVisibleInLegend = false;           //是否显示数据说明
          //series_XX.IsValueShownAsLabel = true;        //线条上是否给出数据的显示
          series_GC.MarkerStyle = MarkerStyle.Diamond;  //线条上的数据点标志类型
          //series_XX.MarkerBorderWidth = 2;
          series_GC.MarkerSize = 2;//

        //预警上限
          Series series_YJS = new Series("YJS");
          series_YJS.ChartType = SeriesChartType.Spline; //曲线类型
          series_YJS.BorderWidth = 2;
          series_YJS.Color = Color.FromArgb(220, 180, 0, 255);//线条宽度
          series_YJS.IsVisibleInLegend = false;           //是否显示数据说明
          //series_XX.IsValueShownAsLabel = true;        //线条上是否给出数据的显示
          series_YJS.MarkerStyle = MarkerStyle.Diamond;  //线条上的数据点标志类型
          //series_XX.MarkerBorderWidth = 2;
          series_YJS.MarkerSize = 7;//
        //预警下限
          Series series_YJX = new Series("YJX");
          series_YJX.ChartType = SeriesChartType.Spline; //曲线类型
          series_YJX.BorderWidth = 2;
          series_YJX.Color = Color.FromArgb(220, 0, 255, 252);//线条宽度
          series_YJX.IsVisibleInLegend = false;           //是否显示数据说明
          //series_XX.IsValueShownAsLabel = true;        //线条上是否给出数据的显示
          series_YJX.MarkerStyle = MarkerStyle.Diamond;  //线条上的数据点标志类型
          //series_XX.MarkerBorderWidth = 2;
          series_YJX.MarkerSize = 7;//

#endregion

#region 设置绘图区域
          Legend legend1 = new Legend();
          legend1.Title = "图例说明";
          legend1.TitleSeparator = LegendSeparatorStyle.Line;
          legend1.LegendStyle = LegendStyle.Column;
          legend1.Docking = Docking.Right;
          legend1.IsDockedInsideChartArea = false;

          LegendItem li2 = new LegendItem();
          li2.Color = System.Drawing.Color.Blue;
          LegendCell lcSym2 = new LegendCell(LegendCellType.SeriesSymbol, "");
          LegendCell lcText2 = new LegendCell(LegendCellType.Text, "安全上限");
          li2.Cells.Add(lcSym2);
          li2.Cells.Add(lcText2);
          legend1.CustomItems.Add(li2);

          LegendItem li1 = new LegendItem();
          li1.Color = Color.FromArgb(220, 238, 43, 217);
          LegendCell lcSym1 = new LegendCell(LegendCellType.SeriesSymbol, "");
          LegendCell lcText1 = new LegendCell(LegendCellType.Text, "安全下限");
          li1.Cells.Add(lcSym1);
          li1.Cells.Add(lcText1);
          legend1.CustomItems.Add(li1);

          LegendItem li3 = new LegendItem();
          li3.Color = Color.FromArgb(220, 255, 246, 0);//图例颜色
          LegendCell lcSym13 = new LegendCell(LegendCellType.SeriesSymbol, "");
          LegendCell lcText13 = new LegendCell(LegendCellType.Text, "实际管存");
          li3.Cells.Add(lcSym13);
          li3.Cells.Add(lcText13);
          legend1.CustomItems.Add(li3);
          

          LegendItem li4 = new LegendItem();
          li4.Color = Color.FromArgb(220, 180, 0, 255);//图例颜色
          LegendCell lcSym14 = new LegendCell(LegendCellType.SeriesSymbol, "");
          LegendCell lcText14 = new LegendCell(LegendCellType.Text, "预警上限");
          li4.Cells.Add(lcSym14);
          li4.Cells.Add(lcText14);
          legend1.CustomItems.Add(li4);


          LegendItem li5 = new LegendItem();
          li5.Color = Color.FromArgb(220, 0, 255, 252);//图例颜色
          LegendCell lcSym15 = new LegendCell(LegendCellType.SeriesSymbol, "");
          LegendCell lcText15 = new LegendCell(LegendCellType.Text, "预警下限");
          li5.Cells.Add(lcSym15);
          li5.Cells.Add(lcText15);
          legend1.CustomItems.Add(li5);

          chart1.Legends.Add(legend1);

        chart1.ChartAreas["chartarea1"].AxisX.IsMarginVisible = false;
        //chart1.ChartAreas["chartarea1"].Area3DStyle.Enable3D = false;
        //背景色设置
        chart1.ChartAreas["chartarea1"].ShadowColor = Color.Transparent;
        chart1.ChartAreas["chartarea1"].BackColor = Color.Azure;
        chart1.ChartAreas["chartarea1"].BackGradientStyle = GradientStyle.TopBottom;
        chart1.ChartAreas["chartarea1"].BackSecondaryColor = Color.Transparent;
        
        //X,Y坐标线颜色和大小
        chart1.ChartAreas["chartarea1"].AxisX.LineColor = Color.Black;
        chart1.ChartAreas["chartarea1"].AxisX.Interval = 1;
      //  chart1.ChartAreas["chartarea1"].AxisY.Interval = 10000;
        chart1.ChartAreas["chartarea1"].AxisY.LineColor = Color.Black;
        chart1.ChartAreas["chartarea1"].AxisX.LineWidth =1;
        chart1.ChartAreas["chartarea1"].AxisY.LineWidth = 1;
        chart1.ChartAreas["chartarea1"].AxisY.Title = "管存,立方米";
        chart1.ChartAreas["chartarea1"].AxisX.IsStartedFromZero = true;


        chart1.ChartAreas["chartarea1"].AxisX2.LineColor = Color.Black;
         chart1.ChartAreas["chartarea1"].AxisX2.IsStartedFromZero = true;
        chart1.ChartAreas["chartarea1"].AxisX2.IsMarginVisible = false;

        // Set Axis Line Style
        chart1.ChartAreas["chartarea1"].AxisX2.LineDashStyle = ChartDashStyle.Solid;

        // Set Arrow Style
        chart1.ChartAreas["chartarea1"].AxisX2.ArrowStyle = AxisArrowStyle.None;
        chart1.ChartAreas["chartarea1"].AxisX2.Interval = 1;
        // Set Line Width
        chart1.ChartAreas["chartarea1"].AxisX2.LineWidth = 2;
        chart1.ChartAreas["chartarea1"].AxisX2.Enabled = AxisEnabled.True;
      // chart1.ChartAreas["chartarea1"].AxisX2.MajorGrid.LineDashStyle = ChartDashStyle.NotSet;

         //chart1.ChartAreas["chartarea1"].AxisX.Interval
        //格网
       chart1.ChartAreas["chartarea1"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet;
        chart1.ChartAreas["chartarea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid;


        series_GC.YAxisType = AxisType.Primary;

         series_GC.XAxisType = AxisType.Primary;

        //第二個Series使用Secondary

        //chart1.Series[1].YAxisType = AxisType.Secondary;

         series_SX.XAxisType = AxisType.Secondary;
         series_XX.XAxisType = AxisType.Secondary;
         series_YJS.XAxisType = AxisType.Secondary;
         series_YJX.XAxisType = AxisType.Secondary;


        
#endregion
        #region 具体数据
        DataTable dt = new DataTable();

        dt.Columns.Add("SJ");
        dt.Columns.Add("V");
        for (int i = 0; i < 12; i++)
        {
            DataRow row = dt.NewRow();
           // row["SJ"] = string.Format("{0:D2}", i*2) + ":00";
            if ( i*2< 10)
                row["SJ"] = "0" + i*2  + ":00";
            else
                row["SJ"] = i*2 + ":00";
            row["V"] = i*2 ;
            dt.Rows.Add(row);
        }
        DataTable dd = new DataTable();

        dd.Columns.Add("SJ");
        dd.Columns.Add("V");
        for (int i = 0; i < 24; i++)
        {
            DataRow row = dd.NewRow();
            // row["SJ"] = string.Format("{0:D2}", i*2) + ":00";
            if ( i < 10)
                row["SJ"] = "0" + i  + ":00";
            else
                row["SJ"] = i + ":00";
            row["V"] = i ;
            dd.Rows.Add(row);
        }

        //初始化各个管存数据
        for (int pointIndex = 0; pointIndex < dt.Rows.Count; pointIndex++)
        {
            //将管存全部初始化为0,然后再进行赋值,如果Y为0的话,就将其设置为空
            series_GC.Points.AddXY(dt.Rows[pointIndex]["SJ"], 0);

        }
        //管存数据的显示
        for (int pointIndex = 0; pointIndex < dt.Rows.Count; pointIndex++)
        {
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                //将各个点显示
                if (dt.Rows[pointIndex]["SJ"].ToString() == dt2.Rows[i]["SJ"].ToString())
                {
                    //设置实际管存数据的显示
                    series_GC.Points[pointIndex].SetValueY(dt2.Rows[i]["GC"].ToString());
                    //series_GC.Points.AddXY(dt2.Rows[i]["GC"].ToString());
                }

            }
           // int iMax = int.Parse(dt2.Rows[dt2.Rows.Count - 1]["SJ"].ToString().Split(':')[0]);
           // int iMin = int.Parse(dt2.Rows[0]["SJ"].ToString().Split(':')[0]);
            if (series_GC.Points[pointIndex].YValues[0] == 0)
           // if (pointIndex > iMax || pointIndex < iMin)
            {
                series_GC.Points[pointIndex].IsEmpty = true;

            }
        }
       

        // 安全上下限
        for (int pointIndex = 0; pointIndex < dd.Rows.Count; pointIndex++)
        {
            series_SX.Points.AddXY(dd.Rows[pointIndex]["SJ"], 0);
            series_XX.Points.AddXY(dd.Rows[pointIndex]["SJ"], 0);

        }
        //安全上下限
        for (int pointIndex = 0; pointIndex < dd.Rows.Count; pointIndex++)
        {
            if (dt1.Rows[0][1].ToString() == "") //如果安全上下限没有数据,则设置为空
            {


                series_SX.Points[pointIndex].IsEmpty = true;
                series_XX.Points[pointIndex].IsEmpty = true;
            }
            else  //如果有数据,则进行设置
            {
                //安全上下限
                series_SX.Points[pointIndex].SetValueY(float.Parse(dt1.Rows[0]["SX"].ToString())*10000);
                series_XX.Points[pointIndex].SetValueY(float.Parse(dt1.Rows[0]["XX"].ToString()) * 10000);
            }

          



            if (series_XX.Points[pointIndex].YValues[0] == 0)
            {

                series_XX.Points[pointIndex].IsEmpty = true;
            }
            if (series_SX.Points[pointIndex].YValues[0] == 0)
            {
                series_SX.Points[pointIndex].IsEmpty = true;
            }

        }
        预警上限,下限
        for (int pointIndex = 0; pointIndex < dd.Rows.Count; pointIndex++)
        {
            series_YJS.Points.AddXY(dd.Rows[pointIndex]["SJ"], 0);
            series_YJX.Points.AddXY(dd.Rows[pointIndex]["SJ"], 0);
        }
        for (int pointIndex = 0; pointIndex < dd.Rows.Count; pointIndex++)
        {
           // int bInt = int.Parse(dd.Rows[pointIndex]["V"].ToString());

            // if (dt.Rows[pointIndex]["SJ"].ToString() == dt2.Rows[dt2.Rows.Count-1]["SJ"].ToString())

          //  if (DateTime.Now.Hour == bInt)
          //  {
                if (dt1.Rows[0][1].ToString() == "") //如果安全上下限没有数据,则设置为空
                {
                    //series_SX.Points[pointIndex].IsEmpty = true;
                    //series_XX.Points[pointIndex].IsEmpty = true;

                    series_YJS.Points[pointIndex].IsEmpty = true;
                    series_YJX.Points[pointIndex].IsEmpty = true;
                }
                else  //如果有数据,则进行设置
                {
                    if (dtc.Rows.Count > 0)
                    {
                        if (pointIndex <= DateTime.Now.Hour)
                        {
                            float YJX = GetDatas_Mulity(isel, dtc, dt1, "XX", pointIndex) * 10000;
                            float YJS = GetDatas_Mulity(isel, dtc, dt1, "SX", pointIndex) * 10000;
                            series_YJS.Points[pointIndex].SetValueY(YJS);
                            series_YJX.Points[pointIndex].SetValueY(YJX);
                        }
                    }

                    
                    // series_SX.Points[pointIndex].SetValueXY(dt.Rows[pointIndex]["SJ"], dt1.Rows[0]["SX"]);
                    // series_XX.Points[pointIndex].SetValueXY(dt.Rows[pointIndex]["SJ"], dt1.Rows[0]["XX"]);
                    //预警上下限的操作
                }
           // }

            if (series_YJX.Points[pointIndex].YValues[0] == 0)
            {

                series_YJX.Points[pointIndex].IsEmpty = true;
            }
            if (series_YJS.Points[pointIndex].YValues[0] == 0)
            {
                series_YJS.Points[pointIndex].IsEmpty = true;
            }
        


        }

        foreach (Series series in chart1.Series)
        {
            // Set empty points visual appearance attributes
            series.EmptyPointStyle.Color = Color.Gray;
            series.EmptyPointStyle.BorderWidth = 2;
            series.EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash;
            series.EmptyPointStyle.MarkerSize = 7;
            series.EmptyPointStyle.MarkerStyle = MarkerStyle.Cross;
            series.EmptyPointStyle.MarkerBorderColor = Color.Black;
            series.EmptyPointStyle.MarkerColor = Color.LightGray;
        }

        //绑定作图区域
        // chart1.Series.Add(series_);
        chart1.Series.Add(series_GC);
        chart1.Series.Add(series_SX);
        chart1.Series.Add(series_XX);
        chart1.Series.Add(series_YJX);
        chart1.Series.Add(series_YJS);

        #endregion



    }
    private float getdata(int start,int count, DataTable dt, string qd,string zd,string zj)
    {
        float sum = 0.0f;
        int i = 0;
        for (int n = 0; n < count; n++)
        {
            if ((start + n) >23)
                i = 23;
            sum = sum + float.Parse(dt.Rows[start+n-i]["SBJXS"].ToString());
        }
        float total=0;
        total =Math.Abs((float.Parse(qd) - float.Parse(zj)) * count - sum * float.Parse(zd));
        return sum;
    }
    private float GetDatas(int count, DataTable dt_SBJXS,DataTable dt_JSSJ,string columns)
    {
        float temp=0;
            try
            {
                if (columns=="SX")
                    temp = float.Parse(dt_JSSJ.Rows[0]["SX"].ToString())-getdata(DateTime.Now.Hour,
                                            count,
                                            dt_SBJXS,
                                           dt_JSSJ.Rows[0]["QDJQL"].ToString(),
                                           dt_JSSJ.Rows[0]["ZDJQL"].ToString(),
                                           dt_JSSJ.Rows[0]["ZJSCL"].ToString()
                                           );
                if (columns == "XX")
                {
                    temp = float.Parse(dt_JSSJ.Rows[0]["XX"].ToString()) +getdata(DateTime.Now.Hour,
                                               count,
                                               dt_SBJXS,
                                              dt_JSSJ.Rows[0]["QDJQL"].ToString(),
                                              dt_JSSJ.Rows[0]["ZDJQL"].ToString(),
                                              dt_JSSJ.Rows[0]["ZJSCL"].ToString()
                                              );
                }
            }
            catch
            { throw; }
                
        
        return temp;
    }
    private float GetDatas_Mulity(int count, DataTable dt_SBJXS, DataTable dt_JSSJ, string columns,int hours)
    {
        float temp = 0;
        try
        {
            if (columns == "SX")
                temp = float.Parse(dt_JSSJ.Rows[0]["SX"].ToString()) - getdata(hours,
                                        count,
                                        dt_SBJXS,
                                       dt_JSSJ.Rows[0]["QDJQL"].ToString(),
                                       dt_JSSJ.Rows[0]["ZDJQL"].ToString(),
                                       dt_JSSJ.Rows[0]["ZJSCL"].ToString()
                                       );
            if (columns == "XX")
            {
                temp = float.Parse(dt_JSSJ.Rows[0]["XX"].ToString()) +getdata(hours,
                                           count,
                                           dt_SBJXS,
                                          dt_JSSJ.Rows[0]["QDJQL"].ToString(),
                                          dt_JSSJ.Rows[0]["ZDJQL"].ToString(),
                                          dt_JSSJ.Rows[0]["ZJSCL"].ToString()
                                          );
            }
        }
        catch
        { throw; }


        return temp;

    }

//效果图如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值