Pre loader

How can I change the FontSize of the Axis dynamically in WPF?

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1
0

Depending on the Size of my control which has the scichart on it, I want to change the FontSize of the Axis-Tick-Labels.

In XAML I defined a style

 <Style x:Key="XAxisLabelStyle" TargetType="s:DefaultTickLabel">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontSize" Value="{Binding AxisFontSize}"/>
        <!-- Align labels to the left side by using the AnchorPoint (docking point) on the right -->
        <Setter Property="HorizontalAnchorPoint" Value="Left"/>
 </Style>

and use it for the chart axis:

<s:SciChartSurface.XAxis>
            <s:NumericAxis Name="XAxis" AxisTitle="m/z" TitleStyle="{StaticResource AxisLabelStyle}" TickLabelStyle="{StaticResource XAxisLabelStyle}">
                <s:NumericAxis.VisibleRange>
                    <s:DoubleRange Min="0" Max="20000" />
                </s:NumericAxis.VisibleRange>
            </s:NumericAxis>
 </s:SciChartSurface.XAxis>

I have a binding to a property (AxisFontSize) which I can set from outside the control. For the axis label it works fine. But the TickLabels don’t change the FontSize. What have I to do?

  • You must to post comments
0
0

I see you’ve already found our article on Adding Color to a NumericAxis (styling the axis).

What are you binding to? Check if there are any binding errors in the output window.

I believe the DataContext of DefaultTickLabel is DefaultTickLabelViewModel. You may need to specify a RelativeSource binding to walk up the VisualTree from DefaultTickLabel to Axis, such as:

<!-- Warning: Untested! -->
<Style x:Key="XAxisLabelStyle" TargetType="s:DefaultTickLabel">

        <!-- Assumes Axis DataContext is a view model with property AxisFontSize -->
        <Setter Property="FontSize" Value="{Binding DataContext.AxisFontSize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type s:AxisBase}}}"/>

 </Style>

Try it, let me know if it works.

Best regards,
Andrew

  • Michael Knitsch
    It nearly works. Here is a picture: http://up.picr.de/20158066fg.jpg From the eight chart the rest of the charts have a strange behavior. They change the fontsize very fast between the small and the standard font. In the output-window I get the following error: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Abt.Controls.SciChart.Visuals.Axes.AxisBase', AncestorLevel='1''. BindingExpression:Path=DataContext.AxisFontSize; DataItem=null; target element is 'NumericTickLabel' (Name=''); target property is 'FontSize' (type 'Double') I am very new to work with WPF and XAML Bindings! In my control which contains the SciChart I have a Property for the Axis-Font public double AxisFontSize { get { return _axisFontSize; } set { if (!_axisFontSize.Equals(value)) { _axisFontSize = value; OnPropertyChanged("AxisFontSize"); } } } private double _axisFontSize; The control inherits from INotifyPropertyChanged to make the binding possible. In the Mainprogram I create the SciCharts in a loop (I change the FontSize only, if there are a specific number of charts to create): var specView = new SpectrumViewSmall(); specView.AxisFontSize = 4; In the control I set DataContext = this in the constructor.
  • You must to post comments
0
0

Facing similar issue, any other suggestion? As explained in the last comment, binding to AxisBase, nearly working fine. But sometime, the same binding fails and start producing flickering plot, due to the font size for the axis label alternating between small and big size.

Images
  • Andrew Burnett-Thompson
    Hi Rajani, best thing to do, since this is an old forum post. (1) open a new forum question explaining the problem (2) say which version of scichart you are using (3) supply code to reproduce the issue. If you can build a cut-down project which isolates the issue you will get the fastest response as our engineers can see exactly what’s the problem. Thanks for reporting! Best regards, Andrew
  • Rajani Davuluri
    Thank you Andrew! I created the support request with sample code from the scichart example suite. Also attached screen grab.
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies