Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#

Programmatically Setting a Silverlight Chart ControlTemplate

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
15 Jul 2012CPOL2 min read 20.5K   9   2
Demonstrates how to code a Silverlight Chart ControlTemplate Programmatically.

Introduction

We were looking for a way to create a ControlTemplate for a Chart control in Silverlight programmatically (code behind). After much searching and piecing together information from blogs, tips, and coder sites, we came up with a way to create the ControlTemplate from the code behind.

This works fairly simple. Put a chart control in the XAML portion, give it an x:Name, then create the chart innards from the code behind.

 

The MainPage.xaml

Below is the Chart XAML set on the main page with the name 'cChart' . Simple, just a chart and some data points to show the columns. No ColumnSeries, Axis, Style, or ControlTemplate set.  

XML
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  mc:Ignorable="d"
  xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
  xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
  x:Class="ChartTesting0.MainPage"
  d:DesignWidth="800"
  d:DesignHeight="600">
  <Grid x:Name="LayoutRoot">
    <StackPanel>
      <chartingToolkit:Chart x:Name="cChart" Title="Programmatically Setting Chart ControlTemplate Example" Width="600" Height="450">
        <chartingToolkit:Chart.DataContext>
          <PointCollection>
            <Point X="0" Y="1"/>
            <Point X="1" Y="4"/>
            <Point X="2" Y="9"/>
            <Point X="3" Y="16"/>
            <Point X="4" Y="25"/>
            <Point X="5" Y="36"/>
            <Point X="6" Y="49"/>
            <Point X="7" Y="64"/>
            <Point X="8" Y="81"/>
            <Point X="9" Y="100"/>
          </PointCollection>
        </chartingToolkit:Chart.DataContext>
      </chartingToolkit:Chart>
    </StackPanel>
  </Grid>
</UserControl> 

The Code Behind - MainPage.xaml.cs   

This adds to the cChart by setting up a ControlTemplate then placing it into the ColumnDataPoint style which is then added to a ColumnSeries and finally added to the cChart.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows.Markup;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;

