2

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);    ????
TaW
  • 53,122
  • 8
  • 69
  • 111
joe
  • 61
  • 2
  • 6
  • What do you want to achieve: Make the DataPoints invisible but still take up space or supress them completely? – TaW Feb 18 '16 at 18:59
  • Actually, I simply wanted to hide zero Y values (in this case arrDouble3) in the line chart. perfectly plots the line but still shows a line. – joe Feb 19 '16 at 09:11
  • Taw, It perfectly works now. Many thanks. Sorry, I was not fully familiar with the voting procedure. – joe Mar 07 '16 at 16:29
  • @joe I used the following and it worked ... but i had to replace zeros with nulls in the array i used as data: `chart1.Series[0].EmptyPointStyle.IsValueShownAsLabel = false; chart1.Series[0].EmptyPointStyle.IsVisibleInLegend = true;` I wanted to show the legend of empty values, but it didn't work – Syed Irfan Ahmad Jul 05 '21 at 13:31

1 Answers1

1

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

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • TaW, many thanks for your comprehensive comment. Actually, I found this ( Foreach .. arrP.IsValueShownAsLabel = false; .. ) loop recommended in the stack overflow. If I understand you correctly the easiest a – joe Feb 19 '16 at 09:58
  • sorry lost the 2 part .. If I understand you correctly the easiest way is to remove Y zero DataPoints from the DataSource. But could I remove DataPoints from the DataSource .. ? Thanks. – joe Feb 19 '16 at 10:02
  • As I understand removing the DataPoint will be Many thanks for your help TaW – joe Feb 19 '16 at 10:11
  • I don't know about your DataSource. But you surely can create a copy without the zero-points..: `var arr2 = arr1.Select(x => x).Where(x => x != 0).ToArray();` – TaW Feb 19 '16 at 10:13
  • Well, marking them with [IsEmpty](https://msdn.microsoft.com/en-us/library/dd456677.aspx) sounds interesting, but for Line charts it will still lead to problems, so you may need to go for one of the two options in my answer after all. See the previous comment! Also the updated answer.. Can you tell me more about the DataSource(es)? – TaW Feb 19 '16 at 10:29
  • With it perfectly works for the line chart. I am in a development phase, where the DataSource so far is simply a 1D double array. – joe Feb 19 '16 at 11:18
  • Well, I'm glad to hear that is works for you. But the line does get interrupted, right? – TaW Feb 19 '16 at 11:54
  • Ok, I understand what you mean. In this case it's a calculation up to a certain date in time, where all zero values are at the end. But maybe using it for zero values in between might interrupt the line. – joe Feb 19 '16 at 15:00
  • Ah, that explains why you are happy. I'll update the answer to exaplain this case to make it more useful for future readers.. – TaW Feb 19 '16 at 15:28
  • I used the following and it worked ... but i had to replace zeros with nulls in the array i used as data: `chart1.Series[0].EmptyPointStyle.IsValueShownAsLabel = false; chart1.Series[0].EmptyPointStyle.IsVisibleInLegend = true;` I wanted to show the legend of empty values, but it didn't work – Syed Irfan Ahmad Jul 05 '21 at 13:33