Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an empty point to a charts with logarithmic scaling

Tags:

c#

mschart

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?

like image 926
sj47sj Avatar asked Sep 17 '12 17:09

sj47sj


2 Answers

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.

like image 123
Alex Avatar answered Oct 14 '22 14:10

Alex


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.

like image 28
Eugee Avatar answered Oct 14 '22 16:10

Eugee