Search code examples
c#mschart

Plotting two y-values for x=0 in a MS Chart control


I found a weird behavior in MS Chart for Windows Forms.

Let's say I want to have a scatter plot with two points (1,10) and (1,20). I can do that in this way:

....
Series series = new Series();
series.ChartType = SeriesChartType.Point;
double[] x = { 1, 1 };
double[] y = { 10, 20 };
series.Points.DataBindXY(x, y);

That works fine. But now I want the same result, but both x-values should be 0.

double[] x = { 0, 0 };
double[] y = { 10, 20 };
series.Points.DataBindXY(x, y);

In that case the chart control creates two data points at 'autogenerated' x positions 1 and 2. It just ignores the given x-values. It is the same behavior if I use

series.Points.AddXY(0, 10);
series.Points.AddXY(0, 20);

I get the same effect for more than two data points. So it turns out that scatter plot does not work if not at least one x-value is nonzero.

I think a possible workaround would be to use multiple series, but that is inacceptable.

Does anyone have a explanation for this behavior or a solution for this problem?


Solution

  • I found a solution by myself:

    You have to add

    series.CustomProperties = "IsXAxisQuantitative=True";
    

    to your code. So the x-values really are treated as values. I don't know why this is not self-evident if I use the BindXY function.