namespace ChartTesting0
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
      // Setup ControlTemplate variables
      ControlTemplate ct; // ControlTemplate to be modified later
      string ctXamlString = ""; // XAML String used to modify ControlTemplate

      // Setup color variables
      int a = 3; // Number of Column Series
      Brush BarColor; // Brush to be set later
      string BarColorsAre = ""; // String to show Column colors in ControlTemplate ToolTip

      for (int i = 0; i < a; i++)
      {
        // Setup the ColumnDataPoint style for the Chart
        var cdpStyle = new Style(typeof(ColumnDataPoint));

        // Set Background color for Column
        BarColor = JADEXCODEColor.JADEColor.HCBPAtoARGB(((double)i / a), 1.0, 0.5, 0.5, 0.0);
        var bg = new Setter(ColumnDataPoint.BackgroundProperty, BarColor);
        cdpStyle.Setters.Add(bg); // Add the background color to the ColoumnDataPoint style

        // Clear the outer border of the ControlTemplate, not needed with in controls being corner rounded.
        var br = new Setter(ColumnDataPoint.BorderBrushProperty, new SolidColorBrush(Colors.Transparent));
        cdpStyle.Setters.Add(br);

        // Setup ToolTip XamlString insert.
        BarColorsAre = "Red = " + ((SolidColorBrush)BarColor).Color.R.ToString() + ", " +
                    "Green = " + ((SolidColorBrush)BarColor).Color.G.ToString() + ", " +
                    "Blue = " + ((SolidColorBrush)BarColor).Color.B.ToString() + ", " +
                    "Alpha = " + ((SolidColorBrush)BarColor).Color.A.ToString();

        // Setup XamlString to be loaded later.
        ctXamlString =
"<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:chartingToolkit=\"clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" TargetType=\"chartingToolkit:ColumnDataPoint\">" +
"<Border x:Name=\"Root\" Opacity=\"0\" BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\">" +
"<VisualStateManager.VisualStateGroups>" +
"<VisualStateGroup x:Name=\"CommonStates\">" +
"<VisualStateGroup.Transitions>" +
"<VisualTransition GeneratedDuration=\"0:0:0.1\"/>" +
"</VisualStateGroup.Transitions>" +
"<VisualState x:Name=\"Normal\"/>" +
"<VisualState x:Name=\"MouseOver\">" +
"<Storyboard>" +
"<DoubleAnimation Duration=\"0\" Storyboard.TargetName=\"MouseOverHighlight\" Storyboard.TargetProperty=\"Opacity\" To=\"0.6\"/>" +
"</Storyboard>" +
"</VisualState>" +
"</VisualStateGroup>" +
"<VisualStateGroup x:Name=\"SelectionStates\">" +
"<VisualStateGroup.Transitions>" +
"<VisualTransition GeneratedDuration=\"0:0:0.1\"/>" +
"</VisualStateGroup.Transitions>" +
"<VisualState x:Name=\"Unselected\"/>" +
"<VisualState x:Name=\"Selected\">" +
"<Storyboard>" +
"<DoubleAnimation Duration=\"0\" Storyboard.TargetName=\"SelectionHighlight\" Storyboard.TargetProperty=\"Opacity\" To=\"0.6\"/>" +
"</Storyboard>" +
"</VisualState>" +
"</VisualStateGroup>" +
"<VisualStateGroup x:Name=\"RevealStates\">" +
"<VisualStateGroup.Transitions>" +
"<VisualTransition GeneratedDuration=\"0:0:0.5\"/>" +
"</VisualStateGroup.Transitions>" +
"<VisualState x:Name=\"Shown\">" +
"<Storyboard>" +
"<DoubleAnimation Duration=\"0\" Storyboard.TargetName=\"Root\" Storyboard.TargetProperty=\"Opacity\" To=\"1\"/>" +
"</Storyboard>" +
"</VisualState>" +
"<VisualState x:Name=\"Hidden\">" +
"<Storyboard>" +
"<DoubleAnimation Duration=\"0\" Storyboard.TargetName=\"Root\" Storyboard.TargetProperty=\"Opacity\" To=\"0\"/>" +
"</Storyboard>" +
"</VisualState>" +
"</VisualStateGroup>" +
"</VisualStateManager.VisualStateGroups>" +

//----------
// Uncomment the following lines to reset the ToolTipService for showing Coulmn Bar values
//"<ToolTipService.ToolTip>" +
//"<ContentControl Content=\"{TemplateBinding FormattedDependentValue}\"/>" +
//"</ToolTipService.ToolTip>" +
//----------

//----------
// Uncomment the following line when Uncommenting the above ToolTipService lines.
//"<Grid Margin=\"-1,0,0,1\">" +
// Comment Out the following line when Uncommenting the above ToolTipService lines.
"<Grid Margin=\"-1,0,0,0\" ToolTipService.ToolTip=\"Bar Colors are " + BarColorsAre + "\">" +
//----------

"<Rectangle RadiusX=\"2\" RadiusY=\"2\" Fill=\"{TemplateBinding Background}\"/>" +
"<Rectangle RadiusX=\"2\" RadiusY=\"2\">" +
"<Rectangle.Fill>" +
"<LinearGradientBrush>" +
"<GradientStop Color=\"#00000000\" Offset=\"0.00\"/>" +
"<GradientStop Color=\"#88000000\" Offset=\"1.50\"/>" +
"</LinearGradientBrush>" +
"</Rectangle.Fill>" +
"</Rectangle>" +
"<Border CornerRadius=\"2\" BorderBrush=\"#88888888\" BorderThickness=\"0.5\">" +
"<Border CornerRadius=\"2\" BorderBrush=\"#44888888\" BorderThickness=\"0.5\"/>" +
"</Border>" +
"<Rectangle x:Name=\"SelectionHighlight\" Fill=\"Red\" Opacity=\"0\"/>" +
"<Rectangle x:Name=\"MouseOverHighlight\" Fill=\"White\" Opacity=\"0\"/>" +
"</Grid>" +
"</Border>" +
"</ControlTemplate>"
        ;

        // Load ControlTemplate XamlString
        ct = (ControlTemplate)XamlReader.Load(ctXamlString);
        var template = new Setter(ColumnDataPoint.TemplateProperty, ct);
        cdpStyle.Setters.Add(template); // Add the ControlTemplate to the ColumnDataPoint style
       
        // Setup the ColumnSeries and set the values and style
        ColumnSeries cs = new ColumnSeries();
        cs.Title = String.Format("Column {0}", i);
        cs.IndependentValuePath = "X";
        cs.DependentValuePath = "Y";
        cs.SetBinding(ColumnSeries.ItemsSourceProperty, new Binding());
        cs.DataPointStyle = cdpStyle;
        cChart.Series.Add(cs);
      }

      // Add Axis labels
      var cax = new CategoryAxis() { Orientation = AxisOrientation.X, Title = "Data" };
      cChart.Axes.Add(cax);
      var lax = new LinearAxis() { Orientation = AxisOrientation.Y, Title = "Value" };
      cChart.Axes.Add(lax);
    }
  }
} 

