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:
- //Getting ChartArea
-
- ChartArea chartArea = chart.GetByName("ChartArea1") as ChartArea;
-
- //Creating a CustomPalette to which we can add colors manually
- //Initially it's empty
- CustomPalette palette = new CustomPalette();
-
- //Setting a new chartArea palette
- chartArea.Palette = palette;
-
- //Getting all Series from ChartArea
- Series[] series = chartArea.GetSeries();
- int numberOfColors = series.Length;
-
- for (int i = 0; i < numberOfColors; i++)
- {
-
- //With the help of HSV system we can get an equidistant hue
- Color nextColor = ColorFromHSV((int)(360.0 * i / numberOfColors), 1, 1);
-
- //Creating Item palettes
- CustomPaletteItem item = new CustomPaletteItem();
-
- //Setting a fill and a stroke for a palette
- item.Fill = new SolidFill(nextColor);
- item.Stroke = new SimpleStroke(LineStyle.Solid, nextColor, 5);
- //Adding an element to palette
- palette.Items.Add(item);
- }
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:
- /// Convert HSV to RGB
- /// h is from 0-360
- /// s,v values are 0-1
- /// r,g,b values are 0-255
- public static Color ColorFromHSV(double hue, double saturation, double value)
- {
- int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
- double f = hue / 60 - Math.Floor(hue / 60);
-
- value = value * 255;
- int v = Convert.ToInt32(value);
- int p = Convert.ToInt32(value * (1 - saturation));
- int q = Convert.ToInt32(value * (1 - f * saturation));
- int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
-
- if (hi == 0)
- return Color.FromArgb(255, v, t, p);
- else if (hi == 1)
- return Color.FromArgb(255, q, v, p);
- else if (hi == 2)
- return Color.FromArgb(255, p, v, t);
- else if (hi == 3)
- return Color.FromArgb(255, p, q, v);
- else if (hi == 4)
- return Color.FromArgb(255, t, p, v);
- else
- return Color.FromArgb(255, v, p, q);
- }
Article ID: 580, Created: June 10, 2013 at 4:55 PM, Modified: June 20, 2013 at 3:29 PM