-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfastchart_line.c
More file actions
269 lines (244 loc) · 10.8 KB
/
Copy pathfastchart_line.c
File metadata and controls
269 lines (244 loc) · 10.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
+----------------------------------------------------------------------+
| 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 "php.h"
#include "Zend/zend_exceptions.h"
#include "php_fastchart.h"
#include "fastchart_palette.h"
#include "fastchart_target.h"
#include "fastchart_axis.h"
int fastchart_line_render_to_target(fastchart_line_obj *self, fastchart_target_t *t)
{
if (self->n_series == 0) {
zend_throw_error(NULL,
"FastChart\\LineChart::draw() requires setSeries() to have been called with non-empty data");
return -1;
}
fastchart_series_t *series = self->series;
int n_series = self->n_series;
int max_len = self->max_len;
/* Compute left + right Y ranges from typed values[]. With
* secondary_y off, all series go to the left axis regardless of
* the per-series right_axis flag. */
int n_right = 0;
if (self->secondary_y) {
for (int s = 0; s < n_series; s++) {
if (series[s].right_axis) n_right++;
}
}
double dmin_l = 0, dmax_l = 0, dmin_r = 0, dmax_r = 0;
int seen_l = 0, seen_r = 0;
for (int s = 0; s < n_series; s++) {
bool right = self->secondary_y && series[s].right_axis;
for (int i = 0; i < series[s].len; i++) {
double d = series[s].values[i];
if (isnan(d)) continue;
if (right) {
fastchart_range_update(d, &dmin_r, &dmax_r, &seen_r);
} else {
fastchart_range_update(d, &dmin_l, &dmax_l, &seen_l);
}
}
}
fastchart_obj *base = (fastchart_obj *)self;
for (int o = 0; o < base->n_combo_overlays; o++) {
const fastchart_combo_overlay *ov = &base->combo_overlays[o];
bool right = self->secondary_y && ov->right_axis;
if (right) n_right++;
int lim = ov->n < max_len ? ov->n : max_len;
for (int i = 0; i < lim; i++) {
double d = ov->values[i];
if (!isfinite(d)) continue;
if (right) {
fastchart_range_update(d, &dmin_r, &dmax_r, &seen_r);
} else {
fastchart_range_update(d, &dmin_l, &dmax_l, &seen_l);
}
}
}
if (self->err_lo && self->err_n > 0 && n_series > 0) {
bool right = self->secondary_y && series[0].right_axis;
double *dmin = right ? &dmin_r : &dmin_l;
double *dmax = right ? &dmax_r : &dmax_l;
int lim = series[0].len < self->err_n
? series[0].len : self->err_n;
for (int i = 0; i < lim; i++) {
double value = series[0].values[i];
if (isnan(value)) continue;
double lo = self->err_lo[i];
double hi = self->err_hi[i];
if (!isnan(lo)) {
double edge = value - lo;
if (isfinite(edge)
&& (self->y_axis_scale != FASTCHART_SCALE_LOG || edge > 0.0)
&& edge < *dmin) {
*dmin = edge;
}
}
if (!isnan(hi)) {
double edge = value + hi;
if (isfinite(edge) && edge > *dmax) *dmax = edge;
}
}
}
if (!seen_l) {
zend_throw_error(NULL,
"FastChart\\LineChart::draw() found no numeric values for the primary Y axis");
return -1;
}
if (n_right > 0 && !seen_r) {
zend_throw_error(NULL,
"FastChart\\LineChart::draw() found no numeric values for the secondary Y axis");
return -1;
}
fastchart_value_range range_l, range_r;
if (self->y_axis_scale == FASTCHART_SCALE_LOG) {
if (fastchart_value_range_compute_log(dmin_l, dmax_l, &range_l) != 0) {
zend_value_error("FastChart\\LineChart::draw(): log Y-axis requires strictly-positive data");
return -1;
}
} else {
fastchart_value_range_compute(dmin_l, dmax_l, 6, &range_l);
if (fastchart_value_range_apply_override((fastchart_obj *)self,
&range_l) != 0) {
return -1;
}
}
if (n_right > 0) {
if (self->y_axis_scale == FASTCHART_SCALE_LOG) {
/* Match the primary axis and AreaChart: a log chart maps the
* secondary series on a log scale too, rejecting non-positive
* data rather than silently falling back to linear. */
if (dmin_r <= 0 ||
fastchart_value_range_compute_log(dmin_r, dmax_r, &range_r) != 0) {
zend_value_error("FastChart\\LineChart::draw(): log Y-axis requires strictly-positive data");
return -1;
}
} else {
fastchart_value_range_compute(dmin_r, dmax_r, 6, &range_r);
}
}
fastchart_rect plot;
fastchart_palette pal;
fastchart_render_cartesian_setup((fastchart_obj *)self, t,
1, 1, NULL, 0, &plot, &pal);
fastchart_draw_y_axis(t, (fastchart_obj *)self, &plot, &pal, &range_l);
if (n_right > 0) {
fastchart_draw_y_axis_right(t, (fastchart_obj *)self, &plot, &pal, &range_r);
}
fastchart_draw_plot_bands(t, (fastchart_obj *)self, &plot, &range_l, &pal);
fastchart_draw_v_plot_bands_categorical(t, (fastchart_obj *)self, &plot,
max_len, &pal);
const char **label_ptrs = fastchart_borrow_category_labels((fastchart_obj *)self, max_len);
fastchart_draw_x_axis_categorical(t, (fastchart_obj *)self, &plot, &pal, max_len, label_ptrs);
if (label_ptrs) efree((void *)label_ptrs);
fastchart_draw_axis_titles(t, (fastchart_obj *)self, &plot, &pal);
int marker_style = self->marker_style >= 0
? (int)self->marker_style
: FASTCHART_MARKER_CIRCLE;
int marker_size = self->marker_size >= 1
? (int)self->marker_size
: 6;
double *err_lo = self->err_lo;
double *err_hi = self->err_hi;
int err_n = self->err_n;
fastchart_pt pts[FASTCHART_MAX_POINTS_PER_SERIES];
for (int s = 0; s < n_series; s++) {
int color = pal.series[s % FASTCHART_PALETTE_SERIES_N];
bool right = self->secondary_y && series[s].right_axis;
const fastchart_value_range *rng = right ? &range_r : &range_l;
double *values = series[s].values;
int n = series[s].len;
if (n < 1) continue;
for (int i = 0; i < n; i++) {
pts[i].x = fastchart_x_categorical_center(&plot, i, max_len);
if (isnan(values[i])) {
pts[i].valid = false;
} else {
pts[i].y = fastchart_y_to_pixel(values[i], rng, &plot);
pts[i].valid = true;
}
}
/* Error bars attach to the first series only -- multi-series
* line charts get crowded fast otherwise. */
if (err_lo && err_n > 0 && s == 0) {
int lim = n < err_n ? n : err_n;
for (int i = 0; i < lim; i++) {
if (!pts[i].valid) continue;
double lo = err_lo[i];
double hi = err_hi[i];
if (isnan(lo) && isnan(hi)) continue;
if (isnan(lo)) lo = 0;
if (isnan(hi)) hi = 0;
if (lo > 0 || hi > 0) {
int py_lo = fastchart_y_to_pixel(values[i] - lo, rng, &plot);
int py_hi = fastchart_y_to_pixel(values[i] + hi, rng, &plot);
int x = pts[i].x;
fastchart_target_line(t, x, py_hi, x, py_lo,
pal.axis, 1, FASTCHART_DASH_SOLID);
fastchart_target_line(t, x - 4, py_hi, x + 4, py_hi,
pal.axis, 1, FASTCHART_DASH_SOLID);
fastchart_target_line(t, x - 4, py_lo, x + 4, py_lo,
pal.axis, 1, FASTCHART_DASH_SOLID);
}
}
}
fastchart_draw_polyline(t, (fastchart_obj *)self, pts, n, color, 2, true);
for (int i = 0; i < n; i++) {
if (!pts[i].valid) continue;
/* marker_color is a target handle so it flows into
* fastchart_draw_marker unchanged. Per-point RGB overrides
* go through the target's dedup table rather than bypassing
* the target layer. */
int marker_color = color;
if (series[s].point_colors) {
zend_long c = series[s].point_colors[i];
if (c >= 0) {
marker_color = fastchart_target_color_rgb(t, (int)c);
}
}
fastchart_draw_marker(t, pts[i].x, pts[i].y,
marker_style, marker_size, marker_color);
fastchart_draw_value_label(t, (fastchart_obj *)self, &pal,
pts[i].x, pts[i].y, values[i]);
}
}
fastchart_draw_overlays_categorical(t, (fastchart_obj *)self, &plot, &pal,
&range_l,
n_right > 0 ? &range_r : NULL,
max_len);
fastchart_draw_h_annotations(t, (fastchart_obj *)self, &plot, &pal, &range_l);
fastchart_draw_v_annotations_categorical(t, (fastchart_obj *)self, &plot, &pal, max_len);
const char *legend_labels[FASTCHART_MAX_SERIES];
for (int s = 0; s < n_series; s++) legend_labels[s] = series[s].label;
fastchart_draw_series_legend(t, (fastchart_obj *)self, &plot, &pal,
n_series, legend_labels);
fastchart_draw_text_annotations(t, (fastchart_obj *)self, &pal);
/* IconPlot overlays at data coordinates. x is treated as a
* fractional category index (0 = first category, max_len-1 = last);
* out-of-range values still render but get clipped to the plot
* edges. y is data-y on the left axis. */
if (self->icons && self->n_icons > 0 && max_len > 0) {
for (int i = 0; i < self->n_icons; i++) {
const fastchart_icon *ic = &self->icons[i];
double frac_x = max_len > 1
? (ic->x + 0.5) / (double)max_len
: 0.5;
int px = fastchart_frac_to_px(frac_x, plot.x0, plot.x1);
int py = fastchart_y_to_pixel(ic->y, &range_l, &plot);
fastchart_blit_icon(t, ic, px, py);
}
}
return 0;
}