【问题标题】:Line Chart with different colors on a different interval at x-axis fill in same series?x轴上不同间隔的不同颜色的折线图填充同一系列?
【发布时间】:2018-04-11 12:44:53
【问题描述】:

我在 Windows 窗体中创建了一个折线图控件。

我将 ChartArea、AxisX 分为四个间隔,但我想将背景颜色(唯一颜色)应用于每个间隔。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您能否进一步解释一下您的控件是如何工作的?我也创建了一个 LineChart 控件,但是我不使用其他控件,我通过代码绘制它。您是否使用Control.CreateGraphics() 通过代码绘制图表?
  • 欢迎来到 SO。到目前为止,请显示您的代码并提供minimal reproducible example。请参阅How to Ask 了解更多信息。
  • 您的问题解决了吗?

标签: c# winforms charts microsoft-chart-controls


【解决方案1】:

AxisX 分为四个间隔,但我想应用背景颜色(唯一颜色)

这些间隔是用彩色的StripLine(s) 创建的。通过代码:

var stripLine = new System.Windows.Forms.DataVisualization.Charting.StripLine()
{
    BackColor = Color.Blue,
    IntervalOffset = 4, // This is where the stripline starts
    StripWidth = 2 // And this is how long the interval is
};

chart1.ChartAreas[0].AxisX.StripLines.Add(stripLine);

您需要为要显示的间隔添加数据点。

或者,也可以从 VS 设计模式从 (Properties) -> ChartAreas -> 选择 ChartArea -> Axes -> 选择要显示的轴 -> StripLines,然后添加 StripLine . 您必须设置BackColorIntervalOffsetStripWidth 才能显示。如果您设置StripLine.Interval,它将按该间隔重复。

【讨论】:

    【解决方案2】:

    您可以绘制这些区域,但这将始终显示在所有图表元素之上,包括网格和数据点。

    因此,正如 NLindborn 所建议的,最好的方法是 StripLines

    它们所有元素之下,并且可以很好地缩放。

    请注意,它们的属性在数据中,因此您需要知道图表中的值,或者更确切地说是 x 轴范围。

    这是使用StripLines的完整代码示例:

    // set up the chart:
    ChartArea ca = chart.ChartAreas[0];
    chart.Series.Clear();
    for (int i = 0; i < 5; i++)
    {
        Series s = chart.Series.Add("Series" + (i+1));
        s.ChartType = SeriesChartType.Line;
        s.BorderWidth = 2;
    }
    
    // add a few test data
    for (int i = 0; i <= 360; i++)
    {
        chart.Series[0].Points.AddXY(i, Math.Sin(i * Math.PI / 180f));
        chart.Series[1].Points.AddXY(i, Math.Cos(i * Math.PI / 180f));
        chart.Series[2].Points.AddXY(i, Math.Sin(i * Math.PI / 90f));
        chart.Series[3].Points.AddXY(i, Math.Cos(i * Math.PI / 90f));
        chart.Series[4].Points.AddXY(i, Math.Sin(i * Math.PI / 30f));
    }
          
    // set up the chart area:  
    Axis ax = ca.AxisX;
    ax.Minimum = 0;
    ax.Maximum = 360;
    ax.Interval = 30;
    
    // a few semi-transparent colors
    List<Color> colors = new List<Color>()  { Color.FromArgb(64, Color.LightBlue),  
      Color.FromArgb(64, Color.LightSalmon),  Color.FromArgb(64, Color.LightSeaGreen),  
      Color.FromArgb(64, Color.LightGoldenrodYellow)};
    

    现在我们准备好创建 StripLines:

    // this is the width of the chart in values:
    double hrange = ax.Maximum - ax.Minimum;
    
    // now we create and add four striplines:
    for (int i = 0; i < 4; i++)
    {
        StripLine sl = new StripLine();
        sl.Interval = hrange;                   // no less than the range, so it won't repeat
        sl.StripWidth = hrange / 4f;            // width
        sl.IntervalOffset = sl.StripWidth * i;  // x-position
        sl.BackColor = colors[i];
        ax.StripLines.Add(sl);
    }
    

    请注意,无论何时更改轴范围,都需要调整带状线数据!

    还要注意 StripLine 使用轴值。

    更新:

    一个常见问题是缩放时移动带状线。没有一点帮助,他们将坚持原来的立场。编码AxisViewChanged 会有所帮助,可能是这样:

    为每个带状线计算IntervalOffset;在第一个最简单的情况下,这应该可以工作:

       chart1.ChartAreas[0].AxisX.StripLines[0].IntervalOffset = 
               chart1.Series[0].Points[0].XValue - e.NewPosition;
    

    对于其他的添加正确的宽度倍数如上!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2012-04-24
      • 2021-08-18
      • 2012-07-10
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多