【问题标题】:How can I fill the area(s) between two Series of Splines or Lines如何填充两个样条线或线系列之间的区域
【发布时间】:2016-05-26 15:22:37
【问题描述】:

我有这个Chart

如何填充两个Series S0 and S1 之间的区域,比如蓝色和黄色Series

【问题讨论】:

    标签: c# winforms charts


    【解决方案1】:

    为此,我们编写Paint 事件之一:

    这里ValueToPixelPosition 函数是有效的,并为我们提供DataPoint 值和Chart 像素之间的必要转换..:

    private void chart1_Paint(object sender, PaintEventArgs e)
    {
        // we assume two series variables are set..:
        if (sps1 == null || sps2 == null) return;
    
        // short references:
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;
    
        // now we convert all values to pixels
        List<PointF> points1 =  sps1.Points.Select(x=>
            new PointF((float)ax.ValueToPixelPosition(x.XValue), 
                       (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();
    
        List<PointF> points2 =  sps2.Points.Select(x=>
            new PointF((float)ax.ValueToPixelPosition(x.XValue), 
                       (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();
    
        // one list forward, the other backward:
        points2.Reverse();
    
        GraphicsPath gp = new GraphicsPath();
        gp.FillMode = FillMode.Winding;  // the right fillmode
    
        // it will work fine with either Splines or Lines:
        if (sps1.ChartType == SeriesChartType.Spline )   gp.AddCurve(points1.ToArray());
        else gp.AddLines(points1.ToArray());
        if (sps2.ChartType == SeriesChartType.Spline) gp.AddCurve(points2.ToArray());
        else gp.AddLines(points2.ToArray()); 
    
        // pick your own color, maybe a mix of the Series colors..
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(66, Color.DarkCyan)))
            e.Graphics.FillPath(brush, gp);
        gp.Dispose();
    }
    

    请注意,这未针对缩放进行测试..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-12
      • 2023-03-22
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2017-11-17
      相关资源
      最近更新 更多