Hide labels on series which have value of zero, in a chart.

6 posts, 0 answers
  1. panchooo
    panchooo avatar
    13 posts
    Member since:
    Jun 2009

    Posted 24 May 2010 Link to this post

    Hello,

    I'm currently working with telerik reporting, and I'm displaying a chart for every row of the datasource of the report.
    The chart displays a stacked bar chart type. I have a very similar issue like this post:

    The difference is that I'm using a report to display the chart, and since I'm in the environment of the report the chart control have only 4 events in which the ItemDataBoundItem, the code I wrote seems to have no visual effect over the chart:

    Private Sub chtDefectsByFamily_ItemDataBound(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chtDefectsByFamily.ItemDataBound 
            Dim ChartItem As Telerik.Reporting.Processing.Chart = DirectCast(sender, Telerik.Reporting.Processing.Chart) 
            Dim ci As Telerik.Reporting.Chart = TryCast(ChartItem.ItemDefinition, Telerik.Reporting.Chart) 
            For Each seriesItem As Telerik.Reporting.Charting.ChartSeries In ci.Series 
                seriesItem.Appearance.LabelAppearance.LabelLocation = Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Inside 
                seriesItem.Appearance.TextAppearance.TextProperties.Color = Color.White 
                For Each serieItem As Telerik.Reporting.Charting.ChartSeriesItem In seriesItem.Items 
                    If serieItem.YValue = 0 Then 
                        serieItem.Label.Visible = False 
                    End If 
                    serieItem.SetDirty() 
                Next 
                seriesItem.SetDirty() 
            Next 
        End Sub 

    Is it possible to modify the chart during/after the binding of the data?? or is there any other way to hide the labels before the chart is shown?
  2. Peter
    Admin
    Peter avatar
    1611 posts

    Posted 27 May 2010 Link to this post

    Hello panchooo,

    As you have noticed the Telerik Reporting Chart is quite different. From your explanation I understand that you are using the Chart's Datasource property to set a datasource, that leads to automatic creation of Chart Series. Those automatically created series can't be modified from events.

    Our suggestion would be to use the Chart's NeedDataSource event to manually create and populate the Series.
    Check out the following help articles:

    Data Binding Chart to a Database Object

    Sincerely yours, Peter
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 30 Jan 2011 Link to this post

    Hi Peter,

    I have a same issue as panchooo and here is what I did in the NeedDataSource event handler.
    • I am using Q3 2010 Release
    • I created a DataTable and populated it using SQL query
    • Assigned the datatable as DataSource to the chart
    • Created ChartSeries and added to the ItemDefinition of the chart
    The chart displays fine except for the labels that displays the zero. I added this code to hide the labels:
                            chartDef.Series.Clear();
                chartDef.Series.Add(mySeries);
     
                foreach (ChartSeriesItem myitem in chartDef.Series[0].Items)
                {
                    if (myitem.YValue == 0)
                    {
                        myitem.Label.Visible = false;
                    }
                }

    Result:
    • I still see the zeros in the label.
    I also checked if it can actually read the series. As a test, I can display the series count in a label but cannot display any of the SeriesItemYvalue. It just returns zero.

    UPDATED:

    I moved the "foreach" loop into ItemDataBound event of the chart. While stepping through the code, I can see the YValues and the test if the value is zero being carried out. It even steps into set the visible property to false. But the final output still shows the label with 0.0.

    Am I missing a statement to update the view? If yes, could you please let me know what it is?

  5. Peter
    Admin
    Peter avatar
    1611 posts

    Posted 01 Feb 2011 Link to this post

    Hello Deepak Shakya,

    Based on your explanation I understand that you are using the Chart's Datasource property to set a datasource, that leads to automatic creation of Chart Series. Those automatically created series can't be modified from events. Thus our suggestion is to create your chart programmatically as elaborated in the Creating Chart Programmatically help article.

    Best wishes,
    Peter
    the Telerik team
    Get started with Telerik Reporting with numerous videos and detailed documentation.
  6. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 01 Feb 2011 Link to this post

    Hi Peter,

    Thanks for your reply. So are you saying that because I used the Datasource property of the Chart in the NeedDataSource event, I can't change the series programmatically?

    • Could you please send me an example as to how I can populate the series using the queried data or simply a DataTable?

    • The link you provided to create the chart programatically only show how you actually hard code the data in the series. How do I transverse through the table to make up a series of data for the chart?
    •  The example also does not show how to assign the "DataLabelsColumn". How do we do this without setting the "DataSource" property in the chart?
    • I am able to show/hide the labels completely from NeedDataSource event. Its just that I can't do it for individual items in the series.

    • I created a chart in Designer to get the positioning correct. Do I have to create the chart programmatically to be able to show/hide the labels for individual items? It would be great if I didn't have to do this.

    Your help is highly appreciated. Thanking you in advance.

     

  7. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 03 Feb 2011 Link to this post

    Hi Peter,
    • Just wanted to let you know that I have solved the problem by generating the Chart programatically instead of using the DataSource property.
    • I have also been able to generate X-Axis Labels programmatically
    • //Print X-Axis Labels
                  for (int j = 0; j < myDataset.Tables[0].Columns.Count; j++)
                  {
                      if (myDataset.Tables[0].Columns[j].ColumnName == "fldShortName")
                      {
       
                          for (int i = 0; i < myDataset.Tables[0].Rows.Count; i++)
                          {
                              ChartAxisItem axisItem = new ChartAxisItem(myDataset.Tables[0].Rows[i][j].ToString());
                              chartDef.PlotArea.XAxis.AddItem(axisItem);
                          }
                          break;
                      }
                  }
    • I also generated the series programmatically and hide zero value labels.
    • //Generate Series Bar Graph
                  for (int j = 0; j < myDataset.Tables[0].Columns.Count; j++)
                  {
                      if (myDataset.Tables[0].Columns[j].ColumnName == "fldData")
                      {
                          ChartSeries nSeries = new ChartSeries("Data", ChartSeriesType.Bar);
                          for (int i = 0; i < myDataset.Tables[0].Rows.Count; i++)
                          {
                              double _value;
                              double.TryParse(myDataset.Tables[0].Rows[i][j].ToString(), out _value);
                              ChartSeriesItem myItem = new ChartSeriesItem(_value);
                              if (_value == 0)
                              {
                                  myItem.Label.Visible = false;
                              }
                              nSeries.AddItem(myItem);
                          }
       
                          nSeries.Appearance.EmptyValue.Mode = Telerik.Reporting.Charting.Styles.EmtyValuesMode.Zero;
                          nSeries.Appearance.Corners.TopRight = Telerik.Reporting.Charting.Styles.CornerType.Round;
                          nSeries.Appearance.Corners.TopLeft = Telerik.Reporting.Charting.Styles.CornerType.Round;
                          nSeries.DefaultLabelValue = "#Y{N1}";
                          nSeries.Appearance.BarWidthPercent = 50;
                          chartDef.Series.Add(nSeries);
                          break;
                      }
                  }
    • Thank you once again for viewing this entry.
     
Back to Top
DevCraft banner