Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating custom tick labels

Is it possible to create custom tick labels on a wpf graph? I would ideally like to be able to replace some of the numeric tick labels with a string.

 

Also i am currently using code found here

 

http://forums.ni.com/t5/Measurement-Studio-for-NET/Hide-first-and-last-Division-in-WPF-graph-axis/td...

 

to hide first and last tick labels. Is there a way to update the number of decimal places displayed after this has been run?

0 Kudos
Message 1 of 12
(8,366 Views)

Since you are already deriving from GeneralValueFormatter to Hide first and last Division in WPF graph axis, you can add additional logic there to modify how particular values are displayed. For example:


    if( MySpecialValue.Equals( value ) )
        return base.VisualizeCore( "my string", args, existingVisual );


To change the number of decimal places displayed, you can customize the Format property to any .NET format string, such as "0.000" to show three decimal places.


Alternatively, you can use the techniques described in this question to further customize the display of division labels.

~ Paul H
0 Kudos
Message 2 of 12
(8,354 Views)

Thanks for your help. Is there any alternative way to set the custom tick strings in a similar way to using the axiscustomdivisions property in windows forms? We could end up with a large number of graphs all requiring different custom tick values in our application and I'm not quite sure how that would work with the custom formatter dervived from the generalvalueformatter.

0 Kudos
Message 3 of 12
(8,335 Views)

Unfortunately, we did not have time to implement custom divisions for the initial release of the WPF controls. For now, the only way to add divisions to a scale is through the MajorDivisions property.


Below is an example helper that adds one custom division to a double scale. You can add this as a static resource and bind the Mode and LabelPresenter properties to have the division appear.


    public class CustomDivisionHelper {
        private readonly RangeDivisionsMode _mode;
        private readonly ValuePresenter _presenter;
        public CustomDivisionHelper( ) : this( 4.2 ) { }
        public CustomDivisionHelper( double customDivision ) {
            _mode = new CustomDivisionsMode( customDivision );
            _presenter = new CustomFormatter( customDivision );
        }

        public RangeDivisionsMode Mode { get { return _mode; } }
        public ValuePresenter Presenter { get { return _presenter; } }

        private sealed class CustomDivisionsMode : RangeDivisionsMode {
            private readonly double _customDivision;
            public CustomDivisionsMode( double customDivision ) { _customDivision = customDivision; }

            public override bool RequiresDivisionsEstimate { get { return Auto.RequiresDivisionsEstimate; } }

            public override IList<TData> GetDivisions<TData>( IRangeDataMapper<TData> mapper, int divisionsEstimate ) {
                var divisions = new List<double>( Auto.GetDivisions( (IRangeDataMapper<double>)mapper, divisionsEstimate ) );
                int index = divisions.BinarySearch( _customDivision );
                if( index < 0 )
                    divisions.Insert( ~index, _customDivision );
                return (IList<TData>)divisions;
            }

            public override IList<TData> GetSubdivisions<TData>( IRangeDataMapper<TData> mapper, IList<TData> dependentDivisions ) {
                return Auto.GetSubdivisions( mapper, dependentDivisions );
            }
        }

        private sealed class CustomFormatter : GeneralValueFormatter {
            private readonly double _customDivision;
            public CustomFormatter( double customDivision ) { _customDivision = customDivision; }

            protected override UIElement VisualizeCore<TData>( TData value, ValuePresenterArgs args, UIElement existingVisual ) {
                UIElement visual;
                if( _customDivision.Equals( value ) ) {
                    visual = base.VisualizeCore( "custom", args, existingVisual );
                    Panel.SetZIndex( visual, 10 );
                }
                else {
                    visual = base.VisualizeCore<TData>( value, args, existingVisual );
                    Panel.SetZIndex( visual, 0 );
                }

                return visual;
            }
        }
    }

~ Paul H
0 Kudos
Message 4 of 12
(8,330 Views)

Dear Paul,

 

I have succesfully applied your suggestion to create ticks and labels at each octave on a logarithmic frequency axis. One problem remains: my range extends up to 10000 Hz. The last label I want to show is "8000". But the graph hides this label (see image), I guess because there is not enough room to show it or it would overlap with some other feature. Could I override this behaviour somehow, so it always shows each label?

 

Thanks,

Bert

 

Capture.PNG

0 Kudos
Message 5 of 12
(8,158 Views)

The last tick label is being hidden because it overlaps the 10 kHz value (the panel does not look at Opacity when it checks for overlapping elements so, even though the maximum label is not user-visible, it is still given precedence).


Use Panel.SetZIndex, as in the example above, to make sure your tick values take precedence over the minimum and maximum tick labels you are hiding with Opacity.

~ Paul H
0 Kudos
Message 6 of 12
(8,150 Views)

Aahh, stupid of me not to read your example correctly. You're absolutely right and it works fine now. Thanks a lot!

0 Kudos
Message 7 of 12
(8,147 Views)

No problem; the z-index aspect was buried in the middle, and there was no comment describing its purpose. Glad it works!

~ Paul H
0 Kudos
Message 8 of 12
(8,143 Views)

Just wanted to let you know that we have added custom divisions in the Measurement Studio 2015 release.

~ Paul H
Message 9 of 12
(6,511 Views)

Dear Paul,

 

Thank you for your information. We recently obtained MS2015. I have now set the custom divisions for my logarithmic axis, one for each octave, as explained earlier. I would, however, also like to see gridlines at these custom divisions. Is that possible?

Otherwise, I would still need to use your previous solution. This solution now gives some error, however, so I have to see if I can get it to work. I will get back here if I can't get it to work.

0 Kudos
Message 10 of 12
(6,352 Views)