Swap x and y axis in a LineSeries chart (DataVisualization.Charting)

935 views Asked by At

I want to make the y axis the independent axis, i.e. turning the horizontal rendering of a line series into a vertical one. This is the code that I started with:

<Window x:Class="Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <charting:Chart>
      <charting:Chart.Axes>
        <charting:LinearAxis Orientation="X" ShowGridLines="True" Name="xAxis" />
        <charting:LinearAxis Orientation="Y" ShowGridLines="True" Name="yAxis" />
      </charting:Chart.Axes>

      <charting:LineSeries
          Name="lineSeries"
          ItemsSource="{Binding}"
          IndependentValuePath="Indep"
          DependentValuePath="Dep">
      </charting:LineSeries>
    </charting:Chart>
  </Grid>
</Window>

First I tried adding

<charting:LineSeries.IndependentAxis>
    <charting:LinearAxis Orientation="Y" />
</charting:LineSeries.IndependentAxis>

<charting:LineSeries.DependentRangeAxis>
    <charting:LinearAxis Orientation="X" />
</charting:LineSeries.DependentRangeAxis>

This results in a System.InvalidOperationException with a message saying

Assigned independent axis cannot be used. This may be due to an unset Orientation property for the axis.

However, when swapping the values of "X" and "Y" in the above snippet, there's no exception and everything works fine, except of course that the axes aren't oriented the way I want them.

Next thing I tried is doing it programmatically:

lineSeries.IndependentAxis = yAxis;
lineSeries.DependentRangeAxis = xAxis;

which gives me the same exception. Again, when swapping xAxis and yAxis in the statements above, there's no exception.

Any idea how to get what I want?

1

There are 1 answers

0
Evgeniy Berezovsky On

Not making any progress on the axis swapping front, I tried the generic WPF way of rotating controls. It's as easy as adding the following under the <charting:Chart> element and it works:

<charting:Chart.LayoutTransform>
  <RotateTransform CenterX="0.5" CenterY="0.5" Angle="90"/>
</charting:Chart.LayoutTransform>

Except that if you really only want to swap the dependent/independent axes, RotateTransform is akin to the sledgehammer. Because then you have to adapt the locations of the axes and also rotate them -90 degrees. However, this is all doable and lacking a more straightforward solution I decided to go with this.