10#include "TinyGPU/Color/RGB565.h"
11#include "TinyGPU/Color/RGB666.h"
12#include "TinyGPU/Color/RGB888.h"
13#include "TinyMaterialDesign/Core/Widget.h"
18template <
typename RGB_T>
33template <
typename RGB_T>
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
70 this->bounds = bounds;
71 if (title !=
nullptr) title_ = title;
78 void setTitle(
const char* title) { title_ = title; }
80 void setFooter(
const char* footer) { footer_ = footer; }
88 return static_cast<
int>(series_.size()) - 1;
91 int seriesCount()
const {
return static_cast<
int>(series_.size()); }
101 void setValues(
int index,
const std::vector<
float>& values) { series(index).values = values; }
102 void setValues(
int index,
const float* values, size_t count) {
103 series(index).values.assign(values, values + count);
108 void addValue(
int index,
float value, size_t maxPoints = 0) {
109 std::vector<
float>& v = series(index).values;
110 if (maxPoints > 0 && v.size() >= maxPoints) v.erase(v.begin(), v.begin() + (v.size() - maxPoints + 1));
119 line.dashed = dashed;
120 lines_.push_back(line);
121 return static_cast<
int>(lines_.size()) - 1;
123 void setReferenceLine(
int index,
float value) { lines_[
static_cast<size_t>(index)].value = value; }
137 void setGridValues(
const std::vector<
float>& values) { gridValues_ = values; }
144 using AxisFormatter = std::function<
void(
float value,
char* buf, size_t len)>;
147
148
149
150
151
152
153
154
155 void setXAxis(
float first,
float last,
int ticks = 3, AxisFormatter format =
nullptr) {
159 xTicks_ = std::max(2, ticks);
160 xFormat_ = std::move(format);
165 void draw(tinygpu::ISurface<RGB_T>& target)
override {
167 const Bounds& b =
this->bounds;
168 const RGB_T background =
card ? t.colors.surface : t.colors.background;
170 const size_t radius = toPx(t.shape.large);
171 target.fillRoundRect(toPx(b.x), toPx(b.y), toPx(b.w), toPx(b.h), radius, t.colors.surface);
172 target.drawRoundRect(toPx(b.x), toPx(b.y), toPx(b.w), toPx(b.h), radius, t.colors.outline);
174 const int32_t pad =
card ? 8 : 0;
175 tinygpu::IFont<RGB_T>& small = *t.typography.label;
176 tinygpu::IFont<RGB_T>& body = *t.typography.body;
177 int32_t top = b
.y + (
card ? 6 : 0);
178 if (!title_.empty()) {
179 text(target, body, b.x + pad, top, title_.c_str(), t.colors.onSurface, background);
180 top +=
static_cast<int32_t>(body.getHeight(1)) + 8;
185 if (!footer_.empty()) {
186 bottom -=
static_cast<int32_t>(small.getHeight(1));
187 text(target, small, b.x + pad, bottom, footer_.c_str(), t.colors.onSurfaceVariant, background);
193 bottom -=
static_cast<int32_t>(small.getHeight(1));
194 drawLegend(target, small, b.x + pad, b.right() - pad, bottom, background);
197 if (xAxis_) bottom -=
static_cast<int32_t>(small.getHeight(1)) + 6;
200 double lo = minY_, hi = maxY_;
204 for (
const ChartSeries<RGB_T>& s : series_) {
205 if (!s.visible)
continue;
206 for (
float v : s.values) {
207 if (!isfinite(v))
continue;
208 lo = std::min<
double>(lo, v);
209 hi = std::max<
double>(hi, v);
217 if (hi - lo < 1e-9) {
223 std::vector<
double> grid;
225 if (!gridValues_.empty()) {
226 for (
float v : gridValues_) grid.push_back(v);
227 step = grid.size() > 1 ? fabs(grid[1] - grid[0]) : hi - lo;
229 step = niceStep(hi - lo, gridCount_);
231 lo = floor(lo / step) * step;
232 hi = ceil(hi / step) * step;
234 for (
double v = ceil(lo / step - 1e-9) * step; v <= hi + step * 1e-6; v += step) grid.push_back(v);
236 if (step <= 0) step = 1;
237 int decimals =
static_cast<
int>(-floor(log10(step)));
238 decimals = std::min(4, std::max(0, decimals));
241 for (
double v : grid) {
242 label(buf,
sizeof(buf), v, step, decimals);
243 labelW = std::max<int32_t>(labelW,
static_cast<int32_t>(small.measureTextWidth(buf)));
246 const int32_t left = b
.x + pad + labelW + 4;
247 const int32_t right = b
.right() - pad;
248 const int32_t plotW = right - left;
249 const int32_t plotH = bottom - top;
250 if (plotW < 4 || plotH < 4)
return;
251 auto yOf = [&](
double v) {
252 v = std::min(std::max(v, lo), hi);
253 return top + plotH -
static_cast<int32_t>((v - lo) / (hi - lo) * plotH);
256 for (
double v : grid) {
257 if (v < lo - step * 1e-6 || v > hi + step * 1e-6)
continue;
258 const int32_t y = yOf(v);
259 if (fabs(v) < step * 1e-6) {
260 target.fillRect(toPx(left), toPx(y), toPx(plotW), 1, t.colors.onSurfaceVariant);
262 dashedLine(target, left, right, y, t.colors.outline, 3, 3);
264 label(buf,
sizeof(buf), v, step, decimals);
265 text(target, small, left - 4 -
static_cast<int32_t>(small.measureTextWidth(buf)),
266 y -
static_cast<int32_t>(small.getHeight(1)) / 2, buf, t.colors.onSurfaceVariant, background);
268 target.fillRect(toPx(left), toPx(top), 1, toPx(plotH), t.colors.outline);
269 if (xAxis_) drawXAxis(target, small, left, right, top + plotH, background);
271 for (
const ChartSeries<RGB_T>& s : series_) {
272 if (s.visible) drawSeries(target, s, left, right, yOf);
274 for (
const ChartReferenceLine<RGB_T>& line : lines_) {
275 const int32_t y = yOf(line.value);
277 dashedLine(target, left, right, y, line.color, 6, 4);
278 dashedLine(target, left, right, y + 1, line.color, 6, 4);
280 target.fillRect(toPx(left), toPx(y), toPx(plotW), 2, line.color);
288 std::vector<ChartSeries<RGB_T>> series_;
289 std::vector<ChartReferenceLine<RGB_T>> lines_;
290 std::vector<
float> gridValues_;
291 bool fixedRange_ =
false;
295 int visiblePoints_ = 0;
297 float xFirst_ = 0, xLast_ = 0;
299 AxisFormatter xFormat_;
303 int32_t x = 0, first = 0, last = 0, min = 0, max = 0;
307 void drawXAxis(tinygpu::ISurface<RGB_T>& target, tinygpu::IFont<RGB_T>& font, int32_t left, int32_t right,
308 int32_t y, RGB_T background) {
311 for (
int i = 0; i < xTicks_; ++i) {
312 const int32_t x = left +
static_cast<int32_t>(
static_cast<int64_t>(right - left) * i / (xTicks_ - 1));
313 target.fillRect(toPx(x), toPx(y), 1, 3, t.colors.outline);
314 const float v = xFirst_ + (xLast_ - xFirst_) * i / (xTicks_ - 1);
316 xFormat_(v, buf,
sizeof(buf));
318 snprintf(buf,
sizeof(buf),
"%g", v);
320 const int32_t w =
static_cast<int32_t>(font.measureTextWidth(buf));
322 const int32_t lx = i == 0 ? x : i == xTicks_ - 1 ? x - w : x - w / 2;
323 text(target, font, lx, y + 4, buf, t.colors.onSurfaceVariant, background);
327 bool hasLegend()
const {
328 for (
const ChartSeries<RGB_T>& s : series_) {
329 if (!s.label.empty())
return true;
336 void drawLegend(tinygpu::ISurface<RGB_T>& target, tinygpu::IFont<RGB_T>& font, int32_t x, int32_t right,
337 int32_t y, RGB_T background) {
338 const int32_t mid = y +
static_cast<int32_t>(font.getHeight(1)) / 2;
339 for (
const ChartSeries<RGB_T>& s : series_) {
340 if (s.label.empty() || !s.visible)
continue;
341 const int32_t w = 14 + 4 +
static_cast<int32_t>(font.measureTextWidth(s.label.c_str()));
342 if (x + w > right)
break;
344 dashedLine(target, x, x + 14, mid, s.color, 4, 2);
345 dashedLine(target, x, x + 14, mid + 1, s.color, 4, 2);
347 target.fillRect(toPx(x), toPx(mid), 14, 2, s.color);
349 text(target, font, x + 18, y, s.label.c_str(), s.color, background);
354 template <
typename YOf>
355 void drawSeries(tinygpu::ISurface<RGB_T>& target,
const ChartSeries<RGB_T>& s, int32_t left, int32_t right,
357 const size_t n = s.values.size();
359 const int32_t plotW = right - left;
363 int32_t dashCounter = 0;
364 int32_t* dash = s.dashed ? &dashCounter :
nullptr;
367 if (col.max > col.min) line(target, col.x, col.min, col.x, col.max, s.color, s.width, dash);
369 line(target, prev.x, prev.last, col.x, col.first, s.color, s.width, dash);
370 }
else if (col.max == col.min) {
371 line(target, col.x, col.first, col.x, col.first, s.color, s.width, dash);
377 for (size_t i = 0; i < n; ++i) {
379 if (visiblePoints_ > 1) {
380 x = right -
static_cast<int32_t>(
static_cast<
double>(n - 1 - i) * plotW / (visiblePoints_ - 1));
381 if (x < left)
continue;
383 x = n > 1 ? left +
static_cast<int32_t>(
static_cast<
double>(plotW) * i / (n - 1)) : left;
385 const float v = s.values[i];
391 const int32_t y = yOf(v);
392 if (open && x != col.x) flush();
395 col.first = col.last = col.min = col.max = y;
399 col.min = std::min(col.min, y);
400 col.max = std::max(col.max, y);
409 static void line(tinygpu::ISurface<RGB_T>& target, int32_t x0, int32_t y0, int32_t x1, int32_t y1,
410 RGB_T color, uint8_t width, int32_t* dash =
nullptr) {
411 const int32_t w = width == 0 ? 1 : width;
412 const int32_t half = w / 2;
413 const int32_t dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
414 const int32_t dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
415 int32_t err = dx + dy;
417 const bool on = dash ==
nullptr || ((*dash)++ % 9) < 5;
418 if (on && x0 - half >= 0 && y0 - half >= 0) target.fillRect(toPx(x0 - half), toPx(y0 - half), toPx(w), toPx(w), color);
419 if (x0 == x1 && y0 == y1)
break;
420 const int32_t e2 = 2 * err;
432 static void dashedLine(tinygpu::ISurface<RGB_T>& target, int32_t x0, int32_t x1, int32_t y, RGB_T color,
433 int32_t dash, int32_t gap) {
434 for (int32_t x = x0; x < x1; x += dash + gap) {
435 const int32_t end = std::min(x + dash, x1);
436 target.fillRect(toPx(x), toPx(y), toPx(end - x), 1, color);
440 static void text(tinygpu::ISurface<RGB_T>& target, tinygpu::IFont<RGB_T>& font, int32_t x, int32_t y,
441 const char* str, RGB_T color, RGB_T background) {
442 font.drawText(target,
static_cast<int16_t>(x),
static_cast<int16_t>(y), str, color, background,
false);
446 static double niceStep(
double range,
int targetTicks) {
447 const double raw = range / targetTicks;
448 const double mag = pow(10.0, floor(log10(raw)));
449 const double norm = raw / mag;
450 const double nice = norm < 1.5 ? 1.0 : norm < 3.0 ? 2.0 : norm < 7.0 ? 5.0 : 10.0;
455 static void label(
char* buf, size_t len,
double v,
double step,
int decimals) {
456 if (fabs(v) < step * 1e-6) {
457 snprintf(buf, len,
"0");
458 }
else if (step >= 1e6) {
459 snprintf(buf, len,
"%gM", v / 1e6);
460 }
else if (step >= 1e3) {
461 snprintf(buf, len,
"%gk", v / 1e3);
463 snprintf(buf, len,
"%.*f", decimals, v);
Line chart with one or more series, a vertical value scale with round-numbered grid lines,...
Definition: LineChart.h:66
void addValue(int index, float value, size_t maxPoints=0)
Definition: LineChart.h:108
LineChart(Bounds bounds, const char *title=nullptr)
Definition: LineChart.h:69
void clearXAxis()
Removes the X axis labels.
Definition: LineChart.h:163
void clearYRange()
Goes back to an automatic Y range.
Definition: LineChart.h:132
void setValues(int index, const std::vector< float > &values)
Replaces the values of the series index.
Definition: LineChart.h:101
bool card
Definition: LineChart.h:76
ChartSeries< RGB_T > & series(int index)
Definition: LineChart.h:98
void setGridValues(const std::vector< float > &values)
Uses exactly these grid values (labelled) instead of the automatic ones.
Definition: LineChart.h:137
int addReferenceLine(float value, RGB_T color, bool dashed=true)
Adds a horizontal reference line and returns its index.
Definition: LineChart.h:115
void setFooter(const char *footer)
One line of text below the plot, e.g. a summary of the values.
Definition: LineChart.h:80
void setSeriesLabel(int index, const char *label)
Definition: LineChart.h:95
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: LineChart.h:165
void setXAxis(float first, float last, int ticks=3, AxisFormatter format=nullptr)
Definition: LineChart.h:155
void setSeriesDashed(int index, bool dashed)
Draw the series dashed.
Definition: LineChart.h:97
void setValues(int index, const float *values, size_t count)
Definition: LineChart.h:102
void setTitle(const char *title)
Definition: LineChart.h:78
int seriesCount() const
Definition: LineChart.h:91
void setVisiblePoints(int count)
Definition: LineChart.h:141
void setGridCount(int count)
Approximate number of grid lines of the automatic scale (default 4).
Definition: LineChart.h:135
int addSeries(RGB_T color, uint8_t width=2)
Adds a series and returns its index.
Definition: LineChart.h:83
void setReferenceLine(int index, float value)
Definition: LineChart.h:123
void setYRange(float minValue, float maxValue)
Fixes the value range of the Y axis.
Definition: LineChart.h:126
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t bottom() const
Definition: Bounds.h:31
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
A horizontal line at a fixed value, e.g. a threshold or a target.
Definition: LineChart.h:34
float value
Definition: LineChart.h:35
bool dashed
Definition: LineChart.h:37
RGB_T color
Definition: LineChart.h:36
One data series of a LineChart.
Definition: LineChart.h:19
uint8_t width
Line width in pixels.
Definition: LineChart.h:24
std::string label
Name in the legend; series without a label are not listed.
Definition: LineChart.h:29
bool dashed
Draw the line dashed (e.g. to tell apart series of similar colors).
Definition: LineChart.h:27
RGB_T color
Definition: LineChart.h:22
std::vector< float > values
The values, oldest/leftmost first. NaN values break the line.
Definition: LineChart.h:21
bool visible
Definition: LineChart.h:25
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50