In progress...

This commit is contained in:
Kolan Sh 2018-01-16 19:31:32 +03:00
parent b6a8c4e6ee
commit 117796a64c
4 changed files with 60 additions and 60 deletions

View File

@ -48,19 +48,12 @@ namespace CairoChart {
public CairoChart.Math math { get; protected set; default = new Math(); }
public Cursors cursors2 { get; protected set; default = new Cursors (); }
public List<Point?> cursors = new List<Point?> ();
public Point active_cursor = Point(); // { get; protected set; default = Point128 (); }
public bool is_cursor_active { get; protected set; default = false; }
public Cursors.Style cursor_style = Cursors.Style();
public Cursors.CursorCrossings[] cursors_crossings = {};
public Chart () {
}
public Chart copy () {
var chart = new Chart ();
chart.active_cursor = this.active_cursor;
chart.bg_color = this.bg_color;
chart.border_color = this.border_color;
chart.joint_x = this.joint_x;
@ -70,12 +63,8 @@ namespace CairoChart {
chart.cur_x_min = this.cur_x_min;
chart.cur_y_max = this.cur_y_max;
chart.cur_y_min = this.cur_y_min;
chart.cursor_style = this.cursor_style;
chart.cursors = this.cursors.copy();
chart.cursors2 = this.cursors2.copy();
chart.cursors_crossings = this.cursors_crossings;
chart.height = this.height;
chart.is_cursor_active = this.is_cursor_active;
chart.legend = this.legend.copy();
chart.plot_x_max = this.plot_x_max;
chart.plot_x_min = this.plot_x_min;
@ -301,8 +290,8 @@ namespace CairoChart {
s.join_relative_y_axes (si, true, ref max_rec_width, ref max_rec_height, ref max_font_indent, ref max_axis_font_width, ref nskip);
// for 4.2. Cursor values for joint X axis
if (si == zoom_first_show && cursors_crossings.length != 0) {
switch (cursor_style.orientation) {
if (si == zoom_first_show && cursors2.cursors_crossings.length != 0) {
switch (cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL:
if (is_x && joint_x)
switch (axis.position) {
@ -402,28 +391,28 @@ namespace CairoChart {
}
public virtual void set_active_cursor (Point p, bool remove = false) {
active_cursor = scr2rel_point(p);
is_cursor_active = ! remove;
cursors2.active_cursor = scr2rel_point(p);
cursors2.is_cursor_active = ! remove;
}
public virtual void add_active_cursor () {
cursors.append (active_cursor);
is_cursor_active = false;
cursors2.cursors.append (cursors2.active_cursor);
cursors2.is_cursor_active = false;
}
public virtual void remove_active_cursor () {
if (cursors.length() == 0) return;
if (cursors2.cursors.length() == 0) return;
var distance = width * width;
uint rm_indx = 0;
uint i = 0;
foreach (var c in cursors) {
foreach (var c in cursors2.cursors) {
double d = distance;
switch (cursor_style.orientation) {
switch (cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL:
d = (rel2scr_x(c.x) - rel2scr_x(active_cursor.x)).abs();
d = (rel2scr_x(c.x) - rel2scr_x(cursors2.active_cursor.x)).abs();
break;
case Cursors.Orientation.HORIZONTAL:
d = (rel2scr_y(c.y) - rel2scr_y(active_cursor.y)).abs();
d = (rel2scr_y(c.y) - rel2scr_y(cursors2.active_cursor.y)).abs();
break;
}
if (d < distance) {
@ -432,9 +421,9 @@ namespace CairoChart {
}
++i;
}
if (distance < cursor_style.select_distance)
cursors.delete_link(cursors.nth(rm_indx));
is_cursor_active = false;
if (distance < cursors2.cursor_style.select_distance)
cursors2.cursors.delete_link(cursors2.cursors.nth(rm_indx));
cursors2.is_cursor_active = false;
}
protected virtual Float128 scr2rel_x (Float128 x) {

View File

@ -2,11 +2,22 @@ namespace CairoChart {
public class Cursors {
public List<Point?> cursors = new List<Point?> ();
public Point active_cursor = Point(); // { get; protected set; default = Point128 (); }
public bool is_cursor_active = false; // { get; protected set; default = false; }
public Cursors.Style cursor_style = Cursors.Style();
public Cursors.CursorCrossings[] cursors_crossings = {};
public Cursors () {
}
public Cursors copy () {
var c = new Cursors ();
c.cursors = cursors.copy();
c.active_cursor = active_cursor;
c.is_cursor_active = is_cursor_active;
c.cursor_style = cursor_style;
c.cursors_crossings = cursors_crossings;
return c;
}
@ -45,9 +56,9 @@ namespace CairoChart {
}
protected List<Point?> 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);
var all_cursors = cursors.copy_deep ((src) => { return src; });
if (is_cursor_active)
all_cursors.append(active_cursor);
return all_cursors;
}
@ -58,7 +69,7 @@ namespace CairoChart {
for (var ci = 0, max_ci = all_cursors.length(); ci < max_ci; ++ci) {
var c = all_cursors.nth_data(ci);
switch (chart.cursor_style.orientation) {
switch (cursor_style.orientation) {
case Orientation.VERTICAL:
if (c.x <= chart.rz_x_min || c.x >= chart.rz_x_max) continue; break;
case Orientation.HORIZONTAL:
@ -71,7 +82,7 @@ namespace CairoChart {
if (!s.zoom_show) continue;
Point128[] points = {};
switch (chart.cursor_style.orientation) {
switch (cursor_style.orientation) {
case Orientation.VERTICAL:
points = chart.math.sort_points (s, s.sort);
break;
@ -81,7 +92,7 @@ namespace CairoChart {
}
for (var i = 0; i + 1 < points.length; ++i) {
switch (chart.cursor_style.orientation) {
switch (cursor_style.orientation) {
case Orientation.VERTICAL:
Float128 y = 0.0;
if (chart.math.vcross(s.get_scr_point(points[i]), s.get_scr_point(points[i+1]), chart.rel2scr_x(c.x),
@ -114,14 +125,14 @@ namespace CairoChart {
local_cursor_crossings += ccs;
}
}
chart.cursors_crossings = local_cursor_crossings;
cursors_crossings = local_cursor_crossings;
}
protected virtual void calc_cursors_value_positions (Chart chart) {
for (var ccsi = 0, max_ccsi = chart.cursors_crossings.length; ccsi < max_ccsi; ++ccsi) {
for (var cci = 0, max_cci = chart.cursors_crossings[ccsi].crossings.length; cci < max_cci; ++cci) {
for (var ccsi = 0, max_ccsi = cursors_crossings.length; ccsi < max_ccsi; ++ccsi) {
for (var cci = 0, max_cci = cursors_crossings[ccsi].crossings.length; cci < max_cci; ++cci) {
// TODO: Ticket #142: find smart algorithm of cursors values placements
unowned CursorCross[] cr = chart.cursors_crossings[ccsi].crossings;
unowned CursorCross[] cr = cursors_crossings[ccsi].crossings;
cr[cci].scr_point = chart.series[cr[cci].series_index].get_scr_point (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);
@ -132,7 +143,7 @@ namespace CairoChart {
protected virtual void cross_what_to_show (Chart chart, Series s, out bool show_x, out bool show_time,
out bool show_date, out bool show_y) {
show_x = show_time = show_date = show_y = false;
switch (chart.cursor_style.orientation) {
switch (cursor_style.orientation) {
case Orientation.VERTICAL:
show_y = true;
if (!chart.joint_x)
@ -185,11 +196,11 @@ namespace CairoChart {
var all_cursors = get_all_cursors(chart);
calc_cursors_value_positions(chart);
for (var cci = 0, max_cci = chart.cursors_crossings.length; cci < max_cci; ++cci) {
for (var cci = 0, max_cci = cursors_crossings.length; cci < max_cci; ++cci) {
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);
unowned CursorCross[] ccs = cursors_crossings[cci].crossings;
cursor_style.line_style.set(chart);
for (var ci = 0, max_ci = ccs.length; ci < max_ci; ++ci) {
var si = ccs[ci].series_index;
var s = chart.series[si];
@ -226,9 +237,9 @@ namespace CairoChart {
chart.context.line_to (ccs[ci].scr_value_point.x, ccs[ci].scr_value_point.y);
}
var c = all_cursors.nth_data(chart.cursors_crossings[cci].cursor_index);
var c = all_cursors.nth_data(cursors_crossings[cci].cursor_index);
switch (chart.cursor_style.orientation) {
switch (cursor_style.orientation) {
case Orientation.VERTICAL:
if (low.y > high.y) continue;
chart.context.move_to (chart.rel2scr_x(c.x), low.y);
@ -381,27 +392,27 @@ namespace CairoChart {
public bool get_cursors_delta (Chart chart, out Float128 delta) {
delta = 0.0;
if (chart.series.length == 0) return false;
if (chart.cursors.length() + (chart.is_cursor_active ? 1 : 0) != 2) return false;
if (chart.joint_x && chart.cursor_style.orientation == Orientation.VERTICAL) {
Float128 val1 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(chart.cursors.nth_data(0).x));
if (cursors.length() + (is_cursor_active ? 1 : 0) != 2) return false;
if (chart.joint_x && cursor_style.orientation == Orientation.VERTICAL) {
Float128 val1 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(cursors.nth_data(0).x));
Float128 val2 = 0;
if (chart.is_cursor_active)
val2 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(chart.active_cursor.x));
if (is_cursor_active)
val2 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(active_cursor.x));
else
val2 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(chart.cursors.nth_data(1).x));
val2 = chart.series[chart.zoom_first_show].get_real_x(chart.rel2scr_x(cursors.nth_data(1).x));
if (val2 > val1)
delta = val2 - val1;
else
delta = val1 - val2;
return true;
}
if (chart.joint_y && chart.cursor_style.orientation == Orientation.HORIZONTAL) {
Float128 val1 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(chart.cursors.nth_data(0).y));
if (chart.joint_y && cursor_style.orientation == Orientation.HORIZONTAL) {
Float128 val1 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(cursors.nth_data(0).y));
Float128 val2 = 0;
if (chart.is_cursor_active)
val2 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(chart.active_cursor.y));
if (is_cursor_active)
val2 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(active_cursor.y));
else
val2 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(chart.cursors.nth_data(1).y));
val2 = chart.series[chart.zoom_first_show].get_real_y(chart.rel2scr_y(cursors.nth_data(1).y));
if (val2 > val1)
delta = val2 - val1;
else

View File

@ -298,7 +298,7 @@ namespace CairoChart {
if (x_min < s.axis_x.zoom_min) x_min += step;
// 4.2. Cursor values for joint X axis
if (chart.joint_x && chart.cursor_style.orientation == Cursors.Orientation.VERTICAL && chart.cursors_crossings.length != 0) {
if (chart.joint_x && chart.cursors2.cursor_style.orientation == Cursors.Orientation.VERTICAL && chart.cursors2.cursors_crossings.length != 0) {
switch (s.axis_x.position) {
case Axis.Position.LOW: chart.cur_y_max -= max_rec_height + s.axis_x.font_indent; break;
case Axis.Position.HIGH: chart.cur_y_min += max_rec_height + s.axis_x.font_indent; break;
@ -422,7 +422,7 @@ namespace CairoChart {
if (y_min < s.axis_y.zoom_min) y_min += step;
// 4.2. Cursor values for joint Y axis
if (chart.joint_y && chart.cursor_style.orientation == Cursors.Orientation.HORIZONTAL && chart.cursors_crossings.length != 0) {
if (chart.joint_y && chart.cursors2.cursor_style.orientation == Cursors.Orientation.HORIZONTAL && chart.cursors2.cursors_crossings.length != 0) {
switch (s.axis_y.position) {
case Axis.Position.LOW: chart.cur_x_min += max_rec_width + s.axis_y.font_indent; break;
case Axis.Position.HIGH: chart.cur_x_max -= max_rec_width + s.axis_y.font_indent; break;

View File

@ -274,7 +274,7 @@ int main (string[] args) {
case Legend.Position.LEFT: radio_button3.set_active(true); break;
case Legend.Position.BOTTOM: radio_button4.set_active(true); break;
}
switch (chart.cursor_style.orientation) {
switch (chart.cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL: radio_button7.set_active(true); break;
case Cursors.Orientation.HORIZONTAL: radio_button8.set_active(true); break;
}
@ -287,7 +287,7 @@ int main (string[] args) {
case Legend.Position.LEFT: radio_button3.set_active(true); break;
case Legend.Position.BOTTOM: radio_button4.set_active(true); break;
}
switch (chart.cursor_style.orientation) {
switch (chart.cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL: radio_button7.set_active(true); break;
case Cursors.Orientation.HORIZONTAL: radio_button8.set_active(true); break;
}
@ -300,7 +300,7 @@ int main (string[] args) {
case Legend.Position.LEFT: radio_button3.set_active(true); break;
case Legend.Position.BOTTOM: radio_button4.set_active(true); break;
}
switch (chart.cursor_style.orientation) {
switch (chart.cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL: radio_button7.set_active(true); break;
case Cursors.Orientation.HORIZONTAL: radio_button8.set_active(true); break;
}
@ -313,7 +313,7 @@ int main (string[] args) {
case Legend.Position.LEFT: radio_button4.set_active(true); break;
case Legend.Position.BOTTOM: radio_button4.set_active(true); break;
}
switch (chart.cursor_style.orientation) {
switch (chart.cursors2.cursor_style.orientation) {
case Cursors.Orientation.VERTICAL: radio_button7.set_active(true); break;
case Cursors.Orientation.HORIZONTAL: radio_button8.set_active(true); break;
}
@ -397,13 +397,13 @@ int main (string[] args) {
radio_button7.toggled.connect ((button) => {
if (button.get_active()) {
chart.cursor_style.orientation = Cursors.Orientation.VERTICAL;
chart.cursors2.cursor_style.orientation = Cursors.Orientation.VERTICAL;
da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height());
}
});
radio_button8.toggled.connect ((button) => {
if (button.get_active()) {
chart.cursor_style.orientation = Cursors.Orientation.HORIZONTAL;
chart.cursors2.cursor_style.orientation = Cursors.Orientation.HORIZONTAL;
da.queue_draw_area(0, 0, da.get_allocated_width(), da.get_allocated_height());
}
});