Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

OpenWPFChart: Assembling Charts from Components. Part II - Controls

, 26 Mar 2009
Chart controls composed from Chart Parts
SourceCode.zip
trunk
ChartControls
ChartControls.csproj.user
OpenWPFChart.Chart.Icon.png
OpenWPFChart.WellLogChart.Icon.png
Properties
Themes
ChartHelpers
AxisPropertiesDialog
ChartHelpers.csproj.user
ChartScaleControl
FontChooser
ItemPropertiesDialog
Properties
ChartParts
Axes
ChartParts.csproj.user
Grid
Items
Data
DataView
Elements
Visuals
NumericalRecipes
Properties
Scales
SampleDataFiles
WellLog
Samples
ControlSamples
ColumnChartControlSample
Properties
Settings.settings
CurveChartControlSample
Properties
Settings.settings
r3xyqstb.tmp_proj
WellLogControlSample
Properties
Settings.settings
DirectCompositionSamples
BasicSample
Properties
Settings.settings
TemperatureSample
Properties
Settings.settings
WellLogSample
Properties
Settings.settings
// <copyright file="AxisPropertiesDialog.xaml.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-03-06</date>
// <summary>OpenWPFChart library. Coordinate Axis Properties dialog.</summary>
// <revision>$Id: AxisPropertiesDialog.xaml.cs 18093 2009-03-16 04:15:06Z unknown $</revision>

using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using OpenWPFChart.Parts;

namespace OpenWPFChart.Helpers
{
	/// <summary>
	/// Coordinate Axis Properties dialog.
	/// </summary>
	public partial class AxisPropertiesDialog : Window
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="AxisPropertiesDialog"/> class.
		/// </summary>
		/// <param name="axis">The axis.</param>
		public AxisPropertiesDialog(Axis axis)
		{
			InitializeComponent();

			if (axis is LinearAxis)
				dataView = new LinearAxisDataView(axis as LinearAxis);
			else if (axis is LogarithmicAxis)
				dataView = new LogarithmicAxisDataView(axis as LogarithmicAxis);
			else if (axis is DateTimeAxis)
				dataView = new DateTimeAxisDataView(axis as DateTimeAxis);
			else if (axis is SeriesAxis)
				dataView = new SeriesAxisDataView(axis as SeriesAxis);
			if (dataView != null)
			{
				dataView.PropertyChanged += data_PropertyChanged;
				DataContext = dataView;
			}
		}

		/// <summary>
		/// The ChartScaleControl defined in a DataTemplate.
		/// </summary>
		ChartScaleControl chartScaleControl;

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Window.ContentRendered"/> event.
		/// </summary>
		/// <remarks>Finds the ChartScaleControl defined in a DataTemplate.</remarks>
		/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
		protected override void OnContentRendered(EventArgs e)
		{
			base.OnContentRendered(e);

			chartScaleControl = findChartScaleControl(this);
			if (chartScaleControl != null)
				chartScaleControl.PropertyChanged += data_PropertyChanged;
		}

		/// <summary>
		/// Finds the ChartScaleControl in VisualTree.
		/// </summary>
		/// <param name="parent">The parent.</param>
		/// <returns></returns>
		static ChartScaleControl findChartScaleControl(DependencyObject parent)
		{
			if (parent is ChartScaleControl)
				return parent as ChartScaleControl;
			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); ++i)
			{
				DependencyObject child = VisualTreeHelper.GetChild(parent, i);
				ChartScaleControl scale = findChartScaleControl(child);
				if (scale != null)
					return scale;
			}
			return null;
		}

		/// <summary>
		/// Enable OK button.
		/// Handles the PropertyChanged event of the data control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
		private void data_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			btnOK.IsEnabled = true;
		}

		#region AxisDataView
		AxisDataView dataView;
		/// <summary>
		/// Gets the axis data.
		/// </summary>
		public AxisDataView AxisData
		{
			get { return dataView; }
		}
		#endregion AxisDataView

		#region ChartScaleControl properties accessors
		public object Start
		{
			get { return chartScaleControl.Start; }
		}

		public object Stop
		{
			get { return chartScaleControl.Stop; }
		}

		public double Scale
		{
			get { return chartScaleControl.Scale; }
		}
		#endregion ChartScaleControl properties accessors

		/// <summary>
		/// Handles the Click event of the btnPenColor control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void btnPenColor_Click(object sender, RoutedEventArgs e)
		{
			dataView.PenColor = selectColor(dataView.PenColor);
		}

		/// <summary>
		/// Selects the color with System.Windows.Forms.ColorDialog.
		/// </summary>
		/// <param name="color">The WPF color.</param>
		/// <returns></returns>
		private static Color selectColor(Color color)
		{
			System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
			dlg.Color = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
			if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
				return color;
			return Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);
		}

		/// <summary>
		/// Handles the Click event of the btnFont control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void btnFont_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Samples.WPFNotepad.FontChoice choice 
				= new Microsoft.Samples.WPFNotepad.FontChoice();

			choice.FontFamily = dataView.FontFamily;
			choice.FontStyle = dataView.FontStyle;
			choice.FontWeight = dataView.FontWeight;
			choice.FontStretch = dataView.FontStretch;
			choice.FontSize = dataView.FontSize;
			choice.Foreground = new SolidColorBrush(dataView.PenColor);

			Microsoft.Samples.WPFNotepad.FontChooser fontChooser =
				new Microsoft.Samples.WPFNotepad.FontChooser(choice) { Owner = this };
			fontChooser.FontChosen += ApplyPlainTextFontChoice;
			fontChooser.ShowDialog();
		}

		/// <summary>
		/// Applies the plain text font choice.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The e.</param>
		void ApplyPlainTextFontChoice(object sender, Microsoft.Samples.WPFNotepad.FontChooserDialogAppliedEventsArgs e)
		{
			dataView.FontFamily = e.FontChoice.FontFamily;
			dataView.FontStyle = e.FontChoice.FontStyle;
			dataView.FontWeight = e.FontChoice.FontWeight;
			dataView.FontStretch = e.FontChoice.FontStretch;
			dataView.FontSize = e.FontChoice.FontSize;
		}

		/// <summary>
		/// Applyes changes and closes.
		/// Handles the Click event of the btnOK control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			DialogResult = true;
		}
	}
}

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 The Code Project Open License (CPOL)

About the Author

Oleg V. Polikarpotchkin
Team Leader
Russian Federation Russian Federation
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 26 Mar 2009
Article Copyright 2009 by Oleg V. Polikarpotchkin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid