I A Khan
I A Khan

Reputation: 8841

C# chart How to Set start points of X axis

I am working with C# chart and ref. to C# - How do you make a chart object start at 0 on the X axis?

Here is my sample data :

     X        Value ( From _LOWER )
======================
    0           0
    0.5        100
    1.0        200
    1.5        300
    2.0        400
    2.5        500
    4.0        600
    5.0        700
    7.5        800
    10.0       900
    12.5       1000

I am using following code :

chart1.Series.Clear();
chart1.ChartAreas[0].AxisY.Interval = 200;
chart1.ChartAreas[0].AxisX.Interval =  0.5;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount ;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Number;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisY.Minimum = 0;

chart1.Series.Add("Trial 1");
chart1.Series["Trial 1"].ChartType = SeriesChartType.Line;
chart1.Series["Trial 1"].BorderWidth = 2;
chart1.Series["Trial 1"].XValueType = ChartValueType.String;
chart1.Series["Trial 1"].IsValueShownAsLabel = true;

this.chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDotDot;
this.chart1.ChartAreas[0].IsSameFontSizeForAllAxes = true;

DataRowCollection _dr = _CObjectsofClasses._obj_CDatabase._MGetDataRows("select _LOWER from tblCHART ORDER BY [_CHARTID]");
foreach (DataRow _drRow in _dr)
   {
     chart1.Series["Trial 1"].Points.AddY(_drRow[0]);
   }

And here is the output

Chart Out Put

As you see in the chart the values start from 1, but it should be started from 0, as shown in chart 100 is displayed on 2 instead of 0.5, 200 is displayed on 3 instead on 1 and so on....

please suggest me where I am wrong.

Update

If I use code (Adding XY axis)

DataRowCollection _dr =    _CObjectsofClasses._obj_CDatabase._MGetDataRows("select _SNAME, _LOWER from tblCHART ORDER BY [_CHARTID]");
foreach (DataRow _drRow in _dr)
{  
   chart1.Series["Trial 1"].Points.AddXY(_drRow[0], _drRow[1]);
}

then I am getting output

enter image description here

not getting 0.5 intervals and zero showing 2 times

Upvotes: 1

Views: 5772

Answers (2)

Bryan Llanes
Bryan Llanes

Reputation: 11

After using AddXY to set x/y points, change your minimum value to double.NaN and set IsMarginVisible to false

    chart1.ChartAreas[0].AxisX.Minimum = double.NaN;
    chart1.ChartAreas[0].AxisX.IsMarginVisible = false;
    chart1.ChartAreas[0].AxisY.Minimum = double.NaN;
    chart1.ChartAreas[0].AxisY.IsMarginVisible = false;

Upvotes: 1

TaW
TaW

Reputation: 54433

If you want control over the X-Values you should add the DataPoints using the AddXY method.

using this

for (int i = 0; i < 15; i++) chart1.Series["Trial 1"].Points.AddY(i * 50);

or this

for (int i = 0; i < 15; i++) chart1.Series["Trial 1"].Points.AddXY(i, i * 50);

results in these two charts..:

enter image description hereenter image description here

All the rest is your code. (The size difference is just from manual resizing..)

It is worth looking into the X-Values of the first version: They all are 0!

In your case you should write:

int i = 0;
foreach (DataRow _drRow in _dr)
{  
    chart1.Series["Trial 1"].Points.AddXY(i++, _drRow[1]);
}

Btw, you may want to rethink setting the the X-Values to string; this usually leaves you with only little control..

Upvotes: 2

Related Questions