Skip to content

Instantly share code, notes, and snippets.

@MareMare
Last active September 22, 2022 08:28
Show Gist options
  • Save MareMare/d294463e700db4a06af1b80e0b66afa4 to your computer and use it in GitHub Desktop.
Save MareMare/d294463e700db4a06af1b80e0b66afa4 to your computer and use it in GitHub Desktop.
DateTimeTickUnits: The X-axis of `DateTime` mixture 24-hour and 12-hour notation.

ScottPlot/ScottPlot#2132


DateTimeTickUnits: The X-axis of DateTime mixture 24-hour and 12-hour notation.

Zoom in on a graph displaying DateTime data on the X-axis with the mouse wheel or right-click.

In this case, the 24-hour and 12-hour displays show different times, depending on whether they zoom to the nearest second or include milliseconds. It appears that 24-hour notation is used for zooms that display up to seconds, and 12-hour notation is used for zooms that also display milliseconds.

Is this as expected?

pattern Actual output comment
(A) Zooms that display up to seconds. 1985/09/23
23:59:59
As expected.
(B) Zooms that also display milliseconds. 1985/09/23
11:59:59.5
The time format expects 23:59:59.5.

Maybe HH is correct in (B) instead of hh?

Animation

The reproduced code is as follows:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ExecuteRecipe(formsPlot1.Plot);
    }

    private void ExecuteRecipe(Plot plt)
    {
        // original source: https://github.com/ScottPlot/ScottPlot/blob/07ab190973cbf8697ff8bdc98b4e6475ce40ea03/src/ScottPlot4/ScottPlot.Cookbook/Recipes/Axis.cs#L208-L222

        // create data sample data
        var myDates = new DateTime[1000]; // Change: 100 --> 1000
        for (var i = 0; i < myDates.Length; i++)
            myDates[i] = new DateTime(1985, 9, 24).AddMinutes(10 * i); // Change: .AddDays(7 * i) --> .AddMinutes(10 * i)

        // Convert DateTime[] to double[] before plotting
        var xs = myDates.Select(x => x.ToOADate()).ToArray();
        var ys = DataGen.RandomWalk(myDates.Length);
        plt.AddScatter(xs, ys);

        // Then tell the axis to display tick labels using a time format
        plt.XAxis.DateTimeFormat(true);
    }
}

DateTimeTickUnits: DateTimeのX軸は、24時間表記と12時間表記が混在しています。

X軸にDateTimeデータを表示したグラフでマウスホイールや右クリックなどでズームします。

このとき、秒までをズームする場合とミリ秒を含めたズームとで時刻の表記が24時間表記と12時間表記とで異なります。 秒までを表示するズームでは24時間表記となり、ミリ秒も表示されるズームでは12時間表記となっているようです。

これは期待通りでしょうか?

パターン 実際の出力 コメント
(A) 秒までが表示されるズーム 1985/09/23
23:59:59
期待通りです。
(B) ミリ秒も表示されるズーム 1985/09/23
11:59:59.5
時刻の書式は 23:59:59.5 を期待しています。

もしかして(B)では hh ではなくて HH が正しいのでしょうか?

Animation

再現コードは次の通りです:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ExecuteRecipe(formsPlot1.Plot);
    }

    private void ExecuteRecipe(Plot plt)
    {
        // original source: https://github.com/ScottPlot/ScottPlot/blob/07ab190973cbf8697ff8bdc98b4e6475ce40ea03/src/ScottPlot4/ScottPlot.Cookbook/Recipes/Axis.cs#L208-L222

        // create data sample data
        var myDates = new DateTime[1000]; // Change: 100 --> 1000
        for (var i = 0; i < myDates.Length; i++)
            myDates[i] = new DateTime(1985, 9, 24).AddMinutes(10 * i); // Change: .AddDays(7 * i) --> .AddMinutes(10 * i)

        // Convert DateTime[] to double[] before plotting
        var xs = myDates.Select(x => x.ToOADate()).ToArray();
        var ys = DataGen.RandomWalk(myDates.Length);
        plt.AddScatter(xs, ys);

        // Then tell the axis to display tick labels using a time format
        plt.XAxis.DateTimeFormat(true);
    }
}
@MareMare
Copy link
Author

Animation

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