OK In progress...

This commit is contained in:
Kolan Sh 2018-01-21 17:27:46 +03:00
parent dc9b896232
commit 2d4d01d532
7 changed files with 253 additions and 162 deletions

View File

@ -4,7 +4,8 @@ namespace CairoChart {
// or specify series name near the axis // or specify series name near the axis
public class Axis { public class Axis {
public Range range = new Range(); public Range range = new Range();
public Text title = new Text (""); Chart chart;
public Text title;
public enum Type { public enum Type {
NUMBERS = 0, NUMBERS = 0,
DATE_TIME DATE_TIME
@ -65,7 +66,7 @@ namespace CairoChart {
public double font_spacing = 5; public double font_spacing = 5;
public virtual Axis copy () { public virtual Axis copy () {
var axis = new Axis (); var axis = new Axis (chart);
axis._date_format = this._date_format; axis._date_format = this._date_format;
axis._dsec_signs = this._dsec_signs; axis._dsec_signs = this._dsec_signs;
axis._format = this._format; axis._format = this._format;
@ -84,7 +85,10 @@ namespace CairoChart {
return axis; return axis;
} }
public Axis () {} public Axis (Chart chart) {
this.chart = chart;
title = new Text (chart, "");
}
public int nrecords = 128; public int nrecords = 128;
@ -103,8 +107,8 @@ namespace CairoChart {
Float128 x = (int64)(range.zmin + range.zrange / nrecords * i) + 1.0/3.0; Float128 x = (int64)(range.zmin + range.zrange / nrecords * i) + 1.0/3.0;
switch (type) { switch (type) {
case Axis.Type.NUMBERS: case Axis.Type.NUMBERS:
var text = new Text (format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style); var text = new Text (chart, format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style);
var sz = text.get_size(chart.ctx); var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width); max_rec_width = double.max (max_rec_width, sz.width);
max_rec_height = double.max (max_rec_height, sz.height); max_rec_height = double.max (max_rec_height, sz.height);
break; break;
@ -114,14 +118,14 @@ namespace CairoChart {
var h = 0.0; var h = 0.0;
if (date_format != "") { if (date_format != "") {
var text = new Text (date + (horizontal ? "_" : ""), font_style); var text = new Text (chart, date + (horizontal ? "_" : ""), font_style);
var sz = text.get_size(chart.ctx); var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width); max_rec_width = double.max (max_rec_width, sz.width);
h = sz.height; h = sz.height;
} }
if (time_format != "") { if (time_format != "") {
var text = new Text (time + (horizontal ? "_" : ""), font_style); var text = new Text (chart, time + (horizontal ? "_" : ""), font_style);
var sz = text.get_size(chart.ctx); var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width); max_rec_width = double.max (max_rec_width, sz.width);
h += sz.height; h += sz.height;
} }

View File

@ -43,7 +43,7 @@ namespace CairoChart {
/** /**
* ``Chart`` Title. * ``Chart`` Title.
*/ */
public Text title = new Text("Cairo Chart"); public Text title;
/** /**
* Legend. * Legend.
@ -99,6 +99,8 @@ namespace CairoChart {
*/ */
public Chart () { public Chart () {
cursors = new Cursors (this); cursors = new Cursors (this);
title = new Text(this, "Cairo Chart");
} }
/** /**
@ -336,13 +338,13 @@ namespace CairoChart {
ctx.stroke (); ctx.stroke ();
} }
protected virtual void draw_title () { protected virtual void draw_title () {
var sz = title.get_size(ctx); var sz = title.size;
var title_height = sz.height + title.vspacing * 2; var title_height = sz.height + title.vspacing * 2;
evarea.y0 += title_height; evarea.y0 += title_height;
evarea.height -= title_height; evarea.height -= title_height;
color = title.color; color = title.color;
ctx.move_to (area.width/2 - sz.width/2, sz.height + title.vspacing); ctx.move_to (area.width/2 - sz.width/2, sz.height + title.vspacing);
title.show(ctx); title.show();
} }
protected virtual void draw_haxes () { protected virtual void draw_haxes () {
for (var si = series.length - 1, nskip = 0; si >=0; --si) for (var si = series.length - 1, nskip = 0; si >=0; --si)

View File

@ -209,15 +209,15 @@ namespace CairoChart {
size = Point128 (); size = Point128 ();
string date, time; string date, time;
s.axis_x.format_date_time(p.x, out date, out time); s.axis_x.format_date_time(p.x, out date, out time);
var date_t = new Text (date, s.axis_x.font_style, s.axis_x.color); var date_t = new Text(chart, date, s.axis_x.font_style, s.axis_x.color);
var time_t = new Text (time, s.axis_x.font_style, s.axis_x.color); var time_t = new Text(chart, time, s.axis_x.font_style, s.axis_x.color);
var x_t = new Text (s.axis_x.format.printf((LongDouble)p.x), s.axis_x.font_style, s.axis_x.color); var x_t = new Text(chart, s.axis_x.format.printf((LongDouble)p.x), s.axis_x.font_style, s.axis_x.color);
var y_t = new Text (s.axis_y.format.printf((LongDouble)p.y), s.axis_y.font_style, s.axis_y.color); var y_t = new Text(chart, s.axis_y.format.printf((LongDouble)p.y), s.axis_y.font_style, s.axis_y.color);
double h_x = 0.0, h_y = 0.0; double h_x = 0.0, h_y = 0.0;
if (show_x) { var sz = x_t.get_size(chart.ctx); size.x = sz.width; h_x = sz.height; } if (show_x) { var sz = x_t.size; size.x = sz.width; h_x = sz.height; }
if (show_date) { var sz = date_t.get_size(chart.ctx); size.x = sz.width; h_x = sz.height; } if (show_date) { var sz = date_t.size; size.x = sz.width; h_x = sz.height; }
if (show_time) { var sz = time_t.get_size(chart.ctx); size.x = double.max(size.x, sz.width); h_x += sz.height; } if (show_time) { var sz = time_t.size; size.x = double.max(size.x, sz.width); h_x += sz.height; }
if (show_y) { var sz = y_t.get_size(chart.ctx); size.x += sz.width; h_y = sz.height; } if (show_y) { var sz = y_t.size; size.x += sz.width; h_y = sz.height; }
if ((show_x || show_date || show_time) && show_y) size.x += double.max(s.axis_x.font_spacing, s.axis_y.font_spacing); if ((show_x || show_date || show_time) && show_y) size.x += double.max(s.axis_x.font_spacing, s.axis_y.font_spacing);
if (show_date && show_time) h_x += s.axis_x.font_spacing; if (show_date && show_time) h_x += s.axis_x.font_spacing;
size.y = double.max (h_x, h_y); size.y = double.max (h_x, h_y);
@ -287,16 +287,16 @@ namespace CairoChart {
case Axis.Type.NUMBERS: text = s.axis_x.format.printf((LongDouble)x); break; case Axis.Type.NUMBERS: text = s.axis_x.format.printf((LongDouble)x); break;
case Axis.Type.DATE_TIME: s.axis_x.format_date_time(x, out text, out time_text); break; case Axis.Type.DATE_TIME: s.axis_x.format_date_time(x, out text, out time_text); break;
} }
var text_t = new Text(text, s.axis_x.font_style, s.axis_x.color); var text_t = new Text(chart, text, s.axis_x.font_style, s.axis_x.color);
var sz = text_t.get_size(chart.ctx); var sz = text_t.size;
var time_text_t = new Text(time_text, s.axis_x.font_style, s.axis_x.color); var time_text_t = new Text(chart, time_text, s.axis_x.font_style, s.axis_x.color);
var print_y = 0.0; var print_y = 0.0;
switch (s.axis_x.position) { switch (s.axis_x.position) {
case Axis.Position.LOW: print_y = chart.area.y1 - s.axis_x.font_spacing case Axis.Position.LOW: print_y = chart.area.y1 - s.axis_x.font_spacing
- (chart.legend.position == Legend.Position.BOTTOM ? chart.legend.height : 0); - (chart.legend.position == Legend.Position.BOTTOM ? chart.legend.height : 0);
break; break;
case Axis.Position.HIGH: case Axis.Position.HIGH:
var title_height = chart.title.get_height(chart.ctx) + (chart.legend.position == Legend.Position.TOP ? var title_height = chart.title.height + (chart.legend.position == Legend.Position.TOP ?
chart.title.vspacing * 2 : chart.title.vspacing); chart.title.vspacing * 2 : chart.title.vspacing);
print_y = chart.area.y0 + title_height + s.axis_x.font_spacing print_y = chart.area.y0 + title_height + s.axis_x.font_spacing
+ (chart.legend.position == Legend.Position.TOP ? chart.legend.height : 0); + (chart.legend.position == Legend.Position.TOP ? chart.legend.height : 0);
@ -306,7 +306,7 @@ namespace CairoChart {
break; break;
case Axis.Type.DATE_TIME: case Axis.Type.DATE_TIME:
print_y += (s.axis_x.date_format == "" ? 0 : sz.height) print_y += (s.axis_x.date_format == "" ? 0 : sz.height)
+ (s.axis_x.time_format == "" ? 0 : time_text_t.get_height(chart.ctx)) + (s.axis_x.time_format == "" ? 0 : time_text_t.height)
+ (s.axis_x.date_format == "" || s.axis_x.time_format == "" ? 0 : s.axis_x.font_spacing); + (s.axis_x.date_format == "" || s.axis_x.time_format == "" ? 0 : s.axis_x.font_spacing);
break; break;
} }
@ -317,13 +317,13 @@ namespace CairoChart {
switch (s.axis_x.type) { switch (s.axis_x.type) {
case Axis.Type.NUMBERS: case Axis.Type.NUMBERS:
text_t.show(chart.ctx); text_t.show();
break; break;
case Axis.Type.DATE_TIME: case Axis.Type.DATE_TIME:
if (s.axis_x.date_format != "") text_t.show(chart.ctx); if (s.axis_x.date_format != "") text_t.show();
print_x = s.compact_rec_x_pos (x, time_text_t); print_x = s.compact_rec_x_pos (x, time_text_t);
chart.ctx.move_to (print_x, print_y - (s.axis_x.date_format == "" ? 0 : sz.height + s.axis_x.font_spacing)); chart.ctx.move_to (print_x, print_y - (s.axis_x.date_format == "" ? 0 : sz.height + s.axis_x.font_spacing));
if (s.axis_x.time_format != "") time_text_t.show(chart.ctx); if (s.axis_x.time_format != "") time_text_t.show();
break; break;
} }
} }
@ -337,7 +337,7 @@ namespace CairoChart {
if (chart.joint_y) { if (chart.joint_y) {
var s = chart.series[chart.zoom_1st_idx]; var s = chart.series[chart.zoom_1st_idx];
var y = s.get_real_y(rel2scr_y(c.y)); var y = s.get_real_y(rel2scr_y(c.y));
var text_t = new Text(s.axis_y.format.printf((LongDouble)y, s.axis_y.font_style)); var text_t = new Text(chart, s.axis_y.format.printf((LongDouble)y, s.axis_y.font_style));
var print_y = s.compact_rec_y_pos (y, text_t); var print_y = s.compact_rec_y_pos (y, text_t);
var print_x = 0.0; var print_x = 0.0;
switch (s.axis_y.position) { switch (s.axis_y.position) {
@ -346,12 +346,12 @@ namespace CairoChart {
+ (chart.legend.position == Legend.Position.LEFT ? chart.legend.width : 0); + (chart.legend.position == Legend.Position.LEFT ? chart.legend.width : 0);
break; break;
case Axis.Position.HIGH: case Axis.Position.HIGH:
print_x = chart.area.x1 - text_t.get_width(chart.ctx) - s.axis_y.font_spacing print_x = chart.area.x1 - text_t.width - s.axis_y.font_spacing
- (chart.legend.position == Legend.Position.RIGHT ? chart.legend.width : 0); - (chart.legend.position == Legend.Position.RIGHT ? chart.legend.width : 0);
break; break;
} }
chart.ctx.move_to (print_x, print_y); chart.ctx.move_to (print_x, print_y);
text_t.show(chart.ctx); text_t.show();
} }
break; break;
} }
@ -375,45 +375,45 @@ namespace CairoChart {
if (show_x) { if (show_x) {
chart.color = s.axis_x.color; chart.color = s.axis_x.color;
var text_t = new Text(s.axis_x.format.printf((LongDouble)point.x), s.axis_x.font_style); var text_t = new Text(chart, s.axis_x.format.printf((LongDouble)point.x), s.axis_x.font_style);
chart.ctx.move_to (svp.x - size.x / 2, svp.y + text_t.get_height(chart.ctx) / 2); chart.ctx.move_to (svp.x - size.x / 2, svp.y + text_t.height / 2);
if (chart.joint_x) chart.color = chart.joint_color; if (chart.joint_x) chart.color = chart.joint_color;
text_t.show(chart.ctx); text_t.show();
} }
if (show_time) { if (show_time) {
chart.color = s.axis_x.color; chart.color = s.axis_x.color;
string date = "", time = ""; string date = "", time = "";
s.axis_x.format_date_time(point.x, out date, out time); s.axis_x.format_date_time(point.x, out date, out time);
var text_t = new Text(time, s.axis_x.font_style); var text_t = new Text(chart, time, s.axis_x.font_style);
var sz = text_t.get_size(chart.ctx); var sz = text_t.size;
var y = svp.y + sz.height / 2; var y = svp.y + sz.height / 2;
if (show_date) y -= sz.height / 2 + s.axis_x.font_spacing / 2; if (show_date) y -= sz.height / 2 + s.axis_x.font_spacing / 2;
chart.ctx.move_to (svp.x - size.x / 2, y); chart.ctx.move_to (svp.x - size.x / 2, y);
if (chart.joint_x) chart.color = chart.joint_color; if (chart.joint_x) chart.color = chart.joint_color;
text_t.show(chart.ctx); text_t.show();
} }
if (show_date) { if (show_date) {
chart.color = s.axis_x.color; chart.color = s.axis_x.color;
string date = "", time = ""; string date = "", time = "";
s.axis_x.format_date_time(point.x, out date, out time); s.axis_x.format_date_time(point.x, out date, out time);
var text_t = new Text(date, s.axis_x.font_style); var text_t = new Text(chart, date, s.axis_x.font_style);
var sz = text_t.get_size(chart.ctx); var sz = text_t.size;
var y = svp.y + sz.height / 2; var y = svp.y + sz.height / 2;
if (show_time) y += sz.height / 2 + s.axis_x.font_spacing / 2; if (show_time) y += sz.height / 2 + s.axis_x.font_spacing / 2;
chart.ctx.move_to (svp.x - size.x / 2, y); chart.ctx.move_to (svp.x - size.x / 2, y);
if (chart.joint_x) chart.color = chart.joint_color; if (chart.joint_x) chart.color = chart.joint_color;
text_t.show(chart.ctx); text_t.show();
} }
if (show_y) { if (show_y) {
chart.color = s.axis_y.color; chart.color = s.axis_y.color;
var text_t = new Text(s.axis_y.format.printf((LongDouble)point.y), s.axis_y.font_style); var text_t = new Text(chart, s.axis_y.format.printf((LongDouble)point.y), s.axis_y.font_style);
var sz = text_t.get_size(chart.ctx); var sz = text_t.size;
chart.ctx.move_to (svp.x + size.x / 2 - sz.width, svp.y + sz.height / 2); chart.ctx.move_to (svp.x + size.x / 2 - sz.width, svp.y + sz.height / 2);
if (chart.joint_y) chart.color = chart.joint_color; if (chart.joint_y) chart.color = chart.joint_color;
text_t.show(chart.ctx); text_t.show();
} }
} }
} }

View File

@ -52,7 +52,7 @@ namespace CairoChart {
switch (position) { switch (position) {
case Position.TOP: case Position.TOP:
x0 = (chart.area.width - width) / 2; x0 = (chart.area.width - width) / 2;
var title_height = chart.title.get_height(chart.ctx) + (chart.legend.position == Legend.Position.TOP ? var title_height = chart.title.height + (chart.legend.position == Legend.Position.TOP ?
chart.title.vspacing * 2 : chart.title.vspacing); chart.title.vspacing * 2 : chart.title.vspacing);
y0 = title_height; y0 = title_height;
break; break;
@ -115,7 +115,7 @@ namespace CairoChart {
if (!s.zoom_show) continue; if (!s.zoom_show) continue;
var title_sz = s.title.get_size(chart.ctx); var title_sz = s.title.size;
// carry // carry
switch (position) { switch (position) {
@ -147,7 +147,7 @@ namespace CairoChart {
// series title // series title
chart.ctx.move_to (x + line_length, y); chart.ctx.move_to (x + line_length, y);
chart.color = s.title.color; chart.color = s.title.color;
s.title.show(chart.ctx); s.title.show();
// series line style // series line style
chart.ctx.move_to (x, y - title_sz.height / 2); chart.ctx.move_to (x, y - title_sz.height / 2);

View File

@ -12,11 +12,11 @@ namespace CairoChart {
} }
public Sort sort = Sort.BY_X; public Sort sort = Sort.BY_X;
public Axis axis_x = new Axis(); public Axis axis_x;
public Axis axis_y = new Axis(); public Axis axis_y;
public Place place = new Place(); public Place place = new Place();
public Text title = new Text (); public Text title;
public Marker marker = null; public Marker marker = null;
public Grid grid = new Grid (); public Grid grid = new Grid ();
@ -45,6 +45,9 @@ namespace CairoChart {
public Series (Chart chart) { public Series (Chart chart) {
this.chart = chart; this.chart = chart;
title = new Text(chart);
axis_x = new Axis(chart);
axis_y = new Axis(chart);
this.marker = new Marker(chart); this.marker = new Marker(chart);
} }
@ -124,8 +127,8 @@ namespace CairoChart {
double max_rec_width = 0; double max_rec_height = 0; double max_rec_width = 0; double max_rec_height = 0;
axis.calc_rec_sizes (chart, out max_rec_width, out max_rec_height, is_x); axis.calc_rec_sizes (chart, out max_rec_width, out max_rec_height, is_x);
var max_font_spacing = axis.font_spacing; var max_font_spacing = axis.font_spacing;
var max_axis_font_width = axis.title.text == "" ? 0 : axis.title.get_width(chart.ctx) + axis.font_spacing; var max_axis_font_width = axis.title.text == "" ? 0 : axis.title.width + axis.font_spacing;
var max_axis_font_height = axis.title.text == "" ? 0 : axis.title.get_height(chart.ctx) + axis.font_spacing; var max_axis_font_height = axis.title.text == "" ? 0 : axis.title.height + axis.font_spacing;
if (is_x) if (is_x)
s.join_relative_x_axes (si, true, ref max_rec_width, ref max_rec_height, ref max_font_spacing, ref max_axis_font_height, ref nskip); s.join_relative_x_axes (si, true, ref max_rec_width, ref max_rec_height, ref max_font_spacing, ref max_axis_font_height, ref nskip);
@ -200,7 +203,7 @@ namespace CairoChart {
max_rec_height = double.max (max_rec_height, tmp_max_rec_height); max_rec_height = double.max (max_rec_height, tmp_max_rec_height);
max_font_spacing = double.max (max_font_spacing, s2.axis_x.font_spacing); max_font_spacing = double.max (max_font_spacing, s2.axis_x.font_spacing);
max_axis_font_height = double.max (max_axis_font_height, s2.axis_x.title.text == "" ? 0 : max_axis_font_height = double.max (max_axis_font_height, s2.axis_x.title.text == "" ? 0 :
s2.axis_x.title.get_height(chart.ctx) + this.axis_x.font_spacing); s2.axis_x.title.height + this.axis_x.font_spacing);
} }
++nskip; ++nskip;
} else { } else {
@ -237,7 +240,7 @@ namespace CairoChart {
max_rec_height = double.max (max_rec_height, tmp_max_rec_height); max_rec_height = double.max (max_rec_height, tmp_max_rec_height);
max_font_spacing = double.max (max_font_spacing, s2.axis_y.font_spacing); max_font_spacing = double.max (max_font_spacing, s2.axis_y.font_spacing);
max_axis_font_width = double.max (max_axis_font_width, s2.axis_y.title.text == "" ? 0 max_axis_font_width = double.max (max_axis_font_width, s2.axis_y.title.text == "" ? 0
: s2.axis_y.title.get_width(chart.ctx) + this.axis_y.font_spacing); : s2.axis_y.title.width + this.axis_y.font_spacing);
++nskip; ++nskip;
} else { } else {
break; break;
@ -259,8 +262,8 @@ namespace CairoChart {
case Axis.Type.DATE_TIME: axis_x.format_date_time(x, out text, out time_text); break; case Axis.Type.DATE_TIME: axis_x.format_date_time(x, out text, out time_text); break;
} }
var scr_x = get_scr_x (x); var scr_x = get_scr_x (x);
var text_t = new Text(text, axis_x.font_style, axis_x.color); var text_t = new Text(chart, text, axis_x.font_style, axis_x.color);
var sz = axis_x.title.get_size(ctx); var sz = axis_x.title.size;
switch (axis_x.position) { switch (axis_x.position) {
case Axis.Position.LOW: case Axis.Position.LOW:
@ -269,14 +272,14 @@ namespace CairoChart {
ctx.move_to (print_x, print_y); ctx.move_to (print_x, print_y);
switch (axis_x.type) { switch (axis_x.type) {
case Axis.Type.NUMBERS: case Axis.Type.NUMBERS:
text_t.show(ctx); text_t.show();
break; break;
case Axis.Type.DATE_TIME: case Axis.Type.DATE_TIME:
if (axis_x.date_format != "") text_t.show(ctx); if (axis_x.date_format != "") text_t.show();
var time_text_t = new Text(time_text, axis_x.font_style, axis_x.color); var time_text_t = new Text(chart, time_text, axis_x.font_style, axis_x.color);
print_x = compact_rec_x_pos (x, time_text_t); print_x = compact_rec_x_pos (x, time_text_t);
ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.get_height(ctx) + axis_x.font_spacing)); ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.height + axis_x.font_spacing));
if (axis_x.time_format != "") time_text_t.show(ctx); if (axis_x.time_format != "") time_text_t.show();
break; break;
} }
// 6. Draw grid lines to the place.zy0. // 6. Draw grid lines to the place.zy0.
@ -297,14 +300,14 @@ namespace CairoChart {
switch (axis_x.type) { switch (axis_x.type) {
case Axis.Type.NUMBERS: case Axis.Type.NUMBERS:
text_t.show(ctx); text_t.show();
break; break;
case Axis.Type.DATE_TIME: case Axis.Type.DATE_TIME:
if (axis_x.date_format != "") text_t.show(ctx); if (axis_x.date_format != "") text_t.show();
var time_text_t = new Text(time_text, axis_x.font_style, axis_x.color); var time_text_t = new Text(chart, time_text, axis_x.font_style, axis_x.color);
print_x = compact_rec_x_pos (x, time_text_t); print_x = compact_rec_x_pos (x, time_text_t);
ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.get_height(ctx) + axis_x.font_spacing)); ctx.move_to (print_x, print_y - (axis_x.date_format == "" ? 0 : text_t.height + axis_x.font_spacing));
if (axis_x.time_format != "") time_text_t.show(ctx); if (axis_x.time_format != "") time_text_t.show();
break; break;
} }
// 6. Draw grid lines to the place.zy1. // 6. Draw grid lines to the place.zy1.
@ -360,7 +363,7 @@ namespace CairoChart {
} }
} }
var sz = s.axis_x.title.get_size(chart.ctx); var sz = s.axis_x.title.size;
// 4.5. Draw Axis title // 4.5. Draw Axis title
if (s.axis_x.title.text != "") { if (s.axis_x.title.text != "") {
@ -373,7 +376,7 @@ namespace CairoChart {
chart.ctx.move_to(scr_x - sz.width / 2.0, scr_y); chart.ctx.move_to(scr_x - sz.width / 2.0, scr_y);
chart.color = s.axis_x.color; chart.color = s.axis_x.color;
if (chart.joint_x) chart.color = chart.joint_color; if (chart.joint_x) chart.color = chart.joint_color;
s.axis_x.title.show(chart.ctx); s.axis_x.title.show();
} }
s.draw_horizontal_records (step, max_rec_height, x_min); s.draw_horizontal_records (step, max_rec_height, x_min);
@ -402,16 +405,16 @@ namespace CairoChart {
else chart.color = axis_y.color; else chart.color = axis_y.color;
var text = axis_y.format.printf((LongDouble)y); var text = axis_y.format.printf((LongDouble)y);
var scr_y = get_scr_y (y); var scr_y = get_scr_y (y);
var text_t = new Text(text, axis_y.font_style, axis_y.color); var text_t = new Text(chart, text, axis_y.font_style, axis_y.color);
var text_sz = text_t.get_size(ctx); var text_sz = text_t.size;
var sz = axis_y.title.get_size(ctx); var sz = axis_y.title.size;
switch (axis_y.position) { switch (axis_y.position) {
case Axis.Position.LOW: case Axis.Position.LOW:
ctx.move_to (chart.evarea.x0 + max_rec_width - text_sz.width + axis_y.font_spacing ctx.move_to (chart.evarea.x0 + max_rec_width - text_sz.width + axis_y.font_spacing
+ (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing), + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing),
compact_rec_y_pos (y, text_t)); compact_rec_y_pos (y, text_t));
text_t.show(ctx); text_t.show();
// 6. Draw grid lines to the place.zx0. // 6. Draw grid lines to the place.zx0.
var line_style = grid.line_style; var line_style = grid.line_style;
if (joint_y) line_style.color = Color(0, 0, 0, 0.5); if (joint_y) line_style.color = Color(0, 0, 0, 0.5);
@ -427,7 +430,7 @@ namespace CairoChart {
ctx.move_to (chart.evarea.x1 - text_sz.width - axis_y.font_spacing ctx.move_to (chart.evarea.x1 - text_sz.width - axis_y.font_spacing
- (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing), - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing),
compact_rec_y_pos (y, text_t)); compact_rec_y_pos (y, text_t));
text_t.show(ctx); text_t.show();
// 6. Draw grid lines to the place.zx1. // 6. Draw grid lines to the place.zx1.
var line_style = grid.line_style; var line_style = grid.line_style;
if (joint_y) line_style.color = Color(0, 0, 0, 0.5); if (joint_y) line_style.color = Color(0, 0, 0, 0.5);
@ -480,7 +483,7 @@ namespace CairoChart {
} }
} }
var sz = s.axis_y.title.get_size(chart.ctx); var sz = s.axis_y.title.size;
// 4.5. Draw Axis title // 4.5. Draw Axis title
if (s.axis_y.title.text != "") { if (s.axis_y.title.text != "") {
@ -497,7 +500,7 @@ namespace CairoChart {
} }
chart.color = s.axis_y.color; chart.color = s.axis_y.color;
if (chart.joint_y) chart.color = chart.joint_color; if (chart.joint_y) chart.color = chart.joint_color;
s.axis_y.title.show(chart.ctx); s.axis_y.title.show();
} }
s.draw_vertical_records (step, max_rec_width, y_min); s.draw_vertical_records (step, max_rec_width, y_min);
@ -517,13 +520,13 @@ namespace CairoChart {
} }
public virtual double compact_rec_x_pos (Float128 x, Text text) { public virtual double compact_rec_x_pos (Float128 x, Text text) {
var sz = text.get_size(chart.ctx); var sz = text.size;
return get_scr_x(x) - sz.width / 2.0 return get_scr_x(x) - sz.width / 2.0
- sz.width * (x - (axis_x.range.zmin + axis_x.range.zmax) / 2.0) / axis_x.range.zrange; - sz.width * (x - (axis_x.range.zmin + axis_x.range.zmax) / 2.0) / axis_x.range.zrange;
} }
public virtual double compact_rec_y_pos (Float128 y, Text text) { public virtual double compact_rec_y_pos (Float128 y, Text text) {
var sz = text.get_size(chart.ctx); var sz = text.size;
return get_scr_y(y) + sz.height / 2.0 return get_scr_y(y) + sz.height / 2.0
+ sz.height * (y - (axis_y.range.zmin + axis_y.range.zmax) / 2.0) / axis_y.range.zrange; + sz.height * (y - (axis_y.range.zmin + axis_y.range.zmax) / 2.0) / axis_y.range.zrange;
} }

View File

@ -1,10 +1,40 @@
namespace CairoChart { namespace CairoChart {
/**
* ``CairoChart`` Text.
*/
public class Text { public class Text {
Chart chart = null;
/**
* ``Text`` string.
*/
public string text = ""; public string text = "";
/**
* ``Text`` font style.
*/
public Font style = Font (); public Font style = Font ();
/**
* ``Text`` color.
*/
public Color color = Color(); public Color color = Color();
/**
* Vertical spacing.
*/
public double vspacing = 4; public double vspacing = 4;
/**
* Horizontal spacing.
*/
public double hspacing = 4; public double hspacing = 4;
/**
* Both vertical & horizontal spacing (set only).
*/
public double spacing { public double spacing {
protected get { protected get {
return 0; return 0;
@ -15,78 +45,130 @@ namespace CairoChart {
default = 4; default = 4;
} }
public virtual Cairo.TextExtents get_extents (Cairo.Context ctx) { /**
ctx.select_font_face (style.family, style.slant, style.weight); * Cairo ``Text`` extents.
ctx.set_font_size (style.size); */
Cairo.TextExtents extents; public virtual Cairo.TextExtents ext {
ctx.text_extents (text, out extents); get {
return extents; chart.ctx.select_font_face (style.family, style.slant, style.weight);
} chart.ctx.set_font_size (style.size);
Cairo.TextExtents ext;
public virtual double get_width (Cairo.Context ctx) { chart.ctx.text_extents (text, out ext);
var extents = get_extents (ctx); return ext;
switch (style.orient) { }
case Gtk.Orientation.HORIZONTAL: return extents.width; protected set {
case Gtk.Orientation.VERTICAL: return extents.height;
default: return 0.0;
} }
} }
public virtual double get_height (Cairo.Context ctx) { /**
var extents = get_extents (ctx); * ``Text`` width.
switch (style.orient) { */
case Gtk.Orientation.HORIZONTAL: return extents.height; public virtual double width {
case Gtk.Orientation.VERTICAL: return extents.width; get {
default: return 0.0; switch (style.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.width;
case Gtk.Orientation.VERTICAL: return ext.height;
default: return 0.0;
}
}
protected set {
} }
} }
/**
* ``Text`` height.
*/
public virtual double height {
get {
switch (style.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.height;
case Gtk.Orientation.VERTICAL: return ext.width;
default: return 0.0;
}
}
protected set {
}
}
/**
* ``Text`` size.
*/
public struct Size { public struct Size {
/**
* ``Text`` width.
*/
double width; double width;
/**
* ``Text`` height.
*/
double height; double height;
} }
public virtual Size get_size (Cairo.Context ctx) { /**
var sz = Size(); * ``Text`` @{link Size}.
var extents = get_extents (ctx); */
switch (style.orient) { public virtual Size size {
case Gtk.Orientation.HORIZONTAL: get {
sz.width = extents.width + extents.x_bearing; var sz = Size();
sz.height = extents.height; var e = ext;
break; switch (style.orient) {
case Gtk.Orientation.VERTICAL: case Gtk.Orientation.HORIZONTAL:
sz.width = extents.height; // + extents.x_bearing ? sz.width = e.width + e.x_bearing;
sz.height = extents.width; // +- extents.y_bearing ? sz.height = e.height;
break; break;
case Gtk.Orientation.VERTICAL:
sz.width = e.height; // + e.x_bearing ?
sz.height = e.width; // +- e.y_bearing ?
break;
}
return sz;
}
protected set {
} }
return sz;
} }
public virtual void show (Cairo.Context ctx) { /**
ctx.select_font_face(style.family, * Show ``Text``.
style.slant, */
style.weight); public virtual void show () {
ctx.set_font_size(style.size); chart.ctx.select_font_face(style.family,
style.slant,
style.weight);
chart.ctx.set_font_size(style.size);
if (style.orient == Gtk.Orientation.VERTICAL) { if (style.orient == Gtk.Orientation.VERTICAL) {
ctx.rotate(- GLib.Math.PI / 2.0); chart.ctx.rotate(- GLib.Math.PI / 2.0);
ctx.show_text(text); chart.ctx.show_text(text);
ctx.rotate(GLib.Math.PI / 2.0); chart.ctx.rotate(GLib.Math.PI / 2.0);
} else { } else {
ctx.show_text(text); chart.ctx.show_text(text);
} }
} }
public Text (string text = "", /**
* Constructs a new ``Text``.
* @param chart ``Chart`` instance.
* @param text ``Text`` string.
* @param style ``Text`` font style.
* @param color ``Text`` color.
*/
public Text (Chart chart,
string text = "",
Font style = Font(), Font style = Font(),
Color color = Color() Color color = Color()
) { ) {
this.chart = chart;
this.text = text; this.text = text;
this.style = style; this.style = style;
this.color = color; this.color = color;
} }
/**
* Gets a copy of the ``Text``.
*/
public virtual Text copy () { public virtual Text copy () {
var text = new Text (); var text = new Text (chart);
text.chart = this.chart;
text.text = this.text; text.text = this.text;
text.style = this.style; text.style = this.style;
text.color = this.color; text.color = this.color;

View File

@ -5,13 +5,13 @@ void plot_chart1 (Chart chart) {
var s2 = new Series (chart); var s2 = new Series (chart);
var s3 = new Series (chart); var s3 = new Series (chart);
s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0);
s1.points = {Point128(0, 0), Point128(2, 1), Point128(1, 3)}; s1.points = {Point128(0, 0), Point128(2, 1), Point128(1, 3)};
s1.axis_x.position = Axis.Position.HIGH; s1.axis_x.position = Axis.Position.HIGH;
s1.axis_x.format = "%.3Lf"; s1.axis_x.format = "%.3Lf";
s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0);
s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)};
s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1);
s3.points = {Point128(9, 17), Point128(2, 10), Point128(122, 31)}; s3.points = {Point128(9, 17), Point128(2, 10), Point128(122, 31)};
s3.axis_y.position = Axis.Position.HIGH; s3.axis_y.position = Axis.Position.HIGH;
@ -34,12 +34,12 @@ void plot_chart1 (Chart chart) {
s2.marker.type = Marker.Type.CIRCLE; s2.marker.type = Marker.Type.CIRCLE;
s3.marker.type = Marker.Type.PRICLE_TRIANGLE; s3.marker.type = Marker.Type.PRICLE_TRIANGLE;
s1.axis_x.title = new Text("Series 1: Axis X."); s1.axis_x.title = new Text(chart, "Series 1: Axis X.");
s1.axis_y.title = new Text("Series 1: Axis Y."); s1.axis_y.title = new Text(chart, "Series 1: Axis Y.");
s2.axis_x.title = new Text("Series 2: Axis X."); s2.axis_x.title = new Text(chart, "Series 2: Axis X.");
s2.axis_y.title = new Text("Series 2: Axis Y."); s2.axis_y.title = new Text(chart, "Series 2: Axis Y.");
s3.axis_x.title = new Text("Series 3: Axis X."); s3.axis_x.title = new Text(chart, "Series 3: Axis X.");
s3.axis_y.title = new Text("Series 3: Axis Y."); s3.axis_y.title = new Text(chart, "Series 3: Axis Y.");
chart.series = { s1, s2, s3 }; chart.series = { s1, s2, s3 };
} }
@ -49,13 +49,13 @@ void plot_chart2 (Chart chart) {
var s2 = new Series (chart); var s2 = new Series (chart);
var s3 = new Series (chart); var s3 = new Series (chart);
s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0);
s1.points = {Point128(-12, 0), Point128(2, 1), Point128(20, 3)}; s1.points = {Point128(-12, 0), Point128(2, 1), Point128(20, 3)};
s2.axis_y.position = Axis.Position.HIGH; s2.axis_y.position = Axis.Position.HIGH;
s1.axis_x.format = "%.3Lf"; s1.axis_x.format = "%.3Lf";
s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0);
s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)};
s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1);
s3.points = {Point128(9, 17), Point128(2, 10), Point128(-15, 31)}; s3.points = {Point128(9, 17), Point128(2, 10), Point128(-15, 31)};
s3.axis_y.position = Axis.Position.HIGH; s3.axis_y.position = Axis.Position.HIGH;
@ -78,12 +78,12 @@ void plot_chart2 (Chart chart) {
s2.marker.type = Marker.Type.PRICLE_SQUARE; s2.marker.type = Marker.Type.PRICLE_SQUARE;
s3.marker.type = Marker.Type.SQUARE; s3.marker.type = Marker.Type.SQUARE;
s1.axis_x.title = new Text("All Series: Axis X."); s1.axis_x.title = new Text(chart, "All Series: Axis X.");
s1.axis_y.title = new Text("Series 1: Axis Y."); s1.axis_y.title = new Text(chart, "Series 1: Axis Y.");
s2.axis_x.title = new Text("All Series: Axis X."); s2.axis_x.title = new Text(chart, "All Series: Axis X.");
s2.axis_y.title = new Text("Series 2: Axis Y."); s2.axis_y.title = new Text(chart, "Series 2: Axis Y.");
s3.axis_x.title = new Text("All Series: Axis X."); s3.axis_x.title = new Text(chart, "All Series: Axis X.");
s3.axis_y.title = new Text("Series 3: Axis Y."); s3.axis_y.title = new Text(chart, "Series 3: Axis Y.");
//s1.axis_x.position = s2.axis_x.position = s3.axis_x.position = Axis.Position.HIGH; //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.type = s2.axis_x.type = s3.axis_x.type = Axis.Type.DATE_TIME;
@ -97,15 +97,15 @@ void plot_chart3 (Chart chart) {
var s2 = new Series (chart); var s2 = new Series (chart);
var s3 = new Series (chart); var s3 = new Series (chart);
s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0);
s1.points = {Point128(0, 70), Point128(2, 155), Point128(1, -3)}; s1.points = {Point128(0, 70), Point128(2, 155), Point128(1, -3)};
s1.axis_x.position = Axis.Position.HIGH; s1.axis_x.position = Axis.Position.HIGH;
s1.axis_y.position = Axis.Position.HIGH; s1.axis_y.position = Axis.Position.HIGH;
s1.axis_x.format = "%.3Lf"; s1.axis_x.format = "%.3Lf";
s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0);
s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)};
s2.axis_y.position = Axis.Position.HIGH; s2.axis_y.position = Axis.Position.HIGH;
s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1);
s3.points = {Point128(9, -17), Point128(2, 10), Point128(122, 31)}; s3.points = {Point128(9, -17), Point128(2, 10), Point128(122, 31)};
s3.axis_y.position = Axis.Position.HIGH; s3.axis_y.position = Axis.Position.HIGH;
@ -128,12 +128,12 @@ void plot_chart3 (Chart chart) {
s2.marker.type = Marker.Type.PRICLE_CIRCLE; s2.marker.type = Marker.Type.PRICLE_CIRCLE;
s3.marker.type = Marker.Type.TRIANGLE; s3.marker.type = Marker.Type.TRIANGLE;
s1.axis_x.title = new Text("Series 1: Axis X."); s1.axis_x.title = new Text(chart, "Series 1: Axis X.");
s1.axis_y.title = new Text("Series 1: Axis Y."); s1.axis_y.title = new Text(chart, "Series 1: Axis Y.");
s2.axis_x.title = new Text("Series 2: Axis X."); s2.axis_x.title = new Text(chart, "Series 2: Axis X.");
s2.axis_y.title = new Text("Series 2: Axis Y."); s2.axis_y.title = new Text(chart, "Series 2: Axis Y.");
s3.axis_x.title = new Text("Series 3: Axis X."); s3.axis_x.title = new Text(chart, "Series 3: Axis X.");
s3.axis_y.title = new Text("Series 3: Axis Y."); s3.axis_y.title = new Text(chart, "Series 3: Axis Y.");
//s1.axis_y.position = s2.axis_y.position = s3.axis_y.position = Axis.Position.LOW; //s1.axis_y.position = s2.axis_y.position = s3.axis_y.position = Axis.Position.LOW;
@ -154,17 +154,17 @@ void plot_chart4 (Chart chart) {
var now = new DateTime.now_local().to_unix(); var now = new DateTime.now_local().to_unix();
var high = (uint64) (253000000000L); var high = (uint64) (253000000000L);
s1.title = new Text("Series 1"); s1.color = Color (1, 0, 0); s1.title = new Text(chart, "Series 1"); s1.color = Color (1, 0, 0);
s1.points = {Point128(now, 70), Point128(now - 100000, 155), Point128(now + 100000, 30)}; s1.points = {Point128(now, 70), Point128(now - 100000, 155), Point128(now + 100000, 30)};
s1.axis_x.position = Axis.Position.HIGH; s1.axis_x.position = Axis.Position.HIGH;
s1.axis_y.position = Axis.Position.HIGH; s1.axis_y.position = Axis.Position.HIGH;
s2.title = new Text("Series 2"); s2.color = Color (0, 1, 0); s2.title = new Text(chart, "Series 2"); s2.color = Color (0, 1, 0);
s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)}; s2.points = {Point128(5, -3), Point128(25, -18), Point128(-11, 173)};
s2.axis_y.position = Axis.Position.HIGH; s2.axis_y.position = Axis.Position.HIGH;
s3.title = new Text("Series 3"); s3.color = Color (0, 0, 1); s3.title = new Text(chart, "Series 3"); s3.color = Color (0, 0, 1);
s3.points = {Point128(high - 2 + 0.73, -17), Point128(high - 1 + 0.234, 10), Point128(high + 1 + 0.411, 31)}; s3.points = {Point128(high - 2 + 0.73, -17), Point128(high - 1 + 0.234, 10), Point128(high + 1 + 0.411, 31)};
s3.axis_y.position = Axis.Position.HIGH; s3.axis_y.position = Axis.Position.HIGH;
s4.title = new Text("Series 4"); s4.color = Color (0.5, 0.3, 0.9); s4.title = new Text(chart, "Series 4"); s4.color = Color (0.5, 0.3, 0.9);
s4.points = {Point128(high + 0.005, -19.05), Point128(high + 0.0051, 28), Point128(high + 0.0052, 55), Point128(high + 0.0053, 44)}; s4.points = {Point128(high + 0.005, -19.05), Point128(high + 0.0051, 28), Point128(high + 0.0052, 55), Point128(high + 0.0053, 44)};
s4.axis_y.position = Axis.Position.HIGH; s4.axis_y.position = Axis.Position.HIGH;
@ -193,14 +193,14 @@ void plot_chart4 (Chart chart) {
s3.marker.type = Marker.Type.TRIANGLE; s3.marker.type = Marker.Type.TRIANGLE;
s4.marker.type = Marker.Type.PRICLE_SQUARE; s4.marker.type = Marker.Type.PRICLE_SQUARE;
s1.axis_x.title = new Text("Series 1: Axis X."); s1.axis_x.title = new Text(chart, "Series 1: Axis X.");
s1.axis_y.title = new Text("Series 1: Axis Y."); s1.axis_y.title = new Text(chart, "Series 1: Axis Y.");
s2.axis_x.title = new Text("Series 2: Axis X."); s2.axis_x.title = new Text(chart, "Series 2: Axis X.");
s2.axis_y.title = new Text("Series 2: Axis Y."); s2.axis_y.title = new Text(chart, "Series 2: Axis Y.");
s3.axis_x.title = new Text("Series 3: Axis X."); s3.axis_x.title = new Text(chart, "Series 3: Axis X.");
s3.axis_y.title = new Text("Series 3: Axis Y."); s3.axis_y.title = new Text(chart, "Series 3: Axis Y.");
s4.axis_x.title = new Text("Series 4: Axis X."); s4.axis_x.title = new Text(chart, "Series 4: Axis X.");
s4.axis_y.title = new Text("Series 4: Axis Y."); s4.axis_y.title = new Text(chart, "Series 4: Axis Y.");
chart.series = { s1, s2, s3, s4 }; chart.series = { s1, s2, s3, s4 };
} }
@ -432,9 +432,9 @@ int main (string[] args) {
var str = chart.cursors.get_cursors_delta_str(); var str = chart.cursors.get_cursors_delta_str();
if (str != "") { if (str != "") {
var text = "Δ = " + str; var text = "Δ = " + str;
var text_t = new Text(text); var text_t = new Text(chart, text);
var w = text_t.get_width(ctx); var w = text_t.width;
var h = text_t.get_height(ctx); var h = text_t.height;
var x0 = chart.plarea.x1 - w - 5; var x0 = chart.plarea.x1 - w - 5;
var y0 = chart.plarea.y0 + h + 5; var y0 = chart.plarea.y0 + h + 5;
chart.color = chart.legend.bg_color; chart.color = chart.legend.bg_color;