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?
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
And in my case it didn't crash with following SeriesChartType
s:
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):
And in my case it didn't crash with following SeriesChartType
s:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With