Additional Code for Colors

We never really liked working with raw colors like argb. Most of the time we were guessing and the process of coming up with a pleasing color scheme never really worked. The control was never good, so we searched for a few ways and found some good information on color at http://en.wikipedia.org/wiki/HSL_and_HSV. From there we merged the two different types of scales, Chroma and Saturation, to come up with one movable scale to work with. It can be all Chroma, all Saturation, or somewhere in between.

The JADEXCODEColor.cs  

C#
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ChartTesting0.Globals;

namespace ChartTesting0.JADEXCODEColor
{
  public class JADEColor
  {
    /// <summary>
    /// Returns the proper ARGB color Brush for a given Hue, Contrast, Brightness, Palette, and Transparency.
    /// </summary>
    /// <param name="Hue">
    /// A double value 0.0 to 1.0, Color Cyclical {0.0000 = Red, 0.1667 = Yellow, 0.3333 = Green, 0.5000 = Cyan, 0.6667 = Blue, 0.8333 = Magenta, 1.0000 = Red}
    /// </param>
    /// <param name="Contrast">
    /// A double value 0.0 to 1.0, Amount of Color {0.0 = No Color/Gray Scale, 0.5 = Some Color, Gray Appearance, 1.0 = All Color}
    /// </param>
    /// <param name="Brightness">
    /// A double value 0.0 to 1.0, Dark/Light of Color {0.0 = Black to Dark, 0.5 = Dim Dark/Light Appearance, 1.0 = Light to White}
    /// </param>
    /// <param name="Palette">
    /// A double value 0.0 to 1.0, Chroma/Saturation Scale of Colors {0.0 = All Chroma Scale (Vibrant), 0.5 = Chroma Saturation Mixed (Vigorous), 1.0 = All Saturation Scale (Vivid)}
    /// </param>
    /// <param name="Transparency">
    /// A double value 0.0 to 1.0, Translucency of Colors {0.0 = No Show Through / All Color, 0.5 = Some Show Through / Some Color, 1.0 = All Show Through / No Color}
    /// </param>
    /// <returns>
    /// Brush ARGB
    /// </returns>
    public static Brush HCBPAtoARGB(double Hue, double Contrast, double Brightness, double Palette, double Transparency)
    {
      //Palette = 0.0  [All Chroma Palette]  to  Palette = 1.0  [All Saturation Palette]
      //Hue, Contrast, Brightness = 0.0 to 1.0 Scale
      double A = 1f - Transparency;
      
      if (A > 1f) A = 1f;

      if (A < 0f) A = 0f;

      Brush brushColor = new SolidColorBrush(Color.FromArgb((byte)(255f * A), (byte)(255f * HCBPtoR(Hue, Contrast, Brightness, Palette)), (byte)(255f * HCBPtoG(Hue, Contrast, Brightness, Palette)), (byte)(255f * HCBPtoB(Hue, Contrast, Brightness, Palette))));

      return brushColor;
    }


