joe joe - 1 year ago 163
C# Question

Chart simply hiding zero values

I have a simply C# chart binding code (which works well), but need to setup a loop for hiding the zero values. The loop is also perfectly working for the DataPoint arrP using the data from the arrDouble3 array.

But how to print now the modified chart then (obviously the last line doesn't work).. many thanks.

chart7.Series["Series3"].ChartType = SeriesChartType.Line;
chart7.Series["Series3"].Points.DataBindXY(xVal, arrDouble3);

foreach (Series series in chart7.Series)
foreach (DataPoint arrP in series.Points)
if (arrP.YValues.Length > 0 && (double)arrP.YValues.GetValue(0) == 0)
arrP.IsValueShownAsLabel = false;

chart7.Series["Series3"].Points.DataBindXY(xVal, arrP); ????

Answer Source

DataPoint does not have a Visible property, so you can't really hide a point.

For severaly chart types, like point, column or bar you can 'fake' hiding points by setting the DataPoint.Color to transparent or to the chart's BackColor, but this will not work for a line chart as it will result in an invisible line fragment that breaks the line of the series.

There is a property IsEmpty which you can set for some DataPoints but the result will still break the lines, no matter how you set the Series.EmptyPointStyle :

In the case of line-type charts, the line color for lines that connect to an empty point is determined by the Color property setting.

So, no matter how you create the points, by adding or by data binding, you will not be able to hide some points.

Instead I see two options:

  • You can simply remove the points that have a Y-Value of zero.

  • Or you can manipulate the points in such a way that makes them blend in with the other lines as if they were not there at all: For this you need to set the Y-Values to a suitable mean value so that both the incoming and the outgoing line have the same slope as the line that would connect the neighboring points.

The latter way is simpler when the x-Value intervals are equal; the real issue is that you lose the information about the Y-Values actually being zero. You can note this fact in the Tag property of the points. - You also need to think about the first and last points, as they have only one neighr and about severaly zeor-points in a row..

The former way is straight forward and you could do it in a loop, in fact in the loop you already have. The issue you may have here is what to do when yu need to access the data of those points. One option is to collect them is a List, or if you prefer in a Dictionary where the index would be the key.

Of course you can't actually remove the DataPointsas they are data bound, so you need to remove them from the DataSource or if you prefer, create an additional source for binding without the zero-values..:

var arrayNZ = array1.Select(x => x).Where(x => x != 0).ToArray();

You may need to adapt the X-Values to use now!

Update: The problem of the broken lines may be moot is all your 'hidden' points sit at one end of the chart.

So all you need to do is add one line to your loop:

  arrP.IsValueShownAsLabel = false;                   
  arrP.IsEmpty = true;                   

It would be even simpler if you could add this as an extended property to the binding by using the Series.DataBind method; but as I have shown here only a fraction of the bindable properties actually work with this call..

Here is how the 'broken' lines look when they are in the middle of the points: enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download