07-03-2019 03:13 AM
How to make the X axis to the middle position of graph in WPF?
Solved! Go to Solution.
07-08-2019 12:14 AM
Hi,
You can refer to this manual on how to configure graph axes
http://zone.ni.com/reference/en-XX/help/372636F-01/mstudiowebhelp/html/wpfgraphaxescursors/
http://zone.ni.com/reference/en-XX/help/372636F-01/mstudiowebhelp/html/wpfgraphplotting/
07-08-2019 11:35 AM
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);
}
}
}