Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

I would like the X axis on my WPF graph to display time in seconds from Zero

I have a graph with three AnalogWaveform<double> with sample rate of 50 as the datasource.

 

What is the best strategy to get the graph to display time in seconds or mm:ss starting at zero and ignore the full timestamp

 

 

0 Kudos
Message 1 of 2
(3,465 Views)

You can use a custom value formatter for the LabelPresenter on the MajorDivisions of your time axis. Here is a sample implementation for waveforms using precision timing, including two ways to format the time difference from Zero (mm:ss and total seconds):


    Code
    public sealed class RelativeTimeFormatter : GeneralValueFormatter {

        public PrecisionDateTime? ZeroValue { get; set; }
        public bool DisplayMinutesAndSeconds { get; set; }

        protected override string FormatCore<TData>( TData value, ValuePresenterArgs args ) {
            var time = value as PrecisionDateTime?;
            if( !time.HasValue )
                return base.FormatCore( value, args );

            // Get the first time value specified for all waveforms
            // or retrieve the last start value observed by the graph.
            var zeroValue = ZeroValue ?? (PrecisionDateTime)args.Data["Zero Value"];

            // Determine the time from Zero.
            PrecisionTimeSpan difference = time.Value - zeroValue;

            // Format result.
            string result;
            if( DisplayMinutesAndSeconds ) {
                long wholeMinutes = difference.WholeSeconds / 60;
                double seconds = difference.TotalSeconds % 60;
                result = wholeMinutes + ":" + seconds.ToString( "00" );
            }
            else {
                result = base.FormatCore( difference.TotalSeconds, args );
            }

            return result;
        }
    }

    XAML
    <ni:AxisDouble Orientation="Horizontal">
        <ni:AxisDouble.MajorDivisions>
            <ni:RangeLabeledDivisions>
                <ni:RangeLabeledDivisions.LabelPresenter>
                    <local:RelativeTimeFormatter DisplayMinutesAndSeconds="True" />
                </ni:RangeLabeledDivisions.LabelPresenter>
            </ni:RangeLabeledDivisions>
        </ni:AxisDouble.MajorDivisions>
    </ni:AxisDouble>

~ Paul H
Message 2 of 2
(3,459 Views)