From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF Graph: how to add space between ticks and labels

Solved!
Go to solution

I would like to know how to set a margin between major tick marks and their labels in a WPF Graph.

Especially in the YAxis, labels are placed very close to the ticks.

 

Space in YAxis.png

0 Kudos
Message 1 of 4
(4,131 Views)
Solution
Accepted by topic author diluculo

Unfortunately, there is no existing configuration property to add extra space around scale labels. I have created a task to address this in a future Measurement Studio release.


As a workaround, you can use a string format that includes additional whitespace in the result:


    <ni:AxisDouble Orientation="Vertical" ...>
        <ni:AxisDouble.MajorDivisions>
            <ni:RangeLabeledDivisions>
                <ni:RangeLabeledDivisions.LabelPresenter>
                    <ni:GeneralValueFormatter Format="{}{0} " />
                </ni:RangeLabeledDivisions.LabelPresenter>
            </ni:RangeLabeledDivisions>
        </ni:AxisDouble.MajorDivisions>
    </ni:AxisDouble>


If you need finer control, you can create a custom label presenter to add additional space to the generated label visual.

~ Paul H
0 Kudos
Message 2 of 4
(4,111 Views)

Paul, thanks for your kind reply.

 

By adding "VisualizeCore" method into your EngineeringValueFormatter class, I can do what I want.

 

 

class EngineeringValueFormatter: ValueFormatter
    {
        ...

        protected override UIElement VisualizeCore<TData>(TData value, ValuePresenterArgs args, UIElement existingVisual)
        {
            UIElement element = base.VisualizeCore<TData>(value, args, existingVisual);

            // Add additional space to the generated label visual.
            var axis = (Axis<TData>)args.Context;            
            var tb = (TextBlock)element;
            if (axis.Orientation == Orientation.Vertical)
            {
                if (axis.Location == RelativeLocation.Near)
                    tb.Margin = new Thickness(0, 0, 5, 0);
                else if (axis.Location == RelativeLocation.Far)
                    tb.Margin = new Thickness(5, 0, 0, 0);
            }
            else if (axis.Orientation == Orientation.Horizontal)
            {
                if (axis.Location == RelativeLocation.Near)
                    tb.Margin = new Thickness(0, 0, 0, 0);
                else if (axis.Location == RelativeLocation.Far)
                    tb.Margin = new Thickness(0, 0, 0, 5);
            }
            return element;
        } 
    }

 

 

 

 

0 Kudos
Message 3 of 4
(4,074 Views)

Just wanted to let you know that value formatters now include a Padding property (although admittedly not as flexible as your custom code!):

<ni:AxisDouble.MajorDivisions>
    <ni:RangeLabeledDivisions>
        <ni:GeneralValueFormatter Padding="0,0,5,0" />
    </ni:RangeLabeledDivisions>
</ni:AxisDouble.MajorDivisions>
~ Paul H
0 Kudos
Message 4 of 4
(2,051 Views)