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

Diffs from 76fb9d8 to d114385

Commits

avatar Kolan Sh In progress... c0ca87a 7 days ago
avatar Kolan Sh calc_axis_rec_sizes() -> Axis.vala d114385 7 days ago

Summary

  • src/Axis.vala (46) ++++++++++++++++++++++++++++++++++++++++++++++
  • src/Chart.vala (76) --------------------------------------------------------------++++++++++++++
93 93 axis.scale_type = this.scale_type;
94 94 axis.title = this.title.copy();
95 95 axis.type = this.type;
96 axis.nrecords = this.nrecords;
96 97 return axis;
97 98 }
98 99
99 100 public Axis () {}
101
102 public int nrecords = 128;
103
104 public virtual void format_date_time (Float128 x, out string date, out string time) {
105 date = time = "";
106 var dt = new DateTime.from_unix_utc((int64)x);
107 date = dt.format(date_format);
108 var dsec_str =
109 ("%."+(dsec_signs.to_string())+"Lf").printf((LongDouble)(x - (int64)x)).offset(1);
110 time = dt.format(time_format) + dsec_str;
111 }
112
113 public virtual void calc_rec_sizes (Chart chart, out double max_rec_width, out double max_rec_height, bool is_horizontal = true) {
114 max_rec_width = max_rec_height = 0;
115 for (var i = 0; i < nrecords; ++i) {
116 Float128 x = (int64)(zoom_min + (zoom_max - zoom_min) / nrecords * i) + 1.0/3.0;
117 switch (type) {
118 case Axis.Type.NUMBERS:
119 var text = new Text (format.printf((LongDouble)x) + (is_horizontal ? "_" : ""), font_style);
120 var sz = text.get_size(chart.context);
121 max_rec_width = double.max (max_rec_width, sz.width);
122 max_rec_height = double.max (max_rec_height, sz.height);
123 break;
124 case Axis.Type.DATE_TIME:
125 string date, time;
126 format_date_time(x, out date, out time);
127
128 var h = 0.0;
129 if (date_format != "") {
130 var text = new Text (date + (is_horizontal ? "_" : ""), font_style);
131 var sz = text.get_size(chart.context);
132 max_rec_width = double.max (max_rec_width, sz.width);
133 h = sz.height;
134 }
135 if (time_format != "") {
136 var text = new Text (time + (is_horizontal ? "_" : ""), font_style);
137 var sz = text.get_size(chart.context);
138 max_rec_width = double.max (max_rec_width, sz.width);
139 h += sz.height;
140 }
141 max_rec_height = double.max (max_rec_height, h);
142 break;
143 }
144 }
145 }
100 146 }
101 147 }
177 177 delta_y /= plot_y_max - plot_y_min; delta_y *= - 1.0;
178 178 var rzxmin = rz_x_min, rzxmax = rz_x_max, rzymin = rz_y_min, rzymax = rz_y_max;
179 179 zoom_out();
180 //draw(); // TODO: optimize here
181 180 delta_x *= plot_x_max - plot_x_min;
182 181 delta_y *= plot_y_max - plot_y_min;
183 182 var xmin = plot_x_min + (plot_x_max - plot_x_min) * rzxmin;
192 192 if (ymax + delta_y > plot_y_max) delta_y = plot_y_max - ymax;
193 193
194 194 zoom_in (xmin + delta_x, ymin + delta_y, xmax + delta_x, ymax + delta_y);
195 //draw(); // TODO: optimize here
196 195 }
197 196
198 197 public double title_width { get; protected set; default = 0.0; }
216 216 context.stroke();
217 217 }
218 218
219 protected int axis_rec_npoints = 128;
220
221 protected virtual void calc_axis_rec_sizes (Axis axis, out double max_rec_width, out double max_rec_height, bool is_horizontal = true) {
222 max_rec_width = max_rec_height = 0;
223 for (var i = 0; i < axis_rec_npoints; ++i) {
224 Float128 x = (int64)(axis.zoom_min + (axis.zoom_max - axis.zoom_min) / axis_rec_npoints * i) + 1.0/3.0;
225 switch (axis.type) {
226 case Axis.Type.NUMBERS:
227 var text = new Text (axis.format.printf((LongDouble)x) + (is_horizontal ? "_" : ""), axis.font_style);
228 var sz = text.get_size(context);
229 max_rec_width = double.max (max_rec_width, sz.width);
230 max_rec_height = double.max (max_rec_height, sz.height);
231 break;
232 case Axis.Type.DATE_TIME:
233 string date, time;
234 format_date_time(axis, x, out date, out time);
235
236 var h = 0.0;
237 if (axis.date_format != "") {
238 var text = new Text (date + (is_horizontal ? "_" : ""), axis.font_style);
239 var sz = text.get_size(context);
240 max_rec_width = double.max (max_rec_width, sz.width);
241 h = sz.height;
242 }
243 if (axis.time_format != "") {
244 var text = new Text (time + (is_horizontal ? "_" : ""), axis.font_style);
245 var sz = text.get_size(context);
246 max_rec_width = double.max (max_rec_width, sz.width);
247 h += sz.height;
248 }
249 max_rec_height = double.max (max_rec_height, h);
250 break;
251 }
252 }
253 }
254
255 219 protected virtual Float128 calc_round_step (Float128 aver_step, bool date_time = false) {
256 220 Float128 step = 1.0;
257 221
293 293 if (!s.zoom_show) continue;
294 294 if (nskip != 0) {--nskip; continue;}
295 295 double max_rec_width = 0; double max_rec_height = 0;
296 calc_axis_rec_sizes (s.axis_x, out max_rec_width, out max_rec_height, true);
296 s.axis_x.calc_rec_sizes (this, out max_rec_width, out max_rec_height, true);
297 297 var max_font_indent = s.axis_x.font_indent;
298 298 var max_axis_font_height = s.axis_x.title.text == "" ? 0 : s.axis_x.title.get_height(context) + s.axis_x.font_indent;
299 299
314 314 }
315 315 if (!has_intersection) {
316 316 double tmp_max_rec_width = 0; double tmp_max_rec_height = 0;
317 calc_axis_rec_sizes (s2.axis_x, out tmp_max_rec_width, out tmp_max_rec_height, true);
317 s2.axis_x.calc_rec_sizes (this, out tmp_max_rec_width, out tmp_max_rec_height, true);
318 318 max_rec_width = double.max (max_rec_width, tmp_max_rec_width);
319 319 max_rec_height = double.max (max_rec_height, tmp_max_rec_height);
320 320 max_font_indent = double.max (max_font_indent, s2.axis_x.font_indent);
347 347 if (!s.zoom_show) continue;
348 348 if (nskip != 0) {--nskip; continue;}
349 349 double max_rec_width = 0; double max_rec_height = 0;
350 calc_axis_rec_sizes (s.axis_y, out max_rec_width, out max_rec_height, false);
350 s.axis_y.calc_rec_sizes (this, out max_rec_width, out max_rec_height, false);
351 351 var max_font_indent = s.axis_y.font_indent;
352 352 var max_axis_font_width = s.axis_y.title.text == "" ? 0 : s.axis_y.title.get_width(context) + s.axis_y.font_indent;
353 353
368 368 }
369 369 if (!has_intersection) {
370 370 double tmp_max_rec_width = 0; double tmp_max_rec_height = 0;
371 calc_axis_rec_sizes (s2.axis_y, out tmp_max_rec_width, out tmp_max_rec_height, false);
371 s2.axis_y.calc_rec_sizes (this, out tmp_max_rec_width, out tmp_max_rec_height, false);
372 372 max_rec_width = double.max (max_rec_width, tmp_max_rec_width);
373 373 max_rec_height = double.max (max_rec_height, tmp_max_rec_height);
374 374 max_font_indent = double.max (max_font_indent, s2.axis_y.font_indent);
414 414 + 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);
415 415 }
416 416
417 protected virtual void format_date_time (Axis axis, Float128 x, out string date, out string time) {
418 date = time = "";
419 var dt = new DateTime.from_unix_utc((int64)x);
420 date = dt.format(axis.date_format);
421 var dsec_str =
422 ("%."+(axis.dsec_signs.to_string())+"Lf").printf((LongDouble)(x - (int64)x)).offset(1);
423 time = dt.format(axis.time_format) + dsec_str;
424 }
425
426 417 protected virtual void draw_horizontal_axis () {
427 418 for (var si = series.length - 1, nskip = 0; si >=0; --si) {
428 419 var s = series[si];
429 420 if (!s.zoom_show) continue;
430 421 if (joint_x && si != zoom_first_show) continue;
431 422
432 // 1. Detect max record width/height by axis_rec_npoints equally selected points using format.
423 // 1. Detect max record width/height by axis.nrecords equally selected points using format.
433 424 double max_rec_width, max_rec_height;
434 calc_axis_rec_sizes (s.axis_x, out max_rec_width, out max_rec_height, true);
425 s.axis_x.calc_rec_sizes (this, out max_rec_width, out max_rec_height, true);
435 426
436 427 // 2. Calculate maximal available number of records, take into account the space width.
437 428 long max_nrecs = (long) ((plot_x_max - plot_x_min) * (s.place.zoom_x_max - s.place.zoom_x_min) / max_rec_width);
478 478 text = s.axis_x.format.printf((LongDouble)x);
479 479 break;
480 480 case Axis.Type.DATE_TIME:
481 format_date_time(s.axis_x, x, out text, out time_text);
481 s.axis_x.format_date_time(x, out text, out time_text);
482 482 break;
483 483 }
484 484 var scr_x = get_scr_x (s, x);
585 585 var s = series[si];
586 586 if (!s.zoom_show) continue;
587 587 if (joint_y && si != zoom_first_show) continue;
588 // 1. Detect max record width/height by axis_rec_npoints equally selected points using format.
588 // 1. Detect max record width/height by axis.nrecords equally selected points using format.
589 589 double max_rec_width, max_rec_height;
590 calc_axis_rec_sizes (s.axis_y, out max_rec_width, out max_rec_height, false);
590 s.axis_y.calc_rec_sizes (this, out max_rec_width, out max_rec_height, false);
591 591
592 592 // 2. Calculate maximal available number of records, take into account the space width.
593 593 long max_nrecs = (long) ((plot_y_max - plot_y_min) * (s.place.zoom_y_max - s.place.zoom_y_min) / max_rec_height);
1108 1108 cross_what_to_show(s, out show_x, out show_time, out show_date, out show_y);
1109 1109 size = Point ();
1110 1110 string date, time;
1111 format_date_time(s.axis_x, p.x, out date, out time);
1111 s.axis_x.format_date_time(p.x, out date, out time);
1112 1112 var date_t = new Text (date, s.axis_x.font_style, s.axis_x.color);
1113 1113 var time_t = new Text (time, s.axis_x.font_style, s.axis_x.color);
1114 1114 var x_t = new Text (s.axis_x.format.printf((LongDouble)p.x), s.axis_x.font_style, s.axis_x.color);
1188 1188 text = s.axis_x.format.printf((LongDouble)x);
1189 1189 break;
1190 1190 case Axis.Type.DATE_TIME:
1191 format_date_time(s.axis_x, x, out text, out time_text);
1191 s.axis_x.format_date_time(x, out text, out time_text);
1192 1192 break;
1193 1193 default:
1194 1194 break;
1287 1287 if (show_time) {
1288 1288 set_source_rgba(s.axis_x.color);
1289 1289 string date = "", time = "";
1290 format_date_time(s.axis_x, point.x, out date, out time);
1290 s.axis_x.format_date_time(point.x, out date, out time);
1291 1291 var text_t = new Text(time, s.axis_x.font_style);
1292 1292 var sz = text_t.get_size(context);
1293 1293 var y = svp.y + sz.height / 2;
1300 1300 if (show_date) {
1301 1301 set_source_rgba(s.axis_x.color);
1302 1302 string date = "", time = "";
1303 format_date_time(s.axis_x, point.x, out date, out time);
1303 s.axis_x.format_date_time(point.x, out date, out time);
1304 1304 var text_t = new Text(date, s.axis_x.font_style);
1305 1305 var sz = text_t.get_size(context);
1306 1306 var y = svp.y + sz.height / 2;
1368 1368 case Axis.Type.DATE_TIME:
1369 1369 var date = "", time = "";
1370 1370 int64 days = (int64)(delta / 24 / 3600);
1371 format_date_time(s.axis_x, delta, out date, out time);
1371 s.axis_x.format_date_time(delta, out date, out time);
1372 1372 str = days.to_string() + " + " + time;
1373 1373 break;
1374 1374 }
1381 1381 public Chart copy () {
1382 1382 var chart = new Chart ();
1383 1383 chart.active_cursor = this.active_cursor;
1384 chart.axis_rec_npoints = this.axis_rec_npoints;
1385 1384 chart.bg_color = this.bg_color;
1386 1385 chart.border_color = this.border_color;
1387 1386 chart.joint_x = this.joint_x;