如何在ms图表中设置渐变停止?

时间:2012-10-25 22:41:15

标签: asp.net vb.net charts mschart

有没有办法在mschart中设置渐变停止? 我能够为每列设置一种颜色但是没有成功地将多种颜色设置为一列,请多多指教一下。

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

enter image description here

这是我的代码:

'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

1 个答案:

答案 0 :(得分:0)

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

Multi gradient faked with stacked columns

要配置图表的代码:

// 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;
}