如何在X轴上搜索DataPoint,然后能够更改Y值?

时间:2015-04-25 22:08:46

标签: c# visual-studio-2010 visual-studio visual-studio-2012 charts

我正在创建一个图表,通过在一个系列的循环中添加x轴值来生成不同的数据点。

在另一个系列中,我希望能够:

  • 搜索系列“总费用”是否与字符串“starttime”在同一点有一个数据点
  • 如果图表根本没有值,则将该点添加到系列
  • 如果图表的值为“starttime”,则将值添加到当前Y值
  • 如果图表在“starttime”没有值,则addXY指向系列

以下是我到目前为止尝试使用的代码:

using System.Windows.Forms.DataVisualization.Charting;

// ...

public DateTime starttime;
public DateTime endtime;
public DateTime totaltime;

private void comboBoxStart_SelectedIndexChanged(object sender, EventArgs e)
    {
        string format = "HH:mm";
        starttime = DateTime.ParseExact(comboBoxStart.Text, format, System.Globalization.CultureInfo.InvariantCulture);
    }

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (textBoxTotalTime.Text != "")
    {
        string format = "HH:mm";
        totaltime = DateTime.ParseExact(
            comboBoxFinish.Text, format, 
            System.Globalization.CultureInfo.InvariantCulture);
        chartTimespan.Series.Add(textBoxName.Text);             

        while(starttime <= endtime)
        {                
            chartTimespan.Series[textBoxName.Text].Points.AddXY(starttime, 6);
            starttime = starttime.AddMinutes(15);

            DataPoint DataPointX = chartTimespan.Series[textBoxName.Text].Points
                .FindByValue((starttime).ToOADate(), "X");

            if(chartTimespan.Series["Total Cost"].Points.Contains(null))
            {
                chartTimespan.Series["Total Cost"].Points.AddXY(starttime, 6);
            }
            else if (!(chartTimespan.Series["Total Cost"].Points.FindByValue((starttime).ToOADate(), "X") == null))
            {
                chartTimespan.Series["Total Cost"].Points.;
            }
            else
            {
                chartTimespan.Series["Total Cost"].Points.AddXY(starttime, 6);
            }    
        }

        chartTimespan.Legends.Add(textBoxName.Text);
        chartTimespan.Series[textBoxName.Text].ChartType = SeriesChartType.Line;
    }
    textBoxName.Text = "";
    comboBoxStart.Text = "";
    comboBoxFinish.Text="";
    textBoxTotalTime.Text = "";
}

为任何帮助干杯,或者如果您认为我应该采用不同的方法!

1 个答案:

答案 0 :(得分:0)

我没有在图表中搜索由于格式化而有时会变得棘手的值,而是更喜欢将chart.Series["name"].Points.Databind与更易管理的集合一起使用。根据我的经验,在使用动态数据时这是有意义的;不是将数据存储在图表中,而是使图表显示(已处理)数据。

对于这种类型的解决方案,首先要设计一些具有相关值的数据对象:

public class ChartItem
{
    public double Value { get; set; }
    public DateTime Time { get; set; }

    public ChartItem(DateTime time, double value)
    {
        Value = value;
        Time = time;
    }
}

在你的Form中你有类似的东西:

public List<ChartItem> chartItems;

public Form1()
{
    InitializeComponent();

    chart1.Series[0].Points.Clear();
    chartItems = new List<ChartItem>();
    chartItems.Add(new ChartItem(DateTime.Today, 1);
    chartItems.Add(new ChartItem(DateTime.Today.AddMinutes(15), 2); // "Or something"
    ...
    chart1.Series[0].Points.DataBind(chartItems, "Time", "Value", "");
    chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd HH:mm";
}

最后在private void pictureBox1_MouseUp中添加一个值:

DateTime time = DateTime.ParseExact(textBox1.Text, "yyyy-MM-dd HH:mm", System.Globalization.DateTimeFormatInfo.InvariantInfo);

const double valueToAdd = 1;

ChartItem item = chartItems.SingleOrDefault(x => x.Time == time);

if (item == null) // No previous time in series
{   
    item = new ChartItem(time, valueToAdd);
    chartItems.Add(item);
}
else // Add to previous time
    item.Value += valueToAdd;

chart1.Series[0].Points.DataBind(chartItems, "Time", "Value", ""); // Update chart