Generic Chart Color Manager class that can be used for any charts

We use many third-party controls to apply colors/manage chart stuff. This generic class removes all confusions.

How to use:

public ChartColorManager.ColorSet CurrentColorSet

{

get { return _highContrast ? ChartColorManager.HighContrastColorSet : ChartColorManager.DefaultColorSet; }

}

ChartColorManager.ColorSet colors = CurrentColorSet;





using System; using System.Collections; using System.Drawing; namespace Common.Charts { /// <summary> /// Summary description for ChartColorManager. /// </summary> public class ChartColorManager { public class ColorSet { private string _name; private int _groupsize; private Color[] _colors; public ColorSet(string name, Color[] colors, int groupsize) { _name = name; if ((colors == null) || (colors.Length == 0)) throw new ArgumentNullException("colors", "colors cannot be null or empty."); if (groupsize <= 0) throw new ArgumentException("groupsize must be >0.", "groupsize"); if ((colors.Length%groupsize) != 0) throw new ArgumentException("# colors must be an exact multiple of groupsize."); _groupsize = groupsize; _colors = colors; } public string Name { get { return _name; } } public int GroupSize { get { return _groupsize; } } public int GroupCount { get { return _colors.Length/_groupsize; } } /// <summary> /// Get a color from this set. For indices larger than the number of colors in the set, /// the ColorSet will 'wrap around'. /// </summary> /// <remarks> /// This property may be safely invoked for indexes greater than the number of colors in this set. /// </remarks> public Color this[int index] { get { if (index < 0) throw new IndexOutOfRangeException("Colorset index must >=0."); return _colors[index%_colors.Length]; } } /// <summary> /// Obtain the group of colors at the given group index. /// </summary> /// <returns></returns> public Color[] GetColorGroup(int groupIndex) { if ((groupIndex < 0) || (groupIndex >= GroupCount)) throw new ArgumentOutOfRangeException("groupIndex", "groupIndex must be >0 and <=GroupCount."); Color[] retval = new Color[_groupsize]; Array.Copy(_colors, groupIndex*_groupsize, retval, 0, retval.Length); return retval; } } private static readonly int[,] _stdColorVals = new int[18,3] { {166, 203, 222}, {91, 176, 226}, {78, 116, 206}, {184, 192, 251}, {150, 145, 221}, {94, 110, 178}, {204, 255, 255}, {7, 205, 227}, {51, 153, 153}, {169, 208, 83}, {98, 196, 98}, {0, 128, 1}, {242, 189, 0}, {213, 166, 0}, {227, 108, 0}, {204, 153, 153}, {227, 45, 62}, {172, 65, 64} }; private static readonly int[,] _hcColorVals = new int[15,3] { {81, 121, 214}, {102, 204, 102}, {250, 215, 0}, {97, 189, 242}, {204, 255, 204}, {244, 121, 0}, /* {117,136,221}, */ {47, 94, 140}, {7, 186, 206}, {186, 229, 92}, {255, 199, 0}, {189, 230, 252}, {199, 199, 199}, {173, 168, 255}, {47, 166, 117}, {31, 210, 230} }; /* private static readonly int[,] _hcColorVals = new int[18,3] { {31,210,230}, {177,213,25}, {243,195,25}, {25,163,255}, {225,255,25}, {225,163,25}, {107,184,229}, {113,202,113}, {217,175,25}, {71,117,209}, {103,181,103}, {255,25,36}, {191,198,251}, {88,231,122}, {255,163,71}, {160,156,224}, {209,255,255}, {71,163,163} }; */ public static ColorSet Create(string name, int[,] rgbValues, int groupSize) { if (rgbValues == null) throw new ArgumentNullException("rgbValues"); Color[] colors = new Color[rgbValues.GetLength(0)]; for (int i = 0; i < colors.Length; ++i) colors[i] = Color.FromArgb(rgbValues[i, 0], rgbValues[i, 1], rgbValues[i, 2]); return new ColorSet(name, colors, groupSize); } public static ColorSet DefaultColorSet { get { return _defaultSet; } } public static ColorSet HighContrastColorSet { get { return _highContrast; } } private static Hashtable _colorSets = new Hashtable(); private static ColorSet _defaultSet; private static ColorSet _highContrast; static ChartColorManager() { try { Add(Create("T1P_Standard", _stdColorVals, 3), true); Add(Create("T1P_HighContrast", _hcColorVals, 3)); SetHighContrast("T1P_HighContrast"); } catch (Exception ex) { // Exceptions in static constructors are notoriously difficult to debug. // Try to make it a little easier: throw new ApplicationException("Error initializing ChartColorManager: " + ex.Message, ex); } } public static void Add(ColorSet colorset) { Add(colorset, false); } /// <summary> /// Add a ColorSet to the manager. /// </summary> /// <param name="colorset"></param> /// <param name="setDefault"></param> /// <remarks>If a ColorSet of the same has already been added, it will be replaced with the new ColorSet.</remarks> public static void Add(ColorSet colorset, bool setDefault) { _colorSets[colorset.Name] = colorset; if (setDefault) SetDefault(colorset.Name); } public static void SetDefault(string colorSetName) { if (!_colorSets.Contains(colorSetName)) throw new ArgumentException(String.Format("Colorset '{0}' not found.", colorSetName), "colorSetName"); _defaultSet = _colorSets[colorSetName] as ColorSet; } public static void SetHighContrast(string colorSetName) { if (!_colorSets.Contains(colorSetName)) throw new ArgumentException(String.Format("Colorset '{0}' not found.", colorSetName), "colorSetName"); _highContrast = _colorSets[colorSetName] as ColorSet; } } }

By [)ia6l0 iii   Popularity  (1149 Views)