Skip to content

Commit 30eb988

Browse files
authored
Merge branch 'master' into master
2 parents 9478e2f + 35f7955 commit 30eb988

File tree

8 files changed

+108
-37
lines changed

8 files changed

+108
-37
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
cmake-build/example_implot
4545
4646
MacOS:
47-
runs-on: macos-11
47+
runs-on: macos-latest
4848

4949
strategy:
5050
fail-fast: false
@@ -141,4 +141,4 @@ jobs:
141141

142142
- name: Run (MingW)
143143
run: .\cmake-build\example_implot.exe
144-
144+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ A: Maybe. Check the demo, gallery, or Announcements ([2020](https://github.com/e
155155

156156
**Q: Does ImPlot support 3D plots?**
157157

158-
A: No, and likely never will since ImGui only deals in 2D rendering.
158+
A: An experimental extension to ImPlot, [ImPlot3D](https://github.com/brenocq/implot3d), provides a similar API for plotting and interacting with 3D data.
159159

160160
**Q: Does ImPlot provide analytic tools?**
161161

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ The list below represents a combination of high-priority work, nice-to-have feat
1515

1616
- add `PlotBubbles` (see MATLAB bubble chart)
1717
- add non-zero references for `PlotBars` etc.
18-
- add exploding to `PlotPieChart` (on hover-highlight?)
1918
- fix appearance of `PlotBars` spacing
2019

2120
## Styling

implot.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ You can read releases logs https://github.com/epezent/implot/releases for more d
125125
126126
*/
127127

128+
#ifndef IMGUI_DEFINE_MATH_OPERATORS
128129
#define IMGUI_DEFINE_MATH_OPERATORS
130+
#endif
129131
#include "implot.h"
132+
#ifndef IMGUI_DISABLE
130133
#include "implot_internal.h"
131134

132135
#include <stdlib.h>
@@ -947,20 +950,6 @@ tm* GetLocTime(const ImPlotTime& t, tm* ptm) {
947950
#endif
948951
}
949952

950-
inline ImPlotTime MkTime(struct tm *ptm) {
951-
if (GetStyle().UseLocalTime)
952-
return MkLocTime(ptm);
953-
else
954-
return MkGmtTime(ptm);
955-
}
956-
957-
inline tm* GetTime(const ImPlotTime& t, tm* ptm) {
958-
if (GetStyle().UseLocalTime)
959-
return GetLocTime(t,ptm);
960-
else
961-
return GetGmtTime(t,ptm);
962-
}
963-
964953
ImPlotTime MakeTime(int year, int month, int day, int hour, int min, int sec, int us) {
965954
tm& Tm = GImPlot->Tm;
966955

@@ -990,6 +979,12 @@ int GetYear(const ImPlotTime& t) {
990979
return Tm.tm_year + 1900;
991980
}
992981

982+
int GetMonth(const ImPlotTime& t) {
983+
tm& Tm = GImPlot->Tm;
984+
ImPlot::GetTime(t, &Tm);
985+
return Tm.tm_mon;
986+
}
987+
993988
ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count) {
994989
tm& Tm = GImPlot->Tm;
995990
ImPlotTime t_out = t;
@@ -5186,7 +5181,7 @@ void ShowUserGuide() {
51865181
ImGui::Indent();
51875182
ImGui::BulletText("Left-click drag on axis labels to pan an individual axis.");
51885183
ImGui::Unindent();
5189-
ImGui::BulletText("Scroll in the plot area to zoom both X any Y axes.");
5184+
ImGui::BulletText("Scroll in the plot area to zoom both X and Y axes.");
51905185
ImGui::Indent();
51915186
ImGui::BulletText("Scroll on axis labels to zoom an individual axis.");
51925187
ImGui::Unindent();
@@ -5898,3 +5893,5 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con
58985893
#endif
58995894

59005895
} // namespace ImPlot
5896+
5897+
#endif // #ifndef IMGUI_DISABLE

implot.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#pragma once
4848
#include "imgui.h"
49+
#ifndef IMGUI_DISABLE
4950

5051
//-----------------------------------------------------------------------------
5152
// [SECTION] Macros and Defines
@@ -288,7 +289,8 @@ enum ImPlotInfLinesFlags_ {
288289
enum ImPlotPieChartFlags_ {
289290
ImPlotPieChartFlags_None = 0, // default
290291
ImPlotPieChartFlags_Normalize = 1 << 10, // force normalization of pie chart values (i.e. always make a full circle if sum < 0)
291-
ImPlotPieChartFlags_IgnoreHidden = 1 << 11 // ignore hidden slices when drawing the pie chart (as if they were not there)
292+
ImPlotPieChartFlags_IgnoreHidden = 1 << 11, // ignore hidden slices when drawing the pie chart (as if they were not there)
293+
ImPlotPieChartFlags_Exploding = 1 << 12 // Explode legend-hovered slice
292294
};
293295

294296
// Flags for PlotHeatmap
@@ -1296,4 +1298,5 @@ IMPLOT_DEPRECATED( IMPLOT_API bool BeginPlot(const char* title_id,
12961298

12971299
} // namespace ImPlot
12981300

1299-
#endif
1301+
#endif // #ifndef IMPLOT_DISABLE_OBSOLETE_FUNCTIONS
1302+
#endif // #ifndef IMGUI_DISABLE

implot_demo.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#endif
2929

3030
#include "implot.h"
31+
#ifndef IMGUI_DISABLE
3132
#include <math.h>
3233
#include <stdio.h>
3334
#include <stdlib.h>
@@ -43,6 +44,8 @@
4344

4445
#define CHECKBOX_FLAG(flags, flag) ImGui::CheckboxFlags(#flag, (unsigned int*)&flags, flag)
4546

47+
#if !defined(IMGUI_DISABLE_DEMO_WINDOWS)
48+
4649
// Encapsulates examples for customizing ImPlot.
4750
namespace MyImPlot {
4851

@@ -612,6 +615,7 @@ void Demo_PieCharts() {
612615
ImGui::DragFloat4("Values", data1, 0.01f, 0, 1);
613616
CHECKBOX_FLAG(flags, ImPlotPieChartFlags_Normalize);
614617
CHECKBOX_FLAG(flags, ImPlotPieChartFlags_IgnoreHidden);
618+
CHECKBOX_FLAG(flags, ImPlotPieChartFlags_Exploding);
615619

616620
if (ImPlot::BeginPlot("##Pie1", ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMouseText)) {
617621
ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations);
@@ -2495,3 +2499,11 @@ void PlotCandlestick(const char* label_id, const double* xs, const double* opens
24952499
}
24962500

24972501
} // namespace MyImplot
2502+
2503+
#else
2504+
2505+
void ImPlot::ShowDemoWindow(bool* p_open) {}
2506+
2507+
#endif
2508+
2509+
#endif // #ifndef IMGUI_DISABLE

implot_internal.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
#pragma once
3333

34-
#include <time.h>
35-
#include "imgui_internal.h"
36-
3734
#ifndef IMPLOT_VERSION
3835
#error Must include implot.h before implot_internal.h
3936
#endif
4037

38+
#ifndef IMGUI_DISABLE
39+
#include <time.h>
40+
#include "imgui_internal.h"
4141

4242
// Support for pre-1.84 versions. ImPool's GetSize() -> GetBufSize()
4343
#if (IMGUI_VERSION_NUM < 18303)
@@ -1565,11 +1565,24 @@ IMPLOT_API tm* GetLocTime(const ImPlotTime& t, tm* ptm);
15651565
// NB: The following functions only work if there is a current ImPlotContext because the
15661566
// internal tm struct is owned by the context! They are aware of ImPlotStyle.UseLocalTime.
15671567

1568+
// // Make a UNIX timestamp from a tm struct according to the current ImPlotStyle.UseLocalTime setting.
1569+
static inline ImPlotTime MkTime(struct tm *ptm) {
1570+
if (GetStyle().UseLocalTime) return MkLocTime(ptm);
1571+
else return MkGmtTime(ptm);
1572+
}
1573+
// Get a tm struct from a UNIX timestamp according to the current ImPlotStyle.UseLocalTime setting.
1574+
static inline tm* GetTime(const ImPlotTime& t, tm* ptm) {
1575+
if (GetStyle().UseLocalTime) return GetLocTime(t,ptm);
1576+
else return GetGmtTime(t,ptm);
1577+
}
1578+
15681579
// Make a timestamp from time components.
15691580
// year[1970-3000], month[0-11], day[1-31], hour[0-23], min[0-59], sec[0-59], us[0,999999]
15701581
IMPLOT_API ImPlotTime MakeTime(int year, int month = 0, int day = 1, int hour = 0, int min = 0, int sec = 0, int us = 0);
15711582
// Get year component from timestamp [1970-3000]
15721583
IMPLOT_API int GetYear(const ImPlotTime& t);
1584+
// Get month component from timestamp [0-11]
1585+
IMPLOT_API int GetMonth(const ImPlotTime& t);
15731586

15741587
// Adds or subtracts time from a timestamp. #count > 0 to add, < 0 to subtract.
15751588
IMPLOT_API ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count);
@@ -1582,6 +1595,11 @@ IMPLOT_API ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit);
15821595
// Combines the date of one timestamp with the time-of-day of another timestamp.
15831596
IMPLOT_API ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& time_part);
15841597

1598+
// Get the current time as a timestamp.
1599+
static inline ImPlotTime Now() { return ImPlotTime::FromDouble((double)time(nullptr)); }
1600+
// Get the current date as a timestamp.
1601+
static inline ImPlotTime Today() { return ImPlot::FloorTime(Now(), ImPlotTimeUnit_Day); }
1602+
15851603
// Formats the time part of timestamp t into a buffer according to #fmt
15861604
IMPLOT_API int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, bool use_24_hr_clk);
15871605
// Formats the date part of timestamp t into a buffer according to #fmt
@@ -1668,3 +1686,5 @@ void Locator_Log10(ImPlotTicker& ticker, const ImPlotRange& range, float pixels,
16681686
void Locator_SymLog(ImPlotTicker& ticker, const ImPlotRange& range, float pixels, bool vertical, ImPlotFormatter formatter, void* formatter_data);
16691687

16701688
} // namespace ImPlot
1689+
1690+
#endif // #ifndef IMGUI_DISABLE

implot_items.cpp

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
// ImPlot v0.17
2424

25+
#ifndef IMGUI_DEFINE_MATH_OPERATORS
2526
#define IMGUI_DEFINE_MATH_OPERATORS
27+
#endif
2628
#include "implot.h"
29+
#ifndef IMGUI_DISABLE
2730
#include "implot_internal.h"
2831

2932
//-----------------------------------------------------------------------------
@@ -2197,20 +2200,53 @@ CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
21972200
// [SECTION] PlotPieChart
21982201
//-----------------------------------------------------------------------------
21992202

2200-
IMPLOT_INLINE void RenderPieSlice(ImDrawList& draw_list, const ImPlotPoint& center, double radius, double a0, double a1, ImU32 col) {
2203+
IMPLOT_INLINE void RenderPieSlice(ImDrawList& draw_list, const ImPlotPoint& center, double radius, double a0, double a1, ImU32 col, bool detached = false) {
22012204
const float resolution = 50 / (2 * IM_PI);
22022205
ImVec2 buffer[52];
2203-
buffer[0] = PlotToPixels(center,IMPLOT_AUTO,IMPLOT_AUTO);
2206+
22042207
int n = ImMax(3, (int)((a1 - a0) * resolution));
22052208
double da = (a1 - a0) / (n - 1);
22062209
int i = 0;
2207-
for (; i < n; ++i) {
2208-
double a = a0 + i * da;
2209-
buffer[i + 1] = PlotToPixels(center.x + radius * cos(a), center.y + radius * sin(a),IMPLOT_AUTO,IMPLOT_AUTO);
2210-
}
2211-
buffer[i+1] = buffer[0];
2210+
2211+
if (detached) {
2212+
const double offset = 0.08; // Offset of the detached slice
2213+
const double width_scale = 0.95; // Scale factor for the width of the detached slice
2214+
2215+
double a_mid = (a0 + a1) / 2;
2216+
double new_a0 = a_mid - (a1 - a0) * width_scale / 2;
2217+
double new_a1 = a_mid + (a1 - a0) * width_scale / 2;
2218+
double new_da = (new_a1 - new_a0) / (n - 1);
2219+
2220+
ImPlotPoint offsetCenter(center.x + offset * cos(a_mid), center.y + offset * sin(a_mid));
2221+
2222+
// Start point (center of the offset)
2223+
buffer[0] = PlotToPixels(offsetCenter, IMPLOT_AUTO, IMPLOT_AUTO);
2224+
2225+
for (; i < n; ++i) {
2226+
double a = new_a0 + i * new_da;
2227+
buffer[i + 1] = PlotToPixels(
2228+
offsetCenter.x + (radius + offset/2) * cos(a),
2229+
offsetCenter.y + (radius + offset/2) * sin(a),
2230+
IMPLOT_AUTO, IMPLOT_AUTO
2231+
);
2232+
}
2233+
2234+
} else {
2235+
buffer[0] = PlotToPixels(center, IMPLOT_AUTO, IMPLOT_AUTO);
2236+
for (; i < n; ++i) {
2237+
double a = a0 + i * da;
2238+
buffer[i + 1] = PlotToPixels(
2239+
center.x + radius * cos(a),
2240+
center.y + radius * sin(a),
2241+
IMPLOT_AUTO, IMPLOT_AUTO);
2242+
}
2243+
}
2244+
// Close the shape
2245+
buffer[i + 1] = buffer[0];
2246+
22122247
// fill
2213-
draw_list.AddConvexPolyFilled(buffer, n + 1, col);
2248+
draw_list.AddConvexPolyFilled(buffer, n + 2, col);
2249+
22142250
// border (for AA)
22152251
draw_list.AddPolyline(buffer, n + 2, col, 0, 2.0f);
22162252
}
@@ -2254,21 +2290,21 @@ void PlotPieChartEx(const char* const label_ids[], const T* values, int count, I
22542290
ImPlotPoint Pmax = ImPlotPoint(center.x + radius, center.y + radius);
22552291
for (int i = 0; i < count; ++i) {
22562292
ImPlotItem* item = GetItem(label_ids[i]);
2257-
22582293
const double percent = normalize ? (double)values[i] / sum : (double)values[i];
22592294
const bool skip = sum <= 0.0 || (ignore_hidden && item != nullptr && !item->Show);
22602295
if (!skip)
22612296
a1 = a0 + 2 * IM_PI * percent;
22622297

22632298
if (BeginItemEx(label_ids[i], FitterRect(Pmin, Pmax))) {
2299+
const bool hovered = ImPlot::IsLegendEntryHovered(label_ids[i]) && ImHasFlag(flags, ImPlotPieChartFlags_Exploding);
22642300
if (sum > 0.0) {
22652301
ImU32 col = GetCurrentItem()->Color;
22662302
if (percent < 0.5) {
2267-
RenderPieSlice(draw_list, center, radius, a0, a1, col);
2303+
RenderPieSlice(draw_list, center, radius, a0, a1, col, hovered);
22682304
}
22692305
else {
2270-
RenderPieSlice(draw_list, center, radius, a0, a0 + (a1 - a0) * 0.5, col);
2271-
RenderPieSlice(draw_list, center, radius, a0 + (a1 - a0) * 0.5, a1, col);
2306+
RenderPieSlice(draw_list, center, radius, a0, a0 + (a1 - a0) * 0.5, col, hovered);
2307+
RenderPieSlice(draw_list, center, radius, a0 + (a1 - a0) * 0.5, a1, col, hovered);
22722308
}
22732309
}
22742310
EndItem();
@@ -2320,7 +2356,9 @@ void PlotPieChart(const char* const label_ids[], const T* values, int count, dou
23202356
fmt((double)values[i], buffer, 32, fmt_data);
23212357
ImVec2 size = ImGui::CalcTextSize(buffer);
23222358
double angle = a0 + (a1 - a0) * 0.5;
2323-
ImVec2 pos = PlotToPixels(center.x + 0.5 * radius * cos(angle), center.y + 0.5 * radius * sin(angle), IMPLOT_AUTO, IMPLOT_AUTO);
2359+
const bool hovered = ImPlot::IsLegendEntryHovered(label_ids[i]) && ImHasFlag(flags, ImPlotPieChartFlags_Exploding);
2360+
const double offset = (hovered ? 0.6 : 0.5) * radius;
2361+
ImVec2 pos = PlotToPixels(center.x + offset * cos(angle), center.y + offset * sin(angle), IMPLOT_AUTO, IMPLOT_AUTO);
23242362
ImU32 col = CalcTextColor(ImGui::ColorConvertU32ToFloat4(item->Color));
23252363
draw_list.AddText(pos - size * 0.5f, col, buffer);
23262364
}
@@ -2806,3 +2844,5 @@ void PlotDummy(const char* label_id, ImPlotDummyFlags flags) {
28062844
}
28072845

28082846
} // namespace ImPlot
2847+
2848+
#endif // #ifndef IMGUI_DISABLE

0 commit comments

Comments
 (0)