1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-22 18:28:53 -05:00

more experimentation with tooltip API

This commit is contained in:
epezent 2020-12-06 23:34:44 -06:00
parent fc815348c2
commit 961b684c3d
3 changed files with 44 additions and 47 deletions

View File

@ -2836,7 +2836,7 @@ bool DragPoint(const char* id, double* x, double* y, bool show_label, const ImVe
char buff2[32];
LabelAxisValue(gp.CurrentPlot->XAxis, gp.XTicks, *x, buff1, 32);
LabelAxisValue(gp.CurrentPlot->YAxis[yax], gp.YTicks[yax], *y, buff2, 32);
gp.Annotations.Append(label_pos, ImVec2(0.0001f,0.00001f), col32, CalcTextColor(color), true, "%s = %s,%s", id, buff1, buff2);
gp.Annotations.Append(label_pos, ImVec2(0.0001f,0.0001f), col32, CalcTextColor(color), true, "%s = %s,%s", id, buff1, buff2);
}
}
bool dragging = false;
@ -2854,14 +2854,6 @@ bool DragPoint(const char* id, double* x, double* y, bool show_label, const ImVe
// TOOLTIPS
//-----------------------------------------------------------------------------
template <typename T>
T GetClosest(T val1, T val2, T target) {
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
template <typename T>
int FindClosestIdx(const T* arr, int n, T target) {
if (target <= arr[0])
@ -2887,41 +2879,43 @@ int FindClosestIdx(const T* arr, int n, T target) {
return mid;
}
// bool BeginTooltip(const double* xs_mono, const double* ys, int* idx_out = NULL);
bool BeginTooltipX(const double* xs_mono, const double* ys, int count, int* idx_out) {
template <typename T>
int Tooltip(const T* xs, const T* ys, int count, bool clamp) {
ImPlotContext& gp = *GImPlot;
if (ImPlot::IsPlotHovered()) {
double mouse_x = ImPlot::GetPlotMousePos().x;
if (mouse_x < (double)xs_mono[0] || mouse_x > (double)xs_mono[count-1])
return false;
const int idx = FindClosestIdx(xs_mono, count, mouse_x);
if (mouse_x < (double)xs[0] || mouse_x > (double)xs[count-1])
return -1;
const int idx = FindClosestIdx(xs, count, (T)mouse_x);
const int yax = GetCurrentYAxis();
const ImVec2 pos = PlotToPixels((double)xs[idx], (double)ys[idx], yax);
const ImVec4 col = GetLastItemColor();
const ImU32 col32 = ImGui::ColorConvertFloat4ToU32(col);
PushPlotClipRect();
ImVec2 pos = PlotToPixels(xs_mono[idx], ys[idx]);
ImVec4 col = GetLastItemColor();
GetPlotDrawList()->AddCircleFilled(pos, 6, ImGui::ColorConvertFloat4ToU32(col));
GetPlotDrawList()->AddCircleFilled(pos, gp.Style.MarkerSize, col32);
PopPlotClipRect();
if (idx_out == NULL) {
ImGui::BeginTooltip();
ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::Text("%.3f,%.3f",xs_mono[idx], ys[idx]);
ImGui::PopStyleColor();
ImGui::EndTooltip();
if (idx >= 0) {
char buff1[32];
char buff2[32];
LabelAxisValue(gp.CurrentPlot->XAxis, gp.XTicks, (double)xs[idx], buff1, 32);
LabelAxisValue(gp.CurrentPlot->YAxis[yax], gp.YTicks[yax], (double)ys[idx], buff2, 32);
gp.Annotations.Append(pos, ImVec2(0.001f,0.001f), col32, CalcTextColor(col), clamp, "%s,%s", buff1, buff2);
return idx;
}
else {
*idx_out = idx;
}
return true;
return -1;
}
return false;
return -1;
}
void EndTooltip() {
}
// bool BeginTooltipY(const double* xs, const double* ys_mono, int* idx_out = NULL);
template IMPLOT_API int Tooltip<ImS8>(const ImS8* xs, const ImS8* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImU8>(const ImU8* xs, const ImU8* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImS16>(const ImS16* xs, const ImS16* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImU16>(const ImU16* xs, const ImU16* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImS32>(const ImS32* xs, const ImS32* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImU32>(const ImU32* xs, const ImU32* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImS64>(const ImS64* xs, const ImS64* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<ImU64>(const ImU64* xs, const ImU64* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<float>(const float* xs, const float* ys, int count, bool clamp);
template IMPLOT_API int Tooltip<double>(const double* xs, const double* ys, int count, bool clamp);
//-----------------------------------------------------------------------------
// LEGEND UTILS

View File

@ -533,16 +533,19 @@ IMPLOT_API bool DragLineY(const char* id, double* y_value, bool show_label = tru
// Shows a draggable point at x,y. #col defaults to ImGuiCol_Text.
IMPLOT_API bool DragPoint(const char* id, double* x, double* y, bool show_label = true, const ImVec4& col = IMPLOT_AUTO_COL, float radius = 4);
template <typename T> IMPLOT_API int Tooltip(const T* xs_mono, const T* ys, int count, bool clamp = true);
// Facilitates adding hover tooltips to plots. If #idx_out is NULL, a default tooltip will be rendered. Otherwise, the value will be set to the hovered idx,
// and it becomes YOUR responsibility to render tooltip information using regular ImGui functions. You MUST make a matching call to EndTooltip if these
// functions return true!
IMPLOT_API bool BeginTooltip(const double* xs_mono, const double* ys, int count, int* idx_out = NULL);
// IMPLOT_API bool BeginTooltip(const double* xs_mono, const double* ys, int count, int* idx_out);
// This variant is optimized to search along monotonically increasing x values (this is the one you want for time-series plots).
IMPLOT_API bool BeginTooltipX(const double* xs_mono, const double* ys, int count, int* idx_out = NULL);
// IMPLOT_API bool BeginTooltipX(const double* xs_mono, const double* ys, int count, int* idx_out);
// This variant is optimized to search along monotonically increasing y values.
IMPLOT_API bool BeginTooltipY(const double* xs, const double* ys_mono, int count, int* idx_out = NULL);
// IMPLOT_API bool BeginTooltipY(const double* xs, const double* ys_mono, int count, int* idx_out);
// Only call EndTooltip if BeginTooltip returns true! e.g. if (BeginTooltipX(...)) { ... EndTooltip(); }
IMPLOT_API void EndTooltip();
// IMPLOT_API void EndTooltip();
//-----------------------------------------------------------------------------
// Legend Utils and Tools

View File

@ -246,12 +246,9 @@ void ShowDemoWindow(bool* p_open) {
ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).");
if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) {
ImPlot::PlotLine("sin(x)", xs1, ys1, 1001);
if (ImPlot::BeginTooltipX(xs1,ys1,1001))
ImPlot::EndTooltip();
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
ImPlot::PlotLine("x^2", xs2, ys2, 11);
if (ImPlot::BeginTooltipX(xs2,ys2,11))
ImPlot::EndTooltip();
ImPlot::Tooltip(xs2,ys2,11);
ImPlot::EndPlot();
}
}
@ -277,8 +274,11 @@ void ShowDemoWindow(bool* p_open) {
if (show_fills) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, fill_ref);
ImPlot::Tooltip(xs1,ys1,101);
ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, fill_ref);
ImPlot::Tooltip(xs1,ys2,101);
ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, fill_ref);
ImPlot::Tooltip(xs1,ys3,101);
ImPlot::PopStyleVar();
}
if (show_lines) {
@ -308,6 +308,7 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);
ImPlot::Tooltip(xs,ys,1001);
ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001);
ImPlot::PlotLine("Overlapping",xs,ys3,1001);
ImPlot::PlotLine("Overlapping",xs,ys4,1001);
@ -331,8 +332,7 @@ void ShowDemoWindow(bool* p_open) {
if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) {
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
if (ImPlot::BeginTooltipX(xs1,ys1,100))
ImPlot::EndTooltip();
ImPlot::Tooltip(xs1,ys1,100);
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1));
ImPlot::PlotScatter("Data 2", xs2, ys2, 50);