Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
PlotTypes.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include "math/Precision.hpp"
13#include "plotters/PathFont.hpp"
14
15#include <algorithm>
16#include <array>
17#include <cmath>
18#include <cstddef>
19#include <cstdint>
20#include <format>
21#include <optional>
22#include <span>
23#include <string>
24#include <string_view>
25#include <vector>
26
27namespace correlation::plotters {
28
34 bool is_vertical{true};
35 std::string label;
36 std::string color_hex;
37};
38
42struct PlotConfig {
44 enum class Theme : std::uint8_t {
47 };
48
50 real_t width = static_cast<real_t>(1200.0);
51 real_t height = static_cast<real_t>(900.0);
52 bool show_grid = true;
53 bool show_minor_grid = false;
54 bool show_markers = false;
55 bool fill_area = false;
57 false;
58 bool y_log_scale = false;
59
60 // Publication settings
61 real_t font_scale = static_cast<real_t>(1.0);
62 real_t line_width = static_cast<real_t>(3.0);
63 real_t marker_size = static_cast<real_t>(3.5);
64 bool show_legend = true;
65 bool use_native_text = false;
66
81
90
91 [[nodiscard]] real_t effectiveWidth() const {
92 switch (preset_size) {
94 return static_cast<real_t>(1050.0);
96 return static_cast<real_t>(2100.0);
98 return static_cast<real_t>(3000.0);
99 default:
100 return width;
101 }
102 }
103
104 [[nodiscard]] real_t effectiveHeight() const {
105 switch (preset_size) {
107 return static_cast<real_t>(788.0);
109 return static_cast<real_t>(1575.0);
111 return static_cast<real_t>(2250.0);
112 default:
113 return height;
114 }
115 }
116
118 [[nodiscard]] std::string_view bgColor() const {
119 return (theme == Theme::Light) ? "#FFFFFF" : "#1e1e2e";
120 }
121
122 [[nodiscard]] std::string_view axisColor() const {
123 return (theme == Theme::Light) ? "#000000" : "#e2e8f0";
124 }
125
126 [[nodiscard]] std::string_view gridColor() const {
127 return (theme == Theme::Light) ? "#808080" : "#45475a";
128 }
129
130 [[nodiscard]] std::string_view textColor() const {
131 return (theme == Theme::Light) ? "#333333" : "#FFFFFF";
132 }
133
134 // Manual Zoom Bounds
135 std::optional<real_t> manual_x_min;
136 std::optional<real_t> manual_x_max;
137 std::optional<real_t> manual_y_min;
138 std::optional<real_t> manual_y_max;
139
140 // Manual Axis Ticks & Grid Placement
141 bool manual_ticks = false;
142 real_t x_major_step = static_cast<real_t>(0.0);
143 real_t y_major_step = static_cast<real_t>(0.0);
145
146 // Analytical Annotations
148
149 // Reference lines
150 std::vector<ReferenceLine> reference_lines;
151};
152
157 bool auto_x{true};
158 std::optional<real_t> manual_x_min;
159 std::optional<real_t> manual_x_max;
160 bool auto_y{true};
161 std::optional<real_t> manual_y_min;
162 std::optional<real_t> manual_y_max;
163 bool auto_ticks{true};
164 real_t x_major_step{static_cast<real_t>(0.0)};
165 real_t y_major_step{static_cast<real_t>(0.0)};
167 bool y_log_scale{false};
168 bool show_major_grid{true};
169 bool show_minor_grid{false};
170 real_t line_width{static_cast<real_t>(3.0)};
171};
172
176struct HoverInfo {
177 bool active = false;
178 real_t mouse_x = static_cast<real_t>(-1.0);
179 real_t mouse_y = static_cast<real_t>(-1.0);
180 real_t widget_width = static_cast<real_t>(0.0);
181 real_t widget_height = static_cast<real_t>(0.0);
182};
183
188enum class TextAnchor : std::uint8_t {
192};
193
198 std::string color_hex;
199 float stroke_width = 2.0F;
200 int dash_style = 0;
201 bool visible = true;
202};
203
208 std::string label;
211 bool is_difference = false;
212};
213
218inline real_t sampleHistogramClamped(const std::vector<real_t> &x_bins,
219 const std::vector<real_t> &y_vals, real_t x_val) {
220 if (x_bins.empty() || y_vals.empty() || x_bins.size() != y_vals.size()) {
221 return static_cast<real_t>(0.0);
222 }
223 if (x_val <= x_bins.front()) {
224 return y_vals.front();
225 }
226 if (x_val >= x_bins.back()) {
227 return y_vals.back();
228 }
229 const auto it_idx = std::ranges::lower_bound(x_bins, x_val);
230 const auto idx = static_cast<std::size_t>(std::distance(x_bins.begin(), it_idx));
231 if (idx == 0) {
232 return y_vals[0];
233 }
234 const real_t x_0 = x_bins[idx - 1];
235 const real_t x_1 = x_bins[idx];
236 const real_t y_0 = y_vals[idx - 1];
237 const real_t y_1 = y_vals[idx];
238 if (std::abs(x_1 - x_0) < static_cast<real_t>(1e-12)) {
239 return y_0;
240 }
241 return y_0 + (x_val - x_0) / (x_1 - x_0) * (y_1 - y_0);
242}
243
248inline std::string renderTextAsPath(const std::string &text, real_t x_pos, real_t y_pos,
249 real_t size, TextAnchor anchor, std::string_view color,
250 bool use_native_text = false) {
251 std::string anchor_str = "start";
252 if (anchor == TextAnchor::Middle) {
253 anchor_str = "middle";
254 } else if (anchor == TextAnchor::End) {
255 anchor_str = "end";
256 }
257
258 if (use_native_text) {
259 return std::format(
260 " <text x=\"{:.1f}\" y=\"{:.1f}\" font-family=\"'Outfit', 'Plus Jakarta Sans', 'Inter', "
261 "'Roboto', 'Helvetica Neue', "
262 "sans-serif\" font-size=\"{:.1f}\" text-anchor=\"{}\" fill=\"{}\">{}</text>\n",
263 x_pos, y_pos, size, anchor_str, color, text);
264 }
265 std::string path_d = Roboto::instance().render(TextRenderParameters{
266 .text = text,
267 .start_x = x_pos,
268 .start_y = y_pos,
269 .font_size = size,
270 .anchor = anchor_str,
271 });
272 return std::format(" <path d=\"{}\" fill=\"{}\" fill-rule=\"evenodd\" stroke=\"none\"/>\n",
273 path_d, color);
274}
275
276namespace detail {
277
279constexpr std::array<std::string_view, 8> K_COLORS = {
280 "#E69F00", // Orange
281 "#56B4E9", // Sky Blue
282 "#009E73", // Bluish Green
283 "#F0E442", // Yellow
284 "#0072B2", // Blue
285 "#D55E00", // Vermillion
286 "#CC79A7", // Reddish Purple
287 "#00C49F", // High-contrast Teal (replaces invisible black)
288};
289
291constexpr std::array<std::string_view, 5> K_GRAYSCALE = {
292 "#000000", "#404040", "#808080", "#B0B0B0", "#D0D0D0",
293};
294
296constexpr std::array<std::string_view, 5> K_VIRIDIS = {
297 "#440154", "#3B528B", "#21908C", "#5DC863", "#FDE725",
298};
299
300struct ColorStop {
301 float t;
302 uint8_t r, g, b;
303};
304
305inline std::string interpolateColorStops(float position, std::span<const ColorStop> stops) {
306 if (stops.empty()) {
307 return "#000000";
308 }
309 if (position <= stops.front().t) {
310 return std::format("#{:02X}{:02X}{:02X}", static_cast<unsigned int>(stops.front().r),
311 static_cast<unsigned int>(stops.front().g),
312 static_cast<unsigned int>(stops.front().b));
313 }
314 if (position >= stops.back().t) {
315 return std::format("#{:02X}{:02X}{:02X}", static_cast<unsigned int>(stops.back().r),
316 static_cast<unsigned int>(stops.back().g),
317 static_cast<unsigned int>(stops.back().b));
318 }
319 for (std::size_t i = 0; i < stops.size() - 1; ++i) {
320 if (position >= stops[i].t && position <= stops[i + 1].t) {
321 const float factor = (position - stops[i].t) / (stops[i + 1].t - stops[i].t);
322 const auto red = static_cast<unsigned int>(std::round(
323 std::lerp(static_cast<float>(stops[i].r), static_cast<float>(stops[i + 1].r), factor)));
324 const auto green = static_cast<unsigned int>(std::round(
325 std::lerp(static_cast<float>(stops[i].g), static_cast<float>(stops[i + 1].g), factor)));
326 const auto blue = static_cast<unsigned int>(std::round(
327 std::lerp(static_cast<float>(stops[i].b), static_cast<float>(stops[i + 1].b), factor)));
328 return std::format("#{:02X}{:02X}{:02X}", red, green, blue);
329 }
330 }
331 return std::format("#{:02X}{:02X}{:02X}", static_cast<unsigned int>(stops.back().r),
332 static_cast<unsigned int>(stops.back().g),
333 static_cast<unsigned int>(stops.back().b));
334}
335
336inline std::string sampleContinuousColormap(float position, PlotConfig::Palette pal) {
337 switch (pal) {
339 constexpr std::array<ColorStop, 5> K_MAGMA_STOPS = {
340 ColorStop{.t = 0.00F, .r = 0, .g = 0, .b = 4},
341 ColorStop{.t = 0.25F, .r = 81, .g = 18, .b = 124},
342 ColorStop{.t = 0.50F, .r = 183, .g = 55, .b = 121},
343 ColorStop{.t = 0.75F, .r = 252, .g = 137, .b = 97},
344 ColorStop{.t = 1.00F, .r = 252, .g = 253, .b = 191},
345 };
346 return interpolateColorStops(position, K_MAGMA_STOPS);
347 }
349 constexpr std::array<ColorStop, 4> K_HEATMAP_STOPS = {
350 ColorStop{.t = 0.00F, .r = 0, .g = 0, .b = 0},
351 ColorStop{.t = 0.33F, .r = 255, .g = 0, .b = 0},
352 ColorStop{.t = 0.66F, .r = 255, .g = 255, .b = 0},
353 ColorStop{.t = 1.00F, .r = 255, .g = 255, .b = 255},
354 };
355 return interpolateColorStops(position, K_HEATMAP_STOPS);
356 }
358 constexpr std::array<ColorStop, 5> K_RAINBOW_STOPS = {
359 ColorStop{.t = 0.00F, .r = 0, .g = 0, .b = 255},
360 ColorStop{.t = 0.25F, .r = 0, .g = 255, .b = 255},
361 ColorStop{.t = 0.50F, .r = 0, .g = 255, .b = 0},
362 ColorStop{.t = 0.75F, .r = 255, .g = 255, .b = 0},
363 ColorStop{.t = 1.00F, .r = 255, .g = 0, .b = 0},
364 };
365 return interpolateColorStops(position, K_RAINBOW_STOPS);
366 }
368 constexpr std::array<ColorStop, 5> K_TURBO_STOPS = {
369 ColorStop{.t = 0.00F, .r = 48, .g = 18, .b = 59},
370 ColorStop{.t = 0.25F, .r = 26, .g = 228, .b = 182},
371 ColorStop{.t = 0.50F, .r = 162, .g = 252, .b = 60},
372 ColorStop{.t = 0.75F, .r = 251, .g = 128, .b = 34},
373 ColorStop{.t = 1.00F, .r = 122, .g = 4, .b = 3},
374 };
375 return interpolateColorStops(position, K_TURBO_STOPS);
376 }
378 constexpr std::array<ColorStop, 5> K_PLASMA_STOPS = {
379 ColorStop{.t = 0.00F, .r = 13, .g = 8, .b = 135},
380 ColorStop{.t = 0.25F, .r = 106, .g = 0, .b = 168},
381 ColorStop{.t = 0.50F, .r = 177, .g = 42, .b = 144},
382 ColorStop{.t = 0.75F, .r = 225, .g = 100, .b = 98},
383 ColorStop{.t = 1.00F, .r = 252, .g = 166, .b = 54},
384 };
385 return interpolateColorStops(position, K_PLASMA_STOPS);
386 }
388 constexpr std::array<ColorStop, 5> K_INFERNO_STOPS = {
389 ColorStop{.t = 0.00F, .r = 0, .g = 0, .b = 4},
390 ColorStop{.t = 0.25F, .r = 87, .g = 9, .b = 107},
391 ColorStop{.t = 0.50F, .r = 187, .g = 55, .b = 84},
392 ColorStop{.t = 0.75F, .r = 249, .g = 142, .b = 9},
393 ColorStop{.t = 1.00F, .r = 252, .g = 255, .b = 164},
394 };
395 return interpolateColorStops(position, K_INFERNO_STOPS);
396 }
398 constexpr std::array<ColorStop, 5> K_CIVIDIS_STOPS = {
399 ColorStop{.t = 0.00F, .r = 0, .g = 32, .b = 81},
400 ColorStop{.t = 0.25F, .r = 58, .g = 71, .b = 108},
401 ColorStop{.t = 0.50F, .r = 107, .g = 112, .b = 116},
402 ColorStop{.t = 0.75F, .r = 161, .g = 156, .b = 114},
403 ColorStop{.t = 1.00F, .r = 254, .g = 254, .b = 98},
404 };
405 return interpolateColorStops(position, K_CIVIDIS_STOPS);
406 }
407 default:
408 return "#000000";
409 }
410}
411
412inline std::string color(std::size_t index, std::size_t total_count, PlotConfig::Palette pal) {
413 switch (pal) {
415 return std::string(K_GRAYSCALE.at(index % K_GRAYSCALE.size()));
417 return std::string(K_VIRIDIS.at(index % K_VIRIDIS.size()));
419 return std::string(K_COLORS.at(index % K_COLORS.size()));
420 default: {
421 const float position =
422 (total_count <= 1) ? 0.5F : static_cast<float>(index) / static_cast<float>(total_count - 1);
423 return sampleContinuousColormap(position, pal);
424 }
425 }
426}
427
428inline std::string color(std::size_t index, PlotConfig::Palette pal) {
429 return color(index, 8, pal);
430}
431
432inline real_t mapValue(real_t value, real_t data_min, real_t data_max, real_t svg_min,
433 real_t svg_max) {
434 if (std::abs(data_max - data_min) < static_cast<real_t>(1e-15)) {
435 return (svg_min + svg_max) / static_cast<real_t>(2.0);
436 }
437 return svg_min + (value - data_min) / (data_max - data_min) * (svg_max - svg_min);
438}
439
440struct DataRange {
441 real_t min = static_cast<real_t>(0.0);
442 real_t max = static_cast<real_t>(0.0);
443};
444
445struct NiceScale {
446 real_t min = static_cast<real_t>(0.0);
447 real_t max = static_cast<real_t>(0.0);
448 real_t spacing = static_cast<real_t>(0.0);
449 std::vector<real_t> ticks;
450
451 NiceScale() = default;
452
453 explicit NiceScale(const DataRange &range, int max_ticks = 6, bool strict_bounds = false,
454 real_t custom_step = static_cast<real_t>(0.0)) {
455 real_t actual_min = range.min;
456 real_t actual_max = range.max;
457 if (actual_max < actual_min) {
458 std::swap(actual_min, actual_max);
459 }
460 if (custom_step > static_cast<real_t>(0.0)) {
461 spacing = custom_step;
462 min = actual_min;
463 max = actual_max;
464 const auto first_tick = std::ceil(min / spacing) * spacing;
465 const auto num_ticks =
466 (max >= first_tick)
467 ? static_cast<int>(std::floor(
468 (max - first_tick + static_cast<real_t>(1e-6) * spacing) / spacing)) +
469 1
470 : 0;
471 for (int idx = 0; idx < num_ticks; ++idx) {
472 ticks.push_back(first_tick + static_cast<real_t>(idx) * spacing);
473 }
474 } else if (std::abs(actual_max - actual_min) < static_cast<real_t>(1e-12)) {
475 min = actual_min - static_cast<real_t>(0.5);
476 max = actual_min + static_cast<real_t>(0.5);
477 spacing = static_cast<real_t>(0.1);
478 ticks.push_back(actual_min);
479 } else {
480 const real_t range_val = niceNum(actual_max - actual_min, false);
481 spacing = niceNum(range_val / static_cast<real_t>(max_ticks - 1), true);
482 if (strict_bounds) {
483 min = actual_min;
484 max = actual_max;
485 const auto first_tick = std::ceil(min / spacing) * spacing;
486 const auto num_ticks =
487 (max >= first_tick)
488 ? static_cast<int>(std::floor(
489 (max - first_tick + static_cast<real_t>(1e-6) * spacing) / spacing)) +
490 1
491 : 0;
492 for (int idx = 0; idx < num_ticks; ++idx) {
493 const auto val = first_tick + static_cast<real_t>(idx) * spacing;
494 ticks.push_back(val);
495 }
496 } else {
497 min = std::floor(actual_min / spacing) * spacing;
498 max = std::ceil(actual_max / spacing) * spacing;
499 const real_t range_span = max - min;
500 const int num_ticks = static_cast<int>(std::round(range_span / spacing)) + 1;
501 for (int idx = 0; idx < num_ticks; ++idx) {
502 const auto value = min + static_cast<real_t>(idx) * spacing;
503 ticks.push_back(value);
504 }
505 }
506 }
507 }
508
509private:
516 static real_t niceNum(real_t range, bool round) {
517 const real_t exponent = std::floor(std::log10(range));
518 const real_t fraction = range / std::pow(static_cast<real_t>(10.0), exponent);
519 auto nice_fraction = static_cast<real_t>(0.0);
520
521 if (round) {
522 if (fraction < static_cast<real_t>(1.5)) {
523 nice_fraction = static_cast<real_t>(1.0);
524 } else if (fraction < static_cast<real_t>(3.0)) {
525 nice_fraction = static_cast<real_t>(2.0);
526 } else if (fraction < static_cast<real_t>(7.0)) {
527 nice_fraction = static_cast<real_t>(5.0);
528 } else {
529 nice_fraction = static_cast<real_t>(10.0);
530 }
531 } else {
532 if (fraction <= static_cast<real_t>(1.0)) {
533 nice_fraction = static_cast<real_t>(1.0);
534 } else if (fraction <= static_cast<real_t>(2.0)) {
535 nice_fraction = static_cast<real_t>(2.0);
536 } else if (fraction <= static_cast<real_t>(5.0)) {
537 nice_fraction = static_cast<real_t>(5.0);
538 } else {
539 nice_fraction = static_cast<real_t>(10.0);
540 }
541 }
542 return nice_fraction * std::pow(static_cast<real_t>(10.0), exponent);
543 }
544};
545
549inline std::string fmtScientific(real_t value) {
550 const real_t abs_value = std::abs(value);
551 if (abs_value < static_cast<real_t>(1e-12)) {
552 return "0";
553 }
554
555 if (abs_value < static_cast<real_t>(0.01) || abs_value >= static_cast<real_t>(10000.0)) {
556 const int exponent = static_cast<int>(std::floor(std::log10(abs_value)));
557 const real_t fraction =
558 value / std::pow(static_cast<real_t>(10.0), static_cast<real_t>(exponent));
559 std::string res = std::format("{:.1f}×10", fraction);
560 const std::string exp_s = std::to_string(exponent);
561 for (const char chr : exp_s) {
562 if (chr == '-') {
563 res += "⁻";
564 } else if (chr == '0') {
565 res += "⁰";
566 } else if (chr == '1') {
567 res += "¹";
568 } else if (chr == '2') {
569 res += "²";
570 } else if (chr == '3') {
571 res += "³";
572 } else if (chr == '4') {
573 res += "⁴";
574 } else if (chr == '5') {
575 res += "⁵";
576 } else if (chr == '6') {
577 res += "⁶";
578 } else if (chr == '7') {
579 res += "⁷";
580 } else if (chr == '8') {
581 res += "⁸";
582 } else if (chr == '9') {
583 res += "⁹";
584 }
585 }
586 return res;
587 }
588
589 std::string str = std::format("{:.2f}", value);
590 const auto dot = str.find('.');
591 if (dot != std::string::npos) {
592 const std::size_t last = str.find_last_not_of('0');
593 if (last != std::string::npos && last > dot) {
594 str = str.substr(0, last + 1);
595 } else if (last == dot) {
596 str = str.substr(0, dot);
597 }
598 }
599 return str;
600}
601
603 real_t kw{1200.0};
604 real_t kh{900.0};
605 real_t px0{100.0};
606 real_t px1{1160.0};
607 real_t py0{50.0};
608 real_t py1{810.0};
609};
610
612 const real_t eff_w = config.effectiveWidth();
613 const real_t eff_h = config.effectiveHeight();
614 const auto k_left = static_cast<real_t>(100.0);
615 const auto k_right = static_cast<real_t>(40.0);
616 const auto k_top = static_cast<real_t>(50.0);
617 const auto k_bottom = static_cast<real_t>(90.0);
619 .kw = eff_w,
620 .kh = eff_h,
621 .px0 = k_left,
622 .px1 = eff_w - k_right,
623 .py0 = k_top,
624 .py1 = eff_h - k_bottom,
625 };
626}
627
628inline std::pair<real_t, real_t> screenToSvg(real_t screen_x, real_t screen_y, real_t widget_w,
629 real_t widget_h, const PlotConfig &config) {
630 const real_t eff_w = config.effectiveWidth();
631 const real_t eff_h = config.effectiveHeight();
632 const real_t widget_aspect = (widget_h > 0) ? (widget_w / widget_h) : (eff_w / eff_h);
633 const real_t plot_aspect = eff_w / eff_h;
634 auto scale = static_cast<real_t>(1.0);
635 auto offset_x = static_cast<real_t>(0.0);
636 auto offset_y = static_cast<real_t>(0.0);
637 if (widget_aspect > plot_aspect) {
638 scale = (widget_h > 0) ? (widget_h / eff_h) : static_cast<real_t>(1.0);
639 offset_x = (widget_w - eff_w * scale) / static_cast<real_t>(2.0);
640 } else {
641 scale = (widget_w > 0) ? (widget_w / eff_w) : static_cast<real_t>(1.0);
642 offset_y = (widget_h - eff_h * scale) / static_cast<real_t>(2.0);
643 }
644 const real_t svg_x = (scale > 0) ? ((screen_x - offset_x) / scale) : screen_x;
645 const real_t svg_y = (scale > 0) ? ((screen_y - offset_y) / scale) : screen_y;
646 return {svg_x, svg_y};
647}
648
649inline std::pair<real_t, real_t> screenToData(real_t screen_x, real_t screen_y, real_t widget_w,
650 real_t widget_h, const PlotConfig &config,
651 const NiceScale &x_scale, const NiceScale &y_scale) {
652 auto [svg_x, svg_y] = screenToSvg(screen_x, screen_y, widget_w, widget_h, config);
653 const auto geom = getViewportGeometry(config);
654 const real_t clamped_x = std::clamp(svg_x, geom.px0, geom.px1);
655 const real_t clamped_y = std::clamp(svg_y, geom.py0, geom.py1);
656
657 const real_t data_x =
658 x_scale.min + (clamped_x - geom.px0) / (geom.px1 - geom.px0) * (x_scale.max - x_scale.min);
659 const real_t data_y =
660 y_scale.min + (geom.py1 - clamped_y) / (geom.py1 - geom.py0) * (y_scale.max - y_scale.min);
661 return {data_x, data_y};
662}
663
664inline std::pair<real_t, real_t> svgToScreen(real_t svg_x, real_t svg_y, real_t widget_w,
665 real_t widget_h, const PlotConfig &config) {
666 const real_t eff_w = config.effectiveWidth();
667 const real_t eff_h = config.effectiveHeight();
668 const real_t widget_aspect = (widget_h > 0) ? (widget_w / widget_h) : (eff_w / eff_h);
669 const real_t plot_aspect = eff_w / eff_h;
670 auto scale = static_cast<real_t>(1.0);
671 auto offset_x = static_cast<real_t>(0.0);
672 auto offset_y = static_cast<real_t>(0.0);
673 if (widget_aspect > plot_aspect) {
674 scale = (widget_h > 0) ? (widget_h / eff_h) : static_cast<real_t>(1.0);
675 offset_x = (widget_w - eff_w * scale) / static_cast<real_t>(2.0);
676 } else {
677 scale = (widget_w > 0) ? (widget_w / eff_w) : static_cast<real_t>(1.0);
678 offset_y = (widget_h - eff_h * scale) / static_cast<real_t>(2.0);
679 }
680 const real_t screen_x = offset_x + svg_x * scale;
681 const real_t screen_y = offset_y + svg_y * scale;
682 return {screen_x, screen_y};
683}
684
685inline std::pair<real_t, real_t> dataToScreen(real_t data_x, real_t data_y, real_t widget_w,
686 real_t widget_h, const PlotConfig &config,
687 const NiceScale &x_scale, const NiceScale &y_scale) {
688 const auto geom = getViewportGeometry(config);
689 const real_t x_denom =
690 (x_scale.max != x_scale.min) ? (x_scale.max - x_scale.min) : static_cast<real_t>(1.0);
691 const real_t y_denom =
692 (y_scale.max != y_scale.min) ? (y_scale.max - y_scale.min) : static_cast<real_t>(1.0);
693
694 const real_t svg_x = geom.px0 + (data_x - x_scale.min) / x_denom * (geom.px1 - geom.px0);
695 const real_t svg_y = geom.py1 - (data_y - y_scale.min) / y_denom * (geom.py1 - geom.py0);
696
697 return svgToScreen(svg_x, svg_y, widget_w, widget_h, config);
698}
699
700} // namespace detail
701} // namespace correlation::plotters
Manager for distribution function calculations (RDF, PAD, S(Q), etc.).
High-fidelity Roboto vector font for SVG path rendering.
Floating point precision type definitions for Correlation.
static Roboto & instance()
Definition PathFont.cpp:15
std::string render(TextRenderParameters const &params)
Renders a string of text as SVG path data using parameters struct.
Definition PathFont.cpp:46
Definition PlotTypes.hpp:276
std::pair< real_t, real_t > svgToScreen(real_t svg_x, real_t svg_y, real_t widget_w, real_t widget_h, const PlotConfig &config)
Definition PlotTypes.hpp:664
std::pair< real_t, real_t > screenToSvg(real_t screen_x, real_t screen_y, real_t widget_w, real_t widget_h, const PlotConfig &config)
Definition PlotTypes.hpp:628
std::pair< real_t, real_t > screenToData(real_t screen_x, real_t screen_y, real_t widget_w, real_t widget_h, const PlotConfig &config, const NiceScale &x_scale, const NiceScale &y_scale)
Definition PlotTypes.hpp:649
constexpr std::array< std::string_view, 5 > K_GRAYSCALE
Grayscale palette for B&W printing.
Definition PlotTypes.hpp:291
constexpr std::array< std::string_view, 8 > K_COLORS
Okabe-Ito colorblind-safe palette (calibrated for high contrast on dark and light).
Definition PlotTypes.hpp:279
std::string interpolateColorStops(float position, std::span< const ColorStop > stops)
Definition PlotTypes.hpp:305
std::string sampleContinuousColormap(float position, PlotConfig::Palette pal)
Definition PlotTypes.hpp:336
real_t mapValue(real_t value, real_t data_min, real_t data_max, real_t svg_min, real_t svg_max)
Definition PlotTypes.hpp:432
PlotViewportGeometry getViewportGeometry(const PlotConfig &config)
Definition PlotTypes.hpp:611
std::string color(std::size_t index, std::size_t total_count, PlotConfig::Palette pal)
Definition PlotTypes.hpp:412
std::pair< real_t, real_t > dataToScreen(real_t data_x, real_t data_y, real_t widget_w, real_t widget_h, const PlotConfig &config, const NiceScale &x_scale, const NiceScale &y_scale)
Definition PlotTypes.hpp:685
std::string fmtScientific(real_t value)
Formats a number for SVG display, using scientific notation if needed.
Definition PlotTypes.hpp:549
constexpr std::array< std::string_view, 5 > K_VIRIDIS
Viridis perceptually uniform palette.
Definition PlotTypes.hpp:296
Definition ColormapService.hpp:18
TextAnchor
Horizontal alignment / anchor options for SVG text rendering.
Definition PlotTypes.hpp:188
@ End
Right-aligned / end-anchored text.
Definition PlotTypes.hpp:191
@ Start
Left-aligned / start-anchored text.
Definition PlotTypes.hpp:189
@ Middle
Center-aligned / middle-anchored text.
Definition PlotTypes.hpp:190
real_t sampleHistogramClamped(const std::vector< real_t > &x_bins, const std::vector< real_t > &y_vals, real_t x_val)
Samples a 1D dataset y(x) at point x using linear interpolation with boundary clamping to [x_min,...
Definition PlotTypes.hpp:218
std::string renderTextAsPath(const std::string &text, real_t x_pos, real_t y_pos, real_t size, TextAnchor anchor, std::string_view color, bool use_native_text=false)
Renders text as a filled SVG path using the Roboto outline font. Uses evenodd fill-rule to render the...
Definition PlotTypes.hpp:248
double real_t
Definition Precision.hpp:18
Container for a single calculated distribution function.
Definition DistributionFunctions.hpp:226
Custom rendering style for individual comparison curves.
Definition PlotTypes.hpp:197
bool visible
Visibility toggle flag.
Definition PlotTypes.hpp:201
float stroke_width
Stroke line thickness in px.
Definition PlotTypes.hpp:199
std::string color_hex
Custom hex color string (e.g. "#E63946"). Empty uses default palette.
Definition PlotTypes.hpp:198
int dash_style
Dash pattern style (0: Solid, 1: Dashed, 2: Dotted).
Definition PlotTypes.hpp:200
Information about the mouse hover position.
Definition PlotTypes.hpp:176
real_t widget_height
Definition PlotTypes.hpp:181
bool active
Definition PlotTypes.hpp:177
real_t mouse_y
Definition PlotTypes.hpp:179
real_t widget_width
Definition PlotTypes.hpp:180
real_t mouse_x
Definition PlotTypes.hpp:178
A labeled histogram for comparison rendering.
Definition PlotTypes.hpp:207
CurveStyle style
Per-curve custom style settings.
Definition PlotTypes.hpp:210
bool is_difference
Flag indicating if this is an overlaid difference curve.
Definition PlotTypes.hpp:211
std::string label
Run / dataset label.
Definition PlotTypes.hpp:208
const correlation::analysis::Histogram * hist
Pointer to histogram data.
Definition PlotTypes.hpp:209
User-configured axis limits, ticks, and scaling for a specific plot metric.
Definition PlotTypes.hpp:156
bool auto_ticks
Definition PlotTypes.hpp:163
std::optional< real_t > manual_y_max
Definition PlotTypes.hpp:162
std::optional< real_t > manual_y_min
Definition PlotTypes.hpp:161
int minor_subdivisions
Definition PlotTypes.hpp:166
std::optional< real_t > manual_x_min
Definition PlotTypes.hpp:158
std::optional< real_t > manual_x_max
Definition PlotTypes.hpp:159
bool show_minor_grid
Definition PlotTypes.hpp:169
real_t x_major_step
Definition PlotTypes.hpp:164
bool auto_x
Definition PlotTypes.hpp:157
bool show_major_grid
Definition PlotTypes.hpp:168
bool y_log_scale
Definition PlotTypes.hpp:167
bool auto_y
Definition PlotTypes.hpp:160
real_t y_major_step
Definition PlotTypes.hpp:165
real_t line_width
Definition PlotTypes.hpp:170
Theme and layout configuration for the plot.
Definition PlotTypes.hpp:42
PresetSize preset_size
Definition PlotTypes.hpp:89
bool show_markers
Whether to render data point markers (dots).
Definition PlotTypes.hpp:54
std::string_view axisColor() const
Definition PlotTypes.hpp:122
std::string_view bgColor() const
Definition PlotTypes.hpp:118
std::string_view textColor() const
Definition PlotTypes.hpp:130
Theme theme
Current theme selection.
Definition PlotTypes.hpp:49
real_t width
SVG canvas width (px).
Definition PlotTypes.hpp:50
bool show_difference_curve
Toggle overlaid absolute difference curve Y_diff = Y_ref - Y_target.
Definition PlotTypes.hpp:56
real_t effectiveWidth() const
Definition PlotTypes.hpp:91
bool use_native_text
Use standard SVG <text> elements instead of Hershey paths.
Definition PlotTypes.hpp:65
bool show_grid
Whether to render background grid lines.
Definition PlotTypes.hpp:52
std::optional< real_t > manual_x_min
Definition PlotTypes.hpp:135
bool y_log_scale
Whether Y-axis uses logarithmic (log10) scale.
Definition PlotTypes.hpp:58
std::optional< real_t > manual_y_max
Definition PlotTypes.hpp:138
bool fill_area
Whether to render matching gradient fills under the curves.
Definition PlotTypes.hpp:55
real_t x_major_step
Definition PlotTypes.hpp:142
std::optional< real_t > manual_y_min
Definition PlotTypes.hpp:137
Palette palette
Definition PlotTypes.hpp:80
real_t line_width
Data line stroke width.
Definition PlotTypes.hpp:62
Theme
Visualization themes for the generated SVG.
Definition PlotTypes.hpp:44
@ Light
Standard light theme for publications.
Definition PlotTypes.hpp:45
@ Dark
Modern dark theme for UI integration (e.g., Catppuccin-esque).
Definition PlotTypes.hpp:46
bool show_legend
Toggle legend visibility.
Definition PlotTypes.hpp:64
int minor_subdivisions
Definition PlotTypes.hpp:144
real_t y_major_step
Definition PlotTypes.hpp:143
real_t marker_size
Data point marker radius (px).
Definition PlotTypes.hpp:63
PresetSize
Standard publication sizes.
Definition PlotTypes.hpp:83
@ DoubleColumn
Definition PlotTypes.hpp:86
@ SingleColumn
Definition PlotTypes.hpp:85
@ Default
Definition PlotTypes.hpp:84
@ Presentation
Definition PlotTypes.hpp:87
real_t height
SVG canvas height (px).
Definition PlotTypes.hpp:51
std::string_view gridColor() const
Definition PlotTypes.hpp:126
std::vector< ReferenceLine > reference_lines
Definition PlotTypes.hpp:150
bool show_minor_grid
Whether to render minor subdivision grid lines.
Definition PlotTypes.hpp:53
Palette
Color palette selections.
Definition PlotTypes.hpp:68
@ Magma
Sequential dark to bright purple/yellow.
Definition PlotTypes.hpp:72
@ Rainbow
Standard Jet/HSV rainbow gradient.
Definition PlotTypes.hpp:74
@ Cividis
Colorblind-optimized perceptually uniform.
Definition PlotTypes.hpp:78
@ Turbo
Google Turbo perceptually smooth rainbow.
Definition PlotTypes.hpp:75
@ Inferno
Sequential dark to bright yellow.
Definition PlotTypes.hpp:77
@ Heatmap
Hot thermal gradient (Black-Red-Yellow-White).
Definition PlotTypes.hpp:73
@ Viridis
Perceptually uniform.
Definition PlotTypes.hpp:71
@ Plasma
Sequential purple to yellow/orange.
Definition PlotTypes.hpp:76
@ OkabeIto
Colorblind-safe palette.
Definition PlotTypes.hpp:69
@ Grayscale
B&W printing friendly.
Definition PlotTypes.hpp:70
real_t font_scale
Multiplier for all font sizes.
Definition PlotTypes.hpp:61
std::optional< real_t > manual_x_max
Definition PlotTypes.hpp:136
bool show_peak_callouts
Definition PlotTypes.hpp:147
real_t effectiveHeight() const
Definition PlotTypes.hpp:104
bool manual_ticks
Definition PlotTypes.hpp:141
User-defined reference marker line (constant X or constant Y).
Definition PlotTypes.hpp:32
std::string color_hex
Optional color (default axis color if empty).
Definition PlotTypes.hpp:36
std::string label
Optional text label (e.g. "r = 2.45 Å").
Definition PlotTypes.hpp:35
bool is_vertical
true for X = const, false for Y = const.
Definition PlotTypes.hpp:34
real_t value
Definition PlotTypes.hpp:33
Parameters for vector text rendering.
Definition PathFont.hpp:41
Definition PlotTypes.hpp:300
uint8_t b
Definition PlotTypes.hpp:302
uint8_t r
Definition PlotTypes.hpp:302
float t
Definition PlotTypes.hpp:301
uint8_t g
Definition PlotTypes.hpp:302
Definition PlotTypes.hpp:440
real_t max
Maximum value in data space.
Definition PlotTypes.hpp:442
real_t min
Minimum value in data space.
Definition PlotTypes.hpp:441
Definition PlotTypes.hpp:445
std::vector< real_t > ticks
Generated tick locations.
Definition PlotTypes.hpp:449
real_t spacing
Calculated distance between ticks.
Definition PlotTypes.hpp:448
real_t max
Ending tick value.
Definition PlotTypes.hpp:447
real_t min
Starting tick value.
Definition PlotTypes.hpp:446
static real_t niceNum(real_t range, bool round)
Rounds a range value to a "nice" human-readable number.
Definition PlotTypes.hpp:516
NiceScale(const DataRange &range, int max_ticks=6, bool strict_bounds=false, real_t custom_step=static_cast< real_t >(0.0))
Definition PlotTypes.hpp:453
real_t kw
Definition PlotTypes.hpp:603
real_t px1
Definition PlotTypes.hpp:606
real_t kh
Definition PlotTypes.hpp:604
real_t py1
Definition PlotTypes.hpp:608
real_t px0
Definition PlotTypes.hpp:605
real_t py0
Definition PlotTypes.hpp:607