1
votes

I'm having a problem with chart controls in VS 3.5 framework . I'm trying plot a graph with x- axis 0 and y-axis 1. But it results with a graph plotted (1,1)

I have a situation where i need to have one single bar with (0,1).

Chart1.Series[0].Points.Add(new DataPoint(0,1));

or

Chart1.Series[0].Points.AddXY(0,1);

Results:

Bar graph (1,1) instead of showing (0,1) point

I have added

Chart1.ChartAreas[""].AxisX.Minimum = 0;

Still Showing the same result ...

This is so frustrating : When i add one more point (1,1)

Eg:

Chart1.Series[0].Points.AddXY(0,1);
Chart1.Series[0].Points.AddXY(1,1);

Then it results :

bar graph with points (0,1) and (1,1) correctly

1

1 Answers

2
votes

You're having this issue because the default series added to a chart is a bar graph. You will have to remove the bar graph series and replace it with a Point series in order to display your data. See sample code below.

// clear data from the chart
chart.Series.Clear();

// add an x-y series to the chart
var xySeries = new Charting.Series() {
    LegendText = "XY Plot",
    ChartType = Charting.SeriesChartType.Point,
    Color = Color.Brown,
    MarkerStyle = Charting.MarkerStyle.Circle,
    MarkerSize = 10
};
chart.Series.Add(xySeries);

// put your point on the series
xySeries.Points.AddXY(1, 1);

// set the axis
chart.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = Charting.ChartDashStyle.Dot;
chart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = Charting.ChartDashStyle.Dot;

This produces the following chart

Chart Sample

You will have to add this using statement to include Charting in the available namespaces.

using Charting = System.Windows.Forms.DataVisualization.Charting;

To change the labels on the X-Axis, you will have to use the AxisLabel property of the DataPoint. See sample below:

// put your point on the series
xySeries.Points.AddXY(0, 1);
xySeries.Points[0].AxisLabel = "0"; // <--- SET AXIS LABEL HERE

Doing will 'coerce' the Chart control to display your chosen axis label.