1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-23 02:38:53 -05:00

Merge branch 'master' into shaded

This commit is contained in:
Evan Pezent 2020-06-12 16:15:19 -05:00
commit 218e7bc5f1
4 changed files with 136 additions and 29 deletions

View File

@ -12,12 +12,12 @@ ImPlot is an immediate mode plotting widget for [Dear ImGui](https://github.com/
## Features
- multiple plot types:
- line
- scatter
- vertical/horizontal bars
- error bars
- line plots
- scatter plots
- vertical/horizontal bars graphs
- vertical/horizontal error bars
- pie charts
- heatmaps
- heatmap charts
- and more likely to come
- mix/match multiple plot items on a single plot
- configurable axes ranges and scaling (linear/log)

View File

@ -655,6 +655,11 @@ ImPlotItem* GetLegendItem(int i) {
return gp.CurrentPlot->Items.GetByIndex(gp.LegendIndices[i]);
}
ImPlotItem* GetLegendItem(const char* label_id) {
ImGuiID id = ImGui::GetID(label_id);
return gp.CurrentPlot->Items.GetByKey(id);
}
const char* GetLegendLabel(int i) {
ImPlotItem* item = gp.CurrentPlot->Items.GetByIndex(gp.LegendIndices[i]);
IM_ASSERT(item->NameOffset != -1 && item->NameOffset < gp.LegendLabels.Buf.Size);
@ -2371,11 +2376,6 @@ inline int PosMod(int l, int r) {
return (l % r + r) % r;
}
// template <typename T>
// inline T StrideIndex(const T* data, int idx, int stride) {
// return *(const T*)(const void*)((const unsigned char*)data + (size_t)idx * stride);
// }
template <typename T>
inline T OffsetAndStride(const T* data, int idx, int count, int offset, int stride) {
idx = PosMod(offset + idx, count);
@ -2934,6 +2934,77 @@ void PlotErrorBars(const char* label_id, const double* xs, const double* ys, con
PlotErrorBarsEx(label_id, getter);
}
//-----------------------------------------------------------------------------
// PLOT ERROR BARS H
//-----------------------------------------------------------------------------
template <typename Getter>
void PlotErrorBarsHEx(const char* label_id, Getter getter) {
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotErrorBarsH() needs to be called between BeginPlot() and EndPlot()!");
ImGuiID id = ImGui::GetID(label_id);
ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id);
if (item != NULL && item->Show == false)
return;
ImDrawList& DrawList = *ImGui::GetWindowDrawList();
PushPlotClipRect();
const ImU32 col = gp.Style.Colors[ImPlotCol_ErrorBar].w == -1 ? ImGui::GetColorU32(ImGuiCol_Text) : ImGui::GetColorU32(gp.Style.Colors[ImPlotCol_ErrorBar]);
const bool rend_whisker = gp.Style.ErrorBarSize > 0;
const float half_whisker = gp.Style.ErrorBarSize * 0.5f;
// find data extents
if (gp.FitThisFrame) {
for (int i = 0; i < getter.Count; ++i) {
ImPlotPointError e = getter(i);
FitPoint(ImPlotPoint(e.x - e.neg, e.y));
FitPoint(ImPlotPoint(e.x + e.pos, e.y));
}
}
for (int i = 0; i < getter.Count; ++i) {
ImPlotPointError e = getter(i);
ImVec2 p1 = PlotToPixels(e.x - e.neg, e.y);
ImVec2 p2 = PlotToPixels(e.x + e.pos, e.y);
DrawList.AddLine(p1, p2, col, gp.Style.ErrorBarWeight);
if (rend_whisker) {
DrawList.AddLine(p1 - ImVec2(0, half_whisker), p1 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight);
DrawList.AddLine(p2 - ImVec2(0, half_whisker), p2 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight);
}
}
PopPlotClipRect();
}
//-----------------------------------------------------------------------------
// float
void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset, int stride) {
GetterError<float> getter(xs, ys, err, err, count, offset, stride);
PlotErrorBarsHEx(label_id, getter);
}
void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset, int stride) {
GetterError<float> getter(xs, ys, neg, pos, count, offset, stride);
PlotErrorBarsHEx(label_id, getter);
}
//-----------------------------------------------------------------------------
// double
void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset, int stride) {
GetterError<double> getter(xs, ys, err, err, count, offset, stride);
PlotErrorBarsHEx(label_id, getter);
}
void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset, int stride) {
GetterError<double> getter(xs, ys, neg, pos, count, offset, stride);
PlotErrorBarsHEx(label_id, getter);
}
//-----------------------------------------------------------------------------
// PLOT PIE CHART
//-----------------------------------------------------------------------------
@ -2988,14 +3059,17 @@ void PlotPieChartEx(const char** label_ids, const T* values, int count, T x, T y
a1 = angle0 * 2 * IM_PI / 360.0f;
char buffer[32];
for (int i = 0; i < count; ++i) {
ImPlotItem* item = GetLegendItem(label_ids[i]);
T percent = normalize ? values[i] / sum : values[i];
a1 = a0 + 2 * IM_PI * percent;
sprintf(buffer, fmt, values[i]);
ImVec2 size = ImGui::CalcTextSize(buffer);
T angle = a0 + (a1 - a0) * 0.5f;
ImVec2 pos = PlotToPixels(center.x + 0.5f * radius * cos(angle), center.y + 0.5f * radius * sin(angle));
DrawList.AddText(pos - size * 0.5f + ImVec2(1,1), IM_COL32(0,0,0,255), buffer);
DrawList.AddText(pos - size * 0.5f, IM_COL32(255,255,255,255), buffer);
if (item->Show) {
sprintf(buffer, fmt, values[i]);
ImVec2 size = ImGui::CalcTextSize(buffer);
T angle = a0 + (a1 - a0) * 0.5f;
ImVec2 pos = PlotToPixels(center.x + 0.5f * radius * cos(angle), center.y + 0.5f * radius * sin(angle));
DrawList.AddText(pos - size * 0.5f + ImVec2(1,1), IM_COL32(0,0,0,255), buffer);
DrawList.AddText(pos - size * 0.5f, IM_COL32(255,255,255,255), buffer);
}
a0 = a1;
}
}

