gtk / cairo-chart

SSH Git

To clone this repository:

git clone git@git.backbone.ws:gtk/cairo-chart.git

To push to this repository:

# Add a new remote
git remote add origin git@git.backbone.ws:gtk/cairo-chart.git

# Push the master branch to the newly added origin, and configure
# this remote and branch as the default:
git push -u origin master

# From now on you can push master to the "origin" remote with:
git push

Commit: #912446f

Parent: #d114385

Math.vala created #1

avatar Kolan Sh 7 days ago

Summary

216 216 context.stroke();
217 217 }
218 218
219 protected virtual Float128 calc_round_step (Float128 aver_step, bool date_time = false) {
220 Float128 step = 1.0;
221
222 if (aver_step > 1.0) {
223 if (date_time) while (step < aver_step) step *= 60;
224 if (date_time) while (step < aver_step) step *= 60;
225 if (date_time) while (step < aver_step) step *= 24;
226 while (step < aver_step) step *= 10;
227 if (step / 5 > aver_step) step /= 5;
228 while (step / 2 > aver_step) step /= 2;
229 } else if (aver_step > 0) {
230 while (step / 10 > aver_step) step /= 10;
231 if (step / 5 > aver_step) step /= 5;
232 while (step / 2 > aver_step) step /= 2;
233 }
234
235 return step;
236 }
237
238 219 public double plot_x_min = 0;
239 220 public double plot_x_max = 0;
240 221 public double plot_y_min = 0;
225 225 public bool joint_y { get; protected set; default = false; }
226 226 public Color joint_axis_color = Color (0, 0, 0, 1);
227 227
228 bool are_intersect (double a_min, double a_max, double b_min, double b_max) {
229 if ( a_min < a_max <= b_min < b_max
230 || b_min < b_max <= a_min < a_max)
231 return false;
232 return true;
233 }
234
235 228 protected virtual void set_vertical_axes_titles () {
236 229 for (var si = 0; si < series.length; ++si) {
237 230 var s = series[si];
279 279 for (int sk = si; sk > sj; --sk) {
280 280 var s3 = series[sk];
281 281 if (!s3.zoom_show) continue;
282 if (are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max)
282 if (math.are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max)
283 283 || s2.axis_x.position != s3.axis_x.position
284 284 || s2.axis_x.type != s3.axis_x.type) {
285 285 has_intersection = true;
333 333 for (int sk = si; sk > sj; --sk) {
334 334 var s3 = series[sk];
335 335 if (!s3.zoom_show) continue;
336 if (are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max)
336 if (math.are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max)
337 337 || s2.axis_y.position != s3.axis_y.position
338 338 || s2.axis_x.type != s3.axis_x.type) {
339 339 has_intersection = true;
370 370 }
371 371 }
372 372
373 bool point_belong (Float128 p, Float128 a, Float128 b) {
374 if (a > b) { Float128 tmp = a; a = b; b = tmp; }
375 if (a <= p <= b) return true;
376 return false;
377 }
378
379 373 protected virtual double compact_rec_x_pos (Series s, Float128 x, Text text) {
380 374 var sz = text.get_size(context);
381 375 return get_scr_x(s, x) - sz.width / 2.0
382 382 + sz.height * (y - (s.axis_y.zoom_min + s.axis_y.zoom_max) / 2.0) / (s.axis_y.zoom_max - s.axis_y.zoom_min);
383 383 }
384 384
385 protected CairoChart.Math math = new Math();
386
385 387 protected virtual void draw_horizontal_axis () {
386 388 for (var si = series.length - 1, nskip = 0; si >=0; --si) {
387 389 var s = series[si];
398 398 long max_nrecs = (long) ((plot_x_max - plot_x_min) * (s.place.zoom_x_max - s.place.zoom_x_min) / max_rec_width);
399 399
400 400 // 3. Calculate grid step.
401 Float128 step = calc_round_step ((s.axis_x.zoom_max - s.axis_x.zoom_min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME);
401 Float128 step = math.calc_round_step ((s.axis_x.zoom_max - s.axis_x.zoom_min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME);
402 402 if (step > s.axis_x.zoom_max - s.axis_x.zoom_min)
403 403 step = s.axis_x.zoom_max - s.axis_x.zoom_min;
404 404
439 439 }
440 440
441 441 // 5. Draw records, update cur_{x,y}_{min,max}.
442 for (Float128 x = x_min, x_max = s.axis_x.zoom_max; point_belong (x, x_min, x_max); x += step) {
442 for (Float128 x = x_min, x_max = s.axis_x.zoom_max; math.point_belong (x, x_min, x_max); x += step) {
443 443 if (joint_x) set_source_rgba(joint_axis_color);
444 444 else set_source_rgba(s.axis_x.color);
445 445 string text = "", time_text = "";
521 521 for (int sk = si; sk > sj; --sk) {
522 522 var s3 = series[sk];
523 523 if (!s3.zoom_show) continue;
524 if (are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max)
524 if (math.are_intersect(s2.place.zoom_x_min, s2.place.zoom_x_max, s3.place.zoom_x_min, s3.place.zoom_x_max)
525 525 || s2.axis_x.position != s3.axis_x.position
526 526 || s2.axis_x.type != s3.axis_x.type) {
527 527 has_intersection = true;
563 563 long max_nrecs = (long) ((plot_y_max - plot_y_min) * (s.place.zoom_y_max - s.place.zoom_y_min) / max_rec_height);
564 564
565 565 // 3. Calculate grid step.
566 Float128 step = calc_round_step ((s.axis_y.zoom_max - s.axis_y.zoom_min) / max_nrecs);
566 Float128 step = math.calc_round_step ((s.axis_y.zoom_max - s.axis_y.zoom_min) / max_nrecs);
567 567 if (step > s.axis_y.zoom_max - s.axis_y.zoom_min)
568 568 step = s.axis_y.zoom_max - s.axis_y.zoom_min;
569 569
608 608 }
609 609
610 610 // 5. Draw records, update cur_{x,y}_{min,max}.
611 for (Float128 y = y_min, y_max = s.axis_y.zoom_max; point_belong (y, y_min, y_max); y += step) {
611 for (Float128 y = y_min, y_max = s.axis_y.zoom_max; math.point_belong (y, y_min, y_max); y += step) {
612 612 if (joint_y) set_source_rgba(joint_axis_color);
613 613 else set_source_rgba(s.axis_y.color);
614 614 var text = s.axis_y.format.printf((LongDouble)y);
660 660 for (int sk = si; sk > sj; --sk) {
661 661 var s3 = series[sk];
662 662 if (!s3.zoom_show) continue;
663 if (are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max)
663 if (math.are_intersect(s2.place.zoom_y_min, s2.place.zoom_y_max, s3.place.zoom_y_min, s3.place.zoom_y_max)
664 664 || s2.axis_y.position != s3.axis_y.position) {
665 665 has_intersection = true;
666 666 break;
33 33 break;
34 34
35 35 case Type.CIRCLE:
36 chart.context.arc (x, y, size / 2, 0, 2*Math.PI);
36 chart.context.arc (x, y, size / 2, 0, 2 * GLib.Math.PI);
37 37 chart.context.fill();
38 38 break;
39 39
52 52 break;
53 53
54 54 case Type.PRICLE_CIRCLE:
55 chart.context.arc (x, y, size / 2, 0, 2*Math.PI);
55 chart.context.arc (x, y, size / 2, 0, 2 * GLib.Math.PI);
56 56 chart.context.stroke();
57 57 break;
58 58
1 namespace CairoChart {
2
3 public class Math {
4
5 public virtual Float128 calc_round_step (Float128 aver_step, bool date_time = false) {
6 Float128 step = 1.0;
7
8 if (aver_step > 1.0) {
9 if (date_time) while (step < aver_step) step *= 60;
10 if (date_time) while (step < aver_step) step *= 60;
11 if (date_time) while (step < aver_step) step *= 24;
12 while (step < aver_step) step *= 10;
13 if (step / 5 > aver_step) step /= 5;
14 while (step / 2 > aver_step) step /= 2;
15 } else if (aver_step > 0) {
16 while (step / 10 > aver_step) step /= 10;
17 if (step / 5 > aver_step) step /= 5;
18 while (step / 2 > aver_step) step /= 2;
19 }
20
21 return step;
22 }
23
24 public bool are_intersect (double a_min, double a_max, double b_min, double b_max) {
25 if ( a_min < a_max <= b_min < b_max
26 || b_min < b_max <= a_min < a_max)
27 return false;
28 return true;
29 }
30
31 public bool point_belong (Float128 p, Float128 a, Float128 b) {
32 if (a > b) { Float128 tmp = a; a = b; b = tmp; }
33 if (a <= p <= b) return true;
34 return false;
35 }
36
37 public Math () {}
38 }
39 }
58 58 style.weight);
59 59 context.set_font_size(style.size);
60 60 if (style.orientation == Font.Orientation.VERTICAL) {
61 context.rotate(- Math.PI / 2.0);
61 context.rotate(- GLib.Math.PI / 2.0);
62 62 context.show_text(text);
63 context.rotate(Math.PI / 2.0);
63 context.rotate(GLib.Math.PI / 2.0);
64 64 } else {
65 65 context.show_text(text);
66 66 }

Comments