Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...