    public static double HCBPtoR(double Hue, double Contrast, double Brightness, double Palette)
    {
      double R = 0f;
      //Palette = 0.0  [All Chroma],  Palette = 1.0  [All Saturation]
      //Chroma Scale
      double r0 = 0f;
      double g0 = 0f;
      double b0 = 0f;
      double h0 = 0f;
      double c0 = 0f;
      double m0 = 0f;
      double x0 = 0f;
      //Saturation Scale
      double r1 = 0f;
      double g1 = 0f;
      double b1 = 0f;
      double h1 = 0f;
      double c1 = 0f;
      double m1 = 0f;
      double x1 = 0f;
      

      c0 = Contrast;
      c1 = (1f - Math.Abs(2f * Brightness - 1f)) * Contrast;
      h0 = Hue - Functions.IntPart(Hue);
      h1 = Hue - Functions.IntPart(Hue);
      x0 = c0 * (1f - Math.Abs(Functions.Modulus((6f * h0), 2f) - 1f));
      x1 = c1 * (1f - Math.Abs(Functions.Modulus((6f * h1), 2f) - 1f));

      if ((((0f / 6f) <= h0) & (h0 < (1f / 6f))))
      {
        r0 = c0;
        g0 = x0;
        b0 = 0f;
      }
      else if (((1f / 6f) <= h0) & (h0 < (2f / 6f)))
      {
        r0 = x0;
        g0 = c0;
        b0 = 0f;
      }
      else if (((2f / 6f) <= h0) & (h0 < (3f / 6f)))
      {
        r0 = 0f;
        g0 = c0;
        b0 = x0;
      }
      else if (((3f / 6f) <= h0) & (h0 < (4f / 6f)))
      {
        r0 = 0f;
        g0 = x0;
        b0 = c0;
      }
      else if (((4f / 6f) <= h0) & (h0 < (5f / 6f)))
      {
        r0 = x0;
        g0 = 0f;
        b0 = c0;
      }
      else if (((5f / 6f) <= h0) & (h0 < (6f / 6f)))
      {
        r0 = c0;
        g0 = 0f;
        b0 = x0;
      }

      if ((((0f / 6f) <= h1) & (h1 < (1f / 6f))))
      {
        r1 = c1;
        g1 = x1;
        b1 = 0f;
      }
      else if (((1f / 6f) <= h1) & (h1 < (2f / 6f)))
      {
        r1 = x1;
        g1 = c1;
        b1 = 0f;
      }
      else if (((2f / 6f) <= h1) & (h1 < (3f / 6f)))
      {
        r1 = 0f;
        g1 = c1;
        b1 = x1;
      }
      else if (((3f / 6f) <= h1) & (h1 < (4f / 6f)))
      {
        r1 = 0f;
        g1 = x1;
        b1 = c1;
      }
      else if (((4f / 6f) <= h1) & (h1 < (5f / 6f)))
      {
        r1 = x1;
        g1 = 0f;
        b1 = c1;
      }
      else if (((5f / 6f) <= h1) & (h1 < (6f / 6f)))
      {
        r1 = c1;
        g1 = 0f;
        b1 = x1;
      }

      m0 = Brightness - (0.3f * r0 + 0.59f * g0 + 0.11f * b0);
      m1 = Brightness - (c1 / 2f);

      R = (1f - Palette) * (r0 + m0) + Palette * (r1 + m1);

      if (R > 1f) R = 1f;

      if (R < 0f) R = 0f;

      return R;
    }

