From accddc2b790f7f0c975e7d82d14483694fd4a81f Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Mon, 28 Aug 2017 14:47:31 +0300 Subject: [PATCH] Split source files on classes. --- src/Axis.vala | 70 ++++++++++++++++++ src/{GtkChart.vala => Chart.vala} | 117 +++++++++--------------------- src/Color.vala | 12 +++ src/Common.vala | 115 ----------------------------- src/FontStyle.vala | 25 +++++++ src/Grid.vala | 15 ++++ src/LabelStyle.vala | 8 ++ src/Legend.vala | 19 +++++ src/LineStyle.vala | 23 ++++++ src/Place.vala | 15 ++++ src/Point.vala | 10 +++ src/Series.vala | 112 ---------------------------- src/Text.vala | 56 ++++++++++++++ test/ChartTest.vala | 98 ++++++++++++------------- 14 files changed, 338 insertions(+), 357 deletions(-) create mode 100644 src/Axis.vala rename src/{GtkChart.vala => Chart.vala} (91%) create mode 100644 src/Color.vala delete mode 100644 src/Common.vala create mode 100644 src/FontStyle.vala create mode 100644 src/Grid.vala create mode 100644 src/LabelStyle.vala create mode 100644 src/Legend.vala create mode 100644 src/LineStyle.vala create mode 100644 src/Place.vala create mode 100644 src/Point.vala create mode 100644 src/Text.vala diff --git a/src/Axis.vala b/src/Axis.vala new file mode 100644 index 0000000..c533f97 --- /dev/null +++ b/src/Axis.vala @@ -0,0 +1,70 @@ +namespace Gtk.CairoChart { + // If one of axis:title or axis:min/max are different + // then draw separate axis for each/all series + // or specify series name near the axis + public class Axis { + public Float128 min = 0; + public Float128 max = 1; + public Text title = new Text (""); + public enum Type { + NUMBERS = 0, + DATE_TIME + } + public enum ScaleType { + LINEAR = 0, // default + // LOGARITHMIC, // TODO + // etc + } + public Type type; + public ScaleType scale_type; + public enum Position { + LOW = 0, + HIGH = 1, + BOTH = 2 + } + public Position position = Position.LOW; + + string _format = "%.2Lf"; + string _date_format = "%Y.%m.%d"; + string _time_format = "%H:%M:%S"; + int _dsec_signs = 2; // 2 signs = centiseconds + public string format { + get { return _format; } + set { + // TODO: check format + _format = value; + } + default = "%.2Lf"; + } + public string date_format { + get { return _date_format; } + set { + // TODO: check format + _date_format = value; + } + default = "%Y.%m.%d"; + } + public string time_format { + get { return _time_format; } + set { + // TODO: check format + _time_format = value; + } + default = "%H:%M:%S"; + } + public int dsec_signs { + get { return _dsec_signs; } + set { + // TODO: check format + _dsec_signs = value; + } + default = 2; + } + public FontStyle font_style = FontStyle (); + public Color color = Color (); + public LineStyle line_style = new LineStyle (); + public double font_indent = 5; + + public Axis () {} + } +} diff --git a/src/GtkChart.vala b/src/Chart.vala similarity index 91% rename from src/GtkChart.vala rename to src/Chart.vala index 4d70531..af9dab2 100644 --- a/src/GtkChart.vala +++ b/src/Chart.vala @@ -1,8 +1,4 @@ -// даты/время: сетка для малых интервалов (< нескольких секунд) -using Cairo; - namespace Gtk.CairoChart { - public class Chart { protected double width = 0; @@ -15,23 +11,6 @@ namespace Gtk.CairoChart { public Text title = new Text ("Cairo Chart"); public Color border_color = Color(0, 0, 0, 0.3); - public class Legend { - public enum Position { - TOP = 0, // default - LEFT, - RIGHT, - BOTTOM - } - public Position position = Position.TOP; - public FontStyle font_style = FontStyle(); - public Color bg_color = Color(1, 1, 1); - public LineStyle border_style = new LineStyle (); - public double indent = 5; - - public Legend () { - border_style.color = Color (0, 0, 0, 0.3); - } - } public Legend legend = new Legend (); @@ -112,30 +91,6 @@ namespace Gtk.CairoChart { } } - // TODO: - public virtual bool button_release_event (Gdk.EventButton event) { - //stdout.puts ("button_release_event\n"); - return true; - } - - // TODO: - public virtual bool button_press_event (Gdk.EventButton event) { - //stdout.puts ("button_press_event\n"); - return true; - } - - // TODO: - public virtual bool motion_notify_event (Gdk.EventMotion event) { - //stdout.puts ("motion_notify_event\n"); - return true; - } - - // TODO: - public virtual bool scroll_notify_event (Gdk.EventScroll event) { - //stdout.puts ("scroll_notify_event\n"); - return true; - } - protected double title_width = 0.0; protected double title_height = 0.0; @@ -405,18 +360,18 @@ namespace Gtk.CairoChart { int axis_rec_npoints = 128; - protected virtual void calc_axis_rec_sizes (Series.Axis axis, out double max_rec_width, out double max_rec_height, bool is_horizontal = true) { + protected virtual void calc_axis_rec_sizes (Axis axis, out double max_rec_width, out double max_rec_height, bool is_horizontal = true) { max_rec_width = max_rec_height = 0; for (var i = 0; i < axis_rec_npoints; ++i) { Float128 x = axis.min + (axis.max - axis.min) / axis_rec_npoints * i; switch (axis.type) { - case Series.Axis.Type.NUMBERS: + case Axis.Type.NUMBERS: var text = new Text (axis.format.printf((LongDouble)x) + (is_horizontal ? "_" : "")); text.style = axis.font_style; max_rec_width = double.max (max_rec_width, text.get_width(context)); max_rec_height = double.max (max_rec_height, text.get_height(context)); break; - case Series.Axis.Type.DATE_TIME: + case Axis.Type.DATE_TIME: var dt = new DateTime.from_unix_utc((int64)x); var text = new Text(""); var h = 0.0; @@ -547,9 +502,9 @@ namespace Gtk.CairoChart { if (!common_x_axes || si == 0) switch (s.axis_x.position) { - case Series.Axis.Position.LOW: plot_area_y_max -= max_rec_height + max_font_indent + max_axis_font_height; break; - case Series.Axis.Position.HIGH: plot_area_y_min += max_rec_height + max_font_indent + max_axis_font_height; break; - case Series.Axis.Position.BOTH: break; + case Axis.Position.LOW: plot_area_y_max -= max_rec_height + max_font_indent + max_axis_font_height; break; + case Axis.Position.HIGH: plot_area_y_min += max_rec_height + max_font_indent + max_axis_font_height; break; + case Axis.Position.BOTH: break; default: break; } } @@ -592,9 +547,9 @@ namespace Gtk.CairoChart { if (!common_y_axes || si == 0) switch (s.axis_y.position) { - case Series.Axis.Position.LOW: plot_area_x_min += max_rec_width + max_font_indent + max_axis_font_width; break; - case Series.Axis.Position.HIGH: plot_area_x_max -= max_rec_width + max_font_indent + max_axis_font_width; break; - case Series.Axis.Position.BOTH: break; + case Axis.Position.LOW: plot_area_x_min += max_rec_width + max_font_indent + max_axis_font_width; break; + case Axis.Position.HIGH: plot_area_x_max -= max_rec_width + max_font_indent + max_axis_font_width; break; + case Axis.Position.BOTH: break; default: break; } } @@ -618,7 +573,7 @@ namespace Gtk.CairoChart { long max_nrecs = (long) ((plot_area_x_max - plot_area_x_min) * (s.place.x_high - s.place.x_low) / max_rec_width); // 3. Calculate grid step. - Float128 step = calc_round_step ((s.axis_x.max - s.axis_x.min) / max_nrecs, s.axis_x.type == Series.Axis.Type.DATE_TIME); + Float128 step = calc_round_step ((s.axis_x.max - s.axis_x.min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME); if (step > s.axis_x.max - s.axis_x.min) step = s.axis_x.max - s.axis_x.min; @@ -637,7 +592,7 @@ namespace Gtk.CairoChart { // 4.5. Draw Axis title if (s.axis_x.title.text != "") switch (s.axis_x.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: var scr_x = plot_area_x_min + (plot_area_x_max - plot_area_x_min) * (s.place.x_low + s.place.x_high) / 2.0; var scr_y = cur_y_max - s.axis_x.font_indent; context.move_to(scr_x - s.axis_x.title.get_width(context) / 2.0, scr_y); @@ -645,7 +600,7 @@ namespace Gtk.CairoChart { if (common_x_axes) set_source_rgba(Color(0,0,0,1)); show_text(s.axis_x.title); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: var scr_x = plot_area_x_min + (plot_area_x_max - plot_area_x_min) * (s.place.x_low + s.place.x_high) / 2.0; var scr_y = cur_y_min + s.axis_x.font_indent + s.axis_x.title.get_height(context); context.move_to(scr_x - s.axis_x.title.get_width(context) / 2.0, scr_y); @@ -653,7 +608,7 @@ namespace Gtk.CairoChart { if (common_x_axes) set_source_rgba(Color(0,0,0,1)); show_text(s.axis_x.title); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; } @@ -663,10 +618,10 @@ namespace Gtk.CairoChart { else set_source_rgba(s.axis_x.color); string text = "", time_text = ""; switch (s.axis_x.type) { - case Series.Axis.Type.NUMBERS: + case Axis.Type.NUMBERS: text = s.axis_x.format.printf((LongDouble)x); break; - case Series.Axis.Type.DATE_TIME: + case Axis.Type.DATE_TIME: var dt = new DateTime.from_unix_utc((int64)x); text = dt.format(s.axis_x.date_format); var dsec_str = @@ -680,16 +635,16 @@ namespace Gtk.CairoChart { * (s.place.x_low + (s.place.x_high - s.place.x_low) / (s.axis_x.max - s.axis_x.min) * (x - s.axis_x.min)); var text_t = new Text(text, s.axis_x.font_style, s.axis_x.color); switch (s.axis_x.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: var print_y = cur_y_max - s.axis_x.font_indent - (s.axis_x.title.text == "" ? 0 : s.axis_x.title.get_height(context) + s.axis_x.font_indent); switch (s.axis_x.type) { - case Series.Axis.Type.NUMBERS: + case Axis.Type.NUMBERS: var print_x = scr_x - text_t.get_width(context) / 2.0 - text_t.get_x_bearing(context) - text_t.get_width(context) * (x - (s.axis_x.min + s.axis_x.max) / 2.0) / (s.axis_x.max - s.axis_x.min); context.move_to (print_x, print_y); show_text(text_t); break; - case Series.Axis.Type.DATE_TIME: + case Axis.Type.DATE_TIME: var print_x = scr_x - text_t.get_width(context) / 2.0 - text_t.get_x_bearing(context) - text_t.get_width(context) * (x - (s.axis_x.min + s.axis_x.max) / 2.0) / (s.axis_x.max - s.axis_x.min); context.move_to (print_x, print_y); @@ -714,16 +669,16 @@ namespace Gtk.CairoChart { else context.line_to (scr_x, double.min (y, plot_area_y_max - (plot_area_y_max - plot_area_y_min) * s.place.y_high)); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: var print_y = cur_y_min + max_rec_height + s.axis_x.font_indent + (s.axis_x.title.text == "" ? 0 : s.axis_x.title.get_height(context) + s.axis_x.font_indent); switch (s.axis_x.type) { - case Series.Axis.Type.NUMBERS: + case Axis.Type.NUMBERS: var print_x = scr_x - text_t.get_width(context) / 2.0 - text_t.get_x_bearing(context) - text_t.get_width(context) * (x - (s.axis_x.min + s.axis_x.max) / 2.0) / (s.axis_x.max - s.axis_x.min); context.move_to (print_x, print_y); show_text(text_t); break; - case Series.Axis.Type.DATE_TIME: + case Axis.Type.DATE_TIME: var print_x = scr_x - text_t.get_width(context) / 2.0 - text_t.get_x_bearing(context) - text_t.get_width(context) * (x - (s.axis_x.min + s.axis_x.max) / 2.0) / (s.axis_x.max - s.axis_x.min); context.move_to (print_x, print_y); @@ -748,7 +703,7 @@ namespace Gtk.CairoChart { else context.line_to (scr_x, double.max (y, plot_area_y_max - (plot_area_y_max - plot_area_y_min) * s.place.y_low)); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; default: break; @@ -779,15 +734,15 @@ namespace Gtk.CairoChart { if (nskip != 0) {--nskip; continue;} switch (s.axis_x.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: cur_y_max -= max_rec_height + s.axis_x.font_indent + (s.axis_x.title.text == "" ? 0 : s.axis_x.title.get_height(context) + s.axis_x.font_indent); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: cur_y_min += max_rec_height + s.axis_x.font_indent + (s.axis_x.title.text == "" ? 0 : s.axis_x.title.get_height(context) + s.axis_x.font_indent); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; default: break; } @@ -825,7 +780,7 @@ namespace Gtk.CairoChart { // 4.5. Draw Axis title if (s.axis_y.title.text != "") switch (s.axis_y.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: var scr_y = plot_area_y_max - (plot_area_y_max - plot_area_y_min) * (s.place.y_low + s.place.y_high) / 2.0; var scr_x = cur_x_min + s.axis_y.font_indent + s.axis_y.title.get_width(context); context.move_to(scr_x, scr_y + s.axis_y.title.get_height(context) / 2.0); @@ -833,7 +788,7 @@ namespace Gtk.CairoChart { if (common_y_axes) set_source_rgba(Color(0,0,0,1)); show_text(s.axis_y.title); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: var scr_y = plot_area_y_max - (plot_area_y_max - plot_area_y_min) * (s.place.y_low + s.place.y_high) / 2.0; var scr_x = cur_x_max - s.axis_y.font_indent; context.move_to(scr_x, scr_y + s.axis_y.title.get_height(context) / 2.0); @@ -841,7 +796,7 @@ namespace Gtk.CairoChart { if (common_y_axes) set_source_rgba(Color(0,0,0,1)); show_text(s.axis_y.title); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; } @@ -854,7 +809,7 @@ namespace Gtk.CairoChart { * (s.place.y_low + (s.place.y_high - s.place.y_low) / (s.axis_y.max - s.axis_y.min) * (y - s.axis_y.min)); var text_t = new Text(text, s.axis_y.font_style, s.axis_y.color); switch (s.axis_y.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: context.move_to (cur_x_min + max_rec_width - (new Text(text)).get_width(context) + s.axis_y.font_indent - text_t.get_x_bearing(context) + (s.axis_y.title.text == "" ? 0 : s.axis_y.title.get_width(context) + s.axis_y.font_indent), scr_y + (new Text(text)).get_height(context) / 2.0 @@ -871,7 +826,7 @@ namespace Gtk.CairoChart { else context.line_to (double.max (x, plot_area_x_min + (plot_area_x_max - plot_area_x_min) * s.place.x_high), scr_y); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: context.move_to (cur_x_max - (new Text(text)).get_width(context) - s.axis_y.font_indent - text_t.get_x_bearing(context) - (s.axis_y.title.text == "" ? 0 : s.axis_y.title.get_width(context) + s.axis_y.font_indent), scr_y + (new Text(text)).get_height(context) / 2.0 @@ -888,7 +843,7 @@ namespace Gtk.CairoChart { else context.line_to (double.min (x, plot_area_x_min + (plot_area_x_max - plot_area_x_min) * s.place.x_low), scr_y); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; default: break; @@ -919,13 +874,13 @@ namespace Gtk.CairoChart { switch (s.axis_y.position) { - case Series.Axis.Position.LOW: + case Axis.Position.LOW: cur_x_min += max_rec_width + s.axis_y.font_indent + (s.axis_y.title.text == "" ? 0 : s.axis_y.title.get_width(context) + s.axis_y.font_indent); break; - case Series.Axis.Position.HIGH: + case Axis.Position.HIGH: cur_x_max -= max_rec_width + s.axis_y.font_indent + (s.axis_y.title.text == "" ? 0 : s.axis_y.title.get_width(context) + s.axis_y.font_indent); break; - case Series.Axis.Position.BOTH: + case Axis.Position.BOTH: break; default: break; } @@ -953,8 +908,8 @@ namespace Gtk.CairoChart { / (s.axis_y.max - s.axis_y.min) * (s.place.y_high - s.place.y_low)); } - delegate int PointComparator(Series.Point a, Series.Point b); - void sort_points(Series.Point[] points, PointComparator compare) { + delegate int PointComparator(Point a, Point b); + void sort_points(Point[] points, PointComparator compare) { for(var i = 0; i < points.length; ++i) { for(var j = i + 1; j < points.length; ++j) { if(compare(points[i], points[j]) > 0) { diff --git a/src/Color.vala b/src/Color.vala new file mode 100644 index 0000000..6cbea9a --- /dev/null +++ b/src/Color.vala @@ -0,0 +1,12 @@ +namespace Gtk.CairoChart { + public struct Color { + double red; + double green; + double blue; + double alpha; + + public Color (double red = 0.0, double green = 0.0, double blue = 0.0, double alpha = 1.0) { + this.red = red; this.green = green; this.blue = blue; this.alpha = alpha; + } + } +} diff --git a/src/Common.vala b/src/Common.vala deleted file mode 100644 index c882426..0000000 --- a/src/Common.vala +++ /dev/null @@ -1,115 +0,0 @@ -using Cairo; - -namespace Gtk.CairoChart { - - public struct Color { - double red; - double green; - double blue; - double alpha; - - public Color (double red = 0.0, double green = 0.0, double blue = 0.0, double alpha = 1.0) { - this.red = red; this.green = green; this.blue = blue; this.alpha = alpha; - } - } - - public enum FontOrient { - HORIZONTAL = 0, - VERTICAL - } - public struct FontStyle { - string family; - FontSlant slant; - FontWeight weight; - - FontOrient orientation; - double size; - - public FontStyle (string family = "Sans", - FontSlant slant = Cairo.FontSlant.NORMAL, - FontWeight weight = Cairo.FontWeight.NORMAL, - double size = 10) { - this.family = family; - this.slant = slant; - this.weight = weight; - this.size = size; - } - } - - public struct LineStyle { - double width; - LineJoin line_join; - LineCap line_cap; - double[]? dashes; - double dash_offset; - Color color; - - public LineStyle (double width = 1, - LineJoin line_join = Cairo.LineJoin.MITER, - LineCap line_cap = Cairo.LineCap.ROUND, - double[]? dashes = null, double dash_offset = 0, - Color color = Color()) { - this.width = width; - this.line_join = line_join; - this.line_cap = line_cap; - this.dashes = dashes; - this.dash_offset = dash_offset; - this.color = color; - } - } - - [Compact] - public class Text { - public string text = ""; - public FontStyle style = FontStyle (); - public Color color = Color(); - - TextExtents get_extents (Cairo.Context context) { - context.select_font_face (style.family, - style.slant, - style.weight); - context.set_font_size (style.size); - TextExtents extents; - context.text_extents (text, out extents); - return extents; - } - - public double get_width (Cairo.Context context) { - var extents = get_extents (context); - if (style.orientation == FontOrient.HORIZONTAL) - return extents.width; - else - return extents.height; - } - - public double get_height (Cairo.Context context) { - var extents = get_extents (context); - if (style.orientation == FontOrient.HORIZONTAL) - return extents.height; - else - return extents.width; - } - - public double get_x_bearing (Cairo.Context context) { - var extents = get_extents (context); - if (style.orientation == FontOrient.HORIZONTAL) - return extents.x_bearing; - else - return extents.y_bearing; - } - - public Text (string text = "", - FontStyle style = FontStyle(), - Color color = Color()) { - this.text = text; - this.style = style; - this.color = color; - } - - public Text.by_instance (Text text) { - this.text = text.text; - this.style = text.style; - this.color = text.color; - } - } -} diff --git a/src/FontStyle.vala b/src/FontStyle.vala new file mode 100644 index 0000000..df4b8de --- /dev/null +++ b/src/FontStyle.vala @@ -0,0 +1,25 @@ +namespace Gtk.CairoChart { + public enum FontOrient { + HORIZONTAL = 0, + VERTICAL + } + + public struct FontStyle { + string family; + Cairo.FontSlant slant; + Cairo.FontWeight weight; + + FontOrient orientation; + double size; + + public FontStyle (string family = "Sans", + Cairo.FontSlant slant = Cairo.FontSlant.NORMAL, + Cairo.FontWeight weight = Cairo.FontWeight.NORMAL, + double size = 10) { + this.family = family; + this.slant = slant; + this.weight = weight; + this.size = size; + } + } +} diff --git a/src/Grid.vala b/src/Grid.vala new file mode 100644 index 0000000..c753fcc --- /dev/null +++ b/src/Grid.vala @@ -0,0 +1,15 @@ +namespace Gtk.CairoChart { + public class Grid { + /*public enum GridType { + PRICK_LINE = 0, // default + LINE + }*/ + public Color color = Color (0, 0, 0, 0.1); + + public LineStyle line_style = new LineStyle (); + + public Grid () { + line_style.dashes = {2, 3}; + } + } +} diff --git a/src/LabelStyle.vala b/src/LabelStyle.vala new file mode 100644 index 0000000..0f6dddf --- /dev/null +++ b/src/LabelStyle.vala @@ -0,0 +1,8 @@ +namespace Gtk.CairoChart { + public class LabelStyle { + FontStyle font_style = FontStyle(); + LineStyle frame_line_style = new LineStyle(); + Color bg_color = Color(); + Color frame_color = Color(); + } +} diff --git a/src/Legend.vala b/src/Legend.vala new file mode 100644 index 0000000..c60f0bd --- /dev/null +++ b/src/Legend.vala @@ -0,0 +1,19 @@ +namespace Gtk.CairoChart { + public class Legend { + public enum Position { + TOP = 0, // default + LEFT, + RIGHT, + BOTTOM + } + public Position position = Position.TOP; + public FontStyle font_style = FontStyle(); + public Color bg_color = Color(1, 1, 1); + public LineStyle border_style = new LineStyle (); + public double indent = 5; + + public Legend () { + border_style.color = Color (0, 0, 0, 0.3); + } + } +} diff --git a/src/LineStyle.vala b/src/LineStyle.vala new file mode 100644 index 0000000..0efad62 --- /dev/null +++ b/src/LineStyle.vala @@ -0,0 +1,23 @@ +namespace Gtk.CairoChart { + public struct LineStyle { + double width; + Cairo.LineJoin line_join; + Cairo.LineCap line_cap; + double[]? dashes; + double dash_offset; + Color color; + + public LineStyle (double width = 1, + Cairo.LineJoin line_join = Cairo.LineJoin.MITER, + Cairo.LineCap line_cap = Cairo.LineCap.ROUND, + double[]? dashes = null, double dash_offset = 0, + Color color = Color()) { + this.width = width; + this.line_join = line_join; + this.line_cap = line_cap; + this.dashes = dashes; + this.dash_offset = dash_offset; + this.color = color; + } + } +} diff --git a/src/Place.vala b/src/Place.vala new file mode 100644 index 0000000..13a9eee --- /dev/null +++ b/src/Place.vala @@ -0,0 +1,15 @@ +namespace Gtk.CairoChart { + public struct Place { + double x_low; + double x_high; + double y_low; + double y_high; + + public Place (double x_low = 0, double x_high = 0, double y_low = 0, double y_high = 0) { + this.x_low = x_low; + this.x_high = x_high; + this.y_low = y_low; + this.y_high = y_high; + } + } +} diff --git a/src/Point.vala b/src/Point.vala new file mode 100644 index 0000000..8710a75 --- /dev/null +++ b/src/Point.vala @@ -0,0 +1,10 @@ +namespace Gtk.CairoChart { + public struct Point { + Float128 x; + Float128 y; + + public Point (Float128 x, Float128 y) { + this.x = x; this.y = y; + } + } +} diff --git a/src/Series.vala b/src/Series.vala index 47d5b69..033e484 100644 --- a/src/Series.vala +++ b/src/Series.vala @@ -4,15 +4,6 @@ namespace Gtk.CairoChart { public class Series { - public struct Point { - Float128 x; - Float128 y; - - public Point (Float128 x, Float128 y) { - this.x = x; this.y = y; - } - } - public Point[] points = {}; public enum Sort { BY_X = 0, @@ -21,92 +12,9 @@ namespace Gtk.CairoChart { } public Sort sort = Sort.BY_X; - // If one of axis:title or axis:min/max are different - // then draw separate axis for each/all series - // or specify series name near the axis - public class Axis { - public Float128 min = 0; - public Float128 max = 1; - public Text title = new Text (""); - public enum Type { - NUMBERS = 0, - DATE_TIME - } - public enum ScaleType { - LINEAR = 0, // default - // LOGARITHMIC, // TODO - // etc - } - public Type type; - public ScaleType scale_type; - public enum Position { - LOW = 0, - HIGH = 1, - BOTH = 2 - } - public Position position = Position.LOW; - - string _format = "%.2Lf"; - string _date_format = "%Y.%m.%d"; - string _time_format = "%H:%M:%S"; - int _dsec_signs = 2; // 2 signs = centiseconds - public string format { - get { return _format; } - set { - // TODO: check format - _format = value; - } - default = "%.2Lf"; - } - public string date_format { - get { return _date_format; } - set { - // TODO: check format - _date_format = value; - } - default = "%Y.%m.%d"; - } - public string time_format { - get { return _time_format; } - set { - // TODO: check format - _time_format = value; - } - default = "%H:%M:%S"; - } - public int dsec_signs { - get { return _dsec_signs; } - set { - // TODO: check format - _dsec_signs = value; - } - default = 2; - } - public FontStyle font_style = FontStyle (); - public Color color = Color (); - public LineStyle line_style = new LineStyle (); - public double font_indent = 5; - - public Axis () {} - } - public Axis axis_x = new Axis(); public Axis axis_y = new Axis(); - public struct Place { - double x_low; - double x_high; - double y_low; - double y_high; - - public Place (double x_low = 0, double x_high = 0, double y_low = 0, double y_high = 0) { - this.x_low = x_low; - this.x_high = x_high; - this.y_low = y_low; - this.y_high = y_high; - } - } - public enum MarkerType { NONE = 0, // default SQUARE, @@ -121,20 +29,6 @@ namespace Gtk.CairoChart { public Text title = new Text (); public MarkerType marker_type = MarkerType.SQUARE; - public class Grid { - /*public enum GridType { - PRICK_LINE = 0, // default - LINE - }*/ - public Color color = Color (0, 0, 0, 0.1); - - public LineStyle line_style = new LineStyle (); - - public Grid () { - line_style.dashes = {2, 3}; - } - } - public Grid grid = new Grid (); public GLib.List cursors = new List (); @@ -159,11 +53,5 @@ namespace Gtk.CairoChart { public Series () { } - public class LabelStyle { - FontStyle font_style = FontStyle(); - LineStyle frame_line_style = new LineStyle(); - Color bg_color = Color(); - Color frame_color = Color(); - } } } diff --git a/src/Text.vala b/src/Text.vala new file mode 100644 index 0000000..a7f6a58 --- /dev/null +++ b/src/Text.vala @@ -0,0 +1,56 @@ +namespace Gtk.CairoChart { + [Compact] + public class Text { + public string text = ""; + public FontStyle style = FontStyle (); + public Color color = Color(); + + Cairo.TextExtents get_extents (Cairo.Context context) { + context.select_font_face (style.family, + style.slant, + style.weight); + context.set_font_size (style.size); + Cairo.TextExtents extents; + context.text_extents (text, out extents); + return extents; + } + + public double get_width (Cairo.Context context) { + var extents = get_extents (context); + if (style.orientation == FontOrient.HORIZONTAL) + return extents.width; + else + return extents.height; + } + + public double get_height (Cairo.Context context) { + var extents = get_extents (context); + if (style.orientation == FontOrient.HORIZONTAL) + return extents.height; + else + return extents.width; + } + + public double get_x_bearing (Cairo.Context context) { + var extents = get_extents (context); + if (style.orientation == FontOrient.HORIZONTAL) + return extents.x_bearing; + else + return extents.y_bearing; + } + + public Text (string text = "", + FontStyle style = FontStyle(), + Color color = Color()) { + this.text = text; + this.style = style; + this.color = color; + } + + public Text.by_instance (Text text) { + this.text = text.text; + this.style = text.style; + this.color = text.color; + } + } +} diff --git a/test/ChartTest.vala b/test/ChartTest.vala index f8981d7..c40e56f 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -6,14 +6,14 @@ void plot_chart1 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = new Color (1, 0, 0); - s1.points = {new Series.Point(0, 0), new Series.Point(2, 1), new Series.Point(1, 3)}; - s1.axis_x.position = Series.Axis.Position.HIGH; + s1.points = {new Point(0, 0), new Point(2, 1), new Point(1, 3)}; + s1.axis_x.position = Axis.Position.HIGH; s1.axis_x.format = "%.3Lf"; s2.title = new Text("Series 2"); s2.color = new Color (0, 1, 0); - s2.points = {new Series.Point(5, -3), new Series.Point(25, -18), new Series.Point(-11, 173)}; + s2.points = {new Point(5, -3), new Point(25, -18), new Point(-11, 173)}; s3.title = new Text("Series 3"); s3.color = new Color (0, 0, 1); - s3.points = {new Series.Point(9, 17), new Series.Point(2, 10), new Series.Point(122, 31)}; - s3.axis_y.position = Series.Axis.Position.HIGH; + s3.points = {new Point(9, 17), new Point(2, 10), new Point(122, 31)}; + s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = 0; s1.axis_x.max = 2; s1.axis_y.min = 0; s1.axis_y.max = 3; @@ -49,14 +49,14 @@ void plot_chart2 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = new Color (1, 0, 0); - s1.points = {new Series.Point(-12, 0), new Series.Point(2, 1), new Series.Point(20, 3)}; - s2.axis_y.position = Series.Axis.Position.HIGH; + s1.points = {new Point(-12, 0), new Point(2, 1), new Point(20, 3)}; + s2.axis_y.position = Axis.Position.HIGH; s1.axis_x.format = "%.3Lf"; s2.title = new Text("Series 2"); s2.color = new Color (0, 1, 0); - s2.points = {new Series.Point(5, -3), new Series.Point(25, -18), new Series.Point(-11, 173)}; + s2.points = {new Point(5, -3), new Point(25, -18), new Point(-11, 173)}; s3.title = new Text("Series 3"); s3.color = new Color (0, 0, 1); - s3.points = {new Series.Point(9, 17), new Series.Point(2, 10), new Series.Point(-15, 31)}; - s3.axis_y.position = Series.Axis.Position.HIGH; + s3.points = {new Point(9, 17), new Point(2, 10), new Point(-15, 31)}; + s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = -15; s1.axis_x.max = 30; s1.axis_y.min = 0; s1.axis_y.max = 3; @@ -92,16 +92,16 @@ void plot_chart3 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = new Color (1, 0, 0); - s1.points = {new Series.Point(0, 70), new Series.Point(2, 155), new Series.Point(1, -3)}; - s1.axis_x.position = Series.Axis.Position.HIGH; - s1.axis_y.position = Series.Axis.Position.HIGH; + s1.points = {new Point(0, 70), new Point(2, 155), new Point(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 = new Color (0, 1, 0); - s2.points = {new Series.Point(5, -3), new Series.Point(25, -18), new Series.Point(-11, 173)}; - s2.axis_y.position = Series.Axis.Position.HIGH; + s2.points = {new Point(5, -3), new Point(25, -18), new Point(-11, 173)}; + s2.axis_y.position = Axis.Position.HIGH; s3.title = new Text("Series 3"); s3.color = new Color (0, 0, 1); - s3.points = {new Series.Point(9, -17), new Series.Point(2, 10), new Series.Point(122, 31)}; - s3.axis_y.position = Series.Axis.Position.HIGH; + s3.points = {new Point(9, -17), new Point(2, 10), new Point(122, 31)}; + s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = 0; s1.axis_x.max = 2; s1.axis_y.min = -20; s1.axis_y.max = 200; @@ -137,27 +137,27 @@ void plot_chart4 (Chart chart) { var s3 = new Series (); var s4 = new Series (); - s1.axis_x.type = Series.Axis.Type.DATE_TIME; - s3.axis_x.type = Series.Axis.Type.DATE_TIME; - s4.axis_x.type = Series.Axis.Type.DATE_TIME; + s1.axis_x.type = Axis.Type.DATE_TIME; + s3.axis_x.type = Axis.Type.DATE_TIME; + s4.axis_x.type = Axis.Type.DATE_TIME; s4.axis_x.dsec_signs = 5; var now = new DateTime.now_local().to_unix(); var high = (uint64) (253000000000L); s1.title = new Text("Series 1"); s1.color = new Color (1, 0, 0); - s1.points = {new Series.Point(now, 70), new Series.Point(now - 100000, 155), new Series.Point(now + 100000, 30)}; - s1.axis_x.position = Series.Axis.Position.HIGH; - s1.axis_y.position = Series.Axis.Position.HIGH; + s1.points = {new Point(now, 70), new Point(now - 100000, 155), new Point(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 = new Color (0, 1, 0); - s2.points = {new Series.Point(5, -3), new Series.Point(25, -18), new Series.Point(-11, 173)}; - s2.axis_y.position = Series.Axis.Position.HIGH; + s2.points = {new Point(5, -3), new Point(25, -18), new Point(-11, 173)}; + s2.axis_y.position = Axis.Position.HIGH; s3.title = new Text("Series 3"); s3.color = new Color (0, 0, 1); - s3.points = {new Series.Point(high - 2 + 0.73, -17), new Series.Point(high - 1 + 0.234, 10), new Series.Point(high + 1 + 0.411, 31)}; - s3.axis_y.position = Series.Axis.Position.HIGH; + s3.points = {new Point(high - 2 + 0.73, -17), new Point(high - 1 + 0.234, 10), new Point(high + 1 + 0.411, 31)}; + s3.axis_y.position = Axis.Position.HIGH; s4.title = new Text("Series 4"); s4.color = new Color (0.5, 0.3, 0.9); - s4.points = {new Series.Point(high + 0.005, -19.05), new Series.Point(high + 0.0051, 28), new Series.Point(high + 0.0052, 55), new Series.Point(high + 0.0053, 44)}; - s4.axis_y.position = Series.Axis.Position.HIGH; + s4.points = {new Point(high + 0.005, -19.05), new Point(high + 0.0051, 28), new Point(high + 0.0052, 55), new Point(high + 0.0053, 44)}; + s4.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = now - 100000; s1.axis_x.max = now + 100000; s1.axis_y.min = -20; s1.axis_y.max = 200; @@ -239,40 +239,40 @@ int main (string[] args) { button1.clicked.connect (() => { chart = chart1; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); switch (chart.legend.position) { - case Chart.Legend.Position.TOP: radio_button1.set_active(true); break; - case Chart.Legend.Position.RIGHT: radio_button2.set_active(true); break; - case Chart.Legend.Position.LEFT: radio_button3.set_active(true); break; - case Chart.Legend.Position.BOTTOM: radio_button4.set_active(true); break; + case Legend.Position.TOP: radio_button1.set_active(true); break; + case Legend.Position.RIGHT: radio_button2.set_active(true); break; + case Legend.Position.LEFT: radio_button3.set_active(true); break; + case Legend.Position.BOTTOM: radio_button4.set_active(true); break; default: break; } }); button2.clicked.connect (() => { chart = chart2; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); switch (chart.legend.position) { - case Chart.Legend.Position.TOP: radio_button1.set_active(true); break; - case Chart.Legend.Position.RIGHT: radio_button2.set_active(true); break; - case Chart.Legend.Position.LEFT: radio_button3.set_active(true); break; - case Chart.Legend.Position.BOTTOM: radio_button4.set_active(true); break; + case Legend.Position.TOP: radio_button1.set_active(true); break; + case Legend.Position.RIGHT: radio_button2.set_active(true); break; + case Legend.Position.LEFT: radio_button3.set_active(true); break; + case Legend.Position.BOTTOM: radio_button4.set_active(true); break; default: break; } }); button3.clicked.connect (() => { chart = chart3; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); switch (chart.legend.position) { - case Chart.Legend.Position.TOP: radio_button1.set_active(true); break; - case Chart.Legend.Position.RIGHT: radio_button2.set_active(true); break; - case Chart.Legend.Position.LEFT: radio_button3.set_active(true); break; - case Chart.Legend.Position.BOTTOM: radio_button4.set_active(true); break; + case Legend.Position.TOP: radio_button1.set_active(true); break; + case Legend.Position.RIGHT: radio_button2.set_active(true); break; + case Legend.Position.LEFT: radio_button3.set_active(true); break; + case Legend.Position.BOTTOM: radio_button4.set_active(true); break; default: break; } }); button4.clicked.connect (() => { chart = chart4; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); switch (chart.legend.position) { - case Chart.Legend.Position.TOP: radio_button1.set_active(true); break; - case Chart.Legend.Position.RIGHT: radio_button2.set_active(true); break; - case Chart.Legend.Position.LEFT: radio_button4.set_active(true); break; - case Chart.Legend.Position.BOTTOM: radio_button4.set_active(true); break; + case Legend.Position.TOP: radio_button1.set_active(true); break; + case Legend.Position.RIGHT: radio_button2.set_active(true); break; + case Legend.Position.LEFT: radio_button4.set_active(true); break; + case Legend.Position.BOTTOM: radio_button4.set_active(true); break; default: break; } }); @@ -303,25 +303,25 @@ int main (string[] args) { radio_button1.toggled.connect ((button) => { if (button.get_active()) { - chart.legend.position = Chart.Legend.Position.TOP; + chart.legend.position = Legend.Position.TOP; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); } }); radio_button2.toggled.connect ((button) => { if (button.get_active()) { - chart.legend.position = Chart.Legend.Position.RIGHT; + chart.legend.position = Legend.Position.RIGHT; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); } }); radio_button3.toggled.connect ((button) => { if (button.get_active()) { - chart.legend.position = Chart.Legend.Position.LEFT; + chart.legend.position = Legend.Position.LEFT; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); } }); radio_button4.toggled.connect ((button) => { if (button.get_active()) { - chart.legend.position = Chart.Legend.Position.BOTTOM; + chart.legend.position = Legend.Position.BOTTOM; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height()); } });