Chris
Chris

Reputation: 805

How to set Interval of X values in Chart control

I have a line chart with some values as shown on the picture. I want X values to be 1 2 3 etc, but now I have data in series and on x i have 0,77 1,77 2,77 3,77. I set

IsStartedFromZero = true;
Interval = 1;
Maximum = 4;
Maximum = 4; 

in chartarea properties

How to force X values to be 1 2 3 4?

CODE:

 Series s = new Series();
        s.Color = Color.Red;
        s.ChartType = SeriesChartType.Line;
        s.BorderWidth = 3;

        s.Points.Add(new DataPoint(1.2, 0));
        s.Points.Add(new DataPoint(1.2,50));
        s.Points.Add(new DataPoint(2, 80));
        s.Points.Add(new DataPoint(3.2, 100));

        Series s1 = new Series();
        s1.Color = Color.Blue;
        s1.ChartType = SeriesChartType.Line;
        s1.BorderWidth = 2;

        s1.Points.Add(new DataPoint(0.8,3.2));
        s1.Points.Add(new DataPoint(0.83,6.5));
        s1.Points.Add(new DataPoint(0.9,12.9));
        s1.Points.Add(new DataPoint(1,25.8));
        s1.Points.Add(new DataPoint(1.1,29));
        s1.Points.Add(new DataPoint(1.2,54.8));
        s1.Points.Add(new DataPoint(1.4,58.1));
        s1.Points.Add(new DataPoint(1.5,61.3));
        s1.Points.Add(new DataPoint(1.6,67.7));
        s1.Points.Add(new DataPoint(2,90.3));
        s1.Points.Add(new DataPoint(2.5,100));



        chart1.Series.Add(s);
        chart1.Series.Add(s1);

        chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
        chart1.ChartAreas[0].AxisX.Maximum = 4;
        chart1.ChartAreas[0].AxisX.Interval = 1;
        chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
        chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;

chart

Upvotes: 4

Views: 25077

Answers (2)

Chris
Chris

Reputation: 805

The answer is to set:

            chart1.ChartAreas[0].AxisX.Minimum = 0;

And that's all!

Upvotes: 4

Brissles
Brissles

Reputation: 3881

I would think that the default behaviour is to the set the first X label to the lowest value contained within your data series. In your case, it seems the lowest value of your blue series is ~0.8, which is below 1.

Given that you're specifying an Interval of 1, and a Maximum of 4 It makes sense that the the X labels would be roughly 0.77, 1.77, 2.77, 3.77.

If you force the X labels to be 1,2,3,4 explicitly after the chart has been bound then your labels won't correspond to your data correctly, and if you align your data to begin at 1.0 then you'll be cropping some of your series data out of the chart.

Depends on what you want to achive, I'd just stick with the default values the chart spits out.

Upvotes: 0

Related Questions