Cairo-Chart/src/Axis.vala

230 lines
4.5 KiB
Vala
Raw Normal View History

2018-01-08 23:33:13 +03:00
namespace CairoChart {
2018-01-22 14:31:34 +03:00
/**
2018-01-22 14:44:31 +03:00
* ``Chart`` axis.
2018-01-22 14:31:34 +03:00
*/
2017-08-28 14:47:31 +03:00
public class Axis {
2018-01-22 14:44:31 +03:00
2018-01-21 17:27:46 +03:00
Chart chart;
public Text title;
2018-01-22 14:54:08 +03:00
string _format = "%.2Lf";
string _date_format = "%Y.%m.%d";
string _time_format = "%H:%M:%S";
int _dsec_signs = 2; // 2 signs = centiseconds
2018-01-22 14:44:31 +03:00
/**
* ``Axis`` range/limits.
*/
public Range range = new Range();
/**
* Data type.
*/
2018-01-22 14:31:34 +03:00
public enum DType {
2018-01-22 14:44:31 +03:00
/**
* Float128 numbers.
*/
2017-08-28 14:47:31 +03:00
NUMBERS = 0,
2018-01-22 14:44:31 +03:00
/**
* Date/Time.
*/
2017-08-28 14:47:31 +03:00
DATE_TIME
}
2018-01-22 14:44:31 +03:00
/**
* Data type.
*/
public DType dtype;
/**
* ``Axis`` scale type.
*/
2018-01-22 14:46:00 +03:00
public enum Scale {
2018-01-22 14:44:31 +03:00
/**
* Linear scale.
*/
LINEAR = 0,
/**
* Logarithmic scale.
*/
// LOGARITHMIC,
2017-08-28 14:47:31 +03:00
}
2018-01-22 14:44:31 +03:00
/**
* Scale type.
*/
2018-01-22 14:46:00 +03:00
public Scale scale;
2018-01-22 14:44:31 +03:00
/**
* ``Axis`` position.
*/
2017-08-28 14:47:31 +03:00
public enum Position {
2018-01-22 14:54:08 +03:00
/**
* Bottom/Left ``Axis``.
*/
2017-08-28 14:47:31 +03:00
LOW = 0,
2018-01-22 14:54:08 +03:00
/**
* Top/Right ``Axis``.
*/
2017-08-28 14:47:31 +03:00
HIGH = 1,
2018-01-22 14:54:08 +03:00
/**
* 2 ``Axes``.
*/
2017-08-28 14:47:31 +03:00
BOTH = 2
}
2018-01-22 14:54:08 +03:00
/**
* Position.
*/
2017-08-28 14:47:31 +03:00
public Position position = Position.LOW;
2018-01-22 14:54:08 +03:00
/**
* Float128 numbers print string format.
*/
public virtual string format {
2017-08-28 14:47:31 +03:00
get { return _format; }
set {
// TODO: check format
_format = value;
}
default = "%.2Lf";
}
2018-01-22 14:54:08 +03:00
/**
* Date print string format.
*/
public virtual string date_format {
2017-08-28 14:47:31 +03:00
get { return _date_format; }
set {
// TODO: check format
_date_format = value;
}
default = "%Y.%m.%d";
}
2018-01-22 14:54:08 +03:00
/**
* Time print string format.
*/
public virtual string time_format {
2017-08-28 14:47:31 +03:00
get { return _time_format; }
set {
// TODO: check format
_time_format = value;
}
default = "%H:%M:%S";
}
2018-01-22 14:54:08 +03:00
/**
* Number of second's signs after point.
*
* 2 signs means centiseconds, 3 signs means milliseconds, etc...
*/
public virtual int dsec_signs {
2017-08-28 14:47:31 +03:00
get { return _dsec_signs; }
set {
// TODO: check format
_dsec_signs = value;
}
default = 2;
}
2018-01-22 14:58:41 +03:00
/**
* ``Axis`` Font style.
*/
2018-01-22 14:54:58 +03:00
public Font font = new Font ();
2018-01-22 14:58:41 +03:00
/**
* ``Axis`` color.
*/
2017-08-28 14:47:31 +03:00
public Color color = Color ();
2018-01-22 14:58:41 +03:00
/**
* ``Axis`` line style.
*/
2018-01-20 13:11:17 +03:00
public LineStyle line_style = LineStyle ();
2018-01-22 14:58:41 +03:00
/**
* ``Axis`` font spacing.
*/
2018-01-19 10:52:06 +03:00
public double font_spacing = 5;
2017-08-28 14:47:31 +03:00
2018-01-22 15:07:19 +03:00
/**
* Constructs a new ``Axis``.
*/
2018-01-22 14:44:31 +03:00
public Axis (Chart chart) {
this.chart = chart;
title = new Text (chart, "");
}
2018-01-15 12:05:21 +03:00
public virtual Axis copy () {
2018-01-21 17:27:46 +03:00
var axis = new Axis (chart);
2017-08-28 18:16:48 +03:00
axis._date_format = this._date_format;
axis._dsec_signs = this._dsec_signs;
axis._format = this._format;
axis._time_format = this._time_format;
axis.color = this.color;
2018-01-19 10:52:06 +03:00
axis.font_spacing = this.font_spacing;
2018-01-22 14:54:58 +03:00
axis.font = this.font;
2017-08-28 18:16:48 +03:00
axis.line_style = this.line_style;
2018-01-21 13:56:29 +03:00
axis.range.max = this.range.max;
axis.range.min = this.range.min;
2017-08-28 18:16:48 +03:00
axis.position = this.position;
2018-01-22 14:46:00 +03:00
axis.scale = this.scale;
2017-08-28 18:16:48 +03:00
axis.title = this.title.copy();
2018-01-22 14:31:34 +03:00
axis.dtype = this.dtype;
2018-01-10 14:15:42 +03:00
axis.nrecords = this.nrecords;
2017-08-28 18:16:48 +03:00
return axis;
}
2018-01-10 14:15:42 +03:00
public int nrecords = 128;
public virtual void format_date_time (Float128 x, out string date, out string time) {
date = time = "";
var dt = new DateTime.from_unix_utc((int64)x);
date = dt.format(date_format);
var dsec_str =
("%."+(dsec_signs.to_string())+"Lf").printf((LongDouble)(x - (int64)x)).offset(1);
time = dt.format(time_format) + dsec_str;
}
2018-01-22 09:19:04 +03:00
public virtual void calc_rec_sizes (out double max_rec_width, out double max_rec_height, bool horizontal = true) {
2018-01-10 14:15:42 +03:00
max_rec_width = max_rec_height = 0;
for (var i = 0; i < nrecords; ++i) {
2018-01-21 15:18:34 +03:00
Float128 x = (int64)(range.zmin + range.zrange / nrecords * i) + 1.0/3.0;
2018-01-22 14:31:34 +03:00
switch (dtype) {
case Axis.DType.NUMBERS:
2018-01-22 14:54:58 +03:00
var text = new Text (chart, format.printf((LongDouble)x) + (horizontal ? "_" : ""), font);
2018-01-21 18:41:41 +03:00
max_rec_width = double.max (max_rec_width, text.width);
max_rec_height = double.max (max_rec_height, text.height);
2018-01-10 14:15:42 +03:00
break;
2018-01-22 14:31:34 +03:00
case Axis.DType.DATE_TIME:
2018-01-10 14:15:42 +03:00
string date, time;
format_date_time(x, out date, out time);
var h = 0.0;
if (date_format != "") {
2018-01-22 14:54:58 +03:00
var text = new Text (chart, date + (horizontal ? "_" : ""), font);
2018-01-21 18:41:41 +03:00
max_rec_width = double.max (max_rec_width, text.width);
h = text.height;
2018-01-10 14:15:42 +03:00
}
if (time_format != "") {
2018-01-22 14:54:58 +03:00
var text = new Text (chart, time + (horizontal ? "_" : ""), font);
2018-01-21 18:41:41 +03:00
max_rec_width = double.max (max_rec_width, text.width);
h += text.height;
2018-01-10 14:15:42 +03:00
}
max_rec_height = double.max (max_rec_height, h);
break;
}
}
}
2018-01-19 14:09:50 +03:00
2018-01-22 14:44:31 +03:00
public virtual void zoom_out () {
range.zoom_out();
2018-01-19 14:09:50 +03:00
}
2017-08-28 14:47:31 +03:00
}
}