How to move axis to the top?
The chart layout template is defined in Themes/Generic.xaml as a style for d3:Chart
.
You can create a custom style where the horizontal axis is located at the top (d3:Figure.Placement="Top"
) and has correct orientation (AxisOrientation="Top"
). For instance,
<d3:PlotAxis x:Name="PART_horizontalAxis"
d3:Figure.Placement="Top"
AxisOrientation="Top"
Foreground="{TemplateBinding Foreground}">
<d3:MouseNavigation IsVerticalNavigationEnabled="False"/>
</d3:PlotAxis>
How to use custom formatting for axis labels?
For example, if values along y are actually hours since certain time moment, and you want to show axis ticks as HH:mm, you need to inject a custom label provider into an axis control.
To do that you can create new axis class derived from Axis
and pass custom label provider to the base constructor:
public class CustomLabelProvider : ILabelProvider
{
public static DateTime Origin = new DateTime(2000, 1, 1);
public FrameworkElement[] GetLabels(double[] ticks)
{
if (ticks == null)
throw new ArgumentNullException("ticks");
List<TextBlock> Labels = new List<TextBlock>();
foreach (double tick in ticks)
{
TextBlock text = new TextBlock();
var time = Origin + TimeSpan.FromHours(tick);
text.Text = time.ToShortTimeString();
Labels.Add(text);
}
return Labels.ToArray();
}
}
public class CustomAxis : Axis
{
public CustomAxis() : base(new CustomLabelProvider(), new TicksProvider())
{
}
}
Now return to the custom Chart template and change for the vertical axis its type from PlotAxis
to CustomAxis
(note that you might need to change type prefix):
<d3:CustomAxis x:Name="PART_verticalAxis"
d3:Figure.Placement="Left"
AxisOrientation="Left"
Foreground="{TemplateBinding Foreground}">
<d3:MouseNavigation IsHorizontalNavigationEnabled="False"/>
</d3:CustomAxis>
If we do the described steps for the LineGraphSample and run it, we get the following:

Finally, the custom chart style:
<Style TargetType="d3:Chart">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="d3:Chart">
<Grid>
<d3:Figure x:Name="PART_figure" Margin="1"
PlotHeight="{Binding PlotHeight, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
PlotWidth="{Binding PlotWidth, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
PlotOriginX="{Binding PlotOriginX, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
PlotOriginY="{Binding PlotOriginY, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
IsAutoFitEnabled="{Binding IsAutoFitEnabled, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
AspectRatio="{Binding AspectRatio, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
ExtraPadding="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<d3:MouseNavigation IsVerticalNavigationEnabled="{TemplateBinding IsVerticalNavigationEnabled}"
IsHorizontalNavigationEnabled="{TemplateBinding IsHorizontalNavigationEnabled}"
x:Name="PART_mouseNavigation"/>
<d3:KeyboardNavigation IsVerticalNavigationEnabled="{TemplateBinding IsVerticalNavigationEnabled}"
IsHorizontalNavigationEnabled="{TemplateBinding IsHorizontalNavigationEnabled}"
x:Name="PART_keyboardNavigation"/>
<d3:VerticalContentControl d3:Figure.Placement="Left"
Content="{TemplateBinding LeftTitle}"
VerticalAlignment="Center"
IsTabStop="False"/>
<d3:CustomAxis x:Name="PART_verticalAxis"
d3:Figure.Placement="Left"
AxisOrientation="Left"
Foreground="{TemplateBinding Foreground}">
<d3:MouseNavigation IsHorizontalNavigationEnabled="False"/>
</d3:CustomAxis>
<d3:AxisGrid x:Name="PART_axisGrid"
VerticalTicks="{Binding Ticks,ElementName=PART_verticalAxis, Mode=OneWay}"
HorizontalTicks="{Binding Ticks,ElementName=PART_horizontalAxis, Mode=OneWay}"
Stroke="{TemplateBinding Foreground}" Opacity="0.25"/>
<ContentControl d3:Figure.Placement="Top"
HorizontalAlignment="Center"
FontSize="16"
Content="{TemplateBinding Title}"
Foreground="{TemplateBinding Foreground}"
IsTabStop="False"/>
<ContentControl d3:Figure.Placement="Bottom"
HorizontalAlignment="Center"
Content="{TemplateBinding BottomTitle}"
Foreground="{TemplateBinding Foreground}"
IsTabStop="False"/>
<d3:VerticalContentControl d3:Figure.Placement="Right"
Content="{TemplateBinding RightTitle}"
VerticalAlignment="Center"
IsTabStop="False"/>
<d3:PlotAxis x:Name="PART_horizontalAxis"
d3:Figure.Placement="Top"
AxisOrientation="Top"
Foreground="{TemplateBinding Foreground}">
<d3:MouseNavigation IsVerticalNavigationEnabled="False"/>
</d3:PlotAxis>
<ContentPresenter/>
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding Foreground}" d3:Figure.Placement="Center"/>
<d3:Legend x:Name="PART_legend"
Foreground="Black" Content="{TemplateBinding LegendContent}"
Visibility="{TemplateBinding LegendVisibility}"/>
</d3:Figure>
<Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsTabStop" Value="False"/>
</Style>