Cairo-Chart/src/Series.vala

184 lines
3.6 KiB
Vala
Raw Normal View History

using Cairo;
2018-01-08 23:33:13 +03:00
namespace CairoChart {
2018-01-22 20:05:40 +03:00
/**
* ``Chart`` series.
*/
public class Series {
2018-01-22 20:05:40 +03:00
protected Chart chart { get; protected set; default = null; }
/**
* 128-bit (X;Y) points.
*/
2018-01-16 13:39:17 +03:00
public Point128[] points = {};
2018-01-22 20:05:40 +03:00
/**
* Sort style.
*/
public enum Sort {
2018-01-22 20:05:40 +03:00
/**
* Sort by X.
*/
BY_X = 0,
2018-01-22 20:05:40 +03:00
/**
* Sort by Y.
*/
BY_Y = 1,
2018-01-22 20:05:40 +03:00
/**
* Do not sort points on draw().
*/
2018-01-08 23:33:13 +03:00
UNSORTED
}
2018-01-22 20:05:40 +03:00
/**
* Sort style.
*/
public Sort sort = Sort.BY_X;
2018-01-22 20:05:40 +03:00
/**
* X-axis.
*/
2018-01-21 17:27:46 +03:00
public Axis axis_x;
2018-01-22 20:05:40 +03:00
/**
* Y-axis.
*/
2018-01-21 17:27:46 +03:00
public Axis axis_y;
2018-01-22 20:05:40 +03:00
/**
* ``Place`` of the ``Series`` on the {@link Chart}.
*/
2018-01-21 14:14:57 +03:00
public Place place = new Place();
2018-01-22 20:05:40 +03:00
/**
* Title of the ``Chart``.
*/
2018-01-21 17:27:46 +03:00
public Text title;
2018-01-22 20:05:40 +03:00
/**
* ``Marker`` style.
*/
2018-01-22 18:54:18 +03:00
public Marker marker;
2018-01-22 20:05:40 +03:00
/**
* Grid style.
*/
public Grid grid = new Grid ();
2018-01-22 20:05:40 +03:00
/**
* ``Series`` line style.
*/
2018-01-20 13:11:17 +03:00
public LineStyle line_style = LineStyle ();
2018-01-22 20:05:40 +03:00
/**
* ``Series`` color (set only).
*/
public Color color {
2018-01-22 20:05:40 +03:00
protected get { return Color(); }
set {
2018-01-22 20:05:40 +03:00
line_style.color = value;
axis_x.color = value;
axis_y.color = value;
grid.style.color = value;
2018-01-22 09:04:26 +03:00
grid.style.color.alpha = 0.5;
}
2018-01-22 17:47:00 +03:00
default = Color (0, 0, 0, 1);
}
2018-01-22 20:05:40 +03:00
/**
* Show the ``Series`` in zoomed area or not.
*/
2017-10-25 19:31:30 +03:00
public bool zoom_show = true;
2018-01-22 20:05:40 +03:00
/**
* Constructs a new ``Series``.
* @param chart ``Chart`` instance.
*/
2018-01-16 14:29:24 +03:00
public Series (Chart chart) {
this.chart = chart;
2018-01-21 17:27:46 +03:00
title = new Text(chart);
axis_x = new Axis(chart);
axis_y = new Axis(chart);
2018-01-21 15:41:04 +03:00
this.marker = new Marker(chart);
2018-01-16 14:29:24 +03:00
}
2018-01-22 20:05:40 +03:00
/**
* Gets a copy of the ``Series``.
*/
2018-01-15 12:05:21 +03:00
public virtual Series copy () {
2018-01-16 14:29:24 +03:00
var series = new Series (chart);
2017-08-28 18:16:48 +03:00
series.axis_x = this.axis_x.copy ();
series.axis_y = this.axis_y.copy ();
series.grid = this.grid.copy ();
series.line_style = this.line_style;
2018-01-10 12:58:35 +03:00
series.marker = this.marker;
2017-08-22 11:54:18 +03:00
series.place = this.place.copy();
2017-12-15 20:50:13 +03:00
series.points = this.points;
2017-08-28 18:16:48 +03:00
series.sort = this.sort;
series.title = this.title.copy();
2017-10-25 19:31:30 +03:00
series.zoom_show = this.zoom_show;
2017-08-28 18:16:48 +03:00
return series;
}
2018-01-22 20:05:40 +03:00
/**
* Draws the ``Series``.
*/
2018-01-16 15:06:10 +03:00
public virtual void draw () {
2018-01-18 20:43:52 +03:00
var points = Math.sort_points(this, sort);
2018-01-19 11:36:33 +03:00
line_style.apply(chart);
2018-01-15 12:32:47 +03:00
// draw series line
for (int i = 1; i < points.length; ++i) {
2018-01-16 18:49:37 +03:00
Point c, d;
2018-01-18 20:43:52 +03:00
if (Math.cut_line (
2018-01-20 20:07:06 +03:00
Point(chart.plarea.x0, chart.plarea.y0),
Point(chart.plarea.x1, chart.plarea.y1),
2018-01-16 18:49:37 +03:00
Point(get_scr_x(points[i - 1].x), get_scr_y(points[i - 1].y)),
Point(get_scr_x(points[i].x), get_scr_y(points[i].y)),
2018-01-15 12:32:47 +03:00
out c, out d)
) {
2018-01-17 10:36:10 +03:00
chart.ctx.move_to (c.x, c.y);
chart.ctx.line_to (d.x, d.y);
2018-01-15 12:32:47 +03:00
}
}
2018-01-17 10:36:10 +03:00
chart.ctx.stroke();
2018-01-15 12:32:47 +03:00
for (int i = 0; i < points.length; ++i) {
2018-01-16 18:49:37 +03:00
var x = get_scr_x(points[i].x);
var y = get_scr_y(points[i].y);
2018-01-20 20:07:06 +03:00
if (Math.point_in_rect (Point(x, y), chart.plarea.x0, chart.plarea.x1,
chart.plarea.y0, chart.plarea.y1))
2018-01-22 13:10:27 +03:00
marker.draw_at_pos(Point(x, y));
2018-01-15 12:32:47 +03:00
}
}
2018-01-15 13:12:42 +03:00
2018-01-22 20:05:40 +03:00
/**
* Gets screen point by real ``Series`` (X;Y) value.
* @param p real ``Series`` (X;Y) value.
*/
2018-01-22 19:03:31 +03:00
public virtual Point get_scr_point (Point128 p) {
return Point (get_scr_x(p.x), get_scr_y(p.y));
}
2018-01-22 20:05:40 +03:00
/**
* Gets real ``Series`` (X;Y) value by plot area screen point.
* @param p (X;Y) screen point.
*/
2018-01-22 19:03:31 +03:00
public virtual Point128 get_real_point (Point p) {
return Point128 (get_real_x(p.x), get_real_y(p.y));
}
2018-01-22 20:05:40 +03:00
/**
* Zooms out the ``Series``.
*/
2018-01-22 19:03:31 +03:00
public virtual void zoom_out () {
zoom_show = true;
axis_x.zoom_out();
axis_y.zoom_out();
place.zoom_out();
}
}
}