Force a gap between points on the x axis (MS .Net Chart Controls, Column Chart)

你。 提交于 2019-11-29 04:49:20

I had the devils own job reproducing your situation. I wanted to help because I thought I could learn something by doing so, but I needed your markup, or better yet, the whole solution! I struggled through the "Error executing child request for ChartImg.axd" when I tried a simple page with a chart. I discovered I needed to add a handler in the config. I then fought through the failure to load assembly System.Web.DataVisualization because the handler element I copied referenced the 3.5 DataVisualization assembly so I changed that to 4.0 and finally saw a graph. What a job THAT was!

The reason your spacer series is not creating a gap is because there are no values in that series. Notice the last two lines of code below, that add zero values to the spacer series. This produces the desired gap between the other series, but you will also find the spacer series listed in your legend if you have one, which is ugly to say the least.

  for (var c = start; c < start + 6; c++)
  {
   var color = colors[c % colors.Count];
   var seriesName = "Series "+ c;//color.Name);
   Chart1.Series.Add(seriesName);
   Chart1.Series[seriesName].BorderColor = color;
   Chart1.Series[seriesName].BorderWidth = 1;
   Chart1.Series[seriesName].Color = Color.FromArgb((int)(255 * .7), color);
   Chart1.Series[seriesName].BackSecondaryColor = Color.FromArgb((int)(255 * .2), color);
   Chart1.Series[seriesName].BackGradientStyle = GradientStyle.TopBottom;
   for (var year = DateTime.Now.AddYears(-5).Year; year < DateTime.Now.Year; year++)
    Chart1.Series[seriesName].Points.Add(new DataPoint(year, rng.Next(0, 20)));

   Chart1.Series[seriesName]["PointWidth"] = (0.6).ToString();

   seriesName = "Spacer:" + seriesName;
   Chart1.Series.Add(seriesName);
   Chart1.Series[seriesName]["PointWidth"] = (0.6).ToString();
   for (var year = DateTime.Now.AddYears(-5).Year; year < DateTime.Now.Year; year++)
    Chart1.Series[seriesName].Points.Add(new DataPoint(year, 0));
  }

You can set the legend text to a space (NB. empty string is ignored and legend text is not set) as follows, but the Legend will still show these spacer series.

    Chart1.Series[seriesName].LegendText = " ";

If you're lucky, you won't need to show the legend, or you can set the spacer series colours to the same colour as the legend background and the legend text to spaces. This results in a double-spacing look in the Legend which is likely to be acceptable.

you can control whether or not the series shows up in the legend by setting the "IsVisibleInLegend" value to false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!