Click here to Skip to main content
15,891,828 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.5K   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.Data;
using System.Globalization;

namespace WpfBulletGraph
{
    /// <summary>
    /// Converts the given value into an array of scale bar ticks between zero and the value given.
    /// </summary>
    class ScaleBarConverter : IValueConverter 
    {
        // the maximum nubmer of ticks which should be rendered
        private static int MaximumNumberOfTicks = 7;

        // multipliers which, when applied is sequence, provide an aesthetic scalebar
        private static double[] Multipliers = new double[] { 2.5f, 2, 2 };

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.Equals(0.0))
                return null;

            double range = (double)value;
            
            // determine the tick spacing
            double tickSpacing = range > 0 ? 1 : -1;            
            int multiplierIndex = 0;
            while (Math.Abs(range / tickSpacing) > MaximumNumberOfTicks)
            {
                tickSpacing *= Multipliers[multiplierIndex % Multipliers.Length];
                multiplierIndex++;
            }

            // determine the total number of scalebar ticks
            int tickCount = (int)(range / tickSpacing) + 1;

            // construct the scale
            double[] scale = new double[tickCount];
            for (int i = 0; i < tickCount; i++)
                scale[i] = i * tickSpacing;

            return scale;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

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