Reputation: 489
I know this question has been asked before quite a few times, but no one seems to have come up with a solution (Not that I could find at least).
So, I have a chart for my application and I've added values
chart1.Series["Life"].Points.AddY(1);
chart1.Series["Life"].Points.AddY(30);
chart1.Series["Life"].Points.AddY(20);
chart1.Series["Life"].Points.AddY(50);
for which I get a chart like
Now this is very displeasing to the eye first of all because the X needs to start from the origin.
So, question 1 - Why is it starting from 1?
Question 2 - How do I solve this?
This is the first time I am making a chart in c# so this might sound like a trivial dilemma.
Also, like I mentioned, I did my research and I found that I should do something like
chart1.ChartAreas[0].AxisX.Interval = 0;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.Minimum = 0; //made this -1
chart1.ChartAreas[0].AxisX.Crossing = 0; //and this too to -1
This didn't work so I decided to play a little with the minimum and crossing by giving them -1. Didn't work. The graph not only moved, but it still starts from 1. HOW?
Upvotes: 1
Views: 4808
Reputation: 14059
You could use the Points.AddXY
method to specify the X coordinate too. To make sure that the line starts from the origin prepend data points with (0, 0)
:
var series = chart1.Series["Life"];
series.Points.AddXY(0, 0);
series.Points.AddXY(1, 1);
series.Points.AddXY(2, 30);
series.Points.AddXY(3, 20);
series.Points.AddXY(4, 50);
chart1.ChartAreas[0].AxisX.Minimum = 0;
Upvotes: 2
Reputation: 35
So you want your chart X-axis to start at 1? why not use:
chart1.ChartAreas[0].AxisX.Minimum = 1;
?
or you want your points to start at 0? why not use :
chart1.ChartAreas["Life"].AxisX.Minimum = 0;
chart1.Series["Life"].Points.AddXY(0,1);
?
Upvotes: 2