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

2019-01-20 14:56发布

问题:

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?

回答1:

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.



回答2:

(I would just comment on Fratyx's answer, but I guess don't have the reputation to do so.)

To elaborate on Fratyx's answer, the IsXAxisQuantitative property only applies to certain SeriesChartType's, and will be ignored for other types.

These are those types: https://referencesource.microsoft.com/#System.Windows.Forms.DataVisualization/Common/Utilities/CustomAttributesRegistry.cs,e51a969ce4c7db16



标签: c# mschart