From 2d4d01d532ca4e99ceacaeba40254af2a55d3aa4 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Sun, 21 Jan 2018 17:27:46 +0300 Subject: [PATCH] OK In progress... --- src/Axis.vala | 22 +++--- src/Chart.vala | 8 ++- src/Cursor.vala | 62 ++++++++-------- src/Legend.vala | 6 +- src/Series.vala | 63 ++++++++-------- src/Text.vala | 170 ++++++++++++++++++++++++++++++++------------ test/ChartTest.vala | 84 +++++++++++----------- 7 files changed, 253 insertions(+), 162 deletions(-) diff --git a/src/Axis.vala b/src/Axis.vala index 855f06d..d216879 100644 --- a/src/Axis.vala +++ b/src/Axis.vala @@ -4,7 +4,8 @@ namespace CairoChart { // or specify series name near the axis public class Axis { public Range range = new Range(); - public Text title = new Text (""); + Chart chart; + public Text title; public enum Type { NUMBERS = 0, DATE_TIME @@ -65,7 +66,7 @@ namespace CairoChart { public double font_spacing = 5; public virtual Axis copy () { - var axis = new Axis (); + var axis = new Axis (chart); axis._date_format = this._date_format; axis._dsec_signs = this._dsec_signs; axis._format = this._format; @@ -84,7 +85,10 @@ namespace CairoChart { return axis; } - public Axis () {} + public Axis (Chart chart) { + this.chart = chart; + title = new Text (chart, ""); + } public int nrecords = 128; @@ -103,8 +107,8 @@ namespace CairoChart { Float128 x = (int64)(range.zmin + range.zrange / nrecords * i) + 1.0/3.0; switch (type) { case Axis.Type.NUMBERS: - var text = new Text (format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style); - var sz = text.get_size(chart.ctx); + var text = new Text (chart, format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style); + var sz = text.size; max_rec_width = double.max (max_rec_width, sz.width); max_rec_height = double.max (max_rec_height, sz.height); break; @@ -114,14 +118,14 @@ namespace CairoChart { var h = 0.0; if (date_format != "") { - var text = new Text (date + (horizontal ? "_" : ""), font_style); - var sz = text.get_size(chart.ctx); + var text = new Text (chart, date + (horizontal ? "_" : ""), font_style); + var sz = text.size; max_rec_width = double.max (max_rec_width, sz.width); h = sz.height; } if (time_format != "") { - var text = new Text (time + (horizontal ? "_" : ""), font_style); - var sz = text.get_size(chart.ctx); + var text = new Text (chart, time + (horizontal ? "_" : ""), font_style); + var sz = text.size; max_rec_width = double.max (max_rec_width, sz.width); h += sz.height; } diff --git a/src/Chart.vala b/src/Chart.vala index 43ce1db..951b9a8 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -43,7 +43,7 @@ namespace CairoChart { /** * ``Chart`` Title. */ - public Text title = new Text("Cairo Chart"); + public Text title; /** * Legend. @@ -99,6 +99,8 @@ namespace CairoChart { */ public Chart () { cursors = new Cursors (this); + title = new Text(this, "Cairo Chart"); + } /** @@ -336,13 +338,13 @@ namespace CairoChart { ctx.stroke (); } protected virtual void draw_title () { - var sz = title.get_size(ctx); + var sz = title.size; var title_height = sz.height + title.vspacing * 2; evarea.y0 += title_height; evarea.height -= title_height; color = title.color; ctx.move_to (area.width/2 - sz.width/2, sz.height + title.vspacing); - title.show(ctx); + title.show(); } protected virtual void draw_haxes () { for (var si = series.length - 1, nskip = 0; si >=0; --si) diff --git a/src/Cursor.vala b/src/Cursor.vala index 88a3586..e7a51dc 100644 --- a/src/Cursor.vala +++ b/src/Cursor.vala @@ -209,15 +209,15 @@ namespace CairoChart { size = Point128 (); string date, time; s.axis_x.format_date_time(p.x, out date, out time); - var date_t = new Text (date, s.axis_x.font_style, s.axis_x.color); - var time_t = new Text (time, s.axis_x.font_style, s.axis_x.color); - var x_t = new Text (s.axis_x.format.printf((LongDouble)p.x), s.axis_x.font_style, s.axis_x.color); - var y_t = new Text (s.axis_y.format.printf((LongDouble)p.y), s.axis_y.font_style, s.axis_y.color); + var date_t = new Text(chart, date, s.axis_x.font_style, s.axis_x.color); + var time_t = new Text(chart, time, s.axis_x.font_style, s.axis_x.color); + var x_t = new Text(chart, s.axis_x.format.printf((LongDouble)p.x), s.axis_x.font_style, s.axis_x.color); + var y_t = new Text(chart, s.axis_y.format.printf((LongDouble)p.y), s.axis_y.font_style, s.axis_y.color); double h_x = 0.0, h_y = 0.0; - if (show_x) { var sz = x_t.get_size(chart.ctx); size.x = sz.width; h_x = sz.height; } - if (show_date) { var sz = date_t.get_size(chart.ctx); size.x = sz.width; h_x = sz.height; } - if (show_time) { var sz = time_t.get_size(chart.ctx); size.x = double.max(size.x, sz.width); h_x += sz.height; } - if (show_y) { var sz = y_t.get_size(chart.ctx); size.x += sz.width; h_y = sz.height; } + if (show_x) { var sz = x_t.size; size.x = sz.width; h_x = sz.height; } + if (show_date) { var sz = date_t.size; size.x = sz.width; h_x = sz.height; } + if (show_time) { var sz = time_t.size; size.x = double.max(size.x, sz.width); h_x += sz.height; } + if (show_y) { var sz = y_t.size; size.x += sz.width; h_y = sz.height; } if ((show_x || show_date || show_time) && show_y) size.x += double.max(s.axis_x.font_spacing, s.axis_y.font_spacing); if (show_date && show_time) h_x += s.axis_x.font_spacing; size.y = double.max (h_x, h_y); @@ -287,16 +287,16 @@ namespace CairoChart { case Axis.Type.NUMBERS: text = s.axis_x.format.printf((LongDouble)x); break; case Axis.Type.DATE_TIME: s.axis_x.format_date_time(x, out text, out time_text); break; } - var text_t = new Text(text, s.axis_x.font_style, s.axis_x.color); - var sz = text_t.get_size(chart.ctx); - var time_text_t = new Text(time_text, s.axis_x.font_style, s.axis_x.color); + var text_t = new Text(chart, text, s.axis_x.font_style, s.axis_x.color); + var sz = text_t.size; + var time_text_t = new Text(chart, time_text, s.axis_x.font_style, s.axis_x.color); var print_y = 0.0; switch (s.axis_x.position) { case Axis.Position.LOW: print_y = chart.area.y1 - s.axis_x.font_spacing - (chart.legend.position == Legend.Position.BOTTOM ? chart.legend.height : 0); break; case Axis.Position.HIGH: - var title_height = chart.title.get_height(chart.ctx) + (chart.legend.position == Legend.Position.TOP ? + var title_height = chart.title.height + (chart.legend.position == Legend.Position.TOP ? chart.title.vspacing * 2 : chart.title.vspacing); print_y = chart.area.y0 + title_height + s.axis_x.font_spacing + (chart.legend.position == Legend.Position.TOP ? chart.legend.height : 0); @@ -306,7 +306,7 @@ namespace CairoChart { break; case Axis.Type.DATE_TIME: print_y += (s.axis_x.date_format == "" ? 0 : sz.height) - + (s.axis_x.time_format == "" ? 0 : time_text_t.get_height(chart.ctx)) + + (s.axis_x.time_format == "" ? 0 : time_text_t.height) + (s.axis_x.date_format == "" || s.axis_x.time_format == "" ? 0 : s.axis_x.font_spacing); break; } @@ -317,13 +317,13 @@ namespace CairoChart { switch (s.axis_x.type) { case Axis.Type.NUMBERS: - text_t.show(chart.ctx); + text_t.show(); break; case Axis.Type.DATE_TIME: - if (s.axis_x.date_format != "") text_t.show(chart.ctx); + if (s.axis_x.date_format != "") text_t.show(); print_x = s.compact_rec_x_pos (x, time_text_t); chart.ctx.move_to (print_x, print_y - (s.axis_x.date_format == "" ? 0 : sz.height + s.axis_x.font_spacing)); - if (s.axis_x.time_format != "") time_text_t.show(chart.ctx); + if (s.axis_x.time_format != "") time_text_t.show(); break; } } @@ -337,7 +337,7 @@ namespace CairoChart { if (chart.joint_y) { var s = chart.series[chart.zoom_1st_idx]; var y = s.get_real_y(rel2scr_y(c.y)); - var text_t = new Text(s.axis_y.format.printf((LongDouble)y, s.axis_y.font_style)); + var text_t = new Text(chart, s.axis_y.format.printf((LongDouble)y, s.axis_y.font_style)); var print_y = s.compact_rec_y_pos (y, text_t); var print_x = 0.0; switch (s.axis_y.position) { @@ -346,12 +346,12 @@ namespace CairoChart { + (chart.legend.position == Legend.Position.LEFT ? chart.legend.width : 0); break; case Axis.Position.HIGH: - print_x = chart.area.x1 - text_t.get_width(chart.ctx) - s.axis_y.font_spacing + print_x = chart.area.x1 - text_t.width - s.axis_y.font_spacing - (chart.legend.position == Legend.Position.RIGHT ? chart.legend.width : 0); break; } chart.ctx.move_to (print_x, print_y); - text_t.show(chart.ctx); + text_t.show(); } break; } @@ -375,45 +375,45 @@ namespace CairoChart { if (show_x) { chart.color = s.axis_x.color; - var text_t = new Text(s.axis_x.format.printf((LongDouble)point.x), s.axis_x.font_style); - chart.ctx.move_to (svp.x - size.x / 2, svp.y + text_t.get_height(chart.ctx) / 2); + var text_t = new Text(chart, s.axis_x.format.printf((LongDouble)point.x), s.axis_x.font_style); + chart.ctx.move_to (svp.x - size.x / 2, svp.y + text_t.height / 2); if (chart.joint_x) chart.color = chart.joint_color; - text_t.show(chart.ctx); + text_t.show(); } if (show_time) { chart.color = s.axis_x.color; string date = "", time = ""; s.axis_x.format_date_time(point.x, out date, out time); - var text_t = new Text(time, s.axis_x.font_style); - var sz = text_t.get_size(chart.ctx); + var text_t = new Text(chart, time, s.axis_x.font_style); + var sz = text_t.size; var y = svp.y + sz.height / 2; if (show_date) y -= sz.height / 2 + s.axis_x.font_spacing / 2; chart.ctx.move_to (svp.x - size.x / 2, y); if (chart.joint_x) chart.color = chart.joint_color; - text_t.show(chart.ctx); + text_t.show(); } if (show_date) { chart.color = s.axis_x.color; string date = "", time = ""; s.axis_x.format_date_time(point.x, out date, out time); - var text_t = new Text(date, s.axis_x.font_style); - var sz = text_t.get_size(chart.ctx); + var text_t = new Text(chart, date, s.axis_x.font_style); + var sz = text_t.size; var y = svp.y + sz.height / 2; if (show_time) y += sz.height / 2 + s.axis_x.font_spacing / 2; chart.ctx.move_to (svp.x - size.x / 2, y); if (chart.joint_x) chart.color = chart.joint_color; - text_t.show(chart.ctx); + text_t.show(); } if (show_y) { chart.color = s.axis_y.color; - var text_t = new Text(s.axis_y.format.printf((LongDouble)point.y), s.axis_y.font_style); - var sz = text_t.get_size(chart.ctx); + var text_t = new Text(chart, s.axis_y.format.printf((LongDouble)point.y), s.axis_y.font_style); + var sz = text_t.size; chart.ctx.move_to (svp.x + size.x / 2 - sz.width, svp.y + sz.height / 2); if (chart.joint_y) chart.color = chart.joint_color; - text_t.show(chart.ctx); + text_t.show(); } } } diff --git a/src/Legend.vala b/src/Legend.vala index ca73e3e..9cca41f 100644 --- a/src/Legend.vala +++ b/src/Legend.vala @@ -52,7 +52,7 @@ namespace CairoChart { switch (position) { case Position.TOP: x0 = (chart.area.width - width) / 2; - var title_height = chart.title.get_height(chart.ctx) + (chart.legend.position == Legend.Position.TOP ? + var title_height = chart.title.height + (chart.legend.position == Legend.Position.TOP ? chart.title.vspacing * 2 : chart.title.vspacing); y0 = title_height; break; @@ -115,7 +115,7 @@ namespace CairoChart { if (!s.zoom_show) continue; - var title_sz = s.title.get_size(chart.ctx); + var title_sz = s.title.size; // carry switch (position) { @@ -147,7 +147,7 @@ namespace CairoChart { // series title chart.ctx.move_to (x + line_length, y); chart.color = s.title.color; - s.title.show(chart.ctx); + s.title.show(); // series line style chart.ctx.move_to (x, y - title_sz.height / 2); diff --git a/src/Series.vala b/src/Series.vala index 949b9ad..7ee93f2 100644 --- a/src/Series.vala +++ b/src/Series.vala @@ -12,11 +12,11 @@ namespace CairoChart { } public Sort sort = Sort.BY_X; - public Axis axis_x = new Axis(); - public Axis axis_y = new Axis(); + public Axis axis_x; + public Axis axis_y; public Place place = new Place(); - public Text title = new Text (); + public Text title; public Marker marker = null; public Grid grid = new Grid (); @@ -45,6 +45,9 @@ namespace CairoChart { public Series (Chart chart) { this.chart = chart; + title = new Text(chart); + axis_x = new Axis(chart); + axis_y = new Axis(chart); this.marker = new Marker(chart); } @@ -124,8 +127,8 @@ namespace CairoChart { double max_rec_width = 0; double max_rec_height = 0; axis.calc_rec_sizes (chart, out max_rec_width, out max_rec_height, is_x); var max_font_spacing = axis.font_spacing; - var max_axis_font_width = axis.title.text == "" ? 0 : axis.title.get_width(chart.ctx) + axis.font_spacing; - var max_axis_font_height = axis.title.text == "" ? 0 : axis.title.get_height(chart.ctx) + axis.font_spacing; + var max_axis_font_width = axis.title.text == "" ? 0 : axis.title.width + axis.font_spacing; + var max_axis_font_height = axis.title.text == "" ? 0 : axis.title.height + axis.font_spacing; if (is_x) s.join_relative_x_axes (si, true, ref max_rec_width, ref max_rec_height, ref max_font_spacing, ref max_axis_font_height, ref nskip); @@ -200,7 +203,7 @@ namespace CairoChart { max_rec_height = double.max (max_rec_height, tmp_max_rec_height); max_font_spacing = double.max (max_font_spacing, s2.axis_x.font_spacing); max_axis_font_height = double.max (max_axis_font_height, s2.axis_x.title.text == "" ? 0 : - s2.axis_x.title.get_height(chart.ctx) + this.axis_x.font_spacing); + s2.axis_x.title.height + this.axis_x.font_spacing); } ++nskip; } else { @@ -237,7 +240,7 @@ namespace CairoChart { max_rec_height = double.max (max_rec_height, tmp_max_rec_height); max_font_spacing = double.max (max_font_spacing, s2.axis_y.font_spacing); max_axis_font_width = double.max (max_axis_font_width, s2.axis_y.title.text == "" ? 0 - : s2.axis_y.title.get_width(chart.ctx) + this.axis_y.font_spacing); + : s2.axis_y.title.width + this.axis_y.font_spacing); ++nskip; } else { break; @@ -259,8 +262,8 @@ namespace CairoChart { case Axis.Type.DATE_TIME: axis_x.format_date_time(x, out text, out time_text); break; } var scr_x = get_scr_x (x); - var text_t = new Text(text, axis_x.font_style, axis_x.color); - var sz = axis_x.title.get_size(ctx); + var text_t = new Text(chart, text, axis_x.font_style, axis_x.color); + var sz = axis_x.title.size; switch (axis_x.position) { case Axis.Position.LOW: @@ -269,14 +272,14 @@ namespace CairoChart { ctx.move_to (print_x, print_y); switch (axis_x.type) { case Axis.Type.NUMBERS: - text_t.show(ctx); + text_t.show(); break; case Axis.Type.DATE_TIME: - if (axis_x.date_format != "") text_t.show(ctx); - var time_text_t = new Text(time_text, axis_x.font_style, axis_x.color); + if (axis_x.date_format != "") text_t.show(); + var time_text_t = new Text(chart, time_text, axis_x.font_style, axis_x.color); print_x = compact_rec_x_pos (x, time_text_t); - ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.get_height(ctx) + axis_x.font_spacing)); - if (axis_x.time_format != "") time_text_t.show(ctx); + ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.height + axis_x.font_spacing)); + if (axis_x.time_format != "") time_text_t.show(); break; } // 6. Draw grid lines to the place.zy0. @@ -297,14 +300,14 @@ namespace CairoChart { switch (axis_x.type) { case Axis.Type.NUMBERS: - text_t.show(ctx); + text_t.show(); break; case Axis.Type.DATE_TIME: - if (axis_x.date_format != "") text_t.show(ctx); - var time_text_t = new Text(time_text, axis_x.font_style, axis_x.color); + if (axis_x.date_format != "") text_t.show(); + var time_text_t = new Text(chart, time_text, axis_x.font_style, axis_x.color); print_x = compact_rec_x_pos (x, time_text_t); - ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.get_height(ctx) + axis_x.font_spacing)); - if (axis_x.time_format != "") time_text_t.show(ctx); + ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.height + axis_x.font_spacing)); + if (axis_x.time_format != "") time_text_t.show(); break; } // 6. Draw grid lines to the place.zy1. @@ -360,7 +363,7 @@ namespace CairoChart { } } - var sz = s.axis_x.title.get_size(chart.ctx); + var sz = s.axis_x.title.size; // 4.5. Draw Axis title if (s.axis_x.title.text != "") { @@ -373,7 +376,7 @@ namespace CairoChart { chart.ctx.move_to(scr_x - sz.width / 2.0, scr_y); chart.color = s.axis_x.color; if (chart.joint_x) chart.color = chart.joint_color; - s.axis_x.title.show(chart.ctx); + s.axis_x.title.show(); } s.draw_horizontal_records (step, max_rec_height, x_min); @@ -402,16 +405,16 @@ namespace CairoChart { else chart.color = axis_y.color; var text = axis_y.format.printf((LongDouble)y); var scr_y = get_scr_y (y); - var text_t = new Text(text, axis_y.font_style, axis_y.color); - var text_sz = text_t.get_size(ctx); - var sz = axis_y.title.get_size(ctx); + var text_t = new Text(chart, text, axis_y.font_style, axis_y.color); + var text_sz = text_t.size; + var sz = axis_y.title.size; switch (axis_y.position) { case Axis.Position.LOW: ctx.move_to (chart.evarea.x0 + max_rec_width - text_sz.width + axis_y.font_spacing + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing), compact_rec_y_pos (y, text_t)); - text_t.show(ctx); + text_t.show(); // 6. Draw grid lines to the place.zx0. var line_style = grid.line_style; if (joint_y) line_style.color = Color(0, 0, 0, 0.5); @@ -427,7 +430,7 @@ namespace CairoChart { ctx.move_to (chart.evarea.x1 - text_sz.width - axis_y.font_spacing - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing), compact_rec_y_pos (y, text_t)); - text_t.show(ctx); + text_t.show(); // 6. Draw grid lines to the place.zx1. var line_style = grid.line_style; if (joint_y) line_style.color = Color(0, 0, 0, 0.5); @@ -480,7 +483,7 @@ namespace CairoChart { } } - var sz = s.axis_y.title.get_size(chart.ctx); + var sz = s.axis_y.title.size; // 4.5. Draw Axis title if (s.axis_y.title.text != "") { @@ -497,7 +500,7 @@ namespace CairoChart { } chart.color = s.axis_y.color; if (chart.joint_y) chart.color = chart.joint_color; - s.axis_y.title.show(chart.ctx); + s.axis_y.title.show(); } s.draw_vertical_records (step, max_rec_width, y_min); @@ -517,13 +520,13 @@ namespace CairoChart { } public virtual double compact_rec_x_pos (Float128 x, Text text) { - var sz = text.get_size(chart.ctx); + var sz = text.size; return get_scr_x(x) - sz.width / 2.0 - sz.width * (x - (axis_x.range.zmin + axis_x.range.zmax) / 2.0) / axis_x.range.zrange; } public virtual double compact_rec_y_pos (Float128 y, Text text) { - var sz = text.get_size(chart.ctx); + var sz = text.size; return get_scr_y(y) + sz.height / 2.0 + sz.height * (y - (axis_y.range.zmin + axis_y.range.zmax) / 2.0) / axis_y.range.zrange; } diff --git a/src/Text.vala b/src/Text.vala index 612ea83..35e4fdb 100644 --- a/src/Text.vala +++ b/src/Text.vala @@ -1,10 +1,40 @@ namespace CairoChart { + + /** + * ``CairoChart`` Text. + */ public class Text { + + Chart chart = null; + + /** + * ``Text`` string. + */ public string text = ""; + + /** + * ``Text`` font style. + */ public Font style = Font (); + + /** + * ``Text`` color. + */ public Color color = Color(); + + /** + * Vertical spacing. + */ public double vspacing = 4; + + /** + * Horizontal spacing. + */ public double hspacing = 4; + + /** + * Both vertical & horizontal spacing (set only). + */ public double spacing { protected get { return 0; @@ -15,78 +45,130 @@ namespace CairoChart { default = 4; } - public virtual Cairo.TextExtents get_extents (Cairo.Context ctx) { - ctx.select_font_face (style.family, style.slant, style.weight); - ctx.set_font_size (style.size); - Cairo.TextExtents extents; - ctx.text_extents (text, out extents); - return extents; - } - - public virtual double get_width (Cairo.Context ctx) { - var extents = get_extents (ctx); - switch (style.orient) { - case Gtk.Orientation.HORIZONTAL: return extents.width; - case Gtk.Orientation.VERTICAL: return extents.height; - default: return 0.0; + /** + * Cairo ``Text`` extents. + */ + public virtual Cairo.TextExtents ext { + get { + chart.ctx.select_font_face (style.family, style.slant, style.weight); + chart.ctx.set_font_size (style.size); + Cairo.TextExtents ext; + chart.ctx.text_extents (text, out ext); + return ext; + } + protected set { } } - public virtual double get_height (Cairo.Context ctx) { - var extents = get_extents (ctx); - switch (style.orient) { - case Gtk.Orientation.HORIZONTAL: return extents.height; - case Gtk.Orientation.VERTICAL: return extents.width; - default: return 0.0; + /** + * ``Text`` width. + */ + public virtual double width { + get { + switch (style.orient) { + case Gtk.Orientation.HORIZONTAL: return ext.width; + case Gtk.Orientation.VERTICAL: return ext.height; + default: return 0.0; + } + } + protected set { } } + /** + * ``Text`` height. + */ + public virtual double height { + get { + switch (style.orient) { + case Gtk.Orientation.HORIZONTAL: return ext.height; + case Gtk.Orientation.VERTICAL: return ext.width; + default: return 0.0; + } + } + protected set { + } + } + + /** + * ``Text`` size. + */ public struct Size { + /** + * ``Text`` width. + */ double width; + + /** + * ``Text`` height. + */ double height; } - public virtual Size get_size (Cairo.Context ctx) { - var sz = Size(); - var extents = get_extents (ctx); - switch (style.orient) { - case Gtk.Orientation.HORIZONTAL: - sz.width = extents.width + extents.x_bearing; - sz.height = extents.height; - break; - case Gtk.Orientation.VERTICAL: - sz.width = extents.height; // + extents.x_bearing ? - sz.height = extents.width; // +- extents.y_bearing ? - break; + /** + * ``Text`` @{link Size}. + */ + public virtual Size size { + get { + var sz = Size(); + var e = ext; + switch (style.orient) { + case Gtk.Orientation.HORIZONTAL: + sz.width = e.width + e.x_bearing; + sz.height = e.height; + break; + case Gtk.Orientation.VERTICAL: + sz.width = e.height; // + e.x_bearing ? + sz.height = e.width; // +- e.y_bearing ? + break; + } + return sz; + } + protected set { } - return sz; } - public virtual void show (Cairo.Context ctx) { - ctx.select_font_face(style.family, - style.slant, - style.weight); - ctx.set_font_size(style.size); + /** + * Show ``Text``. + */ + public virtual void show () { + chart.ctx.select_font_face(style.family, + style.slant, + style.weight); + chart.ctx.set_font_size(style.size); if (style.orient == Gtk.Orientation.VERTICAL) { - ctx.rotate(- GLib.Math.PI / 2.0); - ctx.show_text(text); - ctx.rotate(GLib.Math.PI / 2.0); + chart.ctx.rotate(- GLib.Math.PI / 2.0); + chart.ctx.show_text(text); + chart.ctx.rotate(GLib.Math.PI / 2.0); } else { - ctx.show_text(text); + chart.ctx.show_text(text); } } - public Text (string text = "", + /** + * Constructs a new ``Text``. + * @param chart ``Chart`` instance. + * @param text ``Text`` string. + * @param style ``Text`` font style. + * @param color ``Text`` color. + */ + public Text (Chart chart, + string text = "", Font style = Font(), Color color = Color() ) { + this.chart = chart; this.text = text; this.style = style; this.color = color; } + /** + * Gets a copy of the ``Text``. + */ public virtual Text copy () { - var text = new Text (); + var text = new Text (chart); + text.chart = this.chart; text.text = this.text; text.style = this.style; text.color = this.color; diff --git a/test/ChartTest.vala b/test/ChartTest.vala index 25438c3..392e845 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -5,13 +5,13 @@ void plot_chart1 (Chart chart) { var s2 = new Series (chart); var s3 = new Series (chart); - s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); + s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0); s1.points = {Point128(0, 0), Point128(2, 1), Point128(1, 3)}; s1.axis_x.position = Axis.Position.HIGH; s1.axis_x.format = "%.3Lf"; - s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); + s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0); s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; - s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); + s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1); s3.points = {Point128(9, 17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; @@ -34,12 +34,12 @@ void plot_chart1 (Chart chart) { s2.marker.type = Marker.Type.CIRCLE; s3.marker.type = Marker.Type.PRICLE_TRIANGLE; - s1.axis_x.title = new Text("Series 1: Axis X."); - s1.axis_y.title = new Text("Series 1: Axis Y."); - s2.axis_x.title = new Text("Series 2: Axis X."); - s2.axis_y.title = new Text("Series 2: Axis Y."); - s3.axis_x.title = new Text("Series 3: Axis X."); - s3.axis_y.title = new Text("Series 3: Axis Y."); + s1.axis_x.title = new Text(chart, "Series 1: Axis X."); + s1.axis_y.title = new Text(chart, "Series 1: Axis Y."); + s2.axis_x.title = new Text(chart, "Series 2: Axis X."); + s2.axis_y.title = new Text(chart, "Series 2: Axis Y."); + s3.axis_x.title = new Text(chart, "Series 3: Axis X."); + s3.axis_y.title = new Text(chart, "Series 3: Axis Y."); chart.series = { s1, s2, s3 }; } @@ -49,13 +49,13 @@ void plot_chart2 (Chart chart) { var s2 = new Series (chart); var s3 = new Series (chart); - s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); + s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0); s1.points = {Point128(-12, 0), Point128(2, 1), Point128(20, 3)}; s2.axis_y.position = Axis.Position.HIGH; s1.axis_x.format = "%.3Lf"; - s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); + s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0); s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; - s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); + s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1); s3.points = {Point128(9, 17), Point128(2, 10), Point128(-15, 31)}; s3.axis_y.position = Axis.Position.HIGH; @@ -78,12 +78,12 @@ void plot_chart2 (Chart chart) { s2.marker.type = Marker.Type.PRICLE_SQUARE; s3.marker.type = Marker.Type.SQUARE; - s1.axis_x.title = new Text("All Series: Axis X."); - s1.axis_y.title = new Text("Series 1: Axis Y."); - s2.axis_x.title = new Text("All Series: Axis X."); - s2.axis_y.title = new Text("Series 2: Axis Y."); - s3.axis_x.title = new Text("All Series: Axis X."); - s3.axis_y.title = new Text("Series 3: Axis Y."); + s1.axis_x.title = new Text(chart, "All Series: Axis X."); + s1.axis_y.title = new Text(chart, "Series 1: Axis Y."); + s2.axis_x.title = new Text(chart, "All Series: Axis X."); + s2.axis_y.title = new Text(chart, "Series 2: Axis Y."); + s3.axis_x.title = new Text(chart, "All Series: Axis X."); + s3.axis_y.title = new Text(chart, "Series 3: Axis Y."); //s1.axis_x.position = s2.axis_x.position = s3.axis_x.position = Axis.Position.HIGH; //s1.axis_x.type = s2.axis_x.type = s3.axis_x.type = Axis.Type.DATE_TIME; @@ -97,15 +97,15 @@ void plot_chart3 (Chart chart) { var s2 = new Series (chart); var s3 = new Series (chart); - s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); + s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0); s1.points = {Point128(0, 70), Point128(2, 155), Point128(1, -3)}; s1.axis_x.position = Axis.Position.HIGH; s1.axis_y.position = Axis.Position.HIGH; s1.axis_x.format = "%.3Lf"; - s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); + s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0); s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.axis_y.position = Axis.Position.HIGH; - s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); + s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1); s3.points = {Point128(9, -17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; @@ -128,12 +128,12 @@ void plot_chart3 (Chart chart) { s2.marker.type = Marker.Type.PRICLE_CIRCLE; s3.marker.type = Marker.Type.TRIANGLE; - s1.axis_x.title = new Text("Series 1: Axis X."); - s1.axis_y.title = new Text("Series 1: Axis Y."); - s2.axis_x.title = new Text("Series 2: Axis X."); - s2.axis_y.title = new Text("Series 2: Axis Y."); - s3.axis_x.title = new Text("Series 3: Axis X."); - s3.axis_y.title = new Text("Series 3: Axis Y."); + s1.axis_x.title = new Text(chart, "Series 1: Axis X."); + s1.axis_y.title = new Text(chart, "Series 1: Axis Y."); + s2.axis_x.title = new Text(chart, "Series 2: Axis X."); + s2.axis_y.title = new Text(chart, "Series 2: Axis Y."); + s3.axis_x.title = new Text(chart, "Series 3: Axis X."); + s3.axis_y.title = new Text(chart, "Series 3: Axis Y."); //s1.axis_y.position = s2.axis_y.position = s3.axis_y.position = Axis.Position.LOW; @@ -154,17 +154,17 @@ void plot_chart4 (Chart chart) { var now = new DateTime.now_local().to_unix(); var high = (uint64) (253000000000L); - s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); + s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0); s1.points = {Point128(now, 70), Point128(now - 100000, 155), Point128(now + 100000, 30)}; s1.axis_x.position = Axis.Position.HIGH; s1.axis_y.position = Axis.Position.HIGH; - s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); + s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0); s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.axis_y.position = Axis.Position.HIGH; - s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); + s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1); s3.points = {Point128(high - 2 + 0.73, -17), Point128(high - 1 + 0.234, 10), Point128(high + 1 + 0.411, 31)}; s3.axis_y.position = Axis.Position.HIGH; - s4.title = new Text("Series 4"); s4.color = Color (0.5, 0.3, 0.9); + s4.title = new Text(chart, "Series 4"); s4.color = Color (0.5, 0.3, 0.9); s4.points = {Point128(high + 0.005, -19.05), Point128(high + 0.0051, 28), Point128(high + 0.0052, 55), Point128(high + 0.0053, 44)}; s4.axis_y.position = Axis.Position.HIGH; @@ -193,14 +193,14 @@ void plot_chart4 (Chart chart) { s3.marker.type = Marker.Type.TRIANGLE; s4.marker.type = Marker.Type.PRICLE_SQUARE; - s1.axis_x.title = new Text("Series 1: Axis X."); - s1.axis_y.title = new Text("Series 1: Axis Y."); - s2.axis_x.title = new Text("Series 2: Axis X."); - s2.axis_y.title = new Text("Series 2: Axis Y."); - s3.axis_x.title = new Text("Series 3: Axis X."); - s3.axis_y.title = new Text("Series 3: Axis Y."); - s4.axis_x.title = new Text("Series 4: Axis X."); - s4.axis_y.title = new Text("Series 4: Axis Y."); + s1.axis_x.title = new Text(chart, "Series 1: Axis X."); + s1.axis_y.title = new Text(chart, "Series 1: Axis Y."); + s2.axis_x.title = new Text(chart, "Series 2: Axis X."); + s2.axis_y.title = new Text(chart, "Series 2: Axis Y."); + s3.axis_x.title = new Text(chart, "Series 3: Axis X."); + s3.axis_y.title = new Text(chart, "Series 3: Axis Y."); + s4.axis_x.title = new Text(chart, "Series 4: Axis X."); + s4.axis_y.title = new Text(chart, "Series 4: Axis Y."); chart.series = { s1, s2, s3, s4 }; } @@ -432,9 +432,9 @@ int main (string[] args) { var str = chart.cursors.get_cursors_delta_str(); if (str != "") { var text = "Δ = " + str; - var text_t = new Text(text); - var w = text_t.get_width(ctx); - var h = text_t.get_height(ctx); + var text_t = new Text(chart, text); + var w = text_t.width; + var h = text_t.height; var x0 = chart.plarea.x1 - w - 5; var y0 = chart.plarea.y0 + h + 5; chart.color = chart.legend.bg_color;