We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How can I have text in a Pie Chart Legend but a value in the point label?

I am trying to create a pie chart where my series contains 4 items.

I would like the legend to display the item text

I would like the point labels to show the value for each segment.

How can I achieve this?

Thanks

Nic

3 Replies

AT Anandaraj T Syncfusion Team April 9, 2018 10:55 AM UTC

Hi Nic, 

Thanks for using Syncfusion products. 

We have prepared a simple sample based on your requirements and it can be downloaded from the following link 

Query #1: I would like the legend to display the item text 

We can use the Text property of ChartLegendItem to customize the text displayed in a legend item. 

Please refer the following code snippet to achieve this 

[C#] 
 
            //Replace the text with custom string values of all legend items in pie chart 
            foreach (ChartLegendItem item in this.chartControl1.Legend.Items) 
            { 
                //LegendItem.Children is not null only for pie series or accumulation type series 
                int pointIndex = series.LegendItem.Children.IndexOf(item); 
 
                if (pointIndex != -1) 
                { 
                    //Customize the text displayed in legend item 
                    //You can use any string as value 
                    item.Text = series.Points[pointIndex].Category; 
                } 
                else //This is the case for first point in chart series 
                    item.Text = series.Points[0].Category; 
 
                //Customizing the text color of legend item 
                item.TextColor = Color.Black; 
            } 
 

Query #2: I would like the point labels to show the value for each segment 

We can use the Style.DisplayText property of ChartSeries to show or hide the data labels for points in a series. 

Please refer the following code snippet to achieve this 

[C#] 
 
            //Display label for points 
            series.Style.DisplayText = true; 
 

[Pie chart with different text in legend item and data labels] 
 
 
Please let us know if you have any concern. 

Regards, 
Anand


NA Nic Adams April 10, 2018 11:08 AM UTC

Thanks Anad,

I think I was misled by the fact that adding a data point with a text category causes an exception if you are using BeginUpdate(). I,e if I modify your sample:

            this.chartControl1.BeginUpdate();
            this.chartControl1.Text = "Car Sales in January";
            this.chartControl1.Series.Clear();
            ChartSeries series = new ChartSeries("Car Sales", ChartSeriesType.Pie);
            series.Points.Add("SUV's", 20);
            series.Points.Add("Van", 10);
            series.Points.Add("Minivan", 30);
            series.Points.Add("Sports", 5);
            series.Points.Add("Compact", 30);
            this.chartControl1.Series.Add(series);
                        
            this.chartControl1.EndUpdate();

You will get an exception:

System.NullReferenceException

  HResult=0x80004003

  Message=Object reference not set to an instance of an object.

  Source=Syncfusion.Chart.Base

  StackTrace:

   at Syncfusion.Windows.Forms.Chart.ChartSeriesRenderer.PrepearePoints()

   at Syncfusion.Windows.Forms.Chart.ChartSeriesRenderer.CanRender()

   at Syncfusion.Windows.Forms.Chart.ChartPlace.CalculateSpace(ChartSeriesCollection series, Boolean doManyAreas, IChartAreaHost chart)

   at Syncfusion.Windows.Forms.Chart.ChartSeriesCollection.DrawSeries(Graphics g, IChartAreaHost chart)

   at Syncfusion.Windows.Forms.Chart.ChartArea.Draw2D(PaintEventArgs e, ChartPaintFlags flags)

   at Syncfusion.Windows.Forms.Chart.ChartArea.Draw(PaintEventArgs e, ChartPaintFlags flags)

   at Syncfusion.Windows.Forms.Chart.ChartControl._Paint(Graphics g, Rectangle r)

   at Syncfusion.Windows.Forms.Chart.ChartControl.OnPaint(PaintEventArgs e)

   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)

   at System.Windows.Forms.Control.WmPaint(Message& m)

   at System.Windows.Forms.Control.WndProc(Message& m)

   at Syncfusion.Windows.Forms.Chart.ChartControl.WndProc(Message& m)

   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)



AT Anandaraj T Syncfusion Team April 11, 2018 03:25 PM UTC

Hi Nic, 

Thanks for the update. 

We have confirmed that the reported issue is a defect and logged a defect report. This issue will be fixed in our upcoming service pack release which is expected to be available by the end of April 2018. 

As a workaround solution, we can use the XAxis property of ChartSeries to avoid this issue. The modified sample can be downloaded from the following link 

Please refer the following code snippet to achieve this 

[C#] 
 
            //Setting X-axis for chart series 
            series.XAxis = this.chartControl1.PrimaryXAxis; 
 

Please let us know if you have any concern. 

Regards, 
Anand 


Loader.
Live Chat Icon For mobile
Up arrow icon