0

I want to draw 52 Bar series in a Bar chart to represent weeks in a year. However, there seems to be an issue with the chart, as you can see in the picture below:

enter image description here

I have 2 series that I draw with label, x value,y value. E.g.:

chart1.Series["Data"].Points.AddXY("W1", 0, 168);
chart1.Series["Missing"].Points.AddXY("W1", 0, 0);

However there seems to be a difference between the bar series (sometines thicker sometimes smiller), even though I create them with the same method:

  var series = new Series
        {
            Name = seriesName,
            ChartArea = chartAreaName,
            Color = color,
            ChartType = SeriesChartType.RangeBar,
            XValueType = ChartValueType.Auto,
            YValueType = ChartValueType.Auto,
            SmartLabelStyle = new SmartLabelStyle
            {
                AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes,
                MovingDirection = LabelAlignmentStyles.Right,
                IsMarkerOverlappingAllowed = false
            }
        };

How can I prevent this form happening?

Georgiana M
  • 323
  • 4
  • 20

1 Answers1

0

The effect is a rounding effect that results from having such a large number of bars.

To avoid it you would have to make sure the InnerPlotPosition.Height's number of pixels is divisible by 52.

There is an anti-aliasing property ..:

chart.AntiAliasing = AntiAliasingStyles.All;

..but it already is set to All by default.

Calculating the nearest size is not as simple as one would wish. You can see here for methods that calculate the InnerPlotPosition and you can use ToRectangleF(); to enforce calculating number while the chart is still using automatic values.

But since those numbers are in percent and the other elements, i.e. the title, legend and x-axis with its labels all weigh in, reversing the calculation to find a number that works is not only hard, it will also fail with each resizing of the Chart. And, obviously, when adding more points..

If you can live without resizing you can find a suitable chart.Size by trial and error, e.g. by growing it in a button click: chart.Height += 1;

But imo the best solution is to reduce the Bars' width in a controlled way yourself, maybe like this:

  yourSeries.SetCustomProperty("PixelPointWidth", "3");

The efect will still hapen, but with larger gaps it won't be as noticeable.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111