Click here to Skip to main content
15,891,976 members
Articles / Operating Systems / Windows

WPF Chart Control With Pan, Zoom and More

Rate me:
Please Sign up or sign in to vote.
4.92/5 (42 votes)
10 Dec 2012Public Domain10 min read 387.4K   10.9K   174  
Chart Control for Microsoft .NET 3.0/WPF with pan, zoom, and offline rendering to the clipboard for custom sizes.
// ****************************************************************************
// Copyright Swordfish Computing Australia 2006                              **
// http://www.swordfish.com.au/                                              **
//                                                                           **
// Filename: Swordfish\WPF\Controls\ColorLabel.xaml.cs                       **
// Authored by: John Stewien of Swordfish Computing                          **
// Date: April 2006                                                          **
//                                                                           **
// - Change Log -                                                            **
//*****************************************************************************

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Swordfish.WPF.Charts
{
	/// <summary>
	/// Interaction logic for ColorLabel.xaml
	/// </summary>
	public partial class ColorLabel : UserControl
	{
		// ********************************************************************
		// Constructors
		// ********************************************************************
		#region Constructors

		/// <summary>
		/// Constructor. Initializes class fields.
		/// </summary>
		public ColorLabel()
		{
			InitializeComponent();
			this.Background = new SolidColorBrush(Colors.Transparent);
		}

		/// <summary>
		/// Initializes the text and color properties.
		/// </summary>
		/// <param name="text"></param>
		/// <param name="color"></param>
		public ColorLabel(string text, Color color) : this()
		{
			Text = text;
			Color = color;
		}

		#endregion Constructors

		// ********************************************************************
		// DependencyProperties
		// ********************************************************************
		#region DependencyProperties

		// Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty TextProperty =
			DependencyProperty.Register("Text", typeof(string), typeof(ColorLabel), new UIPropertyMetadata("Blank", new PropertyChangedCallback(UpdateText)));

		// Using a DependencyProperty as the backing store for Color.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ColorProperty =
			DependencyProperty.Register("Color", typeof(Color), typeof(ColorLabel), new UIPropertyMetadata(Colors.Violet, new PropertyChangedCallback(UpdateColor)));

		#endregion DependencyProperties

		// ********************************************************************
		// Properties
		// ********************************************************************
		#region Properties

		/// <summary>
		/// Gets/Sets the text of the color label
		/// </summary>
		public string Text
		{
			get
			{
				return (string)GetValue(TextProperty);
			}
			set
			{
				SetValue(TextProperty, value);
			}
		}

		/// <summary>
		/// Gets/Sets the color to be displayed
		/// </summary>
		public Color Color
		{
			get
			{
				return (Color)GetValue(ColorProperty);
			}
			set
			{
				SetValue(ColorProperty, value);
			}
		}

		#endregion Properties

		// ********************************************************************
		// Event Handlers
		// ********************************************************************
		#region Event Handlers

		/// <summary>
		/// Handles when the Color property is changed
		/// </summary>
		/// <param name="dependency"></param>
		/// <param name="e"></param>
		protected static void UpdateColor(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
		{
			ColorLabel colorLabel = (ColorLabel)dependency;
			Color newColor = (Color)e.NewValue;
			colorLabel.color.Color = newColor;
			/*
			if (newColor == Colors.Transparent)
				colorLabel.colorSize.Visibility = Visibility.Collapsed;
			else
				colorLabel.colorSize.Visibility = Visibility.Visible;
			*/
		}

		/// <summary>
		/// Handles when the Text property is changed
		/// </summary>
		/// <param name="dependency"></param>
		/// <param name="e"></param>
		protected static void UpdateText(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
		{
			ColorLabel colorLabel = (ColorLabel)dependency;
			colorLabel.textBlock.Text = (string)e.NewValue;
			colorLabel.textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
			colorLabel.colorSize.Width = colorLabel.textBlock.DesiredSize.Height * 1.5;
		}

		#endregion Event Handlers
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Founder Cheesy Design
Taiwan Taiwan
John graduated from the University of South Australia in 1997 with a Bachelor of Electronic Engineering Degree, and since then he has worked on hardware and software in many fields including Aerospace, Defence, and Medical giving him over 10 of years experience in C++ and C# programming. In 2009 John Started his own contracting company doing business between Taiwan and Australia.

Comments and Discussions