1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-26 20:28:50 -05:00

finish date/time picker

This commit is contained in:
epezent 2020-09-09 09:00:50 -05:00
parent 729bd762cc
commit 3cdf7add04
3 changed files with 51 additions and 18 deletions

View File

@ -814,6 +814,21 @@ ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
return t.S - t1.S < t2.S - t.S ? t1 : t2; return t.S - t1.S < t2.S - t.S ? t1 : t2;
} }
ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& tod_part) {
tm& Tm = GImPlot->Tm;
GetTime(date_part, &GImPlot->Tm);
int y = Tm.tm_year;
int m = Tm.tm_mon;
int d = Tm.tm_mday;
GetTime(tod_part, &GImPlot->Tm);
Tm.tm_year = y;
Tm.tm_mon = m;
Tm.tm_mday = d;
ImPlotTime t = MkTime(&Tm);
t.Us = tod_part.Us;
return t;
}
int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt) { int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt) {
tm& Tm = GImPlot->Tm; tm& Tm = GImPlot->Tm;
GetTime(t, &Tm); GetTime(t, &Tm);
@ -1685,7 +1700,7 @@ inline void EndDisabledControls(bool cond) {
} }
} }
inline void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed) { void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed) {
ImGui::PushItemWidth(75); ImGui::PushItemWidth(75);
ImPlotAxis& axis = *state.Axis; ImPlotAxis& axis = *state.Axis;
@ -1708,12 +1723,17 @@ inline void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed) {
ImGui::SameLine(); ImGui::SameLine();
BeginDisabledControls(state.LockMin); BeginDisabledControls(state.LockMin);
if (ImGui::BeginMenu("Min Time")) { if (ImGui::BeginMenu("Min Time")) {
if (ShowDatePicker("minpick",&axis.PickerLevel,&axis.PickerTimeMin,&tmin,&tmax)) { if (ShowTimePicker("mintime", &tmin)) {
if (axis.PickerTimeMin >= tmax) { if (tmin >= tmax)
tmax = AddTime(axis.PickerTimeMin, ImPlotTimeUnit_Day, 1); tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
axis.SetMax(tmax.ToDouble()); axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
} }
axis.SetMin(axis.PickerTimeMin.ToDouble()); ImGui::Separator();
if (ShowDatePicker("mindate",&axis.PickerLevel,&axis.PickerTimeMin,&tmin,&tmax)) {
tmin = CombineDateTime(axis.PickerTimeMin, tmin);
if (tmin >= tmax)
tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
@ -1726,12 +1746,17 @@ inline void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed) {
ImGui::SameLine(); ImGui::SameLine();
BeginDisabledControls(state.LockMax); BeginDisabledControls(state.LockMax);
if (ImGui::BeginMenu("Max Time")) { if (ImGui::BeginMenu("Max Time")) {
if (ShowDatePicker("macpick",&axis.PickerLevel,&axis.PickerTimeMax,&tmin,&tmax)) { if (ShowTimePicker("maxtime", &tmax)) {
if (axis.PickerTimeMax <= tmin) { if (tmax <= tmin)
tmin = AddTime(axis.PickerTimeMax, ImPlotTimeUnit_Day, -1); tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
axis.SetMin(tmin.ToDouble()); axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
} }
axis.SetMax(axis.PickerTimeMax.ToDouble()); ImGui::Separator();
if (ShowDatePicker("maxdate",&axis.PickerLevel,&axis.PickerTimeMax,&tmin,&tmax)) {
tmax = CombineDateTime(axis.PickerTimeMax, tmax);
if (tmax <= tmin)
tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@ -623,7 +623,7 @@ void ShowDemoWindow(bool* p_open) {
} }
ImPlot::SetNextPlotLimits(t_min,t_max,0,1); ImPlot::SetNextPlotLimits(t_min,t_max,0,1);
if (ImPlot::BeginPlot("##Time", "Time", "Value", ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) { if (ImPlot::BeginPlot("##Time", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) {
if (data != NULL) { if (data != NULL) {
// downsample our data // downsample our data
int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1; int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1;

View File

@ -196,7 +196,7 @@ enum ImPlotTimeFmt_ {
ImPlotTimeFmt_DayMoYr, // 10/3/91 ImPlotTimeFmt_DayMoYr, // 10/3/91
ImPlotTimeFmt_DayMoYrHrMin, // 10/3/91 7:21pm ImPlotTimeFmt_DayMoYrHrMin, // 10/3/91 7:21pm
ImPlotTimeFmt_DayMoYrHrMinS, // 10/3/91 7:21:29pm ImPlotTimeFmt_DayMoYrHrMinS, // 10/3/91 7:21:29pm
ImPlotTimeFmt_DayMoYrHrMinSUs, // 10/3/91 7:21:29.123456pm ImPlotTimeFmt_DayMoYrHrMinSUs, // 10/3/1991 7:21:29.123456pm
ImPlotTimeFmt_MoYr, // Oct 1991 ImPlotTimeFmt_MoYr, // Oct 1991
ImPlotTimeFmt_Mo, // Oct ImPlotTimeFmt_Mo, // Oct
ImPlotTimeFmt_Yr // 1991 ImPlotTimeFmt_Yr // 1991
@ -646,6 +646,9 @@ IMPLOT_API ImPlotState* GetCurrentPlot();
// Busts the cache for every plot in the current context // Busts the cache for every plot in the current context
IMPLOT_API void BustPlotCache(); IMPLOT_API void BustPlotCache();
// Shows a plot's context menu.
IMPLOT_API void ShowPlotContextMenu(ImPlotState& plot);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] Item Utils // [SECTION] Item Utils
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -696,6 +699,9 @@ IMPLOT_API void PushLinkedAxis(ImPlotAxis& axis);
// Updates axis internal range from points for linked axes. // Updates axis internal range from points for linked axes.
IMPLOT_API void PullLinkedAxis(ImPlotAxis& axis); IMPLOT_API void PullLinkedAxis(ImPlotAxis& axis);
// Shows an axis's context menu.
IMPLOT_API void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed = false);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] Legend Utils // [SECTION] Legend Utils
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -829,16 +835,18 @@ IMPLOT_API ImPlotTime MakeYear(int year);
// Get year component from timestamp [1970-3000] // Get year component from timestamp [1970-3000]
IMPLOT_API int GetYear(const ImPlotTime& t); IMPLOT_API int GetYear(const ImPlotTime& t);
// Adds time to a timestamp. #count must be positive! // Adds or subtracts time from a timestamp. #count > 0 to add, < 0 to subtract.
IMPLOT_API ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count); IMPLOT_API ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count);
// Rounds a timestamp down to nearest. // Rounds a timestamp down to nearest unit.
IMPLOT_API ImPlotTime FloorTime(const ImPlotTime& t, ImPlotTimeUnit unit); IMPLOT_API ImPlotTime FloorTime(const ImPlotTime& t, ImPlotTimeUnit unit);
// Rounds a timestamp up to the nearest unit. // Rounds a timestamp up to the nearest unit.
IMPLOT_API ImPlotTime CeilTime(const ImPlotTime& t, ImPlotTimeUnit unit); IMPLOT_API ImPlotTime CeilTime(const ImPlotTime& t, ImPlotTimeUnit unit);
// Rounds a timestamp up or down to the nearest unit. // Rounds a timestamp up or down to the nearest unit.
IMPLOT_API ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit); IMPLOT_API ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit);
// Combines the date of one timestamp with the time-of-day of another timestamp.
IMPLOT_API ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& tod_part);
// Formates a timestamp t into a buffer according to fmt. // Formulates a timestamp t into a buffer according to fmt.
IMPLOT_API int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt); IMPLOT_API int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt);
// Prints a timestamp to console // Prints a timestamp to console
IMPLOT_API void PrintTime(const ImPlotTime& t, ImPlotTimeFmt fmt = ImPlotTimeFmt_DayMoYrHrMinSUs); IMPLOT_API void PrintTime(const ImPlotTime& t, ImPlotTimeFmt fmt = ImPlotTimeFmt_DayMoYrHrMinSUs);