MScharts图:覆盖列/条值

时间:2011-10-19 10:03:47

标签: c# graph

好的,这可能是一个新问题,但我几天才开始编程。 所以我从MScharts中获取了一个示例图表,它是在运行时动态创建的图形,代码如下:

        private void DynamicChartCreation_Load(object sender, System.EventArgs e)
    {
        // Create a Chart
        Chart1 = new Chart();

        // Create Chart Area
        ChartArea chartArea1 = new ChartArea();

        // Add Chart Area to the Chart
        Chart1.ChartAreas.Add(chartArea1);

        // Create a data series
        Series series1 = new Series();
        Series series2 = new Series();

        // Add data points to the first series
        series1.Points.Add(34);

        // Add data points to the second series
        series2.Points.Add(14);

        // Add series to the chart
        Chart1.Series.Add(series1);
        Chart1.Series.Add(series2);

        // Set chart control location
        Chart1.Location = new System.Drawing.Point(16, 48);

        // Set Chart control size
        Chart1.Size = new System.Drawing.Size(360, 260);

        // Add chart control to the form
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Chart1 });

    }

这是一个柱形图,我希望能够通过组合框动态更改列值。 问题是如何覆盖现有的旧值?

我用Series.point.add尝试了这样:

                Chart1.Series["Series1"].Points.AddY(comboBox_value);

但是,不是将值应用于第一个series1列,而是创建另一个列,并在其旁边显示新值。

任何帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

我在项目中为轴“X”做了自定义,我在这里粘贴代码,供您参考...... 它在我的环境中工作正常......请在您的环境中检查。

protected void SpiLineAreaChart_Customize(object sender, EventArgs e)
{
    foreach (ChartArea area in ((Chart)sender).ChartAreas) {
        foreach (Axis axis in area.Axes) {
            if (axis.Name.StartsWith("X")) {
                foreach (CustomLabel label in axis.CustomLabels) {
                    counter = counter + 1;
                }
                lastpoint = counter;
                midpoint = counter / 2;
                for (int i = 0; i <= counter - 1; i++) {
                    if (i == 0) {
                        axis.CustomLabels(i).Text = MonthVal;
                    } else if (i == midpoint - 1) {
                        StartDate = StartDate.AddDays(31);
                        strstartdate = StartDate.ToString("MMM");
                        axis.CustomLabels(i).Text = strstartdate;
                    } else if (i == lastpoint - 1) {
                        StartDate = StartDate.AddDays(31);
                        strstartdate = StartDate.ToString("MMM");
                        axis.CustomLabels(i).Text = strstartdate;
                    } else {
                        axis.CustomLabels(i).Text = "";
                    }
                }

            }
        }
    }
}