Skip to main content Skip to footer

How To: Set Color to Pie Chart Slices.

When we talk about official reports, Charting is one feature which plays a very important role while analyzing a report and sometimes in decision making. As a complete reporting solution, ActiveReports 7 allow users to create different types of charts. One of the most commonly used chart type is the Pie Chart. Even though the chart control within ActiveReports provides a way to select a pre-defined color palette for the pie chart slices, but a very common question from the users is to how to go about setting custom color for the individual slices. This blog explains how this functionality can be achieved. For this implementation, the first thing which you need to know is the count of data points in the series. This is required to loop through all the data points so that the color for individual slices can be set. To assign the color we need to use the BackdropItem class. Lets take a look how the code looks like:


int i = 0;  
int colorCount = 0;  
double seriesTotal = 0.0;  

for (i = 0; i <= s.Points.Count - 1; i++)  
{  
     BackdropItem bd = default(BackdropItem);  
     switch (colorCount)  
     {  
          case 0:  
              bd = new BackdropItem(System.Drawing.Color.Blue);  
              break;  
          case 1:  
              bd = new BackdropItem(System.Drawing.Color.Yellow);  
              break;  
          case 2:  
              bd = new BackdropItem(System.Drawing.Color.Violet);  
              break;  
          case 3:  
              bd = new BackdropItem(System.Drawing.Color.Red);  
              break;  
          case 4:  
              bd = new BackdropItem(System.Drawing.Color.Black);  
              break;  
          case 5:  
              bd = new BackdropItem(System.Drawing.Color.Pink);  
              break;  
          case 6:  
              bd = new BackdropItem(System.Drawing.Color.Purple);  
              break;  
          case 7:  
              bd = new BackdropItem(System.Drawing.Color.Gray);  
              break;  
          case 8:  
              bd = new BackdropItem(System.Drawing.Color.Green)  
              break;  
          case 9:  
              bd = new BackdropItem(System.Drawing.Color.White);  
              break;  
           case 10:  
              bd = new BackdropItem(System.Drawing.Color.Orange);  
              break;  
     }  

     colorCount = colorCount + 1;  
     s.Points[ i ].Properties["Backdrop"] = bd;  
     seriesTotal = seriesTotal + s.Points[ i ].YValues[0];  
     s.Points[ i ].LegendText = s.Points[ i ].XValue.ToString();  
}  

So basic concept is to iterate through all the points present in the series and set the color individually using the BackdropItem class. This is how the chart looks like: Download the attached samples for complete implementation. Please note that you might need to update the database path before running the sample. It uses Nwind.mdb as the data source. PieChartColor_C# PieChartColor_VB

MESCIUS inc.

comments powered by Disqus