渐变支持仅限于 2 种颜色,因此没有直接的方法可以通过单个系列来实现这一点。但是,您可以通过在每个堆叠系列上使用StackedColumn
and来伪造这种效果。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;
}