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

add ability to fill shaded plot to +/- INFINITY

This commit is contained in:
epezent 2021-01-11 13:22:52 -06:00
parent a6bab98517
commit 5fd1ecdfd7
3 changed files with 50 additions and 15 deletions

View File

@ -406,7 +406,7 @@ template <typename T> IMPLOT_API void PlotStairs(const char* label_id, const T*
template <typename T> IMPLOT_API void PlotStairs(const char* label_id, const T* xs, const T* ys, int count, int offset=0, int stride=sizeof(T)); template <typename T> IMPLOT_API void PlotStairs(const char* label_id, const T* xs, const T* ys, int count, int offset=0, int stride=sizeof(T));
IMPLOT_API void PlotStairsG(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset=0); IMPLOT_API void PlotStairsG(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset=0);
// Plots a shaded (filled) region between two lines, or a line and a horizontal reference. // Plots a shaded (filled) region between two lines, or a line and a horizontal reference. Set y_ref to +/-INFINITY for infinite fill extents.
template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* values, int count, double y_ref=0, double xscale=1, double x0=0, int offset=0, int stride=sizeof(T)); template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* values, int count, double y_ref=0, double xscale=1, double x0=0, int offset=0, int stride=sizeof(T));
template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* xs, const T* ys, int count, double y_ref=0, int offset=0, int stride=sizeof(T)); template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* xs, const T* ys, int count, double y_ref=0, int offset=0, int stride=sizeof(T));
template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset=0, int stride=sizeof(T)); template <typename T> IMPLOT_API void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset=0, int stride=sizeof(T));

View File

@ -264,17 +264,34 @@ void ShowDemoWindow(bool* p_open) {
static bool show_lines = true; static bool show_lines = true;
static bool show_fills = true; static bool show_fills = true;
static float fill_ref = 0; static float fill_ref = 0;
static int shade_mode = 0;
ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine(); ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine();
ImGui::Checkbox("Fills",&show_fills); ImGui::Checkbox("Fills",&show_fills);
ImGui::DragFloat("Reference",&fill_ref, 1, -100, 500); if (show_fills) {
ImGui::SameLine();
if (ImGui::RadioButton("To -INF",shade_mode == 0))
shade_mode = 0;
ImGui::SameLine();
if (ImGui::RadioButton("To +INF",shade_mode == 1))
shade_mode = 1;
ImGui::SameLine();
if (ImGui::RadioButton("To Ref",shade_mode == 2))
shade_mode = 2;
if (shade_mode == 2) {
ImGui::SameLine();
ImGui::SetNextItemWidth(100);
ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500);
}
}
ImPlot::SetNextPlotLimits(0,100,0,500); ImPlot::SetNextPlotLimits(0,100,0,500);
if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) { if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) {
if (show_fills) { if (show_fills) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, fill_ref); ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, fill_ref); ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, fill_ref); ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PopStyleVar(); ImPlot::PopStyleVar();
} }
if (show_lines) { if (show_lines) {

View File

@ -1120,14 +1120,14 @@ void PlotStairsG(const char* label_id, ImPlotPoint (*getter_func)(void* data, in
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename Getter1, typename Getter2> template <typename Getter1, typename Getter2>
inline void PlotShadedEx(const char* label_id, const Getter1& getter1, const Getter2& getter2) { inline void PlotShadedEx(const char* label_id, const Getter1& getter1, const Getter2& getter2, bool fit2) {
if (BeginItem(label_id, ImPlotCol_Fill)) { if (BeginItem(label_id, ImPlotCol_Fill)) {
if (FitThisFrame()) { if (FitThisFrame()) {
for (int i = 0; i < ImMin(getter1.Count, getter2.Count); ++i) { for (int i = 0; i < getter1.Count; ++i)
ImPlotPoint p1 = getter1(i); FitPoint(getter1(i));
ImPlotPoint p2 = getter2(i); if (fit2) {
FitPoint(p1); for (int i = 0; i < getter2.Count; ++i)
FitPoint(p2); FitPoint(getter2(i));
} }
} }
const ImPlotNextItemData& s = GetItemData(); const ImPlotNextItemData& s = GetItemData();
@ -1147,9 +1147,18 @@ inline void PlotShadedEx(const char* label_id, const Getter1& getter1, const Get
template <typename T> template <typename T>
void PlotShaded(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, int offset, int stride) { void PlotShaded(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, int offset, int stride) {
bool fit2 = true;
if (y_ref == -HUGE_VAL) {
fit2 = false;
y_ref = GetPlotLimits().Y.Min;
}
if (y_ref == HUGE_VAL) {
fit2 = false;
y_ref = GetPlotLimits().Y.Max;
}
GetterYs<T> getter1(values,count,xscale,x0,offset,stride); GetterYs<T> getter1(values,count,xscale,x0,offset,stride);
GetterYRef getter2(y_ref,count,xscale,x0); GetterYRef getter2(y_ref,count,xscale,x0);
PlotShadedEx(label_id, getter1, getter2); PlotShadedEx(label_id, getter1, getter2, fit2);
} }
template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* values, int count, double y_ref, double xscale, double x0, int offset, int stride); template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* values, int count, double y_ref, double xscale, double x0, int offset, int stride);
@ -1165,9 +1174,18 @@ template IMPLOT_API void PlotShaded<double>(const char* label_id, const double*
template <typename T> template <typename T>
void PlotShaded(const char* label_id, const T* xs, const T* ys, int count, double y_ref, int offset, int stride) { void PlotShaded(const char* label_id, const T* xs, const T* ys, int count, double y_ref, int offset, int stride) {
bool fit2 = true;
if (y_ref == -HUGE_VAL) {
fit2 = false;
y_ref = GetPlotLimits().Y.Min;
}
if (y_ref == HUGE_VAL) {
fit2 = false;
y_ref = GetPlotLimits().Y.Max;
}
GetterXsYs<T> getter1(xs, ys, count, offset, stride); GetterXsYs<T> getter1(xs, ys, count, offset, stride);
GetterXsYRef<T> getter2(xs, y_ref, count, offset, stride); GetterXsYRef<T> getter2(xs, y_ref, count, offset, stride);
PlotShadedEx(label_id, getter1, getter2); PlotShadedEx(label_id, getter1, getter2, fit2);
} }
template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* xs, const ImS8* ys, int count, double y_ref, int offset, int stride); template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* xs, const ImS8* ys, int count, double y_ref, int offset, int stride);
@ -1185,7 +1203,7 @@ template <typename T>
void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset, int stride) { void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset, int stride) {
GetterXsYs<T> getter1(xs, ys1, count, offset, stride); GetterXsYs<T> getter1(xs, ys1, count, offset, stride);
GetterXsYs<T> getter2(xs, ys2, count, offset, stride); GetterXsYs<T> getter2(xs, ys2, count, offset, stride);
PlotShadedEx(label_id, getter1, getter2); PlotShadedEx(label_id, getter1, getter2, true);
} }
template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* xs, const ImS8* ys1, const ImS8* ys2, int count, int offset, int stride); template IMPLOT_API void PlotShaded<ImS8>(const char* label_id, const ImS8* xs, const ImS8* ys1, const ImS8* ys2, int count, int offset, int stride);
@ -1203,7 +1221,7 @@ template IMPLOT_API void PlotShaded<double>(const char* label_id, const double*
void PlotShadedG(const char* label_id, ImPlotPoint (*g1)(void* data, int idx), void* data1, ImPlotPoint (*g2)(void* data, int idx), void* data2, int count, int offset) { void PlotShadedG(const char* label_id, ImPlotPoint (*g1)(void* data, int idx), void* data1, ImPlotPoint (*g2)(void* data, int idx), void* data2, int count, int offset) {
GetterFuncPtr getter1(g1, data1, count, offset); GetterFuncPtr getter1(g1, data1, count, offset);
GetterFuncPtr getter2(g2, data2, count, offset); GetterFuncPtr getter2(g2, data2, count, offset);
PlotShadedEx(label_id, getter1, getter2); PlotShadedEx(label_id, getter1, getter2, true);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------