From 7db26d215435a0f0932e9e2f1c40bb22ab832e7c Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Tue, 16 Jan 2018 13:39:17 +0300 Subject: [PATCH] In progress... --- src/Chart.vala | 51 +++++++++++++++++++++++---------------------- src/Cursor.vala | 30 +++++++++++++------------- src/Math.vala | 24 ++++++++++----------- src/Point.vala | 10 ++++++++- src/Series.vala | 14 ++++++------- test/ChartTest.vala | 30 +++++++++++++------------- 6 files changed, 84 insertions(+), 75 deletions(-) diff --git a/src/Chart.vala b/src/Chart.vala index 12c5378..8dce740 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -48,8 +48,8 @@ namespace CairoChart { public CairoChart.Math math { get; protected set; default = new Math(); } public Cursors cursors2 { get; protected set; default = new Cursors (); } - public List cursors = new List (); - public Point active_cursor = Point(); // { get; protected set; default = Point (); } + public List cursors = new List (); + public Point128 active_cursor = Point128(); // { get; protected set; default = Point128 (); } public bool is_cursor_active { get; protected set; default = false; } public Cursors.Style cursor_style = Cursors.Style(); @@ -238,26 +238,27 @@ namespace CairoChart { zoom_first_show = 0; } - public virtual void move (double delta_x, double delta_y) { - delta_x /= plot_x_max - plot_x_min; delta_x *= - 1.0; - delta_y /= plot_y_max - plot_y_min; delta_y *= - 1.0; + public virtual void move (Point delta) { + var d = delta; + d.x /= plot_x_max - plot_x_min; d.x *= - 1.0; + d.y /= plot_y_max - plot_y_min; d.y *= - 1.0; var rzxmin = rz_x_min, rzxmax = rz_x_max, rzymin = rz_y_min, rzymax = rz_y_max; zoom_out(); - delta_x *= plot_x_max - plot_x_min; - delta_y *= plot_y_max - plot_y_min; + d.x *= plot_x_max - plot_x_min; + d.y *= plot_y_max - plot_y_min; var xmin = plot_x_min + (plot_x_max - plot_x_min) * rzxmin; var xmax = plot_x_min + (plot_x_max - plot_x_min) * rzxmax; var ymin = plot_y_min + (plot_y_max - plot_y_min) * rzymin; var ymax = plot_y_min + (plot_y_max - plot_y_min) * rzymax; - delta_x *= rzxmax - rzxmin; delta_y *= rzymax - rzymin; + d.x *= rzxmax - rzxmin; d.y *= rzymax - rzymin; - if (xmin + delta_x < plot_x_min) delta_x = plot_x_min - xmin; - if (xmax + delta_x > plot_x_max) delta_x = plot_x_max - xmax; - if (ymin + delta_y < plot_y_min) delta_y = plot_y_min - ymin; - if (ymax + delta_y > plot_y_max) delta_y = plot_y_max - ymax; + if (xmin + d.x < plot_x_min) d.x = plot_x_min - xmin; + if (xmax + d.x > plot_x_max) d.x = plot_x_max - xmax; + if (ymin + d.y < plot_y_min) d.y = plot_y_min - ymin; + if (ymax + d.y > plot_y_max) d.y = plot_y_max - ymax; - zoom_in (Cairo.Rectangle(){x = xmin + delta_x, y = ymin + delta_y, width = xmax - xmin, height = ymax - ymin}); + zoom_in (Cairo.Rectangle(){x = xmin + d.x, y = ymin + d.y, width = xmax - xmin, height = ymax - ymin}); } protected virtual void draw_chart_title () { @@ -269,9 +270,9 @@ namespace CairoChart { title.show(context); } - public virtual void draw_selection (double x0, double y0, double x1, double y1) { + public virtual void draw_selection (Cairo.Rectangle rect) { selection_style.set(this); - context.rectangle (x0, y0, x1 - x0, y1 - y0); + context.rectangle (rect.x, rect.y, rect.width, rect.height); context.stroke(); } @@ -397,8 +398,8 @@ namespace CairoChart { / (s.axis_y.zoom_max - s.axis_y.zoom_min) * (s.place.zoom_y_max - s.place.zoom_y_min)); } - public virtual Point get_scr_point (Series s, Point p) { - return Point (get_scr_x(s, p.x), get_scr_y(s, p.y)); + public virtual Point128 get_scr_point (Series s, Point128 p) { + return Point128 (get_scr_x(s, p.x), get_scr_y(s, p.y)); } public virtual Float128 get_real_x (Series s, double scr_x) { @@ -411,8 +412,8 @@ namespace CairoChart { * (s.axis_y.zoom_max - s.axis_y.zoom_min) / (s.place.zoom_y_max - s.place.zoom_y_min); } - public virtual Point get_real_point (Series s, Point p) { - return Point (get_real_x(s, p.x), get_real_y(s, p.y)); + public virtual Point128 get_real_point (Series s, Point128 p) { + return Point128 (get_real_x(s, p.x), get_real_y(s, p.y)); } protected virtual bool x_in_plot_area (double x) { @@ -427,7 +428,7 @@ namespace CairoChart { return false; } - public virtual bool point_in_plot_area (Point p) { + public virtual bool point_in_plot_area (Point128 p) { if (math.point_in_rect (p, plot_x_min, plot_x_max, plot_y_min, plot_y_max)) return true; return false; @@ -442,7 +443,7 @@ namespace CairoChart { } public virtual void set_active_cursor (double x, double y, bool remove = false) { - active_cursor = Point (scr2rel_x(x), scr2rel_y(y)); + active_cursor = Point128 (scr2rel_x(x), scr2rel_y(y)); is_cursor_active = ! remove; } @@ -483,8 +484,8 @@ namespace CairoChart { protected virtual Float128 scr2rel_y (Float128 y) { return rz_y_max - (plot_y_max - y) / (plot_y_max - plot_y_min) * (rz_y_max - rz_y_min); } - protected virtual Point scr2rel_point (Point p) { - return Point (scr2rel_x(p.x), scr2rel_y(p.y)); + protected virtual Point128 scr2rel_point (Point128 p) { + return Point128 (scr2rel_x(p.x), scr2rel_y(p.y)); } public virtual Float128 rel2scr_x(Float128 x) { @@ -495,8 +496,8 @@ namespace CairoChart { return plot_y_min + (plot_y_max - plot_y_min) * (y - rz_y_min) / (rz_y_max - rz_y_min); } - public virtual Point rel2scr_point (Point p) { - return Point (rel2scr_x(p.x), rel2scr_y(p.y)); + public virtual Point128 rel2scr_point (Point128 p) { + return Point128 (rel2scr_x(p.x), rel2scr_y(p.y)); } } } diff --git a/src/Cursor.vala b/src/Cursor.vala index 7c286f5..c6ef1d8 100644 --- a/src/Cursor.vala +++ b/src/Cursor.vala @@ -30,21 +30,21 @@ namespace CairoChart { protected struct CursorCross { uint series_index; - Point point; - Point size; + Point128 point; + Point128 size; bool show_x; bool show_date; bool show_time; bool show_y; - Point scr_point; - Point scr_value_point; + Point128 scr_point; + Point128 scr_value_point; } protected struct CursorCrossings { uint cursor_index; CursorCross[] crossings; } - protected List get_all_cursors (Chart chart) { + protected List get_all_cursors (Chart chart) { var all_cursors = chart.cursors.copy_deep ((src) => { return src; }); if (chart.is_cursor_active) all_cursors.append(chart.active_cursor); @@ -70,7 +70,7 @@ namespace CairoChart { var s = chart.series[si]; if (!s.zoom_show) continue; - Point[] points = {}; + Point128[] points = {}; switch (chart.cursor_style.orientation) { case Orientation.VERTICAL: points = chart.math.sort_points (s, s.sort); @@ -86,8 +86,8 @@ namespace CairoChart { Float128 y = 0.0; if (chart.math.vcross(chart.get_scr_point(s, points[i]), chart.get_scr_point(s, points[i+1]), chart.rel2scr_x(c.x), chart.plot_y_min, chart.plot_y_max, out y)) { - var point = Point(chart.get_real_x(s, chart.rel2scr_x(c.x)), chart.get_real_y(s, y)); - Point size; bool show_x, show_date, show_time, show_y; + var point = Point128(chart.get_real_x(s, chart.rel2scr_x(c.x)), chart.get_real_y(s, y)); + Point128 size; bool show_x, show_date, show_time, show_y; cross_what_to_show(chart, s, out show_x, out show_time, out show_date, out show_y); calc_cross_sizes (chart, s, point, out size, show_x, show_time, show_date, show_y); CursorCross cc = {si, point, size, show_x, show_date, show_time, show_y}; @@ -98,8 +98,8 @@ namespace CairoChart { Float128 x = 0.0; if (chart.math.hcross(chart.get_scr_point(s, points[i]), chart.get_scr_point(s, points[i+1]), chart.plot_x_min, chart.plot_x_max, chart.rel2scr_y(c.y), out x)) { - var point = Point(chart.get_real_x(s, x), chart.get_real_y(s, chart.rel2scr_y(c.y))); - Point size; bool show_x, show_date, show_time, show_y; + var point = Point128(chart.get_real_x(s, x), chart.get_real_y(s, chart.rel2scr_y(c.y))); + Point128 size; bool show_x, show_date, show_time, show_y; cross_what_to_show(chart, s, out show_x, out show_time, out show_date, out show_y); calc_cross_sizes (chart, s, point, out size, show_x, show_time, show_date, show_y); CursorCross cc = {si, point, size, show_x, show_date, show_time, show_y}; @@ -124,7 +124,7 @@ namespace CairoChart { unowned CursorCross[] cr = chart.cursors_crossings[ccsi].crossings; cr[cci].scr_point = chart.get_scr_point (chart.series[cr[cci].series_index], cr[cci].point); var d_max = double.max (cr[cci].size.x / 1.5, cr[cci].size.y / 1.5); - cr[cci].scr_value_point = Point (cr[cci].scr_point.x + d_max, cr[cci].scr_point.y - d_max); + cr[cci].scr_value_point = Point128 (cr[cci].scr_point.x + d_max, cr[cci].scr_point.y - d_max); } } } @@ -157,12 +157,12 @@ namespace CairoChart { } } - protected virtual void calc_cross_sizes (Chart chart, Series s, Point p, out Point size, + protected virtual void calc_cross_sizes (Chart chart, Series s, Point128 p, out Point128 size, bool show_x = false, bool show_time = false, bool show_date = false, bool show_y = false) { if (show_x == show_time == show_date == show_y == false) cross_what_to_show(chart, s, out show_x, out show_time, out show_date, out show_y); - size = Point (); + 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); @@ -186,8 +186,8 @@ namespace CairoChart { calc_cursors_value_positions(chart); for (var cci = 0, max_cci = chart.cursors_crossings.length; cci < max_cci; ++cci) { - var low = Point(chart.plot_x_max, chart.plot_y_max); // low and high - var high = Point(chart.plot_x_min, chart.plot_y_min); // points of the cursor + var low = Point128(chart.plot_x_max, chart.plot_y_max); // low and high + var high = Point128(chart.plot_x_min, chart.plot_y_min); // points of the cursor unowned CursorCross[] ccs = chart.cursors_crossings[cci].crossings; chart.cursor_style.line_style.set(chart); for (var ci = 0, max_ci = ccs.length; ci < max_ci; ++ci) { diff --git a/src/Math.vala b/src/Math.vala index 3328676..e77e0d2 100644 --- a/src/Math.vala +++ b/src/Math.vala @@ -46,13 +46,13 @@ namespace CairoChart { return false; } - public virtual bool point_in_rect (Point p, double x0, double x1, double y0, double y1) { + public virtual bool point_in_rect (Point128 p, double x0, double x1, double y0, double y1) { if (x_in_range(p.x, x0, x1) && y_in_range(p.y, y0, y1)) return true; return false; } - public virtual bool hcross (Point a1, Point a2, Float128 h_x1, Float128 h_x2, Float128 h_y, out Float128 x) { + public virtual bool hcross (Point128 a1, Point128 a2, Float128 h_x1, Float128 h_x2, Float128 h_y, out Float128 x) { x = 0; if (a1.y == a2.y) return false; if (a1.y >= h_y && a2.y >= h_y || a1.y <= h_y && a2.y <= h_y) return false; @@ -62,7 +62,7 @@ namespace CairoChart { return false; } - public virtual bool vcross (Point a1, Point a2, Float128 v_x, Float128 v_y1, Float128 v_y2, out Float128 y) { + public virtual bool vcross (Point128 a1, Point128 a2, Float128 v_x, Float128 v_y1, Float128 v_y2, out Float128 y) { y = 0; if (a1.x == a2.x) return false; if (a1.x >= v_x && a2.x >= v_x || a1.x <= v_x && a2.x <= v_x) return false; @@ -72,9 +72,9 @@ namespace CairoChart { return false; } - public delegate int PointComparator(Point a, Point b); + public delegate int PointComparator(Point128 a, Point128 b); - public virtual void sort_points_delegate(Point[] points, PointComparator compare) { + public virtual void sort_points_delegate(Point128[] 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) { @@ -86,18 +86,18 @@ namespace CairoChart { } } - public virtual bool cut_line (Point p_min, Point p_max, Point a, Point b, out Point c, out Point d) { + public virtual bool cut_line (Point128 p_min, Point128 p_max, Point128 a, Point128 b, out Point128 c, out Point128 d) { int ncross = 0; Float128 x = 0, y = 0; - Point pc[4]; + Point128 pc[4]; if (hcross(a, b, p_min.x, p_max.x, p_min.y, out x)) - pc[ncross++] = Point(x, p_min.y); + pc[ncross++] = Point128(x, p_min.y); if (hcross(a, b, p_min.x, p_max.x, p_max.y, out x)) - pc[ncross++] = Point(x, p_max.y); + pc[ncross++] = Point128(x, p_max.y); if (vcross(a, b, p_min.x, p_min.y, p_max.y, out y)) - pc[ncross++] = Point(p_min.x, y); + pc[ncross++] = Point128(p_min.x, y); if (vcross(a, b, p_max.x, p_min.y, p_max.y, out y)) - pc[ncross++] = Point(p_max.x, y); + pc[ncross++] = Point128(p_max.x, y); c = a; d = b; if (ncross == 0) { @@ -124,7 +124,7 @@ namespace CairoChart { return false; } - public virtual Point[] sort_points (Series s, Series.Sort sort) { + public virtual Point128[] sort_points (Series s, Series.Sort sort) { var points = s.points; switch(sort) { case Series.Sort.BY_X: diff --git a/src/Point.vala b/src/Point.vala index afde491..07cc591 100644 --- a/src/Point.vala +++ b/src/Point.vala @@ -1,9 +1,17 @@ namespace CairoChart { public struct Point { + double x; + double y; + + public Point (double x = 0.0, double y = 0.0) { + this.x = x; this.y = y; + } + } + public struct Point128 { Float128 x; Float128 y; - public Point (Float128 x = 0.0, Float128 y = 0.0) { + public Point128 (Float128 x = 0.0, Float128 y = 0.0) { this.x = x; this.y = y; } } diff --git a/src/Series.vala b/src/Series.vala index 0786d75..1bdec7e 100644 --- a/src/Series.vala +++ b/src/Series.vala @@ -4,7 +4,7 @@ namespace CairoChart { public class Series { - public Point[] points = {}; + public Point128[] points = {}; public enum Sort { BY_X = 0, BY_Y = 1, @@ -65,12 +65,12 @@ namespace CairoChart { line_style.set(chart); // draw series line for (int i = 1; i < points.length; ++i) { - Point c, d; + Point128 c, d; if (chart.math.cut_line ( - Point(chart.plot_x_min, chart.plot_y_min), - Point(chart.plot_x_max, chart.plot_y_max), - Point(chart.get_scr_x(this, points[i - 1].x), chart.get_scr_y(this, points[i - 1].y)), - Point(chart.get_scr_x(this, points[i].x), chart.get_scr_y(this, points[i].y)), + Point128(chart.plot_x_min, chart.plot_y_min), + Point128(chart.plot_x_max, chart.plot_y_max), + Point128(chart.get_scr_x(this, points[i - 1].x), chart.get_scr_y(this, points[i - 1].y)), + Point128(chart.get_scr_x(this, points[i].x), chart.get_scr_y(this, points[i].y)), out c, out d) ) { chart.context.move_to (c.x, c.y); @@ -81,7 +81,7 @@ namespace CairoChart { for (int i = 0; i < points.length; ++i) { var x = chart.get_scr_x(this, points[i].x); var y = chart.get_scr_y(this, points[i].y); - if (chart.point_in_plot_area (Point (x, y))) + if (chart.point_in_plot_area (Point128 (x, y))) marker.draw_at_pos(chart, x, y); } } diff --git a/test/ChartTest.vala b/test/ChartTest.vala index 95adc2b..d6ceaa4 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -6,13 +6,13 @@ void plot_chart1 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); - s1.points = {Point(0, 0), Point(2, 1), Point(1, 3)}; + 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.points = {Point(5, -3), Point(25, -18), Point(-11, 173)}; + s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); - s3.points = {Point(9, 17), Point(2, 10), Point(122, 31)}; + s3.points = {Point128(9, 17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = 0; s1.axis_x.max = 2; @@ -50,13 +50,13 @@ void plot_chart2 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); - s1.points = {Point(-12, 0), Point(2, 1), Point(20, 3)}; + 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.points = {Point(5, -3), Point(25, -18), Point(-11, 173)}; + s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); - s3.points = {Point(9, 17), Point(2, 10), Point(-15, 31)}; + s3.points = {Point128(9, 17), Point128(2, 10), Point128(-15, 31)}; s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = -15; s1.axis_x.max = 30; @@ -98,15 +98,15 @@ void plot_chart3 (Chart chart) { var s3 = new Series (); s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); - s1.points = {Point(0, 70), Point(2, 155), Point(1, -3)}; + 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.points = {Point(5, -3), Point(25, -18), Point(-11, 173)}; + 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.points = {Point(9, -17), Point(2, 10), Point(122, 31)}; + s3.points = {Point128(9, -17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; s1.axis_x.min = 0; s1.axis_x.max = 2; @@ -155,17 +155,17 @@ void plot_chart4 (Chart chart) { var high = (uint64) (253000000000L); s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); - s1.points = {Point(now, 70), Point(now - 100000, 155), Point(now + 100000, 30)}; + 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.points = {Point(5, -3), Point(25, -18), Point(-11, 173)}; + 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.points = {Point(high - 2 + 0.73, -17), Point(high - 1 + 0.234, 10), Point(high + 1 + 0.411, 31)}; + 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.points = {Point(high + 0.005, -19.05), Point(high + 0.0051, 28), Point(high + 0.0052, 55), Point(high + 0.0053, 44)}; + 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; s1.axis_x.min = now - 100000; s1.axis_x.max = now + 100000; @@ -426,7 +426,7 @@ int main (string[] args) { // user's post draw operations here... if (mouse_state == MouseState.DRAW_SELECTION) - chart.draw_selection (sel_x0, sel_y0, sel_x1, sel_y1); + chart.draw_selection (Cairo.Rectangle() {x = sel_x0, y = sel_y0, width = sel_x1 - sel_x0, height = sel_y1 - sel_y0}); // show delta var str = chart.cursors2.get_cursors_delta_str(chart); @@ -525,7 +525,7 @@ int main (string[] args) { case MouseState.MOVING_CHART: var delta_x = event.x - mov_x0, delta_y = event.y - mov_y0; - chart.move (delta_x, delta_y); + chart.move (Point(){x = delta_x, y = delta_y}); mov_x0 = event.x; mov_y0 = event.y; da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height());