How to Autopaint a Chart Series

How to Autopaint a Chart Series

Let's assume we use a Datasource and a Series Splitter when creating a Chart. This means that the number of Series to be displayed cannot be defined beforehand. Different Series are colored according to the specified Palette preset, but you can run into the situation when the number of Palette presets is limited.
In this example we will demonstrate how the undefined number of different Series could be painted with various color combinations.

To do this, you should execute the following code at report or Chart control loading:


 
  1. //Getting ChartArea
  2.  
  3. ChartArea chartArea = chart.GetByName("ChartArea1") as ChartArea;
  4.  
  5. //Creating a CustomPalette to which we can add colors manually
  6. //Initially it's empty
  7. CustomPalette palette = new CustomPalette();
  8.  
  9. //Setting a new chartArea palette
  10. chartArea.Palette = palette;
  11.  
  12. //Getting all Series from ChartArea
  13. Series[] series = chartArea.GetSeries();
  14. int numberOfColors = series.Length;
  15.  
  16. for (int i = 0; i < numberOfColors; i++)
  17. {
  18.  
  19.   //With the help of HSV system we can get an equidistant hue
  20. Color nextColor = ColorFromHSV((int)(360.0 * i / numberOfColors), 1, 1);
  21.  
  22. //Creating Item palettes
  23.    CustomPaletteItem item = new CustomPaletteItem();
  24.  
  25. //Setting a fill and a stroke for a palette
  26.    item.Fill = new SolidFill(nextColor);
  27.    item.Stroke = new SimpleStroke(LineStyle.Solid, nextColor, 5);
  28. //Adding an element to palette
  29.    palette.Items.Add(item);
  30. }
 

Follow the link to download a sample:http://perpetuumsoft.com/Support/M1/ChartAutoColoringReportSample2010.zip





For more information on HSV system, please refer to http://en.wikipedia.org/wiki/HSL_and_HSV


Conversion of HSV to RGB Color:

 
  1. /// Convert HSV to RGB
  2.        /// h is from 0-360
  3.        /// s,v values are 0-1
  4.        /// r,g,b values are 0-255
  5.        public static Color ColorFromHSV(double hue, double saturation, double value)
  6.        {
  7.            int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
  8.            double f = hue / 60 - Math.Floor(hue / 60);
  9.  
  10.            value = value * 255;
  11.            int v = Convert.ToInt32(value);
  12.            int p = Convert.ToInt32(value * (1 - saturation));
  13.            int q = Convert.ToInt32(value * (1 - f * saturation));
  14.            int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
  15.  
  16.            if (hi == 0)
  17.                return Color.FromArgb(255, v, t, p);
  18.            else if (hi == 1)
  19.                return Color.FromArgb(255, q, v, p);
  20.            else if (hi == 2)
  21.                return Color.FromArgb(255, p, v, t);
  22.            else if (hi == 3)
  23.                return Color.FromArgb(255, p, q, v);
  24.            else if (hi == 4)
  25.                return Color.FromArgb(255, t, p, v);
  26.            else
  27.                return Color.FromArgb(255, v, p, q);
  28.        }
 

Add Feedback