This is a migrated thread and some comments may be shown as answers.

Chart Legend Items not clearing

2 Answers 124 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 27 Jan 2011, 10:44 PM
I have a RadChart that has a dynamic set of lineSeries that i add to it after recieving the data from an end device.  When i try to clear the Chart for new data, the legend Items do not get cleared. 
private void AddTrendPoints(TrendFiles trendfile)
        {
            if (this.trendGraph.Dispatcher.Thread != Thread.CurrentThread)
            {
                this.trendGraph.Dispatcher.Invoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                {
                     
                    #region Setup Series
                    this.trendGraph.DefaultView.ChartArea.DataSeries.Clear();
                    this.trendGraph.DefaultView.ChartArea.Legend.Items.Clear();
                    this.trendGraph.SamplingSettings.SamplingThreshold = trendfile.Records;
                    DataSeries series = new DataSeries();
                    LineSeriesDefinition lineDefinition = new LineSeriesDefinition();
                    ChartLegendItem legendItem = new ChartLegendItem();
                    int count = 0;
  
                    foreach(TrendVariable trendVar in trendfile.Variables)
                    {
                        series = new DataSeries();
                        lineDefinition = new LineSeriesDefinition();
                        legendItem = new ChartLegendItem();
                        lineDefinition.ShowItemLabels = false;
                        lineDefinition.ShowPointMarks = false;
                        lineDefinition.SeriesName = trendVar.Name;
                        lineDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel;
                        series.LegendLabel = trendVar.Name;
                        series.Definition = lineDefinition;
                        series.Definition.Appearance.Stroke = colors[count];
                        series.Definition.Appearance.Fill = colors[count];
                        legendItem.Label = trendVar.Name;
                        legendItem.Background = colors[count];
                        legendItem.Foreground = Brushes.White;
                        trendGraph.DefaultView.ChartLegend.Items.Add(legendItem);
                          
                        foreach (DataPoint point in trendVar.points)
                        {
                            series.Add(point);
                        }
  
                        trendGraph.DefaultView.ChartArea.DataSeries.Add(series);
                         
                        count++;
                    }
  
                    #endregion
  
  
                    return null;
                }), null);
  
            }
            else
            {
                #region Setup Series
                this.trendGraph.DefaultView.ChartArea.DataSeries.Clear();
                this.trendGraph.DefaultView.ChartArea.Legend.Items.Clear();
  
                DataSeries series = new DataSeries();
                LineSeriesDefinition lineDefinition = new LineSeriesDefinition();
                ChartLegendItem legendItem = new ChartLegendItem();
                int count = 0;
  
                foreach (TrendVariable trendVar in trendfile.Variables)
                {
                    series = new DataSeries();
                    lineDefinition = new LineSeriesDefinition();
                    legendItem = new ChartLegendItem();
                    lineDefinition.ShowItemLabels = false;
                    lineDefinition.ShowPointMarks = false;
                    lineDefinition.SeriesName = trendVar.Name;
                    lineDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel;
                    series.LegendLabel = trendVar.Name;
                    series.Definition = lineDefinition;
                    series.Definition.Appearance.Stroke = colors[count];
                    series.Definition.Appearance.Fill = colors[count];
                    legendItem.Label = trendVar.Name;
                    legendItem.Background = colors[count];
                    legendItem.Foreground = Brushes.White;
                    trendGraph.DefaultView.ChartLegend.Items.Add(legendItem);
                    foreach (DataPoint point in trendVar.points)
                    {
                        series.Add(point);
                    }
                    trendGraph.DefaultView.ChartArea.DataSeries.Add(series);
                    count++;
                }
  
                #endregion
  
            }
  
        }


After the initial load, the legend items just append to the legend.  So i get repeat Series legend items.  Its not being cleared on the Legend.Items.Clear() method.

2 Answers, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 01 Feb 2011, 09:00 AM
Hello Brian,

Your series items can get repeated if you haven't turned off the automatic generation of legend items. You can do this using the following code:

trendGraph.DefaultView.ChartLegend.UseAutoGeneratedItems = false;

I attached a sample project that uses your code and when I turn off the automatic generation of legend items it all seams ok.

Hope this helps!


Regards,
Yavor Ivanov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Brian
Top achievements
Rank 1
answered on 02 Feb 2011, 05:53 PM
Ok, great...   thanks!
Tags
Chart
Asked by
Brian
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Brian
Top achievements
Rank 1
Share this question
or