65.9K
CodeProject 正在变化。 阅读更多。
Home

以编程方式设置 Silverlight 图表 ControlTemplate

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2012 年 7 月 15 日

CPOL

2分钟阅读

viewsIcon

21400

演示如何以编程方式编写 Silverlight Chart ControlTemplate 的代码。

引言

我们一直在寻找一种方法,以便以编程方式(代码隐藏)为 Silverlight 中的 Chart 控件创建 ControlTemplate。经过大量搜索,并将来自博客、提示和编码者网站的信息拼凑在一起,我们想出了一种从代码隐藏创建 ControlTemplate 的方法。

这工作起来相当简单。在 XAML 部分放置一个图表控件,给它一个 x:Name,然后从代码隐藏创建图表内部结构。

 

MainPage.xaml

以下是在主页面上设置的图表 XAML,名称为“cChart”。简单,只是一个图表和一些数据点来显示列。没有设置 ColumnSeries、Axis、Style 或 ControlTemplate。 

<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> 

代码隐藏 - MainPage.xaml.cs   

这通过设置一个 ControlTemplate 然后将其放置到 ColumnDataPoint 样式中来添加到 cChart,然后将其添加到 ColumnSeries,最后添加到 cChart。

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);
    }
  }
} 

有关颜色的附加代码

我们真的不喜欢使用像 argb 这样的原始颜色。大多数时候我们都在猜测,并且想出一个令人满意的配色方案的过程从未真正奏效。控件从未好过,所以我们搜索了几种方法,并在 http://en.wikipedia.org/wiki/HSL_and_HSV 上找到了一些关于颜色的好信息。 从那里,我们合并了两种不同的尺度类型,色度和饱和度,以提出一个可移动的尺度来使用。 它可以是所有色度、所有饱和度或介于两者之间。

JADEXCODEColor.cs  

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));
    }
  }
} 

Functions.cs

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;
    }
  }
}
 

结果

以下图像来自运行应用程序,其中 Column Series 的数量 ... int a = 3; // Number of Column Series ... 设置为 1、3 和 5。每个系列根据系列的数量使用上述颜色代码更改颜色。  我们有一个实时演示,显示了 10 个 Column Series,请访问 此处

 

展开提示 

首先,设置样式。 这应该让你对如何处理其他控件的样式编码有一些想法。

其次,创建 controltemplate。 这使得可以将 controltemplate 应用于其他控件。 

第三,操作 controltemplate。 这展示了如何插入值,并且可以扩展为包括代码隐藏中 XAML 代码的完整描述(以编程方式)。

第四,添加样式。 样式可以以类似的方式应用于其他控件。

第五,设置图表轴。 我们放置了一个代码隐藏轴,它还可以包括其他属性设置。

第六,颜色控制。 应用 HCBPAtoARGB() 画笔比猜测更好地控制颜色。

源文件

嗯,就在这里。 

历史 

2012-07-14 - 当前版本 

© . All rights reserved.