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="ColumnChartCanvas.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin.
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-03-13</date>
// <summary>Canvas for the ColumnChart Control.</summary>

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using OpenWPFChart.Parts;

namespace OpenWPFChart.Controls
{
	/// <summary>
	/// Canvas for the ColumnChart Control.
	/// </summary>
	/// <remarks>
	/// ColumnChartCanvas shifts its <see cref="OpenWPFChart.Parts.ColumnChartItemDataView"/>-based children by
	/// column width value.
	/// <para>ColumnChartCanvas sizes according to its contents.</para>
	/// </remarks>
	public class ColumnChartCanvas : Canvas
	{
		/// <summary>
		/// Invoked when the <see cref="T:System.Windows.Media.VisualCollection"/> of a visual object
		/// is modified.
		/// </summary>
		/// <param name="visualAdded">The <see cref="T:System.Windows.Media.Visual"/> that was added
		/// to the collection.</param>
		/// <param name="visualRemoved">The <see cref="T:System.Windows.Media.Visual"/> that was 
		/// removed from the collection.</param>
		/// <remarks>
		/// This method offsets all <see cref="OpenWPFChart.Parts.ColumnChartItemDataView"/> panel items
		/// by setting their Canvas.Left property.
		/// </remarks>
		protected override void OnVisualChildrenChanged(DependencyObject visualAdded
			, DependencyObject visualRemoved)
		{
			double columnWidth = 0d;
			int visibleChildrenCount = 0;
			foreach (UIElement child in Children)
			{
				ColumnChartItemDataView itemDataView = findItemDataView(child);
				if (itemDataView == null)
					continue;
				if (visibleChildrenCount++ == 0)
					columnWidth = itemDataView.ColumnWidth;
				else
				{
					// All items should have the same ColumnWidth property value.
					Debug.Assert(columnWidth == itemDataView.ColumnWidth, "columnWidth == itemDataView.ColumnWidth");
				}
			}
			if (visibleChildrenCount < 2)
				// Doesn't have to shift single item.
				return;

			double offset = -0.5 * columnWidth * (visibleChildrenCount - 1);
			foreach (UIElement child in Children)
			{
				if (findItemDataView(child) == null)
					continue;

				SetLeft(child, offset);
				offset += columnWidth;
			}
		}

		/// <summary>
		/// Finds the item data view.
		/// </summary>
		/// <remarks>
		/// </remarks>
		/// <param name="item">The <see cref="T:System.Windows.ItemsControl"/> item container.</param>
		/// <returns></returns>
		static ColumnChartItemDataView findItemDataView(UIElement item)
		{
			FrameworkElement fe = item as FrameworkElement;
			if (fe == null)
				return null;
			return fe.DataContext as ColumnChartItemDataView;
		}
	}
}

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)

Share

About the Author

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.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