mirror of
https://github.com/gwm17/implot.git
synced 2024-11-22 18:28:53 -05:00
commit v0.1 source code
This commit is contained in:
parent
6ed8d67a0d
commit
b4e4f1a53c
1751
implot.cpp
Normal file
1751
implot.cpp
Normal file
File diff suppressed because it is too large
Load Diff
207
implot.h
Normal file
207
implot.h
Normal file
|
@ -0,0 +1,207 @@
|
|||
// MIT License
|
||||
|
||||
// Copyright (c) 2020 Evan Pezent
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// ImPlot v0.1 WIP
|
||||
|
||||
#pragma once
|
||||
#include <imgui.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Basic types and flags
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef int ImPlotFlags;
|
||||
typedef int ImAxisFlags;
|
||||
typedef int ImPlotCol;
|
||||
typedef int ImPlotStyleVar;
|
||||
typedef int ImMarker;
|
||||
|
||||
// Options for plots
|
||||
enum ImPlotFlags_ {
|
||||
ImPlotFlags_MousePos = 1 << 0, // the mouse position, in plot coordinates, will be displayed in the bottom-right
|
||||
ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left
|
||||
ImPlotFlags_Selection = 1 << 2, // the user will be able to box-select with right-mouse
|
||||
ImPlotFlags_ContextMenu = 1 << 3, // the user will be able to open a context menu with double-right click
|
||||
ImPlotFlags_Crosshairs = 1 << 4, // the default mouse cursor will be replaced with a crosshair when hovered
|
||||
ImPlotFlags_CullData = 1 << 5, // plot data outside the plot area will be culled from rendering
|
||||
ImPlotFlags_AntiAliased = 1 << 6, // lines and fills will be anti-aliased (not recommended)
|
||||
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData
|
||||
};
|
||||
|
||||
// Options for plot axes (X and Y)
|
||||
enum ImAxisFlags_ {
|
||||
ImAxisFlags_GridLines = 1 << 0, // grid lines will be displayed
|
||||
ImAxisFlags_TickMarks = 1 << 1, // tick marks will be displayed for each grid line
|
||||
ImAxisFlags_TickLabels = 1 << 2, // text labels will be displayed for each grid line
|
||||
ImAxisFlags_Invert = 1 << 3, // the axis will be inverted
|
||||
ImAxisFlags_LockMin = 1 << 4, // the axis minimum value will be locked when panning/zooming
|
||||
ImAxisFlags_LockMax = 1 << 5, // the axis maximum value will be locked when panning/zooming
|
||||
ImAxisFlags_Adaptive = 1 << 6, // grid divisions will adapt to the current pixel size the axis
|
||||
ImAxisFlags_LogScale = 1 << 7, // a logartithmic (base 10) axis scale will be used
|
||||
ImAxisFlags_Scientific = 1 << 8, // scientific notation will be used for tick labels if displayed (WIP, not very good yet)
|
||||
ImAxisFlags_Default = ImAxisFlags_GridLines | ImAxisFlags_TickMarks | ImAxisFlags_TickLabels | ImAxisFlags_Adaptive
|
||||
};
|
||||
|
||||
// Plot styling colors
|
||||
enum ImPlotCol_ {
|
||||
ImPlotCol_Line, // plot line/outline color (defaults to a rotating color set)
|
||||
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)
|
||||
ImPlotCol_ErrorBar, // error bar color (defaults to ImGuiCol_Text)
|
||||
ImPlotCol_FrameBg, // plot frame background color (defaults to ImGuiCol_FrameBg)
|
||||
ImPlotCol_PlotBg, // plot area background color (defaults to ImGuiCol_WindowBg)
|
||||
ImPlotCol_PlotBorder, // plot area border color (defaults to ImGuiCol_Text)
|
||||
ImPlotCol_XAxis, // x-axis grid/label color (defaults to ImGuiCol_Text)
|
||||
ImPlotCol_YAxis, // x-axis grid/label color (defaults to ImGuiCol_Text)
|
||||
ImPlotCol_Selection, // box-selection color (defaults to yellow)
|
||||
ImPlotCol_COUNT
|
||||
};
|
||||
|
||||
enum ImPlotStyleVar_ {
|
||||
ImPlotStyleVar_LineWeight, // float, line weight in pixels
|
||||
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_ErrorBarSize, // float, error bar whisker width in pixels
|
||||
ImPlotStyleVar_ErrorBarWeight, // float, error bar whisker weight in pixels
|
||||
ImPlotStyleVar_COUNT
|
||||
};
|
||||
|
||||
// Marker specification
|
||||
enum ImMarker_ {
|
||||
ImMarker_None = 1 << 0, // no marker
|
||||
ImMarker_Circle = 1 << 1, // a circle marker will be rendered at each point
|
||||
ImMarker_Square = 1 << 2, // a square maker will be rendered at each point
|
||||
ImMarker_Diamond = 1 << 3, // a diamond marker will be rendered at each point
|
||||
ImMarker_Up = 1 << 4, // an upward-pointing triangle marker will up rendered at each point
|
||||
ImMarker_Down = 1 << 5, // an downward-pointing triangle marker will up rendered at each point
|
||||
ImMarker_Left = 1 << 6, // an leftward-pointing triangle marker will up rendered at each point
|
||||
ImMarker_Right = 1 << 7, // an rightward-pointing triangle marker will up rendered at each point
|
||||
ImMarker_Cross = 1 << 8, // a cross marker will be rendered at each point (not filled)
|
||||
ImMarker_Plus = 1 << 9, // a plus marker will be rendered at each point (not filled)
|
||||
ImMarker_Asterisk = 1 << 10, // a asterisk marker will be rendered at each point (not filled)
|
||||
};
|
||||
|
||||
// Plot style structure
|
||||
struct ImPlotStyle {
|
||||
float LineWeight; // = 1, line weight in pixels
|
||||
ImMarker Marker; // = ImMarker_None, marker specification
|
||||
float MarkerSize; // = 5, marker size in pixels (roughly the marker's "radius")
|
||||
float MarkerWeight; // = 1, outline weight of markers in pixels
|
||||
float ErrorBarSize; // = 5, error bar whisker width in pixels
|
||||
float ErrorBarWeight; // = 1.5, error bar whisker weight in pixels
|
||||
ImVec4 Colors[ImPlotCol_COUNT]; // array of plot specific colors
|
||||
ImPlotStyle();
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Plot API
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
namespace ImGui {
|
||||
|
||||
// Starts a 2D plotting context. If this function returns true, EndPlot() must
|
||||
// be called, e.g. "if (BeginPlot(...)) { ... EndPlot(); }"". #title_id must
|
||||
// be unique. If you need to avoid ID collisions or don't want to display a
|
||||
// title in the plot, use double hashes (e.g. "MyPlot##Hidden"). If #x_label
|
||||
// and/or #y_label are provided, axes labels will be displayed. Flags are only
|
||||
// set ONCE during the first call to BeginPlot.
|
||||
bool BeginPlot(const char* title_id,
|
||||
const char* x_label = NULL,
|
||||
const char* y_label = NULL,
|
||||
const ImVec2& size = ImVec2(-1,-1),
|
||||
ImPlotFlags flags = ImPlotFlags_Default,
|
||||
ImAxisFlags x_flags = ImAxisFlags_Default,
|
||||
ImAxisFlags y_flags = ImAxisFlags_Default);
|
||||
// Only call EndPlot() if BeginPlot() returns true! Typically called at the end
|
||||
// of an if statement conditioned on BeginPlot().
|
||||
void EndPlot();
|
||||
|
||||
/// Set the axes ranges of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes will be locked.
|
||||
void SetNextPlotRange(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
|
||||
/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked.
|
||||
void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once);
|
||||
/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked.
|
||||
void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
|
||||
/// Returns true if the plot area in the current or most recent call to BeginPlot() is hovered
|
||||
bool IsPlotHovered();
|
||||
/// Returns the mouse position in x,y coordinates of the current or most recent plot.
|
||||
ImVec2 GetPlotMousePos();
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Plot Items
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Plots a standard 2D line and/or scatter plot
|
||||
void Plot(const char* label_id, const float* values, int count, int offset = 0, int stride = sizeof(float));
|
||||
void Plot(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float));
|
||||
void Plot(const char* label_id, ImVec2 (*getter)(void* data, int idx), void* data, int count, int offset = 0);
|
||||
// Plots vertical bars
|
||||
void PlotBar(const char* label_id, const float* values, int count, float width = 0.67f, float shift = 0, int offset = 0, int stride = sizeof(float));
|
||||
void PlotBar(const char* label_id, const float* xs, const float* ys, int count, float width, int offset = 0, int stride = sizeof(float));
|
||||
void PlotBar(const char* label_id, ImVec2 (*getter)(void* data, int idx), void* data, int count, float width, int offset = 0);
|
||||
// Plots horizontal bars
|
||||
void PlotBarH(const char* label_id, const float* values, int count, float height = 0.67f, float shift = 0, int offset = 0, int stride = sizeof(float));
|
||||
void PlotBarH(const char* label_id, const float* xs, const float* ys, int count, float height, int offset = 0, int stride = sizeof(float));
|
||||
void PlotBarH(const char* label_id, ImVec2 (*getter)(void* data, int idx), void* data, int count, float height, int offset = 0);
|
||||
// Plots vertical error bars
|
||||
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 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, ImVec4 (*getter)(void* data, int idx), void* data, int count, int offset = 0);
|
||||
// Plots a text label at point x,y
|
||||
void PlotLabel(const char* text, float x, float y, const ImVec2& pixel_offset = ImVec2(0,0));
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Plot Styling
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Provides access to plot style structure for permanant modifications to colors, sizes, etc.
|
||||
ImPlotStyle& GetPlotStyle();
|
||||
|
||||
// Sets the color palette to be used for plot items
|
||||
void SetPlotPalette(const ImVec4* colors, int num_colors);
|
||||
// Restores the default ImPlot color map
|
||||
void RestorePlotPalette();
|
||||
|
||||
// Temporarily modify a plot color.
|
||||
void PushPlotColor(ImPlotCol idx, ImU32 col);
|
||||
// Temporarily modify a plot color.
|
||||
void PushPlotColor(ImPlotCol idx, const ImVec4& col);
|
||||
// Undo temporary color modification.
|
||||
void PopPlotColor(int count = 1);
|
||||
|
||||
// Temporarily modify a style variable of float type
|
||||
void PushPlotStyleVar(ImPlotStyleVar idx, float val);
|
||||
// Temporarily modify a style variable of int type
|
||||
void PushPlotStyleVar(ImPlotStyleVar idx, int val);
|
||||
// Undo temporary style modification.
|
||||
void PopPlotStyleVar(int count = 1);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Demo
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Shows the ImPlot demo. Add implot_demo.cpp to your sources!
|
||||
void ShowImPlotDemoWindow(bool* p_open = NULL);
|
||||
|
||||
} // namespace ImGui
|
406
implot_demo.cpp
Normal file
406
implot_demo.cpp
Normal file
|
@ -0,0 +1,406 @@
|
|||
// MIT License
|
||||
|
||||
// Copyright (c) 2020 Evan Pezent
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// ImPlot v0.1 WIP
|
||||
|
||||
#include <implot.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
struct ScrollingData {
|
||||
int MaxSize = 1000;
|
||||
int Offset = 0;
|
||||
ImVector<ImVec2> Data;
|
||||
ScrollingData() { Data.reserve(MaxSize); }
|
||||
void AddPoint(float x, float y) {
|
||||
if (Data.size() < MaxSize)
|
||||
Data.push_back(ImVec2(x,y));
|
||||
else {
|
||||
Data[Offset] = ImVec2(x,y);
|
||||
Offset = (Offset + 1) % MaxSize;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct RollingData {
|
||||
float Span = 10.0f;
|
||||
ImVector<ImVec2> Data;
|
||||
RollingData() { Data.reserve(1000); }
|
||||
void AddPoint(float x, float y) {
|
||||
float xmod = ImFmod(x, Span);
|
||||
if (!Data.empty() && xmod < Data.back().x)
|
||||
Data.shrink(0);
|
||||
Data.push_back(ImVec2(xmod, y));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace ImGui {
|
||||
|
||||
void ShowImPlotDemoWindow(bool* p_open) {
|
||||
|
||||
ImVec2 main_viewport_pos = ImGui::GetMainViewport()->Pos;
|
||||
ImGui::SetNextWindowPos(ImVec2(main_viewport_pos.x + 650, main_viewport_pos.y + 20), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver);
|
||||
ImGui::Begin("ImPlot Demo", p_open);
|
||||
ImGui::Text("ImPlot says hello. (0.1 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.");
|
||||
ImGui::BulletText("Left click and drag on an axis to pan an individual axis.");
|
||||
ImGui::BulletText("Scroll in the plot area to zoom both X any Y axes");
|
||||
ImGui::BulletText("Scroll on an axis to zoom an individual axis.");
|
||||
ImGui::BulletText("Right click and drag to box select data.");
|
||||
ImGui::BulletText("Double left click to fit all visible data.");
|
||||
ImGui::BulletText("Double right click to open the plot context menu.");
|
||||
ImGui::BulletText("Click legend label icons to show/hide plot items.");
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Line Plots")) {
|
||||
static float xs1[1001], ys1[1001];
|
||||
for (int i = 0; i < 1001; ++i) {
|
||||
xs1[i] = i * 0.001f;
|
||||
ys1[i] = 0.5f + 0.5f * sin(50 * xs1[i]);
|
||||
}
|
||||
static float xs2[11], ys2[11];
|
||||
for (int i = 0; i < 11; ++i) {
|
||||
xs2[i] = i * 0.1f;
|
||||
ys2[i] = xs2[i] * xs2[i];
|
||||
}
|
||||
if (ImGui::BeginPlot("Line Plot", "x", "f(x)", {-1,300})) {
|
||||
ImGui::Plot("sin(50*x)", xs1, ys1, 1001);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle);
|
||||
ImGui::Plot("x^2", xs2, ys2, 11);
|
||||
ImGui::PopPlotStyleVar();
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Scatter Plots")) {
|
||||
srand(0);
|
||||
static float xs1[100], ys1[100];
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
xs1[i] = i * 0.01f;
|
||||
ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
|
||||
}
|
||||
static float xs2[50], ys2[50];
|
||||
for (int i = 0; i < 50; i++) {
|
||||
xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
|
||||
ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);
|
||||
}
|
||||
if (ImGui::BeginPlot("Scatter Plot", NULL, NULL, {-1,300})) {
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 0);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Cross);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_MarkerSize, 3);
|
||||
ImGui::Plot("Data 1", xs1, ys1, 100);
|
||||
ImGui::PopPlotStyleVar(2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle);
|
||||
ImGui::PushPlotColor(ImPlotCol_MarkerFill, ImVec4{1,0,0,0.25f});
|
||||
ImGui::Plot("Data 2", xs2, ys2, 50);
|
||||
ImGui::PopPlotColor();
|
||||
ImGui::PopPlotStyleVar(2);
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Bar Plots")) {
|
||||
static bool horz = false;
|
||||
ImGui::Checkbox("Horizontal",&horz);
|
||||
if (horz)
|
||||
ImGui::SetNextPlotRange(0, 110, -0.5f, 9.5f, ImGuiCond_Always);
|
||||
else
|
||||
ImGui::SetNextPlotRange(-0.5f, 9.5f, 0, 110, ImGuiCond_Always);
|
||||
if (ImGui::BeginPlot("Bar Plot", horz ? "Score": "Student", horz ? "Student" : "Score", {-1, 300})) {
|
||||
static float midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90};
|
||||
static float final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100};
|
||||
static float grade[10] = {80, 69, 52, 92, 72, 78, 75, 76, 89, 95};
|
||||
if (horz) {
|
||||
ImGui::PlotBarH("Midterm Exam", midtm, 10, 0.2f, -0.2f);
|
||||
ImGui::PlotBarH("Final Exam", final, 10, 0.2f, 0);
|
||||
ImGui::PlotBarH("Course Grade", grade, 10, 0.2f, 0.2f);
|
||||
}
|
||||
else {
|
||||
ImGui::PlotBar("Midterm Exam", midtm, 10, 0.2f, -0.2f);
|
||||
ImGui::PlotBar("Final Exam", final, 10, 0.2f, 0);
|
||||
ImGui::PlotBar("Course Grade", grade, 10, 0.2f, 0.2f);
|
||||
}
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Error Bars")) {
|
||||
float xs[5] = {1,2,3,4,5};
|
||||
float lin[5] = {8,8,9,7,8};
|
||||
float bar[5] = {1,2,5,3,4};
|
||||
float err1[5] = {0.2, 0.4, 0.2, 0.6, 0.4};
|
||||
float err2[5] = {0.4, 0.2, 0.4, 0.8, 0.6};
|
||||
ImGui::SetNextPlotRange(0, 6, 0, 10);
|
||||
if (ImGui::BeginPlot("##ErrorBars",NULL,NULL,ImVec2(-1,300))) {
|
||||
|
||||
ImGui::PlotBar("Bar", xs, bar, 5, 0.5f);
|
||||
ImGui::PlotErrorBars("Bar", xs, bar, err1, 5);
|
||||
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_MarkerSize, 3);
|
||||
ImGui::PushPlotColor(ImPlotCol_ErrorBar, ImVec4(1,0,0,1));
|
||||
ImGui::PlotErrorBars("Line", xs, lin, err1, err2, 5);
|
||||
ImGui::Plot("Line", xs, lin, 5);
|
||||
ImGui::PopPlotStyleVar(2);
|
||||
ImGui::PopPlotColor();
|
||||
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Realtime Plots")) {
|
||||
static bool paused = false;
|
||||
static ScrollingData sdata1, sdata2;
|
||||
static RollingData rdata1, rdata2;
|
||||
ImVec2 mouse = ImGui::GetMousePos();
|
||||
static float t = 0;
|
||||
if (!paused) {
|
||||
t += ImGui::GetIO().DeltaTime;
|
||||
sdata1.AddPoint(t, mouse.x * 0.0005f);
|
||||
rdata1.AddPoint(t, mouse.x * 0.0005f);
|
||||
sdata2.AddPoint(t, mouse.y * 0.0005f);
|
||||
rdata2.AddPoint(t, mouse.y * 0.0005f);
|
||||
}
|
||||
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
|
||||
static int rt_axis = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
|
||||
if (ImGui::BeginPlot("##Scrolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
|
||||
ImGui::Plot("Data 1", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), sdata1.Offset, 2 * sizeof(float));
|
||||
ImGui::Plot("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2 * sizeof(float));
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
ImGui::SetNextPlotRangeX(0, 10, ImGuiCond_Always);
|
||||
if (ImGui::BeginPlot("##Rolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
|
||||
ImGui::Plot("Data 1", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float));
|
||||
ImGui::Plot("Data 2", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float));
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Marker Styles")) {
|
||||
ImGui::SetNextPlotRange(0, 10, 0, 12);
|
||||
if (ImGui::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,300), 0, 0, 0)) {
|
||||
float xs[2] = {1,4};
|
||||
float ys[2] = {10,11};
|
||||
// filled
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle);
|
||||
ImGui::Plot("Circle##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Square); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Square##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Diamond); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Diamond##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Up); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Up##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Down); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Down##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Left); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Left##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Right); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Right##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Cross); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Cross##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Plus); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Plus##Fill", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Asterisk); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Asterisk##Fill", xs, ys, 2);
|
||||
ImGui::PopPlotStyleVar(10);
|
||||
|
||||
xs[0] = 6; xs[1] = 9;
|
||||
ys[0] = 10; ys[1] = 11;
|
||||
ImGui::PushPlotColor(ImPlotCol_MarkerFill, ImVec4(0,0,0,0));
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle);
|
||||
ImGui::Plot("Circle", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Square); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Square", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Diamond); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Diamond", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Up); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Up", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Down); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Down", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Left); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Left", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Right); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Right", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Cross); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Cross", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Plus); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Plus", xs, ys, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Asterisk); ys[0]--; ys[1]--;
|
||||
ImGui::Plot("Asterisk", xs, ys, 2);
|
||||
ImGui::PopPlotColor();
|
||||
ImGui::PopPlotStyleVar(10);
|
||||
|
||||
xs[0] = 5; xs[1] = 5;
|
||||
ys[0] = 1; ys[1] = 11;
|
||||
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_MarkerSize, 8);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_MarkerWeight, 2);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Circle | ImMarker_Cross);
|
||||
ImGui::PushPlotColor(ImPlotCol_MarkerOutline, ImVec4(0,0,0,1));
|
||||
ImGui::PushPlotColor(ImPlotCol_MarkerFill, ImVec4(1,1,1,1));
|
||||
ImGui::PushPlotColor(ImPlotCol_Line, ImVec4(0,0,0,1));
|
||||
ImGui::Plot("Circle|Cross", xs, ys, 2);
|
||||
ImGui::PopPlotStyleVar(4);
|
||||
ImGui::PopPlotColor(3);
|
||||
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Log Scale")) {
|
||||
ImGui::BulletText("Open the plot context menu (double right click) to scales.");
|
||||
static float xs[1001], ys1[1001], ys2[1001], ys3[1001];
|
||||
for (int i = 0; i < 1001; ++i) {
|
||||
xs[i] = (float)(i*0.1f);
|
||||
ys1[i] = sin(xs[i]) + 1;
|
||||
ys2[i] = log(xs[i]);
|
||||
ys3[i] = pow(10.0f, xs[i]);
|
||||
}
|
||||
ImGui::SetNextPlotRange(0.1f, 100, 0, 10);
|
||||
if (ImGui::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_Default | ImAxisFlags_LogScale )) {
|
||||
ImGui::Plot("f(x) = x", xs, xs, 1001);
|
||||
ImGui::Plot("f(x) = sin(x)+1", xs, ys1, 1001);
|
||||
ImGui::Plot("f(x) = log(x)", xs, ys2, 1001);
|
||||
ImGui::Plot("f(x) = 10^x", xs, ys3, 21);
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Querying")) {
|
||||
ImGui::BulletText("Click in the plot area to draw");
|
||||
static ImVector<ImVec2> data;
|
||||
if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, 0, 0)) {
|
||||
if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0))
|
||||
data.push_back(ImGui::GetPlotMousePos());
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Diamond);
|
||||
ImGui::Plot("Art", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(float));
|
||||
ImGui::PopPlotStyleVar();
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Drag and Drop")) {
|
||||
static bool paused = false;
|
||||
static bool init = true;
|
||||
static ScrollingData data[10];
|
||||
static bool show[10];
|
||||
if (init) {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
show[i] = false;
|
||||
data[i].AddPoint(0, 0.25f + 0.5f * (float)rand() / float(RAND_MAX));
|
||||
}
|
||||
init = false;
|
||||
}
|
||||
ImGui::BulletText("Drag data items from the left column onto the plot.");
|
||||
ImGui::BeginGroup();
|
||||
if (ImGui::Button("Clear", {100, 0})) {
|
||||
for (int i = 0; i < 10; ++i)
|
||||
show[i] = false;
|
||||
}
|
||||
if (ImGui::Button(paused ? "Resume" : "Pause", {100,0}))
|
||||
paused = !paused;
|
||||
ImGui::Separator();
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
char label[8];
|
||||
sprintf(label, "data_%d", i);
|
||||
ImGui::Selectable(label, false, 0, {100, 0});
|
||||
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
|
||||
ImGui::SetDragDropPayload("DND_PLOT", &i, sizeof(int));
|
||||
ImGui::TextUnformatted(label);
|
||||
ImGui::EndDragDropSource();
|
||||
}
|
||||
}
|
||||
ImGui::EndGroup();
|
||||
ImGui::SameLine();
|
||||
static float t = 0;
|
||||
if (!paused) {
|
||||
t += ImGui::GetIO().DeltaTime;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
data[i].AddPoint(t, data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX)));
|
||||
}
|
||||
}
|
||||
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
|
||||
if (ImGui::BeginPlot("##DND", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (show[i]) {
|
||||
char label[8];
|
||||
sprintf(label, "data_%d", i);
|
||||
ImGui::Plot(label, &data[i].Data[0].x, &data[i].Data[0].y, data[i].Data.size(), data[i].Offset, 2 * sizeof(float));
|
||||
}
|
||||
}
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
if (ImGui::BeginDragDropTarget()) {
|
||||
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_PLOT")) {
|
||||
int i = *(int*)payload->Data;
|
||||
show[i] = true;
|
||||
}
|
||||
ImGui::EndDragDropTarget();
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
if (ImGui::CollapsingHeader("Custom Styles")) {
|
||||
static ImVec4 my_palette[3] = {
|
||||
{0.000f, 0.980f, 0.604f, 1.0f},
|
||||
{0.996f, 0.278f, 0.380f, 1.0f},
|
||||
{(0.1176470593F), (0.5647059083F), (1.0F), (1.0F)},
|
||||
};
|
||||
ImGui::SetPlotPalette(my_palette, 3);
|
||||
ImGui::PushPlotColor(ImPlotCol_FrameBg, IM_COL32(32,51,77,255));
|
||||
ImGui::PushPlotColor(ImPlotCol_PlotBg, {0,0,0,0});
|
||||
ImGui::PushPlotColor(ImPlotCol_PlotBorder, {0,0,0,0});
|
||||
ImGui::PushPlotColor(ImPlotCol_XAxis, IM_COL32(192, 192, 192, 192));
|
||||
ImGui::PushPlotColor(ImPlotCol_YAxis, IM_COL32(192, 192, 192, 192));
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 2);
|
||||
ImGui::SetNextPlotRange(-0.5f, 9.5f, -0.5f, 9.5f);
|
||||
if (ImGui::BeginPlot("##Custom", NULL, NULL, {-1,300}, ImPlotFlags_Default & ~ImPlotFlags_Legend, 0)) {
|
||||
float lin[10] = {8,8,9,7,8,8,8,9,7,8};
|
||||
float bar[10] = {1,2,5,3,4,1,2,5,3,4};
|
||||
float dot[10] = {7,6,6,7,8,5,6,5,8,7};
|
||||
ImGui::PlotBar("Bar", bar, 10, 0.5f);
|
||||
ImGui::Plot("Line", lin, 10);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 0);
|
||||
ImGui::PushPlotStyleVar(ImPlotStyleVar_Marker, ImMarker_Square);
|
||||
ImGui::Plot("Dot", dot, 10);
|
||||
ImGui::PopPlotStyleVar(2);
|
||||
ImGui::EndPlot();
|
||||
}
|
||||
ImGui::PopPlotColor(5);
|
||||
ImGui::PopPlotStyleVar();
|
||||
ImGui::RestorePlotPalette();
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
ImGui::End();
|
||||
|
||||
}
|
||||
|
||||
} // namespace ImGui
|
Loading…
Reference in New Issue
Block a user