    public static double HCBPtoG(double Hue, double Contrast, double Brightness, double Palette)
    {
      double G = 0f;
      //Palette = 0.0  [All Chroma],  Palette = 1.0  [All Saturation]
      //Chroma Scale
      double r0 = 0f;
      double g0 = 0f;
      double b0 = 0f;
      double h0 = 0f;
      double c0 = 0f;
      double m0 = 0f;
      double x0 = 0f;
      //Saturation Scale
      double r1 = 0f;
      double g1 = 0f;
      double b1 = 0f;
      double h1 = 0f;
      double c1 = 0f;
      double m1 = 0f;
      double x1 = 0f;


      c0 = Contrast;
      c1 = (1f - Math.Abs(2f * Brightness - 1f)) * Contrast;
      h0 = Hue - Functions.IntPart(Hue);
      h1 = Hue - Functions.IntPart(Hue);
      x0 = c0 * (1f - Math.Abs(Functions.Modulus((6f * h0), 2f) - 1f));
      x1 = c1 * (1f - Math.Abs(Functions.Modulus((6f * h1), 2f) - 1f));

      if ((((0f / 6f) <= h0) & (h0 < (1f / 6f))))
      {
        r0 = c0;
        g0 = x0;
        b0 = 0f;
      }
      else if (((1f / 6f) <= h0) & (h0 < (2f / 6f)))
      {
        r0 = x0;
        g0 = c0;
        b0 = 0f;
      }
      else if (((2f / 6f) <= h0) & (h0 < (3f / 6f)))
      {
        r0 = 0f;
        g0 = c0;
        b0 = x0;
      }
      else if (((3f / 6f) <= h0) & (h0 < (4f / 6f)))
      {
        r0 = 0f;
        g0 = x0;
        b0 = c0;
      }
      else if (((4f / 6f) <= h0) & (h0 < (5f / 6f)))
      {
        r0 = x0;
        g0 = 0f;
        b0 = c0;
      }
      else if (((5f / 6f) <= h0) & (h0 < (6f / 6f)))
      {
        r0 = c0;
        g0 = 0f;
        b0 = x0;
      }

      if ((((0f / 6f) <= h1) & (h1 < (1f / 6f))))
      {
        r1 = c1;
        g1 = x1;
        b1 = 0f;
      }
      else if (((1f / 6f) <= h1) & (h1 < (2f / 6f)))
      {
        r1 = x1;
        g1 = c1;
        b1 = 0f;
      }
      else if (((2f / 6f) <= h1) & (h1 < (3f / 6f)))
      {
        r1 = 0f;
        g1 = c1;
        b1 = x1;
      }
      else if (((3f / 6f) <= h1) & (h1 < (4f / 6f)))
      {
        r1 = 0f;
        g1 = x1;
        b1 = c1;
      }
      else if (((4f / 6f) <= h1) & (h1 < (5f / 6f)))
      {
        r1 = x1;
        g1 = 0f;
        b1 = c1;
      }
      else if (((5f / 6f) <= h1) & (h1 < (6f / 6f)))
      {
        r1 = c1;
        g1 = 0f;
        b1 = x1;
      }

      m0 = Brightness - (0.3f * r0 + 0.59f * g0 + 0.11f * b0);
      m1 = Brightness - (c1 / 2f);

      G = (1f - Palette) * (g0 + m0) + Palette * (g1 + m1);

      if (G > 1f) G = 1f;
      
      if (G < 0f) G = 0f;

      return G;
    }