View File

@ -70,7 +70,7 @@ enum ImPlotAxisFlags_ {
// Plot styling colors.
enum ImPlotCol_ {
ImPlotCol_Line, // plot line/outline color (defaults to a rotating color set)
ImPlotCol_Line, // plot line/outline color (defaults to next unused color in current colormap)
ImPlotCol_Fill, // plot fill color for bars (defaults to the current line color)
ImPlotCol_MarkerOutline, // marker outline color (defaults to the current line color)
ImPlotCol_MarkerFill, // marker fill color (defaults to the current line color)
@ -232,12 +232,18 @@ void PlotBarsH(const char* label_id, const float* xs, const float* ys, int count
void PlotBarsH(const char* label_id, const double* xs, const double* ys, int count, double height, int offset = 0, int stride = sizeof(double));
void PlotBarsH(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, double height, int offset = 0);
// Plots vertical error bar.
// Plots vertical error bar. The label_id should match the label_id of the associated line or bar plot.
void PlotErrorBars(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset = 0, int stride = sizeof(float));
void PlotErrorBars(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset = 0, int stride = sizeof(double));
void PlotErrorBars(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset = 0, int stride = sizeof(float));
void PlotErrorBars(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset = 0, int stride = sizeof(double));
// Plots horizontal error bars. The label_id should match the label_id of the associated line or bar plot.
void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset = 0, int stride = sizeof(float));
void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset = 0, int stride = sizeof(double));
void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset = 0, int stride = sizeof(float));
void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset = 0, int stride = sizeof(double));
// Plots a pie chart. If the sum of values > 1 or normalize is true, each value will be normalized. Center and radius are in plot coordinates.
void PlotPieChart(const char** label_ids, const float* values, int count, float x, float y, float radius, bool normalize = false, const char* label_fmt = "%.1f", float angle0 = 90);
void PlotPieChart(const char** label_ids, const double* values, int count, double x, double y, double radius, bool normalize = false, const char* label_fmt = "%.1f", double angle0 = 90);

View File

