Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / WPF

OpenWPFChart: assembling charts from components: Part I - Parts

Rate me:
Please Sign up or sign in to vote.
4.29/5 (17 votes)
19 Mar 2009CPOL14 min read 81.7K   4K   81  
Provides the component model along with base components to assemble charts.
// <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)


Written By
Team Leader
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions