c# - How To Search For a DataPoint on the X-Axis, and then be able to change the Y Value? -
i creating chart generates different data points adding x-axis values in loop in 1 series.
in series want able to:
- search see if series "total cost" has data point @ same point string "starttime"
- if chart has no values @ all, addxy of point series
- if chart has value @ "starttime", add value current y value
- if chart has no value @ "starttime", addxy point series
here code have been trying use far:
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 = ""; }
cheers help, or if think should using different approach!
instead of searching values inside chart, can tricky @ times due formatting, prefer use chart.series["name"].points.databind
more managable collection. in experience makes sense when work dynamic data; rather storing data in chart have chart display (processed) data.
for type of solution first fashion type of data-object hold relevant values:
public class chartitem { public double value { get; set; } public datetime time { get; set; } public chartitem(datetime time, double value) { value = value; time = time; } }
and in form
have like:
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"; }
and add value in 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 previous time item.value += valuetoadd; chart1.series[0].points.databind(chartitems, "time", "value", ""); // update chart
Comments
Post a Comment