Skip to content

Commit 23c1a79

Browse files
committed
feat: populate ImPlotSpec using (string, value) pair
1 parent 6ae00c5 commit 23c1a79

2 files changed

Lines changed: 66 additions & 20 deletions

File tree

implot.h

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ enum ImPlotBin_ {
493493
// ImPlotProp_Marker, ImPlotMarker_Circle,
494494
// ImPlotProp_Flags, ImPlotItemFlags_NoLegend | ImPlotLineFlags_Segments
495495
// });
496+
//
497+
// 3. Inline using (String,value) pairs (order does NOT matter):
498+
//
499+
// ImPlot::PlotLine("MyLine", xs, ys, 100, {
500+
// "LineColor", ImVec4(1,0,0,1),
501+
// "LineWeight", 2.0f,
502+
// "Marker", ImPlotMarker_Circle,
503+
// "Flags", ImPlotItemFlags_NoLegend | ImPlotLineFlags_Segments
504+
// });
496505
struct ImPlotSpec {
497506
ImVec4 LineColor = IMPLOT_AUTO_COL; // line color (applies to lines, bar edges); IMPLOT_AUTO_COL will use next Colormap color or current item color
498507
float LineWeight = 1.0f; // line weight in pixels (applies to lines, bar edges, marker edges)
@@ -511,16 +520,8 @@ struct ImPlotSpec {
511520
// Construct a plot item specification from (ImPlotProp,value) pairs in any order, e.g. ImPlotSpec(ImPlotProp_LineColor, my_color, ImPlotProp_Marker, 4.0f)
512521
template <typename ...Args>
513522
ImPlotSpec(Args... args) {
514-
static_assert((sizeof ...(Args)) % 2 == 0, "Odd number of arguments! You must provide (ImPlotProp,value) pairs!");
515-
SetProp(args...);
516-
}
517-
518-
// Set properties from (ImPlotProp,value) pairs in any order, e.g. SetProp(ImPlotProp_LineColor, my_color, ImPlotProp_Marker, 4.0f)
519-
template <typename Arg, typename ...Args>
520-
void SetProp(ImPlotProp prop, Arg arg, Args... args) {
521-
static_assert((sizeof ...(Args)) % 2 == 0, "Odd number of arguments! You must provide (ImPlotProp,value) pairs!");
522-
SetProp(prop, arg);
523-
SetProp(args...);
523+
static_assert((sizeof ...(Args)) % 2 == 0, "Odd number of arguments! You must provide (property, value) pairs!");
524+
Apply(args...);
524525
}
525526

526527
// Set a property from a scalar value.
@@ -554,6 +555,51 @@ struct ImPlotSpec {
554555
}
555556
IM_ASSERT(0 && "User provided an ImPlotProp which cannot be set from ImVec4 value!");
556557
}
558+
559+
// Helper to convert string to enum
560+
static inline ImPlotProp StringToProp(const char* name) {
561+
if (strcmp(name, "LineColor") == 0) return ImPlotProp_LineColor;
562+
if (strcmp(name, "LineWeight") == 0) return ImPlotProp_LineWeight;
563+
if (strcmp(name, "FillColor") == 0) return ImPlotProp_FillColor;
564+
if (strcmp(name, "FillAlpha") == 0) return ImPlotProp_FillAlpha;
565+
if (strcmp(name, "Marker") == 0) return ImPlotProp_Marker;
566+
if (strcmp(name, "MarkerLineColor") == 0) return ImPlotProp_MarkerLineColor;
567+
if (strcmp(name, "MarkerFillColor") == 0) return ImPlotProp_MarkerFillColor;
568+
if (strcmp(name, "Size") == 0) return ImPlotProp_Size;
569+
if (strcmp(name, "Offset") == 0) return ImPlotProp_Offset;
570+
if (strcmp(name, "Stride") == 0) return ImPlotProp_Stride;
571+
if (strcmp(name, "Flags") == 0) return ImPlotProp_Flags;
572+
return (ImPlotProp)-1;
573+
}
574+
575+
// Set property from string and scalar
576+
template <typename T>
577+
void SetProp(const char* prop_name, T v) {
578+
ImPlotProp prop = StringToProp(prop_name);
579+
if (prop != -1)
580+
SetProp(prop, v);
581+
else
582+
IM_ASSERT(0 && "Invalid ImPlotProp string name!");
583+
}
584+
585+
// Set property from string and ImVec4
586+
void SetProp(const char* prop_name, const ImVec4& v) {
587+
ImPlotProp prop = StringToProp(prop_name);
588+
if (prop != -1)
589+
SetProp(prop, v);
590+
else
591+
IM_ASSERT(0 && "Invalid ImPlotProp string name!");
592+
}
593+
594+
// Base case
595+
void Apply() { }
596+
597+
// Set properties from (ImPlotProp,value) pairs in any order, e.g. SetProp(ImPlotProp_LineColor, my_color, ImPlotProp_Marker, 4.0f)
598+
template <typename Key, typename Value, typename ...Args>
599+
void Apply(Key key, Value val, Args... args) {
600+
SetProp(key, val);
601+
Apply(args...);
602+
}
557603
};
558604

559605
// Double precision version of ImVec2 used by ImPlot. Extensible by end users.

implot_demo.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,11 @@ void Demo_ScatterPlots() {
410410
if (ImPlot::BeginPlot("Scatter Plot")) {
411411
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
412412
ImPlot::PlotScatter("Data 2", xs2, ys2, 50, {
413-
ImPlotProp_Marker, ImPlotMarker_Square,
414-
ImPlotProp_Size, 6,
415-
ImPlotProp_LineColor, GetColormapColor(1),
416-
ImPlotProp_FillColor, GetColormapColor(1),
417-
ImPlotProp_FillAlpha, 0.25f
413+
"Marker", ImPlotMarker_Square,
414+
"Size", 6,
415+
"LineColor", GetColormapColor(1),
416+
"FillColor", GetColormapColor(1),
417+
"FillAlpha", 0.25f
418418
});
419419
ImPlot::EndPlot();
420420
}
@@ -567,10 +567,10 @@ void Demo_ErrorBars() {
567567

568568
if (ImPlot::BeginPlot("##ErrorBars")) {
569569
ImPlot::SetupAxesLimits(0, 6, 0, 10);
570-
570+
571571
ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f);
572572
ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
573-
573+
574574
ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5, {ImPlotProp_LineColor, GetColormapColor(1), ImPlotProp_Size, 0});
575575
ImPlot::PlotLine("Line", xs, lin1, 5, {ImPlotProp_Marker, ImPlotMarker_Square});
576576

@@ -581,8 +581,8 @@ void Demo_ErrorBars() {
581581
ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5, spec);
582582
spec.Flags = ImPlotErrorBarsFlags_Horizontal;
583583
ImPlot::PlotErrorBars("Scatter", xs, lin2, err3, err4, 5, spec);
584-
ImPlot::PlotScatter("Scatter", xs, lin2, 5);
585-
584+
ImPlot::PlotScatter("Scatter", xs, lin2, 5);
585+
586586
ImPlot::EndPlot();
587587
}
588588
}
@@ -600,7 +600,7 @@ void Demo_StemPlots() {
600600
ImPlot::SetupAxisLimits(ImAxis_X1,0,1.0);
601601
ImPlot::SetupAxisLimits(ImAxis_Y1,0,1.6);
602602
ImPlot::PlotStems("Stems 1",xs,ys1,51);
603-
ImPlot::PlotStems("Stems 2", xs, ys2,51, 0, {ImPlotProp_Marker, ImPlotMarker_Circle});
603+
ImPlot::PlotStems("Stems 2", xs, ys2,51, 0, {"Marker", ImPlotMarker_Circle});
604604
ImPlot::EndPlot();
605605
}
606606
}

0 commit comments

Comments
 (0)