图表:覆盖列/条值

本文关键字:条值 覆盖 图表 | 更新日期: 2023-09-27 18:11:35

好吧,这可能是一个新问题,但我只编程了几天。因此,我从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列,而是创建另一个列,新值就在它旁边。

图表:覆盖列/条值

我在我的项目中为轴"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 = "";
                    }
                }
            }
        }
    }
}