簡體   English   中英

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

[英]How To Search For a DataPoint on the X-Axis, and then be able to change the Y Value?

我正在創建一個圖表,該圖表當前通過在一個系列的循環中添加到x軸值來生成不同的數據點。

在另一個系列中,我希望能夠:

  • 搜索以查看“總成本”系列是否具有與字符串“ starttime”相同的數據點
  • 如果圖表根本沒有任何值,則將該點的XY添加到序列中
  • 如果圖表在“開始時間”具有值,則將值添加到當前的Y值
  • 如果圖表在“開始時間”沒有值,則將XY點添加到序列中

到目前為止,這是我一直在嘗試使用的代碼:

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 = "";
}

為任何幫助加油,或者如果您認為我應該使用其他方法!

我寧願使用帶有更多可管理集合的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM