Cairo-Chart/src/Chart.vala

368 lines
9.6 KiB
Vala
Raw Normal View History

2018-01-08 23:33:13 +03:00
namespace CairoChart {
2018-01-17 12:46:25 +03:00
/**
2018-01-18 17:14:54 +03:00
* Cairo/GTK+ ``Chart``.
2018-01-17 12:46:25 +03:00
*/
public class Chart {
2018-01-17 10:18:47 +03:00
/**
2018-01-18 17:14:54 +03:00
* ``Chart`` Position.
2018-01-17 10:18:47 +03:00
*/
2018-01-18 13:52:06 +03:00
public Cairo.Rectangle area = Cairo.Rectangle();
/**
* Current evaluated area.
*/
public Cairo.Rectangle evarea = Cairo.Rectangle()
2018-01-18 16:27:53 +03:00
{ x = 0, y = 0, width = 1, height = 1 };
2018-01-18 13:52:06 +03:00
/**
2018-01-18 16:57:39 +03:00
* Zoom area limits (relative coordinates: 0.0-1.0).
2018-01-18 13:52:06 +03:00
*/
public Cairo.Rectangle zoom = Cairo.Rectangle()
{ x = 0, y = 0, width = 1, height = 1 };
2018-01-18 16:27:53 +03:00
/**
2018-01-18 16:57:39 +03:00
* Plot area bounds.
2018-01-18 16:27:53 +03:00
*/
public Cairo.Rectangle plarea = Cairo.Rectangle()
{ x = 0, y = 0, width = 1, height = 1 };
2018-01-17 10:36:10 +03:00
/**
2018-01-18 18:51:06 +03:00
* Cairo ``Context`` of the Drawing Area.
2018-01-17 10:36:10 +03:00
*/
public Cairo.Context ctx = null;
2018-01-17 12:46:25 +03:00
/**
2018-01-18 18:51:06 +03:00
* Background ``Color``.
2018-01-17 12:46:25 +03:00
*/
2018-01-16 14:59:00 +03:00
public Color bg_color = Color(1, 1, 1);
2018-01-17 12:46:25 +03:00
/**
2018-01-18 18:51:06 +03:00
* Border ``Color``.
2018-01-17 12:46:25 +03:00
*/
2018-01-18 13:52:06 +03:00
public Color border_color = Color(0, 0, 0, 0.3);
2018-01-17 12:46:25 +03:00
/**
2018-01-18 17:14:54 +03:00
* ``Chart`` Title.
2018-01-17 12:46:25 +03:00
*/
2018-01-18 13:52:06 +03:00
public Text title = new Text("Cairo Chart");
2018-01-17 12:46:25 +03:00
/**
* Legend.
*/
2018-01-18 13:52:06 +03:00
public Legend legend = new Legend();
2018-01-17 12:46:25 +03:00
/**
2018-01-19 10:40:35 +03:00
* ``Chart`` Series array.
2018-01-17 12:46:25 +03:00
*/
2017-10-25 19:31:30 +03:00
public Series[] series = {};
2018-01-18 12:29:43 +03:00
/**
2018-01-19 10:40:35 +03:00
* index of the 1'st shown series in a zoommed area.
2018-01-18 12:29:43 +03:00
*/
public int zoom_1st_idx { get; protected set; default = 0; }
2018-01-16 12:09:21 +03:00
2018-01-18 16:53:47 +03:00
/**
* Joint/common X axes or not.
*/
2018-01-16 12:09:21 +03:00
public bool joint_x { get; protected set; default = false; }
2018-01-18 16:53:47 +03:00
/**
* Joint/common Y axes or not.
*/
2018-01-16 12:09:21 +03:00
public bool joint_y { get; protected set; default = false; }
2018-01-18 16:53:47 +03:00
/**
2018-01-18 18:51:06 +03:00
* Joint/common {@link Axis} ``Color``.
2018-01-18 16:53:47 +03:00
*/
2018-01-18 16:57:39 +03:00
public Color joint_color = Color (0, 0, 0, 1);
2018-01-16 12:09:21 +03:00
2018-01-18 16:53:47 +03:00
/**
* Selection line style.
*/
public Line.Style selection_style = Line.Style ();
/**
2018-01-18 17:14:54 +03:00
* ``Chart`` cursors.
2018-01-18 16:53:47 +03:00
*/
2018-01-18 18:10:28 +03:00
public Cursors cursors { get; protected set; default = null; }
2018-01-16 12:09:21 +03:00
2018-01-18 16:53:47 +03:00
/**
* Set paint color for further drawing.
*/
2018-01-16 20:39:59 +03:00
public Color color {
private get { return Color(); }
2018-01-17 10:36:10 +03:00
set { ctx.set_source_rgba (value.red, value.green, value.blue, value.alpha); }
2018-01-16 20:39:59 +03:00
default = Color();
2018-01-16 12:09:21 +03:00
}
2018-01-18 16:53:47 +03:00
/**
2018-01-18 17:14:54 +03:00
* Constructs a new ``Chart``.
2018-01-18 16:53:47 +03:00
*/
2018-01-18 18:10:28 +03:00
public Chart () {
cursors = new Cursors (this);
}
2018-01-16 20:39:59 +03:00
2018-01-18 17:14:54 +03:00
/**
* Gets a copy of the ``Chart``.
*/
2018-01-16 12:09:21 +03:00
public Chart copy () {
var chart = new Chart ();
2018-01-18 21:17:49 +03:00
chart.area = this.area;
2018-01-16 12:09:21 +03:00
chart.bg_color = this.bg_color;
chart.border_color = this.border_color;
2018-01-17 10:36:10 +03:00
chart.ctx = this.ctx;
2018-01-16 19:38:43 +03:00
chart.cursors = this.cursors.copy();
2018-01-18 21:17:49 +03:00
chart.evarea = this.evarea;
chart.joint_color = this.joint_color;
chart.joint_x = this.joint_x;
chart.joint_y = this.joint_y;
2018-01-16 12:09:21 +03:00
chart.legend = this.legend.copy();
2018-01-18 16:27:53 +03:00
chart.plarea = this.plarea;
2018-01-16 12:09:21 +03:00
chart.selection_style = this.selection_style;
chart.series = this.series;
chart.title = this.title.copy();
2018-01-18 21:17:49 +03:00
chart.zoom = this.zoom;
2018-01-18 12:29:43 +03:00
chart.zoom_1st_idx = this.zoom_1st_idx;
2018-01-16 12:09:21 +03:00
return chart;
}
2018-01-18 18:38:22 +03:00
/**
* Clears the ``Chart`` with a {@link bg_color} background color.
*/
public virtual void clear () {
2018-01-17 10:36:10 +03:00
if (ctx != null) {
2018-01-16 20:39:59 +03:00
color = bg_color;
2018-01-17 10:36:10 +03:00
ctx.paint();
2018-01-16 19:51:31 +03:00
}
}
2018-01-18 18:38:22 +03:00
/**
* Draws the ``Chart``.
*/
public virtual bool draw () {
2018-01-18 13:52:06 +03:00
evarea = area;
2018-01-19 10:40:35 +03:00
draw_title ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
2018-01-08 23:33:13 +03:00
legend.draw (this);
2018-01-18 13:52:06 +03:00
fix_evarea ();
2018-01-19 10:31:51 +03:00
rot_axes_titles ();
2018-01-19 11:23:40 +03:00
cursors.get_crossings();
2017-12-01 16:25:07 +03:00
2018-01-19 10:40:35 +03:00
eval_plarea ();
2018-01-19 10:40:35 +03:00
draw_haxes ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
2018-01-19 10:40:35 +03:00
draw_vaxes ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
2018-01-19 10:40:35 +03:00
draw_plarea_border ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
draw_series ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
2018-01-19 11:27:49 +03:00
cursors.draw ();
2018-01-18 13:52:06 +03:00
fix_evarea ();
return true;
}
2018-01-18 18:38:22 +03:00
/**
* Draws selection with a {@link selection_style} line style.
* @param rect selection square.
*/
2018-01-16 20:39:59 +03:00
public virtual void draw_selection (Cairo.Rectangle rect) {
2018-01-19 11:36:33 +03:00
selection_style.apply(this);
2018-01-17 10:36:10 +03:00
ctx.rectangle (rect.x, rect.y, rect.width, rect.height);
ctx.stroke();
2018-01-16 20:39:59 +03:00
}
2018-01-18 18:38:22 +03:00
/**
* Zooms the ``Chart``.
* @param rect selected zoom area.
*/
2018-01-16 13:19:28 +03:00
public virtual void zoom_in (Cairo.Rectangle rect) {
2018-01-19 11:49:46 +03:00
foreach (var s in series) {
if (!s.zoom_show) continue;
2018-01-16 18:49:37 +03:00
var real_x0 = s.get_real_x (rect.x);
2018-01-19 11:44:02 +03:00
var real_x1 = s.get_real_x (rect.x + rect.width);
2018-01-19 13:29:12 +03:00
var real_width = real_x1 - real_x0;
2018-01-16 18:49:37 +03:00
var real_y0 = s.get_real_y (rect.y);
2018-01-19 11:44:02 +03:00
var real_y1 = s.get_real_y (rect.y + rect.height);
2018-01-19 13:29:12 +03:00
var real_height = real_y0 - real_y1;
2017-10-25 19:31:30 +03:00
// if selected square does not intersect with the series's square
2017-08-22 11:54:18 +03:00
if ( real_x1 <= s.axis_x.zoom_min || real_x0 >= s.axis_x.zoom_max
|| real_y0 <= s.axis_y.zoom_min || real_y1 >= s.axis_y.zoom_max) {
s.zoom_show = false;
2017-08-22 11:54:18 +03:00
continue;
}
if (real_x0 >= s.axis_x.zoom_min) {
s.axis_x.zoom_min = real_x0;
2018-01-08 23:33:13 +03:00
s.place.zoom_x_min = 0.0;
2017-08-22 11:54:18 +03:00
} else {
2018-01-19 13:29:12 +03:00
s.place.zoom_x_min = (s.axis_x.zoom_min - real_x0) / real_width;
2017-08-22 11:54:18 +03:00
}
if (real_x1 <= s.axis_x.zoom_max) {
s.axis_x.zoom_max = real_x1;
2018-01-08 23:33:13 +03:00
s.place.zoom_x_max = 1.0;
2017-08-22 11:54:18 +03:00
} else {
2018-01-19 13:29:12 +03:00
s.place.zoom_x_max = (s.axis_x.zoom_max - real_x0) / real_width;
2017-08-22 11:54:18 +03:00
}
if (real_y1 >= s.axis_y.zoom_min) {
s.axis_y.zoom_min = real_y1;
2018-01-08 23:33:13 +03:00
s.place.zoom_y_min = 0.0;
2017-08-22 11:54:18 +03:00
} else {
2018-01-19 13:29:12 +03:00
s.place.zoom_y_min = (s.axis_y.zoom_min - real_y1) / real_height;
2017-08-22 11:54:18 +03:00
}
if (real_y0 <= s.axis_y.zoom_max) {
s.axis_y.zoom_max = real_y0;
2018-01-08 23:33:13 +03:00
s.place.zoom_y_max = 1.0;
2017-08-22 11:54:18 +03:00
} else {
2018-01-19 13:29:12 +03:00
s.place.zoom_y_max = (s.axis_y.zoom_max - real_y1) / real_height;
2017-08-22 11:54:18 +03:00
}
}
2017-10-06 22:46:51 +03:00
2018-01-18 12:29:43 +03:00
zoom_1st_idx = 0;
2018-01-19 11:49:46 +03:00
for (var si = 0; si < series.length; ++si)
if (series[si].zoom_show) {
2018-01-18 12:29:43 +03:00
zoom_1st_idx = si;
2017-10-25 19:31:30 +03:00
break;
}
2018-01-18 12:22:49 +03:00
var new_zoom = zoom;
// TODO
2018-01-18 16:27:53 +03:00
new_zoom.x += (rect.x - plarea.x) / plarea.width * zoom.width;
2018-01-19 11:44:02 +03:00
var x_max = zoom.x + (rect.x + rect.width - plarea.x) / plarea.width * zoom.width;
2018-01-18 12:22:49 +03:00
new_zoom.width = x_max - new_zoom.x;
2018-01-18 16:27:53 +03:00
new_zoom.y += (rect.y - plarea.y) / plarea.height * zoom.height;
2018-01-19 11:44:02 +03:00
var y_max = zoom.y + (rect.y + rect.height - plarea.y) / plarea.height * zoom.height;
2018-01-18 12:22:49 +03:00
new_zoom.height = y_max - new_zoom.y;
zoom = new_zoom;
2017-08-22 11:54:18 +03:00
}
2018-01-18 18:38:22 +03:00
/**
* Zooms out the ``Chart``.
*/
2017-08-22 11:54:18 +03:00
public virtual void zoom_out () {
2017-10-25 19:31:30 +03:00
foreach (var s in series) {
s.zoom_show = true;
2017-08-22 11:54:18 +03:00
s.axis_x.zoom_min = s.axis_x.min;
s.axis_x.zoom_max = s.axis_x.max;
s.axis_y.zoom_min = s.axis_y.min;
s.axis_y.zoom_max = s.axis_y.max;
2018-01-08 23:33:13 +03:00
s.place.zoom_x_min = s.place.x_min;
s.place.zoom_x_max = s.place.x_max;
s.place.zoom_y_min = s.place.y_min;
s.place.zoom_y_max = s.place.y_max;
2017-08-22 11:54:18 +03:00
}
2018-01-18 12:22:49 +03:00
zoom = Cairo.Rectangle() { x = 0, y = 0, width = 1, height = 1 };
2018-01-18 12:29:43 +03:00
zoom_1st_idx = 0;
2017-10-06 22:46:51 +03:00
}
2018-01-18 18:38:22 +03:00
/**
* Moves the ``Chart``.
* @param delta delta Δ(x;y) value to move the ``Chart``.
*/
2018-01-16 13:39:17 +03:00
public virtual void move (Point delta) {
var d = delta;
2018-01-18 16:27:53 +03:00
d.x /= plarea.width; d.x *= - 1.0;
d.y /= plarea.height; d.y *= - 1.0;
2018-01-18 12:22:49 +03:00
var rzxmin = zoom.x, rzxmax = zoom.x + zoom.width, rzymin = zoom.y, rzymax = zoom.y + zoom.height;
2017-10-06 22:46:51 +03:00
zoom_out();
2018-01-18 16:27:53 +03:00
d.x *= plarea.width;
d.y *= plarea.height;
var xmin = plarea.x + plarea.width * rzxmin;
var xmax = plarea.x + plarea.width * rzxmax;
var ymin = plarea.y + plarea.height * rzymin;
var ymax = plarea.y + plarea.height * rzymax;
2017-10-06 22:46:51 +03:00
2018-01-16 13:39:17 +03:00
d.x *= rzxmax - rzxmin; d.y *= rzymax - rzymin;
2017-10-06 22:46:51 +03:00
2018-01-18 16:27:53 +03:00
if (xmin + d.x < plarea.x) d.x = plarea.x - xmin;
if (xmax + d.x > plarea.x + plarea.width) d.x = plarea.x + plarea.width - xmax;
if (ymin + d.y < plarea.y) d.y = plarea.y - ymin;
if (ymax + d.y > plarea.y + plarea.height) d.y = plarea.y + plarea.height - ymax;
2017-10-06 22:46:51 +03:00
2018-01-16 13:39:17 +03:00
zoom_in (Cairo.Rectangle(){x = xmin + d.x, y = ymin + d.y, width = xmax - xmin, height = ymax - ymin});
2017-08-22 11:54:18 +03:00
}
2018-01-18 17:20:06 +03:00
protected virtual void fix_evarea () {
if (evarea.width < 0) evarea.width = 0;
if (evarea.height < 0) evarea.height = 0;
}
2018-01-19 10:31:51 +03:00
protected virtual void rot_axes_titles () {
2018-01-18 17:20:06 +03:00
for (var si = 0; si < series.length; ++si) {
var s = series[si];
s.axis_y.title.style.orientation = Font.Orientation.VERTICAL;
}
}
2018-01-19 10:40:35 +03:00
protected virtual void eval_plarea () {
2018-01-19 10:52:06 +03:00
plarea.x = evarea.x + legend.spacing;
plarea.width = evarea.width - 2 * legend.spacing;
plarea.y = evarea.y + legend.spacing;
plarea.height = evarea.height - 2 * legend.spacing;
2018-01-15 16:40:14 +03:00
// Check for joint axes
joint_x = joint_y = true;
int nzoom_series_show = 0;
for (var si = series.length - 1; si >=0; --si) {
var s = series[si], s0 = series[0];
if (!s.zoom_show) continue;
++nzoom_series_show;
if (!s.equal_x_axis(s0)) joint_x = false;
if (!s.equal_y_axis(s0)) joint_y = false;
}
if (nzoom_series_show == 1) joint_x = joint_y = false;
2018-01-19 10:07:11 +03:00
for (var si = series.length - 1, nskip = 0; si >= 0; --si)
series[si].join_calc(true, si, ref nskip);
for (var si = series.length - 1, nskip = 0; si >= 0; --si)
series[si].join_calc(false, si, ref nskip);
2018-01-15 16:40:14 +03:00
}
2018-01-19 10:40:35 +03:00
protected virtual void draw_plarea_border () {
2018-01-18 17:20:06 +03:00
color = border_color;
ctx.set_dash(null, 0);
ctx.move_to (plarea.x, plarea.y);
ctx.line_to (plarea.x, plarea.y + plarea.height);
ctx.line_to (plarea.x + plarea.width, plarea.y + plarea.height);
ctx.line_to (plarea.x + plarea.width, plarea.y);
ctx.line_to (plarea.x, plarea.y);
ctx.stroke ();
}
2018-01-19 10:40:35 +03:00
protected virtual void draw_title () {
2018-01-18 17:20:06 +03:00
var sz = title.get_size(ctx);
2018-01-19 11:09:33 +03:00
var title_height = sz.height + (legend.position == Legend.Position.TOP ? title.vspacing * 2 : title.vspacing);
2018-01-18 17:20:06 +03:00
evarea.y += title_height;
evarea.height -= title_height;
color = title.color;
2018-01-19 11:09:33 +03:00
ctx.move_to (area.width/2 - sz.width/2, sz.height + title.vspacing);
2018-01-18 17:20:06 +03:00
title.show(ctx);
}
2018-01-19 10:40:35 +03:00
protected virtual void draw_haxes () {
2018-01-18 17:20:06 +03:00
for (var si = series.length - 1, nskip = 0; si >=0; --si)
series[si].draw_horizontal_axis (si, ref nskip);
}
2018-01-19 10:40:35 +03:00
protected virtual void draw_vaxes () {
2018-01-18 17:20:06 +03:00
for (var si = series.length - 1, nskip = 0; si >=0; --si)
series[si].draw_vertical_axis (si, ref nskip);
}
protected virtual void draw_series () {
for (var si = 0; si < series.length; ++si) {
var s = series[si];
if (s.zoom_show && s.points.length != 0)
s.draw();
}
}
}
}