Click here to Skip to main content
15,885,682 members
Articles / Desktop Programming / WPF

Bullet Graphs - A Custom Control - WPF vs. Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.88/5 (50 votes)
10 Oct 2008CPOL19 min read 184.2K   6.4K   163  
This article compares the development of a Line of Business control, the Bullet Graph, in both WPF and Windows Forms.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfBulletGraph
{
    /// <summary>
    /// Interaction logic for BulletGraph.xaml
    /// </summary>
    public partial class BulletGraph : UserControl
    {
        #region properties

        [Category("Bullet Graph")]
        [Browsable(true)]
        public double FeaturedMeasure
        {
            get { return BulletGraphWithLegend.GetFeaturedMeasure(this); }
            set { BulletGraphWithLegend.SetFeaturedMeasure(this, value); }
        }

        [Category("Bullet Graph")]
        [Browsable(true)]
        public double ComparativeMeasure
        {
            get { return BulletGraphWithLegend.GetComparativeMeasure(this); }
            set { BulletGraphWithLegend.SetComparativeMeasure(this, value); }
        }

        [Category("Bullet Graph")]
        [Browsable(true)]
        public double GraphRange
        {
            get { return BulletGraphWithLegend.GetGraphRange(this); }
            set { BulletGraphWithLegend.SetGraphRange(this, value); }
        }

        [Category("Bullet Graph")]
        [Browsable(true)]
        public bool LeftToRight
        {
            get { return BulletGraphWithLegend.GetLeftToRight(this); }
            set { BulletGraphWithLegend.SetLeftToRight(this, value); }
        }

        [Category("Bullet Graph")]
        [Browsable(true)]
        public QualitativeRanges QualitativeRanges
        {
            get { return BulletGraphWithLegend.GetQualitativeRanges(this); }
            set { BulletGraphWithLegend.SetQualitativeRanges(this, value); }
        }

        #endregion

        public BulletGraph()
        {
            InitializeComponent();
                   
            // add a handler to the GraphRange property. This is used to determine if the graph plots negative values.
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(BulletGraphWithLegend.GraphRangeProperty, typeof(BulletGraphWithLegend));
            if (dpd != null)
            {
                dpd.AddValueChanged(this, delegate
                {                  
                    // sort the qualitative range items ascending or descending accordingly  
                    qualitativeRangeBar.Items.SortDescriptions.Add(
                        new SortDescription("Maximum", this.GraphRange < 0 ? ListSortDirection.Ascending : ListSortDirection.Descending));

                });
            }

        }


    }

    public class QualitativeRanges : ObservableCollection<QualitativeRange>
    { }

    /// <summary>
    /// Defines a single qualitative range within a bullet graph.
    /// </summary>
    public class QualitativeRange
    {
        private double? maximum;

        public double? Maximum
        {
            get { return maximum; }
            set { maximum = value; }
        }

        private Color color;

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public override string ToString()
        {
            return "Maximum: " + (Maximum.HasValue ? Maximum.ToString() : "<null>") + ", Color: " + Color;
        }
    }
}

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
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions