简体   繁体   中英

How to make the x-axis to ignore the order of the x values?

First, I'm sorry if my English isn't that good...

I have 4 arrays (the first 2 are for series #1), and I want to bind them to my chart:

double[] yValues1 = { 65.62, 75.54, 60.45, 55.73, 70.42, 200 };
string[] xValues1 = { "France", "Canada", "UK", "USA", "Italy", "India" };
chart.Series["Series1"].Points.DataBindXY(xValues1, yValues1);

double[] yValues2 = { 65.62, 75.54, 60.45, 55.73, 200, 70.42 };
string[] xValues2 = { "France", "Canada", "UK", "USA", "India", "Italy"};
chart.Series["Series2"].Points.DataBindXY(xValues2, yValues2);

and the result is (Observe: the values and the order of Italy and India):

As you can see, India supposed to be with 200 in both series, but the 200 of the second series referred to Italy (that is fifth in the first series), as if it goes according the first series. In brief, I want the two series to be shown regardless the order of the xValues, but according to their strings values.

And by the way, How can I to limit the number of columns and that even though I binded the chart with array with bigger length then that limit?

To accomplish this add a call to AlignDataPointsByAxisLabel:

Chart.AlignDataPointsByAxisLabel();

This will align all data points with the same axis label, regardless of what order they are bound.

The xValue and yValue could be added to a SortedDictionary. This would keep the values and keys paired together.

chart1.Series.Add("Series1");
chart1.Series.Add("Series2");

SortedDictionary<string, double> sd1 = new SortedDictionary<string, double>();
SortedDictionary<string, double> sd2 = new SortedDictionary<string, double>();
sd1.Add("france", 65.52);
sd2.Add("france", 65.52);
sd1.Add("india", 200);
sd2.Add("italy", 40);
sd1.Add("italy", 50);
sd2.Add("india", 200);

chart1.Series["Series1"].Points.DataBindXY(sd1.Keys, sd1.Values);
chart1.Series["Series2"].Points.DataBindXY(sd2.Keys, sd2.Values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM