1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 23:57:26 -04:00

Merge branch 'shaded'

This commit is contained in:
Evan Pezent 2020-06-14 08:28:28 -05:00
commit 504b1ea155
3 changed files with 658 additions and 523 deletions

1056
implot.cpp

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ImPlot v0.3 WIP
// ImPlot v0.4 WIP
#pragma once
#include "imgui.h"
@ -45,12 +45,11 @@ enum ImPlotFlags_ {
ImPlotFlags_Query = 1 << 4, // the user will be able to draw query rects with middle-mouse
ImPlotFlags_ContextMenu = 1 << 5, // the user will be able to open a context menu with double-right click
ImPlotFlags_Crosshairs = 1 << 6, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_CullData = 1 << 7, // plot data outside the plot area will be culled from rendering
ImPlotFlags_AntiAliased = 1 << 8, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_NoChild = 1 << 9, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications)
ImPlotFlags_YAxis2 = 1 << 10, // enable a 2nd y axis
ImPlotFlags_YAxis3 = 1 << 11, // enable a 3rd y axis
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_BoxSelect | ImPlotFlags_ContextMenu | ImPlotFlags_CullData
ImPlotFlags_AntiAliased = 1 << 7, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_NoChild = 1 << 8, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications)
ImPlotFlags_YAxis2 = 1 << 9, // enable a 2nd y-axis
ImPlotFlags_YAxis3 = 1 << 10, // enable a 3rd y-axis
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_BoxSelect | ImPlotFlags_ContextMenu
};
// Options for plot axes (X and Y).
@ -61,10 +60,9 @@ enum ImPlotAxisFlags_ {
ImPlotAxisFlags_Invert = 1 << 3, // the axis will be inverted
ImPlotAxisFlags_LockMin = 1 << 4, // the axis minimum value will be locked when panning/zooming
ImPlotAxisFlags_LockMax = 1 << 5, // the axis maximum value will be locked when panning/zooming
ImPlotAxisFlags_Adaptive = 1 << 6, // grid divisions will adapt to the current pixel size the axis
ImPlotAxisFlags_LogScale = 1 << 7, // a logartithmic (base 10) axis scale will be used
ImPlotAxisFlags_Scientific = 1 << 8, // scientific notation will be used for tick labels if displayed (WIP, not very good yet)
ImPlotAxisFlags_Default = ImPlotAxisFlags_GridLines | ImPlotAxisFlags_TickMarks | ImPlotAxisFlags_TickLabels | ImPlotAxisFlags_Adaptive,
ImPlotAxisFlags_LogScale = 1 << 6, // a logartithmic (base 10) axis scale will be used
ImPlotAxisFlags_Scientific = 1 << 7, // scientific notation will be used for tick labels if displayed (WIP, not very good yet)
ImPlotAxisFlags_Default = ImPlotAxisFlags_GridLines | ImPlotAxisFlags_TickMarks | ImPlotAxisFlags_TickLabels,
ImPlotAxisFlags_Auxiliary = ImPlotAxisFlags_Default & ~ImPlotAxisFlags_GridLines,
};
@ -93,6 +91,7 @@ enum ImPlotStyleVar_ {
ImPlotStyleVar_Marker, // int, marker specification
ImPlotStyleVar_MarkerSize, // float, marker size in pixels (roughly the marker's "radius")
ImPlotStyleVar_MarkerWeight, // float, outline weight of markers in pixels
ImPlotStyleVar_FillAlpha, // float, alpha modifier applied to all plot item fills
ImPlotStyleVar_ErrorBarSize, // float, error bar whisker width in pixels
ImPlotStyleVar_ErrorBarWeight, // float, error bar whisker weight in pixels
ImPlotStyleVar_DigitalBitHeight, // float, digital channels bit height (at 1) in pixels
@ -164,6 +163,7 @@ struct ImPlotStyle {
ImPlotMarker Marker; // = ImPlotMarker_None, marker specification
float MarkerSize; // = 4, marker size in pixels (roughly the marker's "radius")
float MarkerWeight; // = 1, outline weight of markers in pixels
float FillAlpha; // = 1, alpha modifier applied to plot fills
float ErrorBarSize; // = 5, error bar whisker width in pixels
float ErrorBarWeight; // = 1.5, error bar whisker weight in pixels
float DigitalBitHeight; // = 8, digital channels bit height (at y = 1.0f) in pixels
@ -218,6 +218,12 @@ void PlotScatter(const char* label_id, const ImVec2* data, int count, int offset
void PlotScatter(const char* label_id, const ImPlotPoint* data, int count, int offset = 0);
void PlotScatter(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.
void PlotShaded(const char* label_id, const float* xs, const float* ys1, const float* ys2, int count, int offset = 0, int stride = sizeof(float));
void PlotShaded(const char* label_id, const double* xs, const double* ys1, const double* ys2, int count, int offset = 0, int stride = sizeof(double));
void PlotShaded(const char* label_id, const float* xs, const float* ys, int count, float y_ref = 0, int offset = 0, int stride = sizeof(float));
void PlotShaded(const char* label_id, const double* xs, const double* ys, int count, double y_ref = 0, int offset = 0, int stride = sizeof(double));
// Plots a vertical bar graph.
void PlotBars(const char* label_id, const float* values, int count, float width = 0.67f, float shift = 0, int offset = 0, int stride = sizeof(float));
void PlotBars(const char* label_id, const double* values, int count, double width = 0.67f, double shift = 0, int offset = 0, int stride = sizeof(double));
@ -283,6 +289,9 @@ ImPlotLimits GetPlotQuery(int y_axis = -1);
// Provides access to plot style structure for permanant modifications to colors, sizes, etc.
ImPlotStyle& GetStyle();
// Special color used to indicate that a style color should be deduced automatically from defaults or colormaps.
#define IMPLOT_COL_AUTO ImVec4(0,0,0,-1)
// Temporarily modify a plot color. Don't forget to call PopStyleColor!
void PushStyleColor(ImPlotCol idx, ImU32 col);
// Temporarily modify a plot color. Don't forget to call PopStyleColor!
@ -301,7 +310,7 @@ void PopStyleVar(int count = 1);
void SetColormap(ImPlotColormap colormap, int samples = 0);
// Sets a custom colormap.
void SetColormap(const ImVec4* colors, int num_colors);
// Returns the size of the current colormap
// Returns the size of the current colormap.
int GetColormapSize();
// Returns a color from the Color map given an index >= 0 (modulo will be performed)
ImVec4 GetColormapColor(int index);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ImPlot v0.3 WIP
// ImPlot v0.4 WIP
#include "implot.h"
#include <math.h>
@ -35,7 +35,7 @@
/// NB: You don't ever need to typdef of define values for ImPlot. This
/// is only being done here for the sake of demoing both precision types.
// #define IMPLOT_DEMO_USE_DOUBLE
#define IMPLOT_DEMO_USE_DOUBLE
#ifdef IMPLOT_DEMO_USE_DOUBLE
typedef double t_float;
typedef ImPlotPoint t_float2;
@ -137,7 +137,7 @@ void ShowDemoWindow(bool* p_open) {
ImGui::EndMenuBar();
}
//-------------------------------------------------------------------------
ImGui::Text("ImPlot says hello. (0.3 WIP)");
ImGui::Text("ImPlot says hello. (0.4 WIP)");
if (ImGui::CollapsingHeader("Help")) {
ImGui::Text("USER GUIDE:");
ImGui::BulletText("Left click and drag within the plot area to pan X and Y axes.");
@ -171,7 +171,6 @@ void ShowDemoWindow(bool* p_open) {
#else
ImGui::BulletText("The demo data precision is: float");
#endif
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Line Plots")) {
@ -198,7 +197,7 @@ void ShowDemoWindow(bool* p_open) {
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Filled Plots")) {
if (ImGui::CollapsingHeader("Filled Line Plots")) {
static t_float xs1[101], ys1[101], ys2[101], ys3[101];
srand(0);
for (int i = 0; i < 101; ++i) {
@ -207,20 +206,51 @@ void ShowDemoWindow(bool* p_open) {
ys2[i] = RandomRange(275,350);
ys3[i] = RandomRange(150,225);
}
static bool show_lines = true;
static bool show_fills = true;
static float fill_ref = 0;
ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine();
ImGui::Checkbox("Fills",&show_fills);
ImGui::DragFloat("Reference",&fill_ref, 1, -100, 500);
ImPlot::SetNextPlotLimits(0,100,0,500);
if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) {
ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(1,1,0,1));
ImPlot::PushStyleColor(ImPlotCol_Fill, ImVec4(1,1,0,0.25f));
if (show_fills) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, fill_ref);
ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, fill_ref);
ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, fill_ref);
ImPlot::PopStyleVar();
}
if (show_lines) {
ImPlot::PlotLine("Stock 1", xs1, ys1, 101);
ImPlot::PopStyleColor(2);
ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(1,0,1,1));
ImPlot::PushStyleColor(ImPlotCol_Fill, ImVec4(1,0,1,0.25f));
ImPlot::PlotLine("Stock 2", xs1, ys2, 101);
ImPlot::PopStyleColor(2);
ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(1,0,0,1));
ImPlot::PushStyleColor(ImPlotCol_Fill, ImVec4(1,0,0,0.25f));
ImPlot::PlotLine("Stock 3", xs1, ys3, 101);
ImPlot::PopStyleColor(2);
}
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Shaded Plots")) {
static t_float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001];
srand(0);
for (int i = 0; i < 1001; ++i) {
xs[i] = i * 0.001f;
ys[i] = 0.25f + 0.25f * Sin(25 * xs[i]) * Sin(5 * xs[i]) + RandomRange(-0.01f, 0.01f);
ys1[i] = ys[i] + RandomRange(0.1f, 0.12f);
ys2[i] = ys[i] - RandomRange(0.1f, 0.12f);
ys3[i] = 0.75f + 0.2f * Sin(25 * xs[i]);
ys4[i] = 0.75f + 0.1f * Cos(25 * xs[i]);
}
static float alpha = 0.25f;
ImGui::DragFloat("Alpha",&alpha,0.01f,0,1);
if (ImPlot::BeginPlot("Shaded Plots")) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);
ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001);
ImPlot::PlotLine("Overlapping",xs,ys3,1001);
ImPlot::PlotLine("Overlapping",xs,ys4,1001);
ImPlot::PopStyleVar();
ImPlot::EndPlot();
}
}
@ -241,11 +271,9 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 6);
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Square);
ImPlot::PushStyleColor(ImPlotCol_MarkerFill, ImVec4(1,0,0,0.25f));
ImPlot::PushStyleColor(ImPlotCol_MarkerOutline, ImVec4(0,0,0,0));
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
ImPlot::PopStyleColor(2);
ImPlot::PopStyleVar(2);
ImPlot::PopStyleVar(3);
ImPlot::EndPlot();
}
}
@ -277,7 +305,6 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PlotBars("Final Exam", final, 10, 0.2f, 0);
ImPlot::PlotBars("Course Grade", grade, 10, 0.2f, 0.2f);
}
ImPlot::SetColormap(ImPlotColormap_Default);
ImPlot::EndPlot();
}
}
@ -300,7 +327,7 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PushStyleVar(ImPlotStyleVar_ErrorBarSize, size);
ImPlot::PushStyleVar(ImPlotStyleVar_ErrorBarWeight, weight);
ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f);
// error bars should have the same label ID as the associated plot
// error bars can be grouped with the associated item by using the same label ID
ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3);
@ -415,9 +442,9 @@ void ShowDemoWindow(bool* p_open) {
static int rt_axis = ImPlotAxisFlags_Default & ~ImPlotAxisFlags_TickLabels;
if (ImPlot::BeginPlot("##Scrolling", NULL, NULL, ImVec2(-1,150), ImPlotFlags_Default, rt_axis, rt_axis | ImPlotAxisFlags_LockMin)) {
ImPlot::PlotLine("Data 1", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), sdata1.Offset, 2 * sizeof(t_float));
ImPlot::PushStyleColor(ImPlotCol_Fill, ImVec4(1,0,0,0.25f));
ImPlot::PlotLine("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2 * sizeof(t_float));
ImPlot::PopStyleColor();
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotShaded("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), 0, sdata2.Offset, 2 * sizeof(t_float));
ImPlot::PopStyleVar();
ImPlot::EndPlot();
}
ImPlot::SetNextPlotLimitsX(0, history, ImGuiCond_Always);
@ -712,6 +739,7 @@ void ShowDemoWindow(bool* p_open) {
}
ImGui::EndGroup();
ImGui::SameLine();
srand((unsigned int)ImGui::GetTime()*10000000);
static t_float t = 0;
if (!paused) {
t += ImGui::GetIO().DeltaTime;