From ada0922c696a8ba7677d78656172b9b664f818f5 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 18 Jan 2018 13:52:06 +0300 Subject: [PATCH] OK In progress... --- src/Chart.vala | 83 ++++++++++++++++++++++++--------------------- src/Cursor.vala | 8 ++--- src/Legend.vala | 26 +++++++------- src/Series.vala | 40 +++++++++++----------- test/ChartTest.vala | 4 +-- 5 files changed, 83 insertions(+), 78 deletions(-) diff --git a/src/Chart.vala b/src/Chart.vala index 7161bd8..63170ca 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -8,7 +8,19 @@ namespace CairoChart { /** * Chart Position. */ - public Cairo.Rectangle pos = Cairo.Rectangle(); + public Cairo.Rectangle area = Cairo.Rectangle(); + + /** + * Current evaluated area. + */ + public Cairo.Rectangle evarea = Cairo.Rectangle() + { x = 0, y = 0, width = 1, height = 1 }; + + /** + * Zoom Limits (relative coordinates: 0.0-1.0). + */ + public Cairo.Rectangle zoom = Cairo.Rectangle() + { x = 0, y = 0, width = 1, height = 1 }; /** * Cairo Context of the Drawing Area. @@ -20,38 +32,26 @@ namespace CairoChart { */ public Color bg_color = Color(1, 1, 1); - /** - * Chart Title. - */ - public Text title = new Text ("Cairo Chart"); - /** * Border Color. */ public Color border_color = Color(0, 0, 0, 0.3); + /** + * Chart Title. + */ + public Text title = new Text("Cairo Chart"); + /** * Legend. */ - public Legend legend = new Legend (); + public Legend legend = new Legend(); /** * Chart Series. */ public Series[] series = {}; - /** - * Current calculated Plot Position. - */ - public Cairo.Rectangle calc_pos = Cairo.Rectangle() - { x = 0, y = 0, width = 1, height = 1 }; - - /** - * Zoom Limits (relative coordinates: 0.0-1.0). - */ - public Cairo.Rectangle zoom = Cairo.Rectangle() - { x = 0, y = 0, width = 1, height = 1 }; - /** * 1'st shown series index in zoom area. */ @@ -61,6 +61,11 @@ namespace CairoChart { public Line.Style selection_style = Line.Style (); + /** + * Plot Area Bounds. + */ + //public Cairo.Rectangle plot = ; + public double plot_x_min = 0; public double plot_x_max = 0; public double plot_y_min = 0; @@ -88,7 +93,7 @@ namespace CairoChart { chart.joint_x = this.joint_x; chart.joint_y = this.joint_y; chart.ctx = this.ctx; - chart.calc_pos = this.calc_pos; + chart.evarea = this.evarea; chart.cursors = this.cursors.copy(); chart.legend = this.legend.copy(); chart.plot_x_max = this.plot_x_max; @@ -100,14 +105,14 @@ namespace CairoChart { chart.series = this.series; chart.title = this.title.copy(); chart.title_indent = this.title_indent; - chart.pos = this.pos; + chart.area = this.area; chart.zoom_1st_idx = this.zoom_1st_idx; return chart; } - protected virtual void fix_calc_pos () { - if (calc_pos.width < 0) calc_pos.width = 0; - if (calc_pos.height < 0) calc_pos.height = 0; + protected virtual void fix_evarea () { + if (evarea.width < 0) evarea.width = 0; + if (evarea.height < 0) evarea.height = 0; } protected virtual void set_vertical_axes_titles () { for (var si = 0; si < series.length; ++si) { @@ -126,13 +131,13 @@ namespace CairoChart { public virtual bool draw () { - calc_pos = pos; + evarea = area; draw_chart_title (); - fix_calc_pos (); + fix_evarea (); legend.draw (this); - fix_calc_pos (); + fix_evarea (); set_vertical_axes_titles (); @@ -141,29 +146,29 @@ namespace CairoChart { calc_plot_area (); draw_horizontal_axes (); - fix_calc_pos (); + fix_evarea (); draw_vertical_axes (); - fix_calc_pos (); + fix_evarea (); draw_plot_area_border (); - fix_calc_pos (); + fix_evarea (); draw_series (); - fix_calc_pos (); + fix_evarea (); cursors.draw_cursors (this); - fix_calc_pos (); + fix_evarea (); return true; } protected virtual void draw_chart_title () { var sz = title.get_size(ctx); var title_height = sz.height + (legend.position == Legend.Position.TOP ? title_indent * 2 : title_indent); - calc_pos.y += title_height; - calc_pos.height -= title_height; + evarea.y += title_height; + evarea.height -= title_height; color = title.color; - ctx.move_to (pos.width/2 - sz.width/2, sz.height + title_indent); + ctx.move_to (area.width/2 - sz.width/2, sz.height + title_indent); title.show(ctx); } public virtual void draw_selection (Cairo.Rectangle rect) { @@ -298,10 +303,10 @@ namespace CairoChart { series[si].join_calc(is_x, si, ref nskip); } protected virtual void calc_plot_area () { - plot_x_min = calc_pos.x + legend.indent; - plot_x_max = calc_pos.x + calc_pos.width - legend.indent; - plot_y_min = calc_pos.y + legend.indent; - plot_y_max = calc_pos.y + calc_pos.height - legend.indent; + plot_x_min = evarea.x + legend.indent; + plot_x_max = evarea.x + evarea.width - legend.indent; + plot_y_min = evarea.y + legend.indent; + plot_y_max = evarea.y + evarea.height - legend.indent; // Check for joint axes joint_x = joint_y = true; diff --git a/src/Cursor.vala b/src/Cursor.vala index 0fd1b78..39c0740 100644 --- a/src/Cursor.vala +++ b/src/Cursor.vala @@ -301,13 +301,13 @@ namespace CairoChart { var time_text_t = new Text(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.pos.y + chart.pos.height - s.axis_x.font_indent + case Axis.Position.LOW: print_y = chart.area.y + chart.area.height - s.axis_x.font_indent - (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 ? chart.title_indent * 2 : chart.title_indent); - print_y = chart.pos.y + title_height + s.axis_x.font_indent + print_y = chart.area.y + title_height + s.axis_x.font_indent + (chart.legend.position == Legend.Position.TOP ? chart.legend.height : 0); switch (s.axis_x.type) { case Axis.Type.NUMBERS: @@ -351,11 +351,11 @@ namespace CairoChart { var print_x = 0.0; switch (s.axis_y.position) { case Axis.Position.LOW: - print_x = chart.pos.x + s.axis_y.font_indent + print_x = chart.area.x + s.axis_y.font_indent + (chart.legend.position == Legend.Position.LEFT ? chart.legend.width : 0); break; case Axis.Position.HIGH: - print_x = chart.pos.x + chart.pos.width - text_t.get_width(chart.ctx) - s.axis_y.font_indent + print_x = chart.area.x + chart.area.width - text_t.get_width(chart.ctx) - s.axis_y.font_indent - (chart.legend.position == Legend.Position.RIGHT ? chart.legend.width : 0); break; } diff --git a/src/Legend.vala b/src/Legend.vala index bd52386..538e15a 100644 --- a/src/Legend.vala +++ b/src/Legend.vala @@ -51,25 +51,25 @@ namespace CairoChart { if (chart.ctx != null) { switch (position) { case Position.TOP: - x0 = (chart.pos.width - width) / 2; + x0 = (chart.area.width - width) / 2; var title_height = chart.title.get_height(chart.ctx) + (chart.legend.position == Legend.Position.TOP ? chart.title_indent * 2 : chart.title_indent); y0 = title_height; break; case Position.BOTTOM: - x0 = (chart.pos.width - width) / 2; - y0 = chart.pos.height - height; + x0 = (chart.area.width - width) / 2; + y0 = chart.area.height - height; break; case Position.LEFT: x0 = 0; - y0 = (chart.pos.height - height) / 2; + y0 = (chart.area.height - height) / 2; break; case Position.RIGHT: - x0 = chart.pos.width - width; - y0 = (chart.pos.height - height) / 2; + x0 = chart.area.width - width; + y0 = (chart.area.height - height) / 2; break; } chart.color = bg_color; @@ -122,7 +122,7 @@ namespace CairoChart { case Position.TOP: case Position.BOTTOM: var ser_title_width = title_sz.width + line_length; - if (leg_width_sum + (leg_width_sum == 0 ? 0 : text_hspace) + ser_title_width > chart.pos.width) { // carry + if (leg_width_sum + (leg_width_sum == 0 ? 0 : text_hspace) + ser_title_width > chart.area.width) { // carry leg_height_sum += max_font_h; switch (process_type) { case ProcessType.CALC: @@ -203,18 +203,18 @@ namespace CairoChart { height = leg_height_sum; switch (position) { case Position.TOP: - chart.calc_pos.y += height; - chart.calc_pos.height -= height; + chart.evarea.y += height; + chart.evarea.height -= height; break; case Position.BOTTOM: - chart.calc_pos.height -= height; + chart.evarea.height -= height; break; case Position.LEFT: - chart.calc_pos.x += width; - chart.calc_pos.width -= width; + chart.evarea.x += width; + chart.evarea.width -= width; break; case Position.RIGHT: - chart.calc_pos.width -= width; + chart.evarea.width -= width; break; } break; diff --git a/src/Series.vala b/src/Series.vala index ae16215..cc8bb3b 100644 --- a/src/Series.vala +++ b/src/Series.vala @@ -258,7 +258,7 @@ namespace CairoChart { switch (axis_x.position) { case Axis.Position.LOW: - var print_y = chart.calc_pos.y + chart.calc_pos.height - axis_x.font_indent - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); + var print_y = chart.evarea.y + chart.evarea.height - axis_x.font_indent - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); var print_x = compact_rec_x_pos (x, text_t); ctx.move_to (print_x, print_y); switch (axis_x.type) { @@ -277,7 +277,7 @@ namespace CairoChart { var line_style = grid.line_style; if (joint_x) line_style.color = Color(0, 0, 0, 0.5); line_style.set(chart); - double y = chart.calc_pos.y + chart.calc_pos.height - max_rec_height - axis_x.font_indent - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); + double y = chart.evarea.y + chart.evarea.height - max_rec_height - axis_x.font_indent - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); ctx.move_to (scr_x, y); if (joint_x) ctx.line_to (scr_x, chart.plot_y_min); @@ -285,7 +285,7 @@ namespace CairoChart { ctx.line_to (scr_x, double.min (y, chart.plot_y_max - (chart.plot_y_max - chart.plot_y_min) * place.zoom_y_max)); break; case Axis.Position.HIGH: - var print_y = chart.calc_pos.y + max_rec_height + axis_x.font_indent + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); + var print_y = chart.evarea.y + max_rec_height + axis_x.font_indent + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); var print_x = compact_rec_x_pos (x, text_t); ctx.move_to (print_x, print_y); @@ -305,7 +305,7 @@ namespace CairoChart { var line_style = grid.line_style; if (joint_x) line_style.color = Color(0, 0, 0, 0.5); line_style.set(chart); - double y = chart.calc_pos.y + max_rec_height + axis_x.font_indent + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); + double y = chart.evarea.y + max_rec_height + axis_x.font_indent + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_indent); ctx.move_to (scr_x, y); if (joint_x) ctx.line_to (scr_x, chart.plot_y_max); @@ -348,8 +348,8 @@ namespace CairoChart { // 4.2. Cursor values for joint X axis if (chart.joint_x && chart.cursors.cursor_style.orientation == Cursors.Orientation.VERTICAL && chart.cursors.cursors_crossings.length != 0) { switch (s.axis_x.position) { - case Axis.Position.LOW: chart.calc_pos.height -= max_rec_height + s.axis_x.font_indent; break; - case Axis.Position.HIGH: var tmp = max_rec_height + s.axis_x.font_indent; chart.calc_pos.y += tmp; chart.calc_pos.height -= tmp; break; + case Axis.Position.LOW: chart.evarea.height -= max_rec_height + s.axis_x.font_indent; break; + case Axis.Position.HIGH: var tmp = max_rec_height + s.axis_x.font_indent; chart.evarea.y += tmp; chart.evarea.height -= tmp; break; } } @@ -360,8 +360,8 @@ namespace CairoChart { var scr_x = chart.plot_x_min + (chart.plot_x_max - chart.plot_x_min) * (s.place.zoom_x_min + s.place.zoom_x_max) / 2.0; double scr_y = 0.0; switch (s.axis_x.position) { - case Axis.Position.LOW: scr_y = chart.calc_pos.y + chart.calc_pos.height - s.axis_x.font_indent; break; - case Axis.Position.HIGH: scr_y = chart.calc_pos.y + s.axis_x.font_indent + sz.height; break; + case Axis.Position.LOW: scr_y = chart.evarea.y + chart.evarea.height - s.axis_x.font_indent; break; + case Axis.Position.HIGH: scr_y = chart.evarea.y + s.axis_x.font_indent + sz.height; break; } chart.ctx.move_to(scr_x - sz.width / 2.0, scr_y); chart.color = s.axis_x.color; @@ -380,12 +380,12 @@ namespace CairoChart { switch (s.axis_x.position) { case Axis.Position.LOW: - chart.calc_pos.height -= max_rec_height + s.axis_x.font_indent + chart.evarea.height -= max_rec_height + s.axis_x.font_indent + (s.axis_x.title.text == "" ? 0 : sz.height + s.axis_x.font_indent); break; case Axis.Position.HIGH: var tmp = max_rec_height + s.axis_x.font_indent + (s.axis_x.title.text == "" ? 0 : sz.height + s.axis_x.font_indent); - chart.calc_pos.y += tmp; chart.calc_pos.height -= tmp; + chart.evarea.y += tmp; chart.evarea.height -= tmp; break; } } @@ -406,7 +406,7 @@ namespace CairoChart { switch (axis_y.position) { case Axis.Position.LOW: - ctx.move_to (chart.calc_pos.x + max_rec_width - text_sz.width + axis_y.font_indent + ctx.move_to (chart.evarea.x + max_rec_width - text_sz.width + axis_y.font_indent + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent), compact_rec_y_pos (y, text_t)); text_t.show(ctx); @@ -414,7 +414,7 @@ namespace CairoChart { var line_style = grid.line_style; if (joint_y) line_style.color = Color(0, 0, 0, 0.5); line_style.set(chart); - double x = chart.calc_pos.x + max_rec_width + axis_y.font_indent + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent); + double x = chart.evarea.x + max_rec_width + axis_y.font_indent + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent); ctx.move_to (x, scr_y); if (joint_y) ctx.line_to (chart.plot_x_max, scr_y); @@ -422,7 +422,7 @@ namespace CairoChart { ctx.line_to (double.max (x, chart.plot_x_min + (chart.plot_x_max - chart.plot_x_min) * place.zoom_x_max), scr_y); break; case Axis.Position.HIGH: - ctx.move_to (chart.calc_pos.x + chart.calc_pos.width - text_sz.width - axis_y.font_indent + ctx.move_to (chart.evarea.x + chart.evarea.width - text_sz.width - axis_y.font_indent - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent), compact_rec_y_pos (y, text_t)); text_t.show(ctx); @@ -430,7 +430,7 @@ namespace CairoChart { var line_style = grid.line_style; if (joint_y) line_style.color = Color(0, 0, 0, 0.5); line_style.set(chart); - double x = chart.calc_pos.x + chart.calc_pos.width - max_rec_width - axis_y.font_indent - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent); + double x = chart.evarea.x + chart.evarea.width - max_rec_width - axis_y.font_indent - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_indent); ctx.move_to (x, scr_y); if (joint_y) ctx.line_to (chart.plot_x_min, scr_y); @@ -472,8 +472,8 @@ namespace CairoChart { // 4.2. Cursor values for joint Y axis if (chart.joint_y && chart.cursors.cursor_style.orientation == Cursors.Orientation.HORIZONTAL && chart.cursors.cursors_crossings.length != 0) { switch (s.axis_y.position) { - case Axis.Position.LOW: var tmp = max_rec_width + s.axis_y.font_indent; chart.calc_pos.x += tmp; chart.calc_pos.width -= tmp; break; - case Axis.Position.HIGH: chart.calc_pos.width -= max_rec_width + s.axis_y.font_indent; break; + case Axis.Position.LOW: var tmp = max_rec_width + s.axis_y.font_indent; chart.evarea.x += tmp; chart.evarea.width -= tmp; break; + case Axis.Position.HIGH: chart.evarea.width -= max_rec_width + s.axis_y.font_indent; break; } } @@ -484,11 +484,11 @@ namespace CairoChart { var scr_y = chart.plot_y_max - (chart.plot_y_max - chart.plot_y_min) * (s.place.zoom_y_min + s.place.zoom_y_max) / 2.0; switch (s.axis_y.position) { case Axis.Position.LOW: - var scr_x = chart.calc_pos.x + s.axis_y.font_indent + sz.width; + var scr_x = chart.evarea.x + s.axis_y.font_indent + sz.width; chart.ctx.move_to(scr_x, scr_y + sz.height / 2.0); break; case Axis.Position.HIGH: - var scr_x = chart.calc_pos.x + chart.calc_pos.width - s.axis_y.font_indent; + var scr_x = chart.evarea.x + chart.evarea.width - s.axis_y.font_indent; chart.ctx.move_to(scr_x, scr_y + sz.height / 2.0); break; } @@ -509,10 +509,10 @@ namespace CairoChart { switch (s.axis_y.position) { case Axis.Position.LOW: var tmp = max_rec_width + s.axis_y.font_indent + (s.axis_y.title.text == "" ? 0 : sz.width + s.axis_y.font_indent); - chart.calc_pos.x += tmp; chart.calc_pos.width -= tmp; + chart.evarea.x += tmp; chart.evarea.width -= tmp; break; case Axis.Position.HIGH: - chart.calc_pos.width -= max_rec_width + s.axis_y.font_indent + chart.evarea.width -= max_rec_width + s.axis_y.font_indent + (s.axis_y.title.text == "" ? 0 : sz.width + s.axis_y.font_indent); break; } } diff --git a/test/ChartTest.vala b/test/ChartTest.vala index 2cadb6f..85d937d 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -415,8 +415,8 @@ int main (string[] args) { da.draw.connect((ctx) => { chart.ctx = ctx; - chart.pos.width = da.get_allocated_width(); - chart.pos.height = da.get_allocated_height(); + chart.area.width = da.get_allocated_width(); + chart.area.height = da.get_allocated_height(); chart.clear(); // user's pre draw operations here...