Dynamic Data Display charting: change Y axis range

rbasniak picture rbasniak · Mar 7, 2012 · Viewed 13.5k times · Source

Does anyone know if is there any way to change the Y axis limits on a line chart from D3?

I'm using the last version from Codeplex source... The WPF one, not the Silverlight version.

I'm plotting readings from Android sensors and the range values change quite a bit. Sometimes it's around zero and the graph looks like this:

Ranging from -0.05 to +0.05

Then the magnitude of the values changes and the Y axis is rescaled to show the entire data and the graph now looks like this:

Ranging from -0.8 to + 0.6

See how the Y values changed a lot from Picture 1 to 2.

What I want is to make the Y axis with fixed values that I choose, lets say -10 to +10. Then the graph will be plot always with the same scale.

Is there any way to do that?

The XAML code for the graph is this:

    <d3:ChartPlotter
        Name="gyroGraph"
        Margin="21,5,10,0"
        Grid.ColumnSpan="2"
        Background="White"
        Grid.Row="1"
        LegendVisibility="Collapsed"
        NewLegendVisible="False"
        MainHorizontalAxisVisibility="Collapsed"
        MainVerticalAxisVisibility="Collapsed">
        <d3:VerticalAxis
            FontSize="8" />
        <d3:Header
            Content="Angular Velocity"
            FontSize="11" />
    </d3:ChartPlotter>

Thanks and best regards, Rodrigo Basniak

Answer

Moobie picture Moobie · Jun 11, 2013

Hope it is not too late, but I encountered the same problem and found a solution in d3's forum: http://dynamicdatadisplay.codeplex.com/discussions/281164

The code was created by the d3 user 'fromDKwithlove'.

This is how you should apply the Restriction:

ViewportAxesRangeRestriction restr = new ViewportAxesRangeRestriction();
restr.YRange = new DisplayRange(-5, 105);
plotter.Viewport.Restrictions.Add(restr);

And this is the class(es) behind the code:

public class DisplayRange
{
    public double Start { get; set; }
    public double End { get; set; }

    public DisplayRange(double start, double end)
    {
        Start = start;
        End = end;
    }
}

public class ViewportAxesRangeRestriction : IViewportRestriction
{
    public DisplayRange XRange = null;
    public DisplayRange YRange = null;

    public Rect Apply(Rect oldVisible, Rect newVisible, Viewport2D viewport)
    {
        if (XRange != null)
        {
            newVisible.X = XRange.Start;
            newVisible.Width = XRange.End - XRange.Start;
        }

        if (YRange != null)
        {
            newVisible.Y = YRange.Start;
            newVisible.Height = YRange.End - YRange.Start;
        }

        return newVisible;
    }

    public event EventHandler Changed;
}