1

有没有办法在 mschart 中设置渐变停止点?我可以为每列设置一种颜色,但未能成功为一列设置多种颜色,请指教,非常感谢。

这就是我想要实现的目标:

在此处输入图像描述

这是我的代码:

'set color variation on chart 1
Private Sub chart_ssi_rzli_1_Customize(ByVal sender As Object, ByVal e As System.EventArgs) Handles chart_ssi_rzli_1.Customize
    chart_ssi_rzli_1.Series(0).SmartLabelStyle.Enabled = True

    'set max and min y axis according to the current goal ranges.
    chart_ssi_rzli_1.ChartAreas(0).AxisY.Minimum = Session("rzli_red") - 6
    chart_ssi_rzli_1.ChartAreas(0).AxisY.Maximum = Session("rzli_blue") + 4

    For Each dp As DataPoint In chart_ssi_rzli_1.Series(0).Points

        If dp.YValues(0) >= Session("rzli_blue") Then
            dp.Color = Drawing.Color.LightSkyBlue
        ElseIf dp.YValues(0) >= Session("rzli_green") And dp.YValues(0) < Session("rzli_blue") Then
            dp.Color = Drawing.Color.LightGreen
        ElseIf dp.YValues(0) >= Session("rzli_yellow") And dp.YValues(0) < Session("rzli_green") Then
            dp.Color = Drawing.Color.Yellow
        ElseIf dp.YValues(0) <= Session("rzli_red") Then
            dp.Color = Drawing.Color.FromArgb(245, 56, 61)
        End If
    Next

End Sub
4

1 回答 1

0

渐变支持仅限于 2 种颜色,因此没有直接的方法可以通过单个系列来实现这一点。但是,您可以通过在每个堆叠系列上使用StackedColumnand来伪造这种效果。GradientStyle.TopBottom这涉及将您的数据系列分成几个,可能不值得付出努力。您还需要从图例中隐藏附加系列。

用堆叠列伪造的多梯度

配置图表的代码:

// Chart settings inside Form4.Designer.cs
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
series1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
series1.BackSecondaryColor = System.Drawing.Color.Lime;
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
series1.Color = System.Drawing.Color.Yellow;
series1.IsVisibleInLegend = false;
series1.Legend = "Legend1";
series1.Name = "Series1";
series2.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
series2.BackSecondaryColor = System.Drawing.Color.Yellow;
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
series2.IsVisibleInLegend = false;
series2.Legend = "Legend1";
series2.Name = "Series2";
series3.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
series3.BackSecondaryColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
series3.Color = System.Drawing.Color.Red;
series3.Legend = "Legend1";
series3.Name = "Series3";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Series.Add(series3);
this.chart1.Size = new System.Drawing.Size(292, 273);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";

以及创建数据点的代码:

// How to populate the series correctly in Forms.cs
public Form4()
{
    InitializeComponent();

    Random rand = new Random();
    chart1.Series[0].Points.Clear();
    chart1.Series[1].Points.Clear();
    chart1.Series[2].Points.Clear();
    for (int i = 0; i < 5; i++)
    {
        chart1.Series[0].Points.Add(66);
        chart1.Series[1].Points.Add(9);  // 66 + 9 = 75
        chart1.Series[2].Points.Add(rand.Next(1, 25));
    }

    chart1.ChartAreas[0].AxisY.Minimum = 60;
}
于 2012-10-29T10:50:51.820 回答