如何将我的图表更改为列渐变填充样式?

时间:2015-05-19 21:15:13

标签: c# .net winforms

这就是我在构造函数中所做的:

drawPoints.Clear();
paintToCalaculate = false;
chart1.Invalidate();
Series S0 = chart1.Series[0];
S0.ChartType = SeriesChartType.Column;
S0.IsValueShownAsLabel = true;
chart1.Series["Series1"]["PixelPointWidth"] = "0.6";
chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";
S0.Color = Color.Transparent;
S0.LegendText = "";
area.BackColor = Color.White;
area.BackSecondaryColor = Color.LightSteelBlue;
area.BackGradientStyle = GradientStyle.DiagonalRight;
area = chart1.ChartAreas[0];
chart1.Series["Series1"].Points.AddXY(10, 10);
area.AxisX.Minimum = 1;
area.AxisX.Maximum = 30;
area.AxisY.Minimum = 1;
area.AxisY.Maximum = 120;
area.AxisX.MajorGrid.LineColor = Color.LightSlateGray;
area.AxisY.MajorGrid.LineColor = Color.LightSlateGray;

问题在于我使用图表绘制事件来绘制点和线:

Pen pen = new Pen(Color.Blue, 2.5f);
SolidBrush myBrush = new SolidBrush(Color.Red);
private void chart1_Paint(object sender, PaintEventArgs e)
{
    if (paintToCalaculate)
    {
        Series s = chart1.Series.FindByName("dummy");
        if (s == null) s = chart1.Series.Add("dummy");
        drawPoints.Clear();
        s.Points.Clear();
        foreach (PointF p in valuePoints)
        {
            s.Points.AddXY(p.X, p.Y);
            DataPoint pt = s.Points[0];
            double x = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(pt.XValue);
            double y = chart1.ChartAreas[0].AxisY.ValueToPixelPosition(pt.YValues[0]);
            drawPoints.Add(new Point((int)x, (int)y));
            s.Points.Clear();
        }
        paintToCalaculate = false;
        chart1.Series.Remove(s);
    }

    foreach (Point p in drawPoints)
    {
        e.Graphics.FillEllipse(Brushes.Red, p.X - 2, p.Y - 2, 4, 4);
    }

    if (drawPoints.Count > 1)
    {
        e.Graphics.DrawLines(pen, drawPoints.ToArray());
    }
}

我希望它看起来像这种风格:

Chart

它应该在示例中看起来像。但是因为我使用油漆事件可能不会这样。

编辑尝试了这个解决方案我在form1设计器中为测试添加了一个新的图表控件,名为chart2。然后在构造函数中我做了:

chart2.Titles.Add(("Introducing Chart Controls"));

            ChartArea chartarea2 = new ChartArea("Main");
            chart2.ChartAreas.Add(chartarea2);

            Series seriesColumns = new Series("RandomColumns");
            seriesColumns.Color = Color.Blue;
            chart2.Series.Add(seriesColumns);
            Random rnd = new Random(10);
            for (int i = 0; i < 10; i++)
            {
                seriesColumns.Points.Add((rnd.Next(100)));
            }
            DataPoint dp = new DataPoint(seriesColumns);
            dp.Color = Color.Red;
            dp.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.LeftRight;
            seriesColumns.Points.Add(dp);

但没有淡入/淡出颜色变化。没有任何事情发生。

这就是chart2的样子:

chart2

1 个答案:

答案 0 :(得分:2)

要获得渐变效果,您不必使用Paint事件。 DataPoint具有BackGradientStyle属性,可自动为您生成渐变。它将使用DataPoint的颜色。您可以在下面的示例中进行设置:

var dp = new DataPoint(8D, 12D);
dp.Color = Color.Red;
dp.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.LeftRight;
var series = this.chart1.Series[0];
series.Points.Add(dp);

这里的渐变逐渐淡出。如果您希望它传递给另一种颜色设置BackSecondaryColor属性。

dp.BackSecondaryColor = Color.Green;