C#charting:如何在堆积区域图中绘制空点

时间:2013-11-08 02:13:32

标签: c# charts mschart stacked-area-chart

如何在C#图表库中绘制空白点?我不希望在图表中绘制最后一个空值为11/10的0值,但是尽管将其设置为空点,但它的值为0。

查看此处的图表。

enter image description here

以下是代码:

void Main()
{
    System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new Chart();
    ChartArea chartArea1 = new ChartArea("foobar");
    chartArea1.Name = "ChartArea1";
    chart1.ChartAreas.Add(chartArea1);

    chart1.Name = "chart1";
    chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Fire;     
    Series series1 = new Series("seriesfoo")
     {
         XValueType = ChartValueType.Date,
        ChartArea = "ChartArea1",
        ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea,
        Legend = "Legend1",
        Name = "MyGraph",
        IsXValueIndexed = true,
    };

     Series series2 = new Series("seriesbar")
     {
        XValueType = ChartValueType.Date,    
        ChartArea = "ChartArea1",
        ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line,
        Legend = "Legend1",
        Name = "Gap",
        IsXValueIndexed = true,
        YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary,
        YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double
     };

    chart1.Series.Add(series1);
    chart1.Series.Add(series2);
    chart1.Size = new System.Drawing.Size(797, 267);
    chart1.Text = "chart1";

    int[] dat = {20, 19, 10};
    for (int i = 0; i < dat.Length; i++ )
    {
        // Add X and Y values for a point. 
        chart1.Series[0].Points.AddXY(DateTime.Today.AddDays(i), dat[i]);
    }

    int[] dat2 = {20, 16, 12, 8, 4, 0};
    for (int i = 0; i < dat2.Length; i++ )
    {
        chart1.Series[1].Points.AddXY(DateTime.Today.AddDays(i), dat2[i]);
    }

    chart1.DataManipulator.InsertEmptyPoints(1, IntervalType.Days, "MyGraph, Gap");
    chart1.DataManipulator.IsEmptyPointIgnored  = true;
    MemoryStream stream = new MemoryStream(2048);
    chart1.SaveImage("C:\\temp\\chart.png", ChartImageFormat.Png);
}

1 个答案:

答案 0 :(得分:2)

我找到了答案,不得不设置。

chart1.Series[0].EmptyPointStyle.Color = Color.Transparent;

另外,我要从系列中删除下面的属性,这会干扰上面的风格,并且尽管设置了空的点颜色,但总是提到这一点。

BackGradientStyle = GradientStyle.TopBottom,