-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlegend.vala
More file actions
108 lines (90 loc) · 3.63 KB
/
Copy pathlegend.vala
File metadata and controls
108 lines (90 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Cairo;
namespace LiveChart {
public abstract class Legend : Drawable, Object {
public bool visible { get; set; default = true; }
public Labels labels = new Labels();
protected Gee.ArrayList<Serie> series = new Gee.ArrayList<Serie>();
protected BoundingBox bounding_box = BoundingBox() {
x=0,
y=0,
width=0,
height=0
};
public Gdk.RGBA main_color {
get; set;
default= Gdk.RGBA() {
red = 1.0f,
green = 1.0f,
blue = 1.0f,
alpha = 1.0f
};
}
public void add_legend(Serie serie) {
series.add(serie);
}
public void remove_legend(Serie serie){
if(series.contains(serie)){
series.remove(serie);
}
}
public void remove_all_legend(){
series.clear();
}
public abstract void draw(Context ctx, Config config);
public BoundingBox get_bounding_box() {
return bounding_box;
}
}
public class HorizontalLegend : Legend {
private const int COLOR_BLOCK_WIDTH = 15;
private const int COLOR_BLOCK_HEIGHT = 10;
public override void draw(Context ctx, Config config) {
if (visible) {
var y_padding = get_y_padding(config);
var boundaries = config.boundaries();
var pos = 0;
series.foreach((serie) => {
ctx.set_source_rgba(serie.main_color.red, serie.main_color.green, serie.main_color.blue, 1);
ctx.rectangle(boundaries.x.min + pos, boundaries.y.max + y_padding, HorizontalLegend.COLOR_BLOCK_WIDTH, HorizontalLegend.COLOR_BLOCK_HEIGHT);
ctx.fill();
labels.font.configure(ctx);
TextExtents extents = name_extents(serie.name, ctx);
ctx.move_to(boundaries.x.min + pos + HorizontalLegend.COLOR_BLOCK_WIDTH + 3, boundaries.y.max + y_padding + extents.height + (HorizontalLegend.COLOR_BLOCK_HEIGHT - extents.height) / 2);
ctx.show_text(serie.name);
pos += HorizontalLegend.COLOR_BLOCK_WIDTH + (int) extents.width + 20;
return true;
});
ctx.stroke();
this.update_bounding_box(config, pos);
this.debug(ctx);
}
}
private int get_y_padding(Config config) {
return (int) (Grid.ABSCISSA_TIME_PADDING * 2 + config.x_axis.labels.extents.height);
}
private TextExtents name_extents(string name, Context ctx) {
TextExtents name_extents;
ctx.text_extents(name, out name_extents);
return name_extents;
}
private void update_bounding_box(Config config, int width) {
var boundaries = config.boundaries();
this.bounding_box = BoundingBox() {
x=boundaries.x.min,
y=boundaries.y.max + get_y_padding(config),
width=width,
height=10
};
}
protected void debug(Context ctx) {
var debug = Environment.get_variable("LIVE_CHART_DEBUG");
if(debug != null) {
ctx.rectangle(bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height);
ctx.stroke();
}
}
}
public class NoopLegend : Legend {
public override void draw(Context ctx, Config config) {}
}
}