@ -114,7 +114,7 @@ struct BenchmarkItem {
}
Col = ImVec4((float)RandomRange(0,1),(float)RandomRange(0,1),(float)RandomRange(0,1),1);
}
~BenchmarkItem() { delete Data; }
~BenchmarkItem() { delete[] Data; }
t_float2* Data;
ImVec4 Col;
};
@ -164,7 +164,7 @@ void ShowDemoWindow(bool* p_open) {
ImGui::Indent();
ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased.");
ImGui::BulletText("AA for demo plots can be enabled from the plot's context menu.");
ImGui::BulletText("If allowable, you are better off using hardware AA (e.g. MSAA).");
ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA).");
ImGui::Unindent();
#ifdef IMPLOT_DEMO_USE_DOUBLE
ImGui::BulletText("The demo data precision is: double");
@ -185,11 +185,15 @@ void ShowDemoWindow(bool* p_open) {
xs2[i] = i * 0.1f;
ys2[i] = xs2[i] * xs2[i];
}
static float weight = ImPlot::GetStyle().LineWeight;
ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).");
ImGui::DragFloat("Line Weight", &weight, 0.05f, 1.0f, 5.0f, "%.2f px");
if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) {
ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, weight);
ImPlot::PlotLine("0.5 + 0.5*sin(50*x)", xs1, ys1, 1001);
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle);
ImPlot::PlotLine("x^2", xs2, ys2, 11);
ImPlot::PopStyleVar();
ImPlot::PopStyleVar(2);
ImPlot::EndPlot();
}
}
@ -280,21 +284,37 @@ void ShowDemoWindow(bool* p_open) {
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Error Bars")) {
t_float xs[5] = {1,2,3,4,5};
t_float lin[5] = {8,8,9,7,8};
t_float bar[5] = {1,2,5,3,4};
t_float lin1[5] = {8,8,9,7,8};
t_float lin2[5] = {6,7,6,9,6};
t_float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
t_float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
t_float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f};
t_float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f};
static float size = ImPlot::GetStyle().ErrorBarSize;
static float weight = ImPlot::GetStyle().ErrorBarWeight;
ImGui::DragFloat("Error Bar Size", &size, 0.1f, 0, 10,"%.2f px");
ImGui::DragFloat("Error Bar Weight",&weight,0.01f,1,3,"%.2f px");
ImPlot::SetNextPlotLimits(0, 6, 0, 10);
if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) {
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
ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3);
ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImVec4(1,0,0,1));
ImPlot::PlotErrorBars("Line", xs, lin, err1, err2, 5);
ImPlot::PlotLine("Line", xs, lin, 5);
ImPlot::PopStyleVar(2);
ImPlot::PopStyleColor();
ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(1));
ImPlot::PlotErrorBars("Line1", xs, lin1, err1, err2, 5);
ImPlot::PlotLine("Line1", xs, lin1, 5);
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Square);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3);
ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2));
ImPlot::PlotErrorBars("Line2", xs, lin2, err2, 5);
ImPlot::PlotErrorBarsH("Line2", xs, lin2, err3, err4, 5);
ImPlot::PlotLine("Line2", xs, lin2, 5);
ImPlot::PopStyleVar(6);
ImPlot::PopStyleColor(2);
ImPlot::EndPlot();
}
}
@ -408,15 +428,21 @@ void ShowDemoWindow(bool* p_open) {
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Markers and Text")) {
if (ImGui::CollapsingHeader("Colormaps, Markers, and Text")) {
static ImPlotColormap map = ImPlotColormap_Default;
if (ImGui::Button("Change Colormap##2"))
map = (map + 1) % ImPlotColormap_COUNT;
ImGui::SameLine();
ImGui::LabelText("##Colormap Index", "%s", cmap_names[map]);
static float mk_size = ImPlot::GetStyle().MarkerSize;
static float mk_weight = ImPlot::GetStyle().MarkerWeight;
ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px");
ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px");
ImGui::PushID(map); // NB: This is merely a workaround so that the demo can cycle color maps. You wouldn't need to do this in your own code!
ImPlot::SetNextPlotLimits(0, 10, 0, 12);
if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), 0, 0, 0)) {
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, mk_size);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerWeight, mk_weight);
ImPlot::SetColormap(map);
t_float xs[2] = {1,4};
t_float ys[2] = {10,11};
@ -480,7 +506,7 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PushStyleColor(ImPlotCol_MarkerFill, ImVec4(1,1,1,1));
ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(0,0,0,1));
ImPlot::PlotLine("Circle|Cross", xs, ys, 2);
ImPlot::PopStyleVar(4);
ImPlot::PopStyleVar(6);
ImPlot::PopStyleColor(3);
ImPlot::PlotText("Filled Markers", 1.5, 11.75);
@ -946,6 +972,7 @@ void ShowDemoWindow(bool* p_open) {
static BenchmarkItem items[n_items];
ImGui::BulletText("Make sure VSync is disabled.");
ImGui::BulletText("%d lines with %d points each @ %.3f FPS.",n_items,1000,ImGui::GetIO().Framerate);
ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
if (ImPlot::BeginPlot("##Bench",NULL,NULL,ImVec2(-1,0),ImPlotFlags_Default | ImPlotFlags_NoChild)) {
char buff[16];
for (int i = 0; i < 100; ++i) {