    public static double HCBPtoB(double Hue, double Contrast, double Brightness, double Palette)
    {
      double B = 0f;
      //Palette = 0.0  [All Chroma],  Palette = 1.0  [All Saturation]
      //Chroma Scale
      double r0 = 0f;
      double g0 = 0f;
      double b0 = 0f;
      double h0 = 0f;
      double c0 = 0f;
      double m0 = 0f;
      double x0 = 0f;
      //Saturation Scale
      double r1 = 0f;
      double g1 = 0f;
      double b1 = 0f;
      double h1 = 0f;
      double c1 = 0f;
      double m1 = 0f;
      double x1 = 0f;


      c0 = Contrast;
      c1 = (1f - Math.Abs(2f * Brightness - 1f)) * Contrast;
      h0 = Hue - Functions.IntPart(Hue);
      h1 = Hue - Functions.IntPart(Hue);
      x0 = c0 * (1f - Math.Abs(Functions.Modulus((6f * h0), 2f) - 1f));
      x1 = c1 * (1f - Math.Abs(Functions.Modulus((6f * h1), 2f) - 1f));

      if ((((0f / 6f) <= h0) & (h0 < (1f / 6f))))
      {
        r0 = c0;
        g0 = x0;
        b0 = 0f;
      }
      else if (((1f / 6f) <= h0) & (h0 < (2f / 6f)))
      {
        r0 = x0;
        g0 = c0;
        b0 = 0f;
      }
      else if (((2f / 6f) <= h0) & (h0 < (3f / 6f)))
      {
        r0 = 0f;
        g0 = c0;
        b0 = x0;
      }
      else if (((3f / 6f) <= h0) & (h0 < (4f / 6f)))
      {
        r0 = 0f;
        g0 = x0;
        b0 = c0;
      }
      else if (((4f / 6f) <= h0) & (h0 < (5f / 6f)))
      {
        r0 = x0;
        g0 = 0f;
        b0 = c0;
      }
      else if (((5f / 6f) <= h0) & (h0 < (6f / 6f)))
      {
        r0 = c0;
        g0 = 0f;
        b0 = x0;
      }

      if ((((0f / 6f) <= h1) & (h1 < (1f / 6f))))
      {
        r1 = c1;
        g1 = x1;
        b1 = 0f;
      }
      else if (((1f / 6f) <= h1) & (h1 < (2f / 6f)))
      {
        r1 = x1;
        g1 = c1;
        b1 = 0f;
      }
      else if (((2f / 6f) <= h1) & (h1 < (3f / 6f)))
      {
        r1 = 0f;
        g1 = c1;
        b1 = x1;
      }
      else if (((3f / 6f) <= h1) & (h1 < (4f / 6f)))
      {
        r1 = 0f;
        g1 = x1;
        b1 = c1;
      }
      else if (((4f / 6f) <= h1) & (h1 < (5f / 6f)))
      {
        r1 = x1;
        g1 = 0f;
        b1 = c1;
      }
      else if (((5f / 6f) <= h1) & (h1 < (6f / 6f)))
      {
        r1 = c1;
        g1 = 0f;
        b1 = x1;
      }

      m0 = Brightness - (0.3f * r0 + 0.59f * g0 + 0.11f * b0);
      m1 = Brightness - (c1 / 2f);

      B = (1f - Palette) * (b0 + m0) + Palette * (b1 + m1);

      if (B > 1f) B = 1f;

      if (B < 0f) B = 0f;

      return B;
    }

