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

Custom Gauge Controls for Windows Phone 7: Part I

, 25 Feb 2013
The first article in this series presents the implementation considerations and the way to use some custom gauges in WP7.
GaugeLibV2-noexe.zip
GaugeLib
GaugeLib.csproj.user
Properties
Themes
GaugeLibV2.zip
Bin
Debug
GaugeLib.dll
GaugeLib.csproj.user
WPScadaControls.zip
WPScadaControls
WPClientV2
ApplicationIcon.png
Background.png
Bin
Debug
ApplicationIcon.png
Background.png
WPClientV2.dll
WPClientV2.xap
WPScadaControlsV2.dll
Release
obj
Debug
TempPE
Properties
SplashScreenImage.jpg
WPClientV2.csproj.user
WPScadaControlsV2
Bin
Debug
WPScadaControlsV2.dll
Release
obj
Debug
TempPE
Themes
Properties
ScaleDiagram.cd
Themes
WPScadaControlsV2.csproj.user
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WPScadaControlsV2
{
    public class RadialGaugeHelper
    {
        public static Point GetCenterPosition(RadialType type, Size finalSize, double minAngle, double maxAngle, SweepDirection sweepDir)
        {
            //get the quadrants of the limits
            int q1 = GetQuadrant(minAngle);
            int q2 = GetQuadrant(maxAngle);
            if (sweepDir == SweepDirection.Counterclockwise)
            {
                q1++; q2++;
                if (q1 > 4) q1 = 1;
                if (q2 > 4) q2 = 1;
            }
            else
            {
                //q2->q4 and q4->q2
                if (q1 % 2 == 0) q1 = 6 - q1;
                if (q2 % 2 == 0) q2 = 6 - q2;
            }
            //calculate the difference
            int diff = q2 - q1;
            if (Math.Abs(diff) == 0)
            {//quarter possibility
                if (type == RadialType.Quadrant)
                {
                    return GetCenterForQuadrant(q2, finalSize);
                }
                else if (type == RadialType.Semicircle)
                {
                    if (q1 == 1 || q1 == 2)
                        return new Point(finalSize.Width / 2, finalSize.Height);
                    else
                        return new Point(finalSize.Width / 2, 0);
                }
                else
                {//full circle
                    return new Point(finalSize.Width / 2, finalSize.Height / 2);
                }
            }
            else if (Math.Abs(diff) == 1 || (Math.Abs(diff)==3 && (maxAngle-minAngle)<=180))
            {//semicircle possibility
                if (type == RadialType.Quadrant || type == RadialType.Semicircle)
                {
                    return GetCenterForSemicircle(q1, q2, finalSize);
                }
                else
                {//full circle
                    return new Point(finalSize.Width / 2, finalSize.Height / 2);
                }
            }
            else
            {//full circle
                return new Point(finalSize.Width / 2, finalSize.Height / 2);
            }

        }
        public static double GetRadius(RadialType type, Size finalSize, double minAngle, double maxAngle, SweepDirection sweepDir)
        {
            
            //get the quadrants of the limits
            int q1 = GetQuadrant(minAngle);
            int q2 = GetQuadrant(maxAngle);
            if (sweepDir == SweepDirection.Counterclockwise)
            {
                q1++; q2++;
                if (q1 > 4) q1 = 1;
                if (q2 > 4) q2 = 1;
            }
            else
            {
                //q2->q4 and q4->q2
                if (q1 % 2 == 0) q1 = 6 - q1;
                if (q2 % 2 == 0) q2 = 6 - q2;
            }
            //calculate the difference
            int diff = q2 - q1;
            if (Math.Abs(diff) == 0)
            {//quarter possibility
                if (type == RadialType.Quadrant)
                {
                    return Math.Min(finalSize.Width, finalSize.Height);
                }
                else if (type == RadialType.Semicircle)
                {
                    return finalSize.Height;
                }
                else
                {//full circle
                    return Math.Min(finalSize.Width / 2, finalSize.Height / 2);
                }
            }
            else if (Math.Abs(diff) == 1 || (Math.Abs(diff) == 3 && (maxAngle - minAngle) <= 180))
            {//semicircle possibility
                if (type == RadialType.Quadrant || type == RadialType.Semicircle)
                {
                    return GetRadiusForSemicircle(q1, q2, finalSize);
                }
                else
                {//full circle
                    return Math.Min(finalSize.Width / 2, finalSize.Height / 2);
                }
            }
            else
            {//full circle
                return Math.Min(finalSize.Width / 2, finalSize.Height / 2);
            }
        }

        #region helper methods 
        private static double GetRadiusForSemicircle(int q1, int q2, Size finalSize)
        {
            if (q1 == 1 && q2 == 2 || q2 == 1 && q1 == 2)
            {
                return Math.Min(finalSize.Width/2, finalSize.Height );
            }
            else if (q1 == 2 && q2 == 3 || q1 == 3 && q2 == 2)
            {
                return Math.Min(finalSize.Width , finalSize.Height/2);
            }
            else if (q1 == 3 && q2 == 4 || q2 == 3 && q1 == 4)
            {
                return Math.Min(finalSize.Width / 2, finalSize.Height);
            }
            else
            {
                return Math.Min(finalSize.Width, finalSize.Height / 2);
            }
        }
        private static Point GetCenterForQuadrant(int q, Size finalSize)
        {
            if (q == 1)
                return new Point(0, finalSize.Height);
            else if (q == 2)
                return new Point(finalSize.Width, finalSize.Height);
            else if (q == 3)
                return new Point(finalSize.Width, 0);
            else
                return new Point(0, 0);
        }
        private static Point GetCenterForSemicircle(int q1, int q2, Size finalSize)
        {
            if (q1 == 1 && q2 == 2 || q2 == 1 && q1 == 2)
                return new Point(finalSize.Width/2, finalSize.Height);
            else if (q1 == 2 && q2 == 3 || q1 == 3 && q2 == 2)
                return new Point(finalSize.Width, finalSize.Height/2);
            else if (q1 == 3 && q2 == 4 || q1 == 4 && q2 == 3)
                return new Point(finalSize.Width/2, 0);
            else
                return new Point(0, finalSize.Height / 2);
        }
        private static int GetQuadrant(double angle)
        {
            angle = angle * Math.PI / 180;
            if (Math.Sin(angle) >= 0 && Math.Cos(angle) >= 0)
                return 1;
            else if (Math.Sin(angle) >= 0 && Math.Cos(angle) < 0)
                return 2;
            else if (Math.Sin(angle) < 0 && Math.Cos(angle) < 0)
                return 3;
            else
                return 4;
        }
        #endregion
    }
}

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

Florin Badea
Software Developer
Romania Romania
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 25 Feb 2013
Article Copyright 2011 by Florin Badea
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid