From 0093299e093e621ec00e1a18b1a4a625d7d12c64 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Sun, 17 Dec 2017 17:21:39 +0300 Subject: [PATCH] =?UTF-8?q?Closes=20#144:=20Show=20=CE=94=20X=20or=20?= =?UTF-8?q?=CE=94=20Y=20for=202=20cursors.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Chart.vala | 62 ++++++++++++++++++++++++++++++++++++++++++--- test/ChartTest.vala | 18 +++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/Chart.vala b/src/Chart.vala index c017356..4861227 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -75,7 +75,7 @@ namespace Gtk.CairoChart { return true; } - protected virtual void set_source_rgba (Color color) { + public virtual void set_source_rgba (Color color) { context.set_source_rgba (color.red, color.green, color.blue, color.alpha); } @@ -522,8 +522,8 @@ namespace Gtk.CairoChart { public double plot_area_y_min = 0; public double plot_area_y_max = 0; - bool common_x_axes = false; - bool common_y_axes = false; + public bool common_x_axes { get; protected set; default = false; } + public bool common_y_axes { get; protected set; default = false; } public Color common_axis_color = Color (0, 0, 0, 1); bool are_intersect (double a_min, double a_max, double b_min, double b_max) { @@ -1625,6 +1625,62 @@ namespace Gtk.CairoChart { } } + public bool get_cursors_delta (out Float128 delta) { + delta = 0.0; + if (series.length == 0) return false; + if (cursors.length() + (is_cursor_active ? 1 : 0) != 2) return false; + if (common_x_axes && cursors_orientation == CursorOrientation.VERTICAL) { + Float128 val1 = get_real_x (series[zoom_first_show], rel2scr_x(cursors.nth_data(0).x)); + Float128 val2 = 0; + if (is_cursor_active) + val2 = get_real_x (series[zoom_first_show], rel2scr_x(active_cursor.x)); + else + val2 = get_real_x (series[zoom_first_show], rel2scr_x(cursors.nth_data(1).x)); + if (val2 > val1) + delta = val2 - val1; + else + delta = val1 - val2; + return true; + } + if (common_y_axes && cursors_orientation == CursorOrientation.HORIZONTAL) { + Float128 val1 = get_real_y (series[zoom_first_show], rel2scr_y(cursors.nth_data(0).y)); + Float128 val2 = 0; + if (is_cursor_active) + val2 = get_real_y (series[zoom_first_show], rel2scr_y(active_cursor.y)); + else + val2 = get_real_y (series[zoom_first_show], rel2scr_y(cursors.nth_data(1).y)); + if (val2 > val1) + delta = val2 - val1; + else + delta = val1 - val2; + return true; + } + return false; + } + + public string get_cursors_delta_str () { + Float128 delta = 0.0; + if (!get_cursors_delta(out delta)) return ""; + var str = ""; + var s = series[zoom_first_show]; + if (common_x_axes) + switch (s.axis_x.type) { + case Axis.Type.NUMBERS: + str = s.axis_x.format.printf((LongDouble)delta); + break; + case Axis.Type.DATE_TIME: + var date = "", time = ""; + int64 days = (int64)(delta / 24 / 3600); + format_date_time(s.axis_x, delta, out date, out time); + str = days.to_string() + " + " + time; + break; + } + if (common_y_axes) { + str = s.axis_y.format.printf((LongDouble)delta); + } + return str; + } + public Chart copy () { var chart = new Chart (); chart.active_cursor = this.active_cursor; diff --git a/test/ChartTest.vala b/test/ChartTest.vala index badf72d..a3e4f15 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -85,6 +85,7 @@ void plot_chart2 (Chart chart) { //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; + //s1.axis_x.max = s2.axis_x.max = s3.axis_x.max = 5*24*3600; chart.series = { s1, s2, s3 }; } @@ -423,6 +424,23 @@ int main (string[] args) { if (mouse_state == MouseState.DRAW_SELECTION) chart.draw_selection (sel_x0, sel_y0, sel_x1, sel_y1); + // show delta + var str = chart.get_cursors_delta_str(); + if (str != "") { + var text = "Δ = " + str; + var text_t = new Text(text); + var w = text_t.get_width(context); + var h = text_t.get_height(context); + var x0 = chart.plot_area_x_max - w - 5; + var y0 = chart.plot_area_y_min + h + 5; + chart.set_source_rgba(chart.legend.bg_color); + context.rectangle (x0, y0 - h, w, h); + context.fill(); + context.move_to (x0, y0); + chart.set_source_rgba(chart.common_axis_color); + context.show_text(text); + } + return true;//ret; });