Skip to content

Commit 1b95bb2

Browse files
committed
feat: initial changes to support pies
1 parent 3bc1229 commit 1b95bb2

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

include/Settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct Settings
3030
inline static const float PLOT_MIN_DELAY = IF_PLATFORM_WINDOWS(1.f, 0.01f);
3131
inline static const float PLOT_MAX_DELAY = 500.f;
3232

33-
enum class PLOT_TYPES { BARS, LINES, HEATMAP };
33+
enum class PLOT_TYPES { BARS, LINES, HEATMAP, PIE };
3434
inline static PLOT_TYPES PLOT_TYPE = PLOT_TYPES::BARS;
3535

3636
inline static bool PLOT_SHOW_SCALE = true;

src/Interface.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include "Interface.h"
33

4+
#include <string>
5+
46
#include "imgui-SFML.h"
57
#include "imgui.h"
68
#include "implot.h"
@@ -206,9 +208,9 @@ void Interface::draw(sf::RenderWindow& window) {
206208
ImGui::Checkbox("Show scale", &Settings::PLOT_SHOW_SCALE);
207209

208210
static const std::pair<const char*, Settings::PLOT_TYPES> typeNames[] = { {"Bars", Settings::PLOT_TYPES::BARS}, {"Lines", Settings::PLOT_TYPES::LINES},
209-
{"Heatmap", Settings::PLOT_TYPES::HEATMAP} };
211+
{"Heatmap", Settings::PLOT_TYPES::HEATMAP}, {"Pie", Settings::PLOT_TYPES::PIE} };
210212
static int typeChoosed = 0;
211-
if (ImGui::SliderInt("Plot type", &typeChoosed, 0, 2, typeNames[typeChoosed].first))
213+
if (ImGui::SliderInt("Plot type", &typeChoosed, 0, 3, typeNames[typeChoosed].first))
212214
Settings::PLOT_TYPE = typeNames[typeChoosed].second;
213215

214216
ImGui::Separator();
@@ -374,7 +376,11 @@ void Interface::draw(sf::RenderWindow& window) {
374376
ImGui::SameLine();
375377
}
376378

377-
if (ImPlot::BeginPlot("##MainPlot", {-1, plotSizeHeight}, ImPlotFlags_NoMenus | ImPlotFlags_NoMouseText)) {
379+
ImPlotFlags plotFlags = ImPlotFlags_NoMenus | ImPlotFlags_NoMouseText;
380+
if (Settings::PLOT_TYPE == Settings::PLOT_TYPES::PIE)
381+
plotFlags |= ImPlotFlags_NoLegend;
382+
383+
if (ImPlot::BeginPlot("##MainPlot", {-1, plotSizeHeight}, plotFlags)) {
378384
static std::pair<unsigned, unsigned> gridSize;
379385

380386
if (!Settings::PLOT_SHOW_SCALE || Settings::PLOT_TYPE == Settings::PLOT_TYPES::HEATMAP)
@@ -408,6 +414,32 @@ void Interface::draw(sf::RenderWindow& window) {
408414
ImPlot::PopStyleColor(); ImPlot::PopStyleColor(); ImPlot::PopStyleColor();
409415
}
410416
}
417+
else if(Settings::PLOT_TYPE == Settings::PLOT_TYPES::PIE) {
418+
ImPlot::SetupAxesLimits(0, 1, 0, 1, ImPlotCond_Always);
419+
420+
if(numbersSize > 0) {
421+
// cache label strings so ImPlot receives stable pointers each frame
422+
static std::vector<std::string> pieLabels;
423+
static std::vector<const char*> pieLabelIds;
424+
static unsigned pieLabelsInitialized = 0;
425+
426+
if(pieLabels.size() != numbersSize) {
427+
pieLabels.resize(numbersSize);
428+
pieLabelIds.resize(numbersSize);
429+
pieLabelsInitialized = 0;
430+
}
431+
432+
for(unsigned i = pieLabelsInitialized; i < numbersSize; ++i)
433+
pieLabels[i] = std::to_string(i);
434+
435+
pieLabelsInitialized = numbersSize;
436+
437+
for(unsigned i = 0; i < numbersSize; ++i)
438+
pieLabelIds[i] = pieLabels[i].c_str();
439+
440+
ImPlot::PlotPieChart(pieLabelIds.data(), &numbers[0], numbersSize, 0.5, 0.5, 0.48, "%.0f");
441+
}
442+
}
411443
else if(Settings::PLOT_TYPE == Settings::PLOT_TYPES::LINES) {
412444
ImPlot::SetupAxesLimits(0, numbersSize - 1, 0, Settings::SHUFFLE_MAX_VALUE, ImPlotCond_Always);
413445

0 commit comments

Comments
 (0)