|
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.