OK In progress...

This commit is contained in:
Kolan Sh 2018-01-21 18:41:41 +03:00
parent 2d4d01d532
commit ee52791eca
7 changed files with 127 additions and 106 deletions

View File

@ -60,7 +60,7 @@ namespace CairoChart {
}
default = 2;
}
public Font font_style = Font ();
public Font font_style = new Font ();
public Color color = Color ();
public LineStyle line_style = LineStyle ();
public double font_spacing = 5;
@ -108,9 +108,8 @@ namespace CairoChart {
switch (type) {
case Axis.Type.NUMBERS:
var text = new Text (chart, format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style);
var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width);
max_rec_height = double.max (max_rec_height, sz.height);
max_rec_width = double.max (max_rec_width, text.width);
max_rec_height = double.max (max_rec_height, text.height);
break;
case Axis.Type.DATE_TIME:
string date, time;
@ -119,15 +118,13 @@ namespace CairoChart {
var h = 0.0;
if (date_format != "") {
var text = new Text (chart, date + (horizontal ? "_" : ""), font_style);
var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width);
h = sz.height;
max_rec_width = double.max (max_rec_width, text.width);
h = text.height;
}
if (time_format != "") {
var text = new Text (chart, time + (horizontal ? "_" : ""), font_style);
var sz = text.size;
max_rec_width = double.max (max_rec_width, sz.width);
h += sz.height;
max_rec_width = double.max (max_rec_width, text.width);
h += text.height;
}
max_rec_height = double.max (max_rec_height, h);
break;

View File

@ -303,8 +303,11 @@ namespace CairoChart {
if (evarea.height < 0) evarea.height = 0;
}
protected virtual void rot_axes_titles () {
foreach (var s in series)
s.axis_y.title.style.orient = Gtk.Orientation.VERTICAL;
foreach (var s in series) {
//s.axis_y.title.font = new Font();//s.axis_y.title.font.copy();
s.axis_y.title.font.orient = Gtk.Orientation.VERTICAL;
s.axis_y.title.font.size = 50;
}
}
protected virtual void eval_plarea () {
@ -338,12 +341,11 @@ namespace CairoChart {
ctx.stroke ();
}
protected virtual void draw_title () {
var sz = title.size;
var title_height = sz.height + title.vspacing * 2;
var title_height = title.height + title.vspacing * 2;
evarea.y0 += title_height;
evarea.height -= title_height;
color = title.color;
ctx.move_to (area.width/2 - sz.width/2, sz.height + title.vspacing);
ctx.move_to (area.width/2 - title.width/2, title.height + title.vspacing);
title.show();
}
protected virtual void draw_haxes () {

View File

@ -214,10 +214,10 @@ namespace CairoChart {
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(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;
if (show_x) { var sz = x_t.size; 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.size; size.x = double.max(size.x, sz.width); h_x += sz.height; }
if (show_y) { var sz = y_t.size; size.x += sz.width; h_y = sz.height; }
if (show_x) { size.x = x_t.width; h_x = x_t.height; }
if (show_date) { size.x = date_t.width; h_x = date_t.height; }
if (show_time) { size.x = double.max(size.x, time_t.width); h_x += time_t.height; }
if (show_y) { size.x += y_t.width; h_y = y_t.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_date && show_time) h_x += s.axis_x.font_spacing;
size.y = double.max (h_x, h_y);
@ -288,7 +288,6 @@ namespace CairoChart {
case Axis.Type.DATE_TIME: s.axis_x.format_date_time(x, out text, out time_text); break;
}
var text_t = new Text(chart, text, s.axis_x.font_style, s.axis_x.color);
var sz = text_t.size;
var time_text_t = new Text(chart, time_text, s.axis_x.font_style, s.axis_x.color);
var print_y = 0.0;
switch (s.axis_x.position) {
@ -302,10 +301,10 @@ namespace CairoChart {
+ (chart.legend.position == Legend.Position.TOP ? chart.legend.height : 0);
switch (s.axis_x.type) {
case Axis.Type.NUMBERS:
print_y += sz.height;
print_y += text_t.height;
break;
case Axis.Type.DATE_TIME:
print_y += (s.axis_x.date_format == "" ? 0 : sz.height)
print_y += (s.axis_x.date_format == "" ? 0 : text_t.height)
+ (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);
break;
@ -322,7 +321,7 @@ namespace CairoChart {
case Axis.Type.DATE_TIME:
if (s.axis_x.date_format != "") text_t.show();
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 : text_t.height + s.axis_x.font_spacing));
if (s.axis_x.time_format != "") time_text_t.show();
break;
}
@ -386,9 +385,8 @@ namespace CairoChart {
string date = "", time = "";
s.axis_x.format_date_time(point.x, out date, out time);
var text_t = new Text(chart, time, s.axis_x.font_style);
var sz = text_t.size;
var y = svp.y + sz.height / 2;
if (show_date) y -= sz.height / 2 + s.axis_x.font_spacing / 2;
var y = svp.y + text_t.height / 2;
if (show_date) y -= text_t.height / 2 + s.axis_x.font_spacing / 2;
chart.ctx.move_to (svp.x - size.x / 2, y);
if (chart.joint_x) chart.color = chart.joint_color;
text_t.show();
@ -399,9 +397,8 @@ namespace CairoChart {
string date = "", time = "";
s.axis_x.format_date_time(point.x, out date, out time);
var text_t = new Text(chart, date, s.axis_x.font_style);
var sz = text_t.size;
var y = svp.y + sz.height / 2;
if (show_time) y += sz.height / 2 + s.axis_x.font_spacing / 2;
var y = svp.y + text_t.height / 2;
if (show_time) y += text_t.height / 2 + s.axis_x.font_spacing / 2;
chart.ctx.move_to (svp.x - size.x / 2, y);
if (chart.joint_x) chart.color = chart.joint_color;
text_t.show();
@ -410,8 +407,7 @@ namespace CairoChart {
if (show_y) {
chart.color = s.axis_y.color;
var text_t = new Text(chart, s.axis_y.format.printf((LongDouble)point.y), s.axis_y.font_style);
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 - text_t.width, svp.y + text_t.height / 2);
if (chart.joint_y) chart.color = chart.joint_color;
text_t.show();
}

View File

@ -3,32 +3,33 @@ namespace CairoChart {
/**
* ``Font`` style.
*/
public struct Font {
[Compact]
public class Font : Object {
/**
* A font family name, encoded in UTF-8.
*/
string family;
public string family;
/**
* The new font size, in user space units.
*/
double size;
public double size;
/**
* The slant for the font.
*/
Cairo.FontSlant slant;
public Cairo.FontSlant slant;
/**
* The weight for the font.
*/
Cairo.FontWeight weight;
public Cairo.FontWeight weight;
/**
* Font/Text orientation.
*/
Gtk.Orientation orient;
public Gtk.Orientation orient;
/**
* Constructs a new ``Font``.
@ -50,5 +51,12 @@ namespace CairoChart {
this.weight = weight;
this.orient = orient;
}
/**
* Gets a copy of the ``Font``.
*/
public Font copy () {
return new Font(family, size, slant, weight, orient);
}
}
}

View File

@ -10,7 +10,7 @@ namespace CairoChart {
}
public Position position = Position.TOP;
public Font font_style = Font();
public Font font_style = new Font();
public Color bg_color = Color(1, 1, 1);
public LineStyle border_style = LineStyle ();
public double spacing = 5;
@ -115,13 +115,11 @@ namespace CairoChart {
if (!s.zoom_show) continue;
var title_sz = s.title.size;
// carry
switch (position) {
case Position.TOP:
case Position.BOTTOM:
var ser_title_width = title_sz.width + line_length;
var ser_title_width = s.title.width + line_length;
if (leg_width_sum + (leg_width_sum == 0 ? 0 : text_hspace) + ser_title_width > chart.area.width) { // carry
leg_height_sum += max_font_h;
switch (process_type) {
@ -150,34 +148,34 @@ namespace CairoChart {
s.title.show();
// series line style
chart.ctx.move_to (x, y - title_sz.height / 2);
chart.ctx.move_to (x, y - s.title.height / 2);
s.line_style.apply(chart);
chart.ctx.rel_line_to (line_length, 0);
chart.ctx.stroke();
s.marker.draw_at_pos (x + line_length / 2, y - title_sz.height / 2);
s.marker.draw_at_pos (x + line_length / 2, y - s.title.height / 2);
break;
}
switch (position) {
case Position.TOP:
case Position.BOTTOM:
var ser_title_width = title_sz.width + line_length;
var ser_title_width = s.title.width + line_length;
leg_width_sum += (leg_width_sum == 0 ? 0 : text_hspace) + ser_title_width;
max_font_h = double.max (max_font_h, title_sz.height) + (leg_height_sum != 0 ? text_vspace : 0);
max_font_h = double.max (max_font_h, s.title.height) + (leg_height_sum != 0 ? text_vspace : 0);
break;
case Position.LEFT:
case Position.RIGHT:
switch (process_type) {
case ProcessType.CALC:
max_font_heights += title_sz.height + (leg_height_sum != 0 ? text_vspace : 0);
width = double.max (width, title_sz.width + line_length);
max_font_heights += s.title.height + (leg_height_sum != 0 ? text_vspace : 0);
width = double.max (width, s.title.width + line_length);
break;
case ProcessType.DRAW:
heights_idx++;
break;
}
leg_height_sum += title_sz.height + (leg_height_sum != 0 ? text_vspace : 0);
leg_height_sum += s.title.height + (leg_height_sum != 0 ? text_vspace : 0);
break;
}
}

View File

@ -263,11 +263,10 @@ namespace CairoChart {
}
var scr_x = get_scr_x (x);
var text_t = new Text(chart, text, axis_x.font_style, axis_x.color);
var sz = axis_x.title.size;
switch (axis_x.position) {
case Axis.Position.LOW:
var print_y = chart.evarea.y1 - axis_x.font_spacing - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_spacing);
var print_y = chart.evarea.y1 - axis_x.font_spacing - (axis_x.title.text == "" ? 0 : axis_x.title.height + axis_x.font_spacing);
var print_x = compact_rec_x_pos (x, text_t);
ctx.move_to (print_x, print_y);
switch (axis_x.type) {
@ -286,7 +285,7 @@ namespace CairoChart {
var line_style = grid.line_style;
if (joint_x) line_style.color = Color(0, 0, 0, 0.5);
line_style.apply(chart);
double y = chart.evarea.y1 - max_rec_height - axis_x.font_spacing - (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_spacing);
double y = chart.evarea.y1 - max_rec_height - axis_x.font_spacing - (axis_x.title.text == "" ? 0 : axis_x.title.height + axis_x.font_spacing);
ctx.move_to (scr_x, y);
if (joint_x)
ctx.line_to (scr_x, chart.plarea.y0);
@ -294,7 +293,7 @@ namespace CairoChart {
ctx.line_to (scr_x, double.min (y, chart.plarea.y0 + chart.plarea.height * (1.0 - place.zy1)));
break;
case Axis.Position.HIGH:
var print_y = chart.evarea.y0 + max_rec_height + axis_x.font_spacing + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_spacing);
var print_y = chart.evarea.y0 + max_rec_height + axis_x.font_spacing + (axis_x.title.text == "" ? 0 : axis_x.title.height + axis_x.font_spacing);
var print_x = compact_rec_x_pos (x, text_t);
ctx.move_to (print_x, print_y);
@ -314,7 +313,7 @@ namespace CairoChart {
var line_style = grid.line_style;
if (joint_x) line_style.color = Color(0, 0, 0, 0.5);
line_style.apply(chart);
double y = chart.evarea.y0 + max_rec_height + axis_x.font_spacing + (axis_x.title.text == "" ? 0 : sz.height + axis_x.font_spacing);
double y = chart.evarea.y0 + max_rec_height + axis_x.font_spacing + (axis_x.title.text == "" ? 0 : axis_x.title.height + axis_x.font_spacing);
ctx.move_to (scr_x, y);
if (joint_x)
ctx.line_to (scr_x, chart.plarea.y1);
@ -363,17 +362,15 @@ namespace CairoChart {
}
}
var sz = s.axis_x.title.size;
// 4.5. Draw Axis title
if (s.axis_x.title.text != "") {
var scr_x = chart.plarea.x0 + chart.plarea.width * (s.place.zx0 + s.place.zx1) / 2.0;
double scr_y = 0.0;
switch (s.axis_x.position) {
case Axis.Position.LOW: scr_y = chart.evarea.y1 - s.axis_x.font_spacing; break;
case Axis.Position.HIGH: scr_y = chart.evarea.y0 + s.axis_x.font_spacing + sz.height; break;
case Axis.Position.HIGH: scr_y = chart.evarea.y0 + s.axis_x.font_spacing + axis_x.title.height; break;
}
chart.ctx.move_to(scr_x - sz.width / 2.0, scr_y);
chart.ctx.move_to(scr_x - axis_x.title.width / 2.0, scr_y);
chart.color = s.axis_x.color;
if (chart.joint_x) chart.color = chart.joint_color;
s.axis_x.title.show();
@ -388,7 +385,7 @@ namespace CairoChart {
if (nskip != 0) {--nskip; return;}
var tmp = max_rec_height + s.axis_x.font_spacing + (s.axis_x.title.text == "" ? 0 : sz.height + s.axis_x.font_spacing);
var tmp = max_rec_height + s.axis_x.font_spacing + (s.axis_x.title.text == "" ? 0 : axis_x.title.height + s.axis_x.font_spacing);
switch (s.axis_x.position) {
case Axis.Position.LOW: chart.evarea.y1 -= tmp; break;
case Axis.Position.HIGH: chart.evarea.y0 += tmp; break;
@ -406,20 +403,18 @@ namespace CairoChart {
var text = axis_y.format.printf((LongDouble)y);
var scr_y = get_scr_y (y);
var text_t = new Text(chart, text, axis_y.font_style, axis_y.color);
var text_sz = text_t.size;
var sz = axis_y.title.size;
switch (axis_y.position) {
case Axis.Position.LOW:
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),
ctx.move_to (chart.evarea.x0 + max_rec_width - text_t.width + axis_y.font_spacing
+ (axis_y.title.text == "" ? 0 : axis_y.title.width + axis_y.font_spacing),
compact_rec_y_pos (y, text_t));
text_t.show();
// 6. Draw grid lines to the place.zx0.
var line_style = grid.line_style;
if (joint_y) line_style.color = Color(0, 0, 0, 0.5);
line_style.apply(chart);
double x = chart.evarea.x0 + max_rec_width + axis_y.font_spacing + (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing);
double x = chart.evarea.x0 + max_rec_width + axis_y.font_spacing + (axis_y.title.text == "" ? 0 : axis_y.title.width + axis_y.font_spacing);
ctx.move_to (x, scr_y);
if (joint_y)
ctx.line_to (chart.plarea.x1, scr_y);
@ -427,15 +422,15 @@ namespace CairoChart {
ctx.line_to (double.max (x, chart.plarea.x0 + chart.plarea.width * place.zx1), scr_y);
break;
case Axis.Position.HIGH:
ctx.move_to (chart.evarea.x1 - text_sz.width - axis_y.font_spacing
- (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing),
ctx.move_to (chart.evarea.x1 - text_t.width - axis_y.font_spacing
- (axis_y.title.text == "" ? 0 : axis_y.title.width + axis_y.font_spacing),
compact_rec_y_pos (y, text_t));
text_t.show();
// 6. Draw grid lines to the place.zx1.
var line_style = grid.line_style;
if (joint_y) line_style.color = Color(0, 0, 0, 0.5);
line_style.apply(chart);
double x = chart.evarea.x1 - max_rec_width - axis_y.font_spacing - (axis_y.title.text == "" ? 0 : sz.width + axis_y.font_spacing);
double x = chart.evarea.x1 - max_rec_width - axis_y.font_spacing - (axis_y.title.text == "" ? 0 : axis_y.title.width + axis_y.font_spacing);
ctx.move_to (x, scr_y);
if (joint_y)
ctx.line_to (chart.plarea.x0, scr_y);
@ -483,19 +478,17 @@ namespace CairoChart {
}
}
var sz = s.axis_y.title.size;
// 4.5. Draw Axis title
if (s.axis_y.title.text != "") {
var scr_y = chart.plarea.y0 + chart.plarea.height * (1.0 - (s.place.zy0 + s.place.zy1) / 2.0);
switch (s.axis_y.position) {
case Axis.Position.LOW:
var scr_x = chart.evarea.x0 + s.axis_y.font_spacing + sz.width;
chart.ctx.move_to(scr_x, scr_y + sz.height / 2.0);
var scr_x = chart.evarea.x0 + s.axis_y.font_spacing + axis_y.title.width;
chart.ctx.move_to(scr_x, scr_y + axis_y.title.height / 2.0);
break;
case Axis.Position.HIGH:
var scr_x = chart.evarea.x1 - s.axis_y.font_spacing;
chart.ctx.move_to(scr_x, scr_y + sz.height / 2.0);
chart.ctx.move_to(scr_x, scr_y + axis_y.title.height / 2.0);
break;
}
chart.color = s.axis_y.color;
@ -512,7 +505,7 @@ namespace CairoChart {
if (nskip != 0) {--nskip; return;}
var tmp = max_rec_width + s.axis_y.font_spacing + (s.axis_y.title.text == "" ? 0 : sz.width + s.axis_y.font_spacing);
var tmp = max_rec_width + s.axis_y.font_spacing + (s.axis_y.title.text == "" ? 0 : axis_y.title.width + s.axis_y.font_spacing);
switch (s.axis_y.position) {
case Axis.Position.LOW: chart.evarea.x0 += tmp; break;
case Axis.Position.HIGH: chart.evarea.x1 -= tmp; break;
@ -520,15 +513,13 @@ namespace CairoChart {
}
public virtual double compact_rec_x_pos (Float128 x, Text text) {
var sz = text.size;
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;
return get_scr_x(x) - text.width / 2.0
- text.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) {
var sz = text.size;
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;
return get_scr_y(y) + text.height / 2.0
+ text.height * (y - (axis_y.range.zmin + axis_y.range.zmax) / 2.0) / axis_y.range.zrange;
}
public virtual double get_scr_x (Float128 x) {

View File

@ -5,17 +5,40 @@ namespace CairoChart {
*/
public class Text {
Chart chart = null;
Chart chart;
string _text;
Font _font;
Cairo.TextExtents? _ext;
/**
* ``Text`` string.
*/
public string text = "";
public string text {
get {
return _text;
}
set {
_text = value;
//_ext = null;// TODO: check necessity
}
}
/**
* ``Text`` font style.
*/
public Font style = Font ();
public Font font {
get {
return _font;
}
set {
_font = value;
// TODO: check necessity
//_font.notify.connect((s, p) => {
// _ext = null;
//});
//_ext = null;// TODO: check necessity
}
}
/**
* ``Text`` color.
@ -48,13 +71,14 @@ namespace CairoChart {
/**
* Cairo ``Text`` extents.
*/
public virtual Cairo.TextExtents ext {
virtual Cairo.TextExtents ext {
get {
chart.ctx.select_font_face (style.family, style.slant, style.weight);
chart.ctx.set_font_size (style.size);
Cairo.TextExtents ext;
chart.ctx.text_extents (text, out ext);
return ext;
if (_ext == null) {
chart.ctx.select_font_face (font.family, font.slant, font.weight);
chart.ctx.set_font_size (font.size);
chart.ctx.text_extents (text, out _ext);
}
return _ext;
}
protected set {
}
@ -65,7 +89,7 @@ namespace CairoChart {
*/
public virtual double width {
get {
switch (style.orient) {
switch (font.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.width;
case Gtk.Orientation.VERTICAL: return ext.height;
default: return 0.0;
@ -80,7 +104,7 @@ namespace CairoChart {
*/
public virtual double height {
get {
switch (style.orient) {
switch (font.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.height;
case Gtk.Orientation.VERTICAL: return ext.width;
default: return 0.0;
@ -93,7 +117,7 @@ namespace CairoChart {
/**
* ``Text`` size.
*/
public struct Size {
struct Size {
/**
* ``Text`` width.
*/
@ -108,11 +132,11 @@ namespace CairoChart {
/**
* ``Text`` @{link Size}.
*/
public virtual Size size {
virtual Size size {
get {
var sz = Size();
var e = ext;
switch (style.orient) {
switch (font.orient) {
case Gtk.Orientation.HORIZONTAL:
sz.width = e.width + e.x_bearing;
sz.height = e.height;
@ -132,11 +156,11 @@ namespace CairoChart {
* Show ``Text``.
*/
public virtual void show () {
chart.ctx.select_font_face(style.family,
style.slant,
style.weight);
chart.ctx.set_font_size(style.size);
if (style.orient == Gtk.Orientation.VERTICAL) {
chart.ctx.select_font_face(font.family,
font.slant,
font.weight);
chart.ctx.set_font_size(font.size);
if (font.orient == Gtk.Orientation.VERTICAL) {
chart.ctx.rotate(- GLib.Math.PI / 2.0);
chart.ctx.show_text(text);
chart.ctx.rotate(GLib.Math.PI / 2.0);
@ -149,18 +173,22 @@ namespace CairoChart {
* Constructs a new ``Text``.
* @param chart ``Chart`` instance.
* @param text ``Text`` string.
* @param style ``Text`` font style.
* @param font ``Text`` font style.
* @param color ``Text`` color.
*/
public Text (Chart chart,
string text = "",
Font style = Font(),
Font font = new Font(),
Color color = Color()
) {
this.chart = chart;
this.text = text;
this.style = style;
this.font = font;
this.color = color;
// TODO: check necessity
//_font.notify.connect((s, p) => {
// _ext = null;
//});
}
/**
@ -169,8 +197,9 @@ namespace CairoChart {
public virtual Text copy () {
var text = new Text (chart);
text.chart = this.chart;
text.text = this.text;
text.style = this.style;
text._text = this._text;
text._font = this._font;
text._ext = this._ext;
text.color = this.color;
return text;
}