Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make the X axis to the middle position of graph in WPF?

Solved!
Go to solution

How to make the X axis to the middle position of graph in WPF?

0 Kudos
Message 1 of 3
(2,440 Views)
Solution
Accepted by topic author xPlus

Using the same technique as WPF Intensity Graph Change ColorScale Orientation, you can control the display location of the X axis using a ScaleHost.

 

In XAML, you would disable the standard display of the horizontal axis by setting Location to None, and add a scale host to the Children collection of the graph:

<ni:Graph x:Name="graph" DefaultInteraction="PanVertical">
    <ni:Graph.Axes>
        <ni:AxisDouble x:Name="xAxis" Location="None" Orientation="Horizontal">
            <ni:AxisDouble.MajorDivisions>
                <ni:RangeLabeledDivisions Mode="AutoWithoutExtremes" />
            </ni:AxisDouble.MajorDivisions>
        </ni:AxisDouble>
        <ni:AxisDouble x:Name="yAxis" Orientation="Vertical" />
    </ni:Graph.Axes>
    <ni:Graph.Children>
        <niPrimitives:ScaleHost x:Name="xAxisHost"
                                niPrimitives:LayeredGraph.TargetOrientation="Horizontal"
                                niPrimitives:LayeredGraph.TargetPosition="Far"
                                niPrimitives:RelativePanel.RelativeWidth="1.0"
                                niPrimitives:RelativeCartesianScalePanel.IncludeOverhangInSize="False"
                                Owner="{Binding ElementName=graph}"
                                Scale="{Binding ElementName=xAxis}"
                                />
    </ni:Graph.Children>
</ni:Graph>

In code, we can monitor the vertical axis for changes, and re-position the scale host to match:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        // Listen for changes the the vertical axis.
        var dataMapper = this.yAxis.GetDataMapper();
        dataMapper.DataMapperChanged += this.OnDataMapperChanged;
        this.OnDataMapperChanged(dataMapper, null);
    }

    private void OnDataMapperChanged(object sender, DataMapperChangedEventArgs e)
    {
        var dataMapper = (IDataMapper<double>)sender;

        // Position the horizontal axis at the origin of the vertical axis.
        double relativeOrigin = dataMapper.Map(0.0);
        RelativePanel.SetRelativeVerticalPosition(this.xAxisHost, relativeOrigin);

        // Orient the axis to maximize label visibility.
        if (relativeOrigin <= 0.5)
        {
            LayeredGraph.SetTargetPosition(this.xAxisHost, RelativePosition.Far);
            RelativePanel.SetRelativeVerticalAlignment(this.xAxisHost, RelativeAlignment.Near);
        }
        else
        {
            LayeredGraph.SetTargetPosition(this.xAxisHost, RelativePosition.Near);
            RelativePanel.SetRelativeVerticalAlignment(this.xAxisHost, RelativeAlignment.Far);
        }
    }
}
~ Paul H
Message 3 of 3
(2,411 Views)