Skip to content

Support timecode-style DateTime tick labels #537

Closed

Description

@vrdriver

Hi Scott,

I've got a bit of a curly question.

I was wondering if there is a way to reformat the data on the X Axis?

I'm able to open audio files and display their waveforms, however I'd like to be able to show the X Axis in a time code, instead of samples.

I've looked at the format-strings for plt.Ticks(numericFormatStringX: "t"), however it's just reformatting values in the millions and trying to get a time, which isn't working.

With 44100 samples a second, you can imagine the numbers don't work well at all...

Something like this maybe, I don't know...

x value = formatstring( x value / samplerate, "d:hh:mm:ss:mmm")

Is there a way to convert these values to time properly? Or can you point me in the right direction?

I'm at this so far:

formsPlot1.Configure(lockVerticalAxis: true);                    
formsPlot1.plt.PlotSignal(dataX1);
formsPlot1.plt.AxisAuto();
formsPlot1.plt.AxisBounds(0, samples, -1, 1);
formsPlot1.plt.Ticks(numericFormatStringX: "c"); // Obviously wrong
formsPlot1.Render();

Cheers,
Steve

Activity

  1. StendProg commented on Sep 9, 2020

    @StendProg
    Contributor

    Hi @vrdriver,

    You can provide sampleRate to PlotSingal() and set XAxis to date time by:

    double SampleRateDays = 44100.0 * 3600 * 24;
    plt.PlotSingal(dataX1, sampleRate: SampleRateDays);
    plt.Ticks(dateTimeX: true);

    Please note that the sampling rate is provided in samples per day, not samples per second.
    The disadvantage of this solution is that the minimum tick size is 1 second.

    Maybe for your task it is enough just to specify the sampling rate, and then 1 count will mean 1 second without converting the XAxis to DateTime format?

    plt.PlotSignal(dataX1, sampleRate: 44100);
  2. vrdriver commented on Sep 10, 2020

    @vrdriver
    Author

    I've just tried that, and while it didn't work, I'll keep plugging away. You may have pointed me in the right direction though.

  3. vrdriver commented on Sep 10, 2020

    @vrdriver
    Author

    I've discovered that while the above suggestions do change the axis, it also seems to change the entire data set when it's plotted, rather than just the time axis. But alas It won't format for me at "hh:mm:ss:mmm" on the X Axis.
    After having a brief look through the ~ScottPlot\Config\DateTimeTicks.cs file, it doesn't seem like trivial change to me at this stage.

  4. StendProg commented on Sep 10, 2020

    @StendProg
    Contributor

    I found a temponary solution for the problem. Do you want to see something like that?
    image

    last 4 digit is ms.

    This solution temponary, i don't think should be implemented in PR, I just describe it if you familar with source code changes:

    1. Return back to old dateTime axis system.
      change https://github.com/swharden/ScottPlot/blob/044c2587d990afea5d109d49f8d0432580b57cd5/src/ScottPlot/Config/TickCollection.cs#L59
      to RecalculatePositionsAutomaticNumeric(settings);
    2. Add support for ms ticks formating by replacing
      https://github.com/swharden/ScottPlot/blob/044c2587d990afea5d109d49f8d0432580b57cd5/src/ScottPlot/Config/TickCollection.cs#L365-L366 with
                    else if (dtTickSep.TotalSeconds > .5)
                        dtFmt = "{0:yyyy-MM-dd\nH:mm:ss}";
                    else
                        dtFmt = "{0:d:hh:mm:ss:ffff}";

    or just hack into and specify format you want after that if/elses

     dtFmt = "{0:hh:mm:ss:fff}"

    Please note that ms specified with f.

    New DateTime Axis system is too complex for me. I think only Scott (@swharden ) can change it, when he will have time.

  5. StendProg commented on Sep 10, 2020

    @StendProg
    Contributor

    PR #539 brings millisecond ticks on DateTimeAxis.

  6. vrdriver commented on Sep 11, 2020

    @vrdriver
    Author

    Wow, I'm absolutely amazed that you'd go to all this trouble. Thanks so much. I've seen the PR details and have not had a chance to try anything as yet. But, I have made a quick screenshot of my work in progress of what I'm working on and trying to do, specifically for context. Please note, it's not finished, and is currently in 'play/design mode'.
    
    Screen Shot 2020-09-11 at 10 18 34

    I'll review the PR and see if I need to add anything further to it.

  7. swharden commented on Sep 11, 2020

    @swharden
    Member

    Hi @vrdriver, thanks for raising this issue! I like the idea of supporting time codes, as using ScottPlot to work with audio is an excellent use for this graphing library. Your screenshot mock-up really helps me better understand the goal here, thanks for posting it!

    I anticipate being able to put some time into this over the weekend, then releasing an updated NuGet package once we get it figured out. @StendProg is making excellent progress in #539, so I anticipate we will figure this out soon!

  8. vrdriver commented on Sep 11, 2020

    @vrdriver
    Author

    Thanks Scott, and yes, like you have done with your spectral viewer (which I may incorporate at some stage), I'm also using naudio. I see those two as the perfect combination for this kind of thing.
    You may have come across this video series, and while good for the concept, your ScottPlot blew it out of the water for speed.
    https://www.youtube.com/watch?v=BP2MhB2KQe0

  9. swharden commented on Sep 20, 2020

    @swharden
    Member

    The bug relating to display of negative values (in #549) is still present but we are getting closer!
    EDIT: Tick spacing looks bad if the plot is zoomed to include more negative time because negative DateTime values are not valid. A solution is to offset the plot, perhaps by today's date (~2000 years)

    var plt = new ScottPlot.Plot(800, 300);
    
    // simulate 10 seconds of audio data
    int pointsPerSecond = 44100;
    Random rand = new Random(0);
    double[] ys = DataGen.RandomWalk(rand, pointsPerSecond * 10);
    
    // for DateTime compatibility, sample rate must be points/day
    double pointsPerDay = 24.0 * 60 * 60 * pointsPerSecond;
    plt.PlotSignal(ys, sampleRate: pointsPerDay);
    
    plt.Ticks(dateTimeX: true, dateTimeFormatStringX: "HH:mm:ss:fff");

    image

    @StendProg, I added a format argument to IDateTimeUnit.GetTicksAndLabels(), then modified DateTimeTickUnitBase.GetTicksAndLabels() to use that format string if it is not null. This is probably the best way to do this, but you may want to look at 16740ec to be sure you agree.

  10. added a commit that references this issue on Sep 20, 2020
  11. changed the title [-]Change the format of the X Axis data?[/-] [+]Support timecode-style DateTime tick labels[/+] on Sep 20, 2020
  12. swharden commented on Sep 20, 2020

    @swharden
    Member

    I think this is fully solved now. I'll release a NuGet package in an hour or two, and @vrdriver I look forward to hearing how it works in your hands! 👍

    timecode2

  13. vrdriver commented on Sep 21, 2020

    @vrdriver
    Author

    Hi @swharden and @StendProg, Thank you both so much for working on this.

    I spent some time on this tonight. Obviously not enough just yet as it's not working for me as yet. I was hoping it was going to be a drop in replacement, but I haven't had that happen just yet...

    Here's a rather boring video of me attempting to use the program with the new parameters added.
    https://youtu.be/CQB2IJ_pdQ8

    I've deliberately left in the reference preceding code related to naudio of how I'm getting my values so you know where I'm coming from a little more.

    I did find that it performed very strangely with adding the samplerate field option and the suggested values. You'll see that in the second part of the video, where it's just basically me scrolling in.

    The first section of the video is where it plots fine, but the timecode is still a little wrong, Then after adding the samplerate code, you can see where the wave form almost vanishes to a sliver at .00000000000000001 (you get the picture) seconds.

    I did have the times working at one stage, however I discovered that the graph itself wasn't actually wasn't plotting, correctly - or at all. I'll keep playing with it and see how I go. If could be that I've got other settings added.

    The upgrade went without too many hitches. This surprisingly appeared
    Screen Shot 2020-09-21 at 20 07 55

    But, I was able to quickly fix that up quickly though.
    Screen Shot 2020-09-21 at 20 08 43

    In all honesty, it's probably something I've done or a setting I've added, so I'll keep trying and see how I go though, but I thought I'd just keep you up to date. I'm excited to get it going. :)

    FormsPlot formsPlot1 = Application.OpenForms["Form1"].Controls["formsPlot1"] as FormsPlot;
     
    double pointsPerDay = 24.0 * 60 * 60 * Global.special_sample_rate[0]; // 44100
    formsPlot1.plt.Clear();
    formsPlot1.plt.AxisBounds(0, samples, -1, 1);
    formsPlot1.Configure(lockVerticalAxis: true);
    
    // At the moment, adding samplerate breaks it.
    // sampleRate: pointsPerDay,   
    
    formsPlot1.plt.PlotSignal(dataX1,  color: Color.SkyBlue); // Makes it draw
    formsPlot1.plt.PlotSignal(dataX2,   color: Color.LightBlue); // Makes it draw
    formsPlot1.plt.Style(ScottPlot.Style.Gray1);
    formsPlot1.plt.AxisAuto(tightenLayout: true);
    formsPlot1.plt.Ticks(displayTicksY: false );
    formsPlot1.plt.Frame(left: false, right: false, top: false);
    formsPlot1.plt.TightenLayout(padding: 0, render: true);
    formsPlot1.Configure(recalculateLayoutOnMouseUp: false);
    formsPlot1.plt.Grid(lineStyle: LineStyle.Dot);
                        
    formsPlot1.Render();  
    
  14. StendProg commented on Sep 21, 2020

    @StendProg
    Contributor

    Hi @vrdriver,
    It looks like you forgot to specify plt.Ticks (dateTimeX: true);
    Well, do not forget to provide the sampleRate in PlotSignal.

  15. swharden commented on Sep 21, 2020

    @swharden
    Member

    @vrdriver you may find this cookbook example helpful
    https://swharden.com/scottplot/cookbooks/4.0.40/#customize-axis-time-code-axis

    var plt = new ScottPlot.Plot(600, 400);
    
    // simulate 10 seconds of audio data
    int pointsPerSecond = 44100;
    Random rand = new Random(0);
    double[] ys = DataGen.RandomWalk(rand, pointsPerSecond * 10);
    
    // For DateTime compatibility, sample rate must be points/day.
    // Also, avoid negative dates by offsetting the plot by today's date.
    double secondsPerDay = 24 * 60 * 60;
    double pointsPerDay = secondsPerDay * pointsPerSecond;
    double today = DateTime.Today.ToOADate();
    
    plt.PlotSignal(ys, sampleRate: pointsPerDay, xOffset: today);
    plt.Ticks(dateTimeX: true, dateTimeFormatStringX: "HH:mm:ss.fff");

    image

  16. vrdriver commented on Sep 22, 2020

    @vrdriver
    Author

    Hi Scott, Yes, I was looking at the cookbook online and in the codebase. I think I'd taken out the "plt.Ticks (dateTimeX: true);" in the video example previously, as it was doing something weird at the time as well. I'd tested it all.
    Looking back on it now, I think that my original problem could have been that I was setting up the control on form_load to make it look pretty when the program loaded, which could have easily given it the wrong settings

    With all that in mind though, I have some good news. I created a new plot control, and your code tested out and worked fine (no surprises there). So, with that in mind, I proceeded to integrate my dataset in to it.

    After a lot of trial and error I've managed to get it to render correctly.

    FormsPlot formsPlot2 = Application.OpenForms["Form1"].Controls["formsPlot2"] as FormsPlot;
    int pointsPerSecond = 44100; // fixed value for the example
    // For DateTime compatibility, sample rate must be points/day.
    // Also, avoid negative dates by offsetting the plot by today's date.
    double secondsPerDay = 24 * 60 * 60;
    double pointsPerDay = secondsPerDay * pointsPerSecond;
    double today = DateTime.Today.ToOADate();
    formsPlot2.plt.Clear();
    formsPlot2.plt.AxisBounds(0, ((double)samples / (double)pointsPerDay), 0, 1);
    formsPlot2.Configure(lockVerticalAxis: true);
    formsPlot2.plt.PlotSignal(dataX1, sampleRate: pointsPerDay, color: Color.SkyBlue); // Makes it draw
    formsPlot2.plt.PlotSignal(dataX2, sampleRate: pointsPerDay, color: Color.LightBlue); // Makes it draw
    formsPlot2.plt.Ticks(rulerModeX: true, displayTicksY: false, dateTimeX: true, dateTimeFormatStringX: "HH:mm:ss.fff");
    formsPlot2.Render();
    

    The trial and error was not the initial rendering, but getting it to function correctly with my existing code base.
    It's worth noting that because of the sampleRate conversion that happens, all of the VLines are now in micro values.

    vLine_intro: 0.00137029864662816

    I'll just have to do a bit of refactoring to get the values back to the time values, but I don't think this will be too hard in the end.

    Screen Shot 2020-09-23 at 00 53 03

    And the time is working.
    Screen Shot 2020-09-23 at 00 54 18

    Thanks again.

  17. swharden commented on Sep 22, 2020

    @swharden
    Member

    all of the VLines are now in micro values. vLine_intro = 0.00137029864662816

    This is indeed awkward! When .NET converts a DateTime to a double, the number it produces is the number of days since 1900. Since the values are days, a minute and a half will be a very small fraction of a day.

    I don't think this will be too hard in the end.

    I'm really happy you figured out something that will work for you! Your application is looking fantastic, congratulations!

    Let us know if any questions pop up in the future 👍
    Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions