How do I add a gap between series in MsChart?
I have created a chart with following code:
ChartAmalkerd.Titles[0].Text = "xxxx";
ChartAmalkerd.Series.Add("x");
ChartAmalkerd.Series["x"].ChartType = SeriesChartType.Column;
开发者_开发问答 ChartAmalkerd.Series["x"]["PointWidth"] = (0.5).ToString();
ChartAmalkerd.Series["x"].Points.AddY(10);
ChartAmalkerd.Series["x"].IsValueShownAsLabel = true;
ChartAmalkerd.Series.Add("y");
ChartAmalkerd.Series["y"].ChartType = SeriesChartType.Column;
ChartAmalkerd.Series["y"].Points.AddY(20);
ChartAmalkerd.Series["y"]["PointWidth"] = (0.5).ToString();
ChartAmalkerd.Series["y"].IsValueShownAsLabel = true;
ChartAmalkerd.Series.Add("y");
ChartAmalkerd.Series["z"].ChartType = SeriesChartType.Column;
ChartAmalkerd.Series["z"].Points.AddY(20);
ChartAmalkerd.Series["z"]["PointWidth"] = (0.5).ToString();
ChartAmalkerd.Series["z"].IsValueShownAsLabel = true;
but the columns are together and there is no any gap between columns. How do I add a gap between columns?
I would normally come up with less hacky solutions, but as I have no time to research tonight - I at least have semi-solutions that might serve your purpose depending on your requirements, so I will give you something to start with.
What you are experiencing is the default mschart behavior if you do not set an X datapoint for this chart type.
If you never set an x for the points in a series each datapoint 'x' will be defined by the data point index position within its series + 1. You are using three series, each with one data point, so in your example all points are set to an x value of 1 automatically.
Based on this, each column above will be squeezing in to be drawn as close to their automatically generated x value as possible, which in this case, all points are x = 1.
There may be a better workaround, however, one solution to this would be to assign an x value with an offset based on the number of series/data points you plan on using.
If you know you are going to have three series you can add apply a unique offset for each series
For example
chart1.Series["x"].Points.AddXY(chart1.Series["x"].Points.Count + 1 - 0.05, yValue);
chart1.Series["y"].Points.AddXY(chart1.Series["y"].Points.Count + 1, yValue);
chart1.Series["z"].Points.AddXY(chart1.Series["z"].Points.Count + 1 + 0.05, yValue);
NOTICE: You will discover interesting 'bugs'/features if you do not add the '+ 1' to the datapoint count for series "y" above. Without it, the first data point of series 'y' will be zero. When you leave things at zero, microsoft charting will assume you have not set anything and use a default behavior.
Another hacky workaround is less pretty if you are using grid lines, but it will work.
Use your original code, but add white borders! i.e. add this to each series (or perhaps just the center series)
chart1.Series["y"].BorderWidth = 2;
chart1.Series["y"].BorderColor = Color.White;
The workaround code examples I cite are tailored to your code sample above, which has three series, each with one data point, but can be adapted for more series and more data points per series to make it more dynamic. Let me know if you need help with such a task and I will try to get you going.
I will return and edit this answer later if I have time to research a less hacky answer this weekend. Good luck!
chart1.Series["y"].BorderWidth = 2;
chart1.Series["y"].BorderColor = Color.Transparent;
works nicely.
精彩评论