Reputation: 1149
I have a chart control on a windows form and recently changed the data I'm adding for the x axis from a string representing the date to a datetime type.
before changing I set the minimum X axis to 1 one so that the line starts at the Y axis now I'm using a date I can't do the same!
Is there a simple way to always start a line on a chart control at the Y axis?
Here is what I was doing and what I'm now trying:
chart2.ChartAreas[0].AxisX.Minimum = 1;
And was adding data to the chart thus:
seriesAve.Points.AddXY(strDate, average); //StrDate is a string, average is a double.
I now do it this way:
seriesAve.Points.AddXY(dt.Date, average);
DateTime tpdate = dateTimePicker1.Value;
// results in cannot convert datetime to double.
chart2.ChartAreas[0].AxisX.Minimum = tpdate.Date;
Upvotes: 0
Views: 2420
Reputation: 54463
All X- and Y-Values in a Chart
are stored as doubles
.
When adding a Value as as DateTime
it gets converted to double implicitly with the ToOADate
conversion function.
So when you need to set a value like the Minimum
, Maximum
etc you need to call this function in your code:
chart2.ChartAreas[0].AxisX.Minimum = tpdate.Date.ToOADate();
To convert it back to a DateTime
use the DateTime.FromOADate
function:
DateTime tpdate = DateTime.FromOADate( chart2.ChartAreas[0].AxisX.Minimum);
Upvotes: 2