-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfastchart_linear_meter.c
More file actions
197 lines (180 loc) · 8.26 KB
/
Copy pathfastchart_linear_meter.c
File metadata and controls
197 lines (180 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
+----------------------------------------------------------------------+
| Copyright (c) 2025-2026, Ilia Alshanetsky |
| Copyright (c) 2025-2026, Advanced Internet Designs Inc. |
+----------------------------------------------------------------------+
| This source file is subject to the BSD 3-Clause license that is |
| bundled with this package in the file LICENSE. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <ilia@ilia.ws> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include "php.h"
#include "Zend/zend_exceptions.h"
#include "php_fastchart.h"
#include "fastchart_palette.h"
#include "fastchart_target.h"
#include "fastchart_axis.h"
#include "fastchart_text.h"
int fastchart_linear_meter_render_to_target(fastchart_linear_meter_obj *self, fastchart_target_t *t)
{
fastchart_palette pal;
fastchart_palette_init(t, (int)self->theme, &pal);
fastchart_palette_apply_overrides(t, (fastchart_obj *)self, &pal);
int W, H;
fastchart_target_get_dims(t, &W, &H);
fastchart_paint_canvas_bg(t, (fastchart_obj *)self, &pal);
/* Title reservation (optional). */
int top_pad = 12;
int title_h = 0;
const char *title_font = fastchart_resolve_font((fastchart_obj *)self, FC_FONT_TITLE);
double base_size = self->font_size > 0 ? self->font_size : FASTCHART_DEFAULT_FONT_SIZE;
double title_size = fastchart_resolve_font_size((fastchart_obj *)self, FC_FONT_TITLE, base_size * 1.4);
if (self->title && ZSTR_LEN(self->title) > 0 && title_font) {
if (fastchart_text_measure(t, title_font, title_size, ZSTR_VAL(self->title),
NULL, &title_h, NULL, 0) == 0) {
top_pad += title_h + 10;
}
}
double mn = self->meter_min;
double mx = self->meter_max;
double v = self->meter_value;
if (mx <= mn) mx = mn + 1.0;
if (v < mn) v = mn;
if (v > mx) v = mx;
/* Layout: orientation determines the long axis. The bar runs
* from min->max along the long axis, with zones painted into
* matching ranges. A small padding keeps the bar visually inset
* from the canvas edges. */
int bar_x0, bar_y0, bar_x1, bar_y1;
int label_size_min = (int)(base_size * 1.2);
if (self->meter_orientation == FASTCHART_METER_HORIZONTAL) {
int margin_x = 60; /* room for min/max value labels */
int margin_y = label_size_min * 2;
bar_x0 = margin_x;
bar_x1 = W - margin_x;
bar_y0 = top_pad + (H - top_pad - margin_y * 2) / 3;
bar_y1 = bar_y0 + (H - top_pad - margin_y * 2) / 3;
if (bar_y1 - bar_y0 < 12) bar_y1 = bar_y0 + 12;
} else {
int margin_x = label_size_min * 4;
int margin_y = 30;
bar_y0 = top_pad + margin_y;
bar_y1 = H - margin_y;
bar_x0 = (W - margin_x) / 2 - 12;
bar_x1 = bar_x0 + 24;
}
fastchart_apply_plot_rect((fastchart_obj *)self,
&bar_x0, &bar_y0, &bar_x1, &bar_y1);
/* Background fill (the inactive part of the bar). */
fastchart_target_rect(t, bar_x0, bar_y0,
bar_x1 - bar_x0 + 1, bar_y1 - bar_y0 + 1,
pal.grid, 1, 0);
/* Zone fills along the bar long axis. */
for (int i = 0; i < self->n_zones; i++) {
const fastchart_gauge_zone *zn = &self->zones[i];
double t0 = (zn->from - mn) / (mx - mn);
double t1 = (zn->to - mn) / (mx - mn);
if (t0 < 0) t0 = 0;
if (t0 > 1) t0 = 1;
if (t1 < 0) t1 = 0;
if (t1 > 1) t1 = 1;
if (t1 <= t0) continue;
int color;
if (zn->color_rgb >= 0) {
color = fastchart_target_color_rgb(t, zn->color_rgb);
} else {
color = pal.series[i % FASTCHART_PALETTE_SERIES_N];
}
if (self->meter_orientation == FASTCHART_METER_HORIZONTAL) {
int zx0 = bar_x0 + (int)(t0 * (bar_x1 - bar_x0));
int zx1 = bar_x0 + (int)(t1 * (bar_x1 - bar_x0));
fastchart_target_rect(t, zx0, bar_y0,
zx1 - zx0 + 1, bar_y1 - bar_y0 + 1,
color, 1, 0);
} else {
/* Y inverts: t=0 sits at the bottom (mn), t=1 at the top. */
int zy1 = bar_y1 - (int)(t0 * (bar_y1 - bar_y0));
int zy0 = bar_y1 - (int)(t1 * (bar_y1 - bar_y0));
fastchart_target_rect(t, bar_x0, zy0,
bar_x1 - bar_x0 + 1, zy1 - zy0 + 1,
color, 1, 0);
}
}
fastchart_target_rect(t, bar_x0, bar_y0,
bar_x1 - bar_x0 + 1, bar_y1 - bar_y0 + 1,
pal.border, 0, 1);
/* Pointer: a thick line + filled triangle at the value. */
double frac = (v - mn) / (mx - mn);
if (self->meter_orientation == FASTCHART_METER_HORIZONTAL) {
int px = bar_x0 + (int)(frac * (bar_x1 - bar_x0));
fastchart_target_line(t, px, bar_y0 - 4, px, bar_y1 + 4,
pal.text, 2, FASTCHART_DASH_SOLID);
fastchart_point_t tri[3] = {
{ px - 6, bar_y0 - 10 },
{ px + 6, bar_y0 - 10 },
{ px, bar_y0 - 2 },
};
fastchart_target_polygon(t, tri, 3, pal.text, 1, 0);
} else {
int py = bar_y1 - (int)(frac * (bar_y1 - bar_y0));
fastchart_target_line(t, bar_x0 - 4, py, bar_x1 + 4, py,
pal.text, 2, FASTCHART_DASH_SOLID);
fastchart_point_t tri[3] = {
{ bar_x1 + 10, py - 6 },
{ bar_x1 + 10, py + 6 },
{ bar_x1 + 2, py },
};
fastchart_target_polygon(t, tri, 3, pal.text, 1, 0);
}
/* Min / max / value labels. */
const char *font = fastchart_resolve_font((fastchart_obj *)self, FC_FONT_LABEL);
double size = fastchart_resolve_font_size((fastchart_obj *)self, FC_FONT_LABEL, base_size);
if (font) {
const char *fmt = self->meter_value_format
? ZSTR_VAL(self->meter_value_format) : "%.0f";
char *min_buf = fastchart_format_double_label(fmt, mn);
char *max_buf = fastchart_format_double_label(fmt, mx);
char *val_buf = fastchart_format_double_label(fmt, v);
if (self->meter_orientation == FASTCHART_METER_HORIZONTAL) {
int label_y = bar_y1 + (int)(size * 1.5);
fastchart_text_draw(t, font, size, pal.text,
bar_x0, label_y, FASTCHART_ALIGN_LEFT,
min_buf, NULL, 0);
fastchart_text_draw(t, font, size, pal.text,
bar_x1, label_y, FASTCHART_ALIGN_RIGHT,
max_buf, NULL, 0);
int px = bar_x0 + (int)(frac * (bar_x1 - bar_x0));
fastchart_text_draw(t, font, size * 1.1, pal.text,
px, bar_y0 - 16, FASTCHART_ALIGN_CENTER,
val_buf, NULL, 0);
} else {
int label_x = bar_x0 - 8;
fastchart_text_draw(t, font, size, pal.text,
label_x, bar_y1 + (int)(size * 0.4),
FASTCHART_ALIGN_RIGHT, min_buf, NULL, 0);
fastchart_text_draw(t, font, size, pal.text,
label_x, bar_y0 + (int)(size * 0.4),
FASTCHART_ALIGN_RIGHT, max_buf, NULL, 0);
int py = bar_y1 - (int)(frac * (bar_y1 - bar_y0));
fastchart_text_draw(t, font, size * 1.1, pal.text,
bar_x1 + 30, py + (int)(size * 0.4),
FASTCHART_ALIGN_LEFT, val_buf, NULL, 0);
}
efree(min_buf);
efree(max_buf);
efree(val_buf);
}
if (self->title && ZSTR_LEN(self->title) > 0 && title_font && title_h > 0) {
fastchart_text_draw(t, title_font, title_size, pal.text,
W / 2, 12 + title_h, FASTCHART_ALIGN_CENTER,
ZSTR_VAL(self->title), NULL, 0);
}
fastchart_draw_text_annotations(t, (fastchart_obj *)self, &pal);
return 0;
}