    public class Dark
    {
      public static Brush Red =     new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x00, 0x00));
      public static Brush Orange =  new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x40, 0x00));
      public static Brush Yellow =  new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0x00));
      public static Brush Lime =    new SolidColorBrush(Color.FromArgb(0xFF, 0x40, 0x80, 0x00));
      public static Brush Green =   new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x80, 0x00));
      public static Brush Guaca =   new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x80, 0x40));
      public static Brush Cyan =    new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x80, 0x80));
      public static Brush Sky =     new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x40, 0x80));
      public static Brush Blue =    new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x80));
      public static Brush Purple =  new SolidColorBrush(Color.FromArgb(0xFF, 0x40, 0x00, 0x80));
      public static Brush Magenta = new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x00, 0x80));
      public static Brush Maroon =  new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x00, 0x40));

      public static Brush Gray =    new SolidColorBrush(Color.FromArgb(0xFF, 0x40, 0x40, 0x40));
    }

    public class Medium
    {
      public static Brush Red =     new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x00, 0x00));
      public static Brush Orange =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x80, 0x00));
      public static Brush Yellow =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0xF0, 0x00));
      public static Brush Lime =    new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0xF0, 0x00));
      public static Brush Green =   new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xF0, 0x00));
      public static Brush Guaca =   new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xF0, 0x80));
      public static Brush Cyan =    new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xF0, 0xF0));
      public static Brush Sky =     new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x80, 0xF0));
      public static Brush Blue =    new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0xF0));
      public static Brush Purple =  new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x00, 0xF0));
      public static Brush Magenta = new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x00, 0xF0));
      public static Brush Maroon =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x00, 0x80));

      public static Brush Gray =    new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0x80));
    }

    public class Light
    {
      public static Brush Red =     new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x80, 0x80));
      public static Brush Orange =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0xC0, 0x80));
      public static Brush Yellow =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0xF0, 0x80));
      public static Brush Lime =    new SolidColorBrush(Color.FromArgb(0xFF, 0xC0, 0xF0, 0x80));
      public static Brush Green =   new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0xF0, 0x80));
      public static Brush Guaca =   new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0xF0, 0xC0));
      public static Brush Cyan =    new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0xF0, 0xF0));
      public static Brush Sky =     new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0xC0, 0xF0));
      public static Brush Blue =    new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0xF0));
      public static Brush Purple =  new SolidColorBrush(Color.FromArgb(0xFF, 0xC0, 0x80, 0xF0));
      public static Brush Magenta = new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x80, 0xF0));
      public static Brush Maroon =  new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0x80, 0xC0));

      public static Brush Gray =    new SolidColorBrush(Color.FromArgb(0xFF, 0xC0, 0xC0, 0xC0));
    }
  }
} 

The Functions.cs

C#
using Microsoft.VisualBasic;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace ChartTesting0.Globals
{
  public class Functions
  {
    public static double Modulus(double a, double n)
    {
      return  (a - (n * IntPart(a / n)));
    }
    
    public static double IntPart(double n)
    {
      double I = Math.Floor(n);
      if (Math.Sign(n) == -1) I = Math.Ceiling(n);
      if (Math.Sign(n) == 0) I = 0;
      return I;
    }

    public static double DecPart(double n)
    {
      double D = (n - Math.Floor(n));
      if (Math.Sign(n) == -1) D = (n - Math.Ceiling(n));
      if (Math.Sign(n) == 0) D = 0;
      return D;
    }
  }
}

The Results

The following images are from running the application with the Number of Column Series ... int a = 3; // Number of Column Series ... set at 1, 3, and 5 . Each series changes color based on the number of the series using the above color code.  We have a live demo showing a 10 Column Series at here.

 

The Expansion Hints 

First, setting the styles. This should give you some ideas on how to handle coding a Style for other controls.

Second, creating a controltemplate. This makes it possilbe to apply a controltemplate to other controls. 

Third, manipulating the controltemplate. This shows how to insert values and can be expanded to include a full writeup of XAML code in the code behind (programmatically).

Fourth, adding a style. The style could be applied to other controls in a similar way.

Fifth, setting the chart axis. We placed a code behind axes and it can include other property settings as well.

Sixth, color control. The application of the HCBPAtoARGB() Brush gives a better control over color than guessing.

The Source Files

Well, here it is.  

History 

2012-07-14 - Current Release 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 95921312-Dec-12 2:16
Member 95921312-Dec-12 2:16 
GeneralMy vote of 5 Pin
Christian Amado27-Jul-12 10:58
professionalChristian Amado27-Jul-12 10:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.