8

I'm using C# charts to display/compare some data. I changed the graph scale to logarithmic (as my data points have huge differences) but since logarithmic scaling doesn't support zero values, I want to just add an empty point (or skip a data point) for such cases. I have tried the following but non works and all crashes:

if (/*the point is zero*/)
{
    // myChart.Series["mySeries"].Points.AddY(null);
    // or
    // myChart.Series["mySeries"].Points.AddY();
    // or just skip the point
}

Is it possible to add an empty point or just skip a point?

CSDev
  • 3,177
  • 6
  • 19
  • 37
sj47sj
  • 185
  • 6

2 Answers2

6

I found two ways to solve the problem.

One is using double.NaN (suddenly!):

if (myChart.ChartAreas[0].AxisY.IsLogarithmic && y == 0)
    myChart.Series["mySeries"].Points.AddY(double.NaN);
    // or ...Points.Add(double.NaN)

This looks like zero enter image description here

And in my case it didn't crash with following SeriesChartTypes:

Column, Doughnut, FastPoint, Funnel, Kagi, Pie, Point, Polar, Pyramid, Radar, Renko, Spline, SplineArea, StackedBar, StackedColumn, ThreeLineBreak

The other way is a built-in concept of an empty point:

if (myChart.ChartAreas[0].AxisY.IsLogarithmic && y == 0)
    myChart.Series["mySeries"].Points.Add(new DataPoint { IsEmpty = true });

This looks like a missing point (a gap): enter image description here

And in my case it didn't crash with following SeriesChartTypes:

Area, Bar, Column, Doughnut, FastPoint, Funnel, Line, Pie, Point, Polar, Pyramid, Radar, Renko, Spline, SplineArea, StackedArea, StackedArea100, StackedBar, StackedBar100, StackedColumn, StackedColumn100, StepLine, ThreeLineBreak

The 2nd approach feels like the right (by design) one. The 1st one looks like a hack, that accidentally appears to work.

CSDev
  • 3,177
  • 6
  • 19
  • 37
3

You can do a trick. You obviously have to clear you series. When you start adding points to an empty points collection x-coordinates are generated automatically starting at 1. You can create surrogate x yourself and skip some x-values.

int x = 0;
foreach(var y in yValues)
{
    x++;
    if (myChart.ChartAreas[0].AxisY.IsLogarithmic && y == 0)
        continue;
    myChart.Series["mySeries"].Points.AddXY(x, y);
}

With bar-like chart types it will look like a missing value.

Eugee
  • 31
  • 5
  • My upvote for a good point. But what if chart type is `Line`? Then your approach gives something looking like a __repeated__ value, __not__ an empty one. – CSDev Aug 04 '19 at 18:04
  • 1
    @Alex thanks. I admit my solution has weak points. But so does any one. It's up to OP to decide which one to use in the situation to get the best result. – Eugee Aug 07 '19 at 17:13