Skip to content

Commit

Permalink
WIP not working POC
Browse files Browse the repository at this point in the history
  • Loading branch information
lcallarec committed Nov 25, 2020
1 parent 5f8ddce commit e08e13f
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 51 deletions.
10 changes: 5 additions & 5 deletions examples/static-chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public class Example : Gtk.Window {

var chart = new LiveChart.Static.StaticChart();
chart.add_serie(heap);
chart.config.y_axis.unit = "MB";
chart.config.y_axis.unit = "°C";
var categories = new Gee.ArrayList<string>();
categories.add("paris");
categories.add("london");
categories.add("mexico");
categories.add("seville");
chart.set_categories(categories);
heap.add("paris", 5000);
heap.add("london", 7000);
heap.add("mexico", 0);
heap.add("seville", 3000);
heap.add("paris", 16);
heap.add("london", 12);
heap.add("mexico", 21);
heap.add("seville", 26);

Gtk.Box box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
box.pack_start(chart, true, true, 0);
Expand Down
1 change: 1 addition & 0 deletions src/area.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
public class Area : Drawable, Object {
private Points points;
private Gdk.RGBA color;

private BoundingBox bounding_box = BoundingBox() {
x=0,
y=0,
Expand Down
7 changes: 3 additions & 4 deletions src/bar.vala
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Cairo;

namespace LiveChart {
public class Bar : SerieRenderer {
public class Bar : LiveSerieRenderer {
public Bar(Values values = new Values()) {
base();
this.values = values;
base(values);
}

public override void draw(Context ctx, Config config) {
if (visible) {
var points = Points.create(values, config);
var points = points_factory.create(config);
if (points.size > 0) {
line.configure(ctx);

Expand Down
7 changes: 3 additions & 4 deletions src/line.vala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Cairo;

namespace LiveChart {
public class Line : SerieRenderer {
public class Line : LiveSerieRenderer {

public Line(Values values = new Values()) {
base();
this.values = values;
base(values);
}

public override void draw(Context ctx, Config config) {
if (visible) {
var points = Points.create(values, config);
var points = points_factory.create(config);
if (points.size > 0) {
this.draw_line(points, ctx, config);
ctx.stroke();
Expand Down
2 changes: 1 addition & 1 deletion src/line_area.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace LiveChart {

public override void draw(Context ctx, Config config) {
if (visible) {
var points = Points.create(values, config);
var points = points_factory.create(config);
if (points.size > 0) {
this.draw_line(points, ctx, config);
ctx.stroke_preserve();
Expand Down
8 changes: 4 additions & 4 deletions src/max_bound_line.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ using Cairo;

namespace LiveChart {

public class MaxBoundLine : SerieRenderer {
public class MaxBoundLine : LiveSerieRenderer {

public MaxBoundLine() {
base();
this.values = new Values();
base(new Values());
}

public MaxBoundLine.from_serie(Serie serie) {
this.values = serie.get_values();
values = serie.get_values();
base(values);
}

public override void draw(Context ctx, Config config) {
Expand Down
8 changes: 4 additions & 4 deletions src/min_bound_line.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ using Cairo;

namespace LiveChart {

public class MinBoundLine : SerieRenderer {
public class MinBoundLine : LiveSerieRenderer {

public MinBoundLine(Values values = new Values()) {
base();
this.values = values;
base(values);
}

public MinBoundLine.from_serie(Serie serie) {
this.values = serie.get_values();
values = serie.get_values();
base(values);
}

public override void draw(Context ctx, Config config) {
Expand Down
72 changes: 61 additions & 11 deletions src/points.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LiveChart {
public double height;
}

public class Points : Object {
public class Points<T> : Object {

private Gee.ArrayList<Point?> points = new Gee.ArrayList<Point?>();
public Bounds bounds {
Expand All @@ -30,10 +30,6 @@ namespace LiveChart {
}
}

public double realtime_delta {
get; set;
}

public new Point get(int at) {
return points.get(at);
}
Expand All @@ -50,30 +46,84 @@ namespace LiveChart {
public Point last() {
return this.get(this.size - 1);
}

}
public interface PointsFactory<T> : Object {
public abstract Points<T?> create(Config config);
}

public class TimeStampedPointsFactory : PointsFactory<TimestampedValue?>, Object {
private Values values;

public TimeStampedPointsFactory(Values values) {
this.values = values;
}

public static Points create(Values values, Config config) {
public Points<TimestampedValue?> create(Config config) {
var boundaries = config.boundaries();

Points points = new Points();
var points = new Points<TimestampedValue?>();
if (values.size > 0) {
var last_value = values.last();
points.realtime_delta = (((GLib.get_real_time() / 1000) - last_value.timestamp) * config.x_axis.get_ratio()) / 1000;
var realtime_delta = (((GLib.get_real_time() / 1000) - last_value.timestamp) * config.x_axis.get_ratio()) / 1000;

foreach (TimestampedValue value in values) {
var point = Points.value_to_point(last_value, value, config, boundaries, points.realtime_delta);
var point = value_to_point(last_value, value, config, boundaries, realtime_delta);
points.add(point);
}
}

return points;
return points as Points<TimestampedValue>;
}

private static Point value_to_point(TimestampedValue last_value, TimestampedValue current_value, Config config, Boundaries boundaries, double realtime_delta) {
public static Point value_to_point(TimestampedValue last_value, TimestampedValue current_value, Config config, Boundaries boundaries, double realtime_delta) {
return Point() {
x = (boundaries.x.max - (last_value.timestamp - current_value.timestamp) / 1000 * config.x_axis.get_ratio()) - realtime_delta,
y = boundaries.y.max - (current_value.value * config.y_axis.get_ratio()),
height = current_value.value * config.y_axis.get_ratio()
};
}
}

public class NamedPointsFactory : PointsFactory<NamedValue?>, Object {
private StaticValues values;

public NamedPointsFactory(StaticValues values) {
this.values = values;
}

public Points<NamedValue?> create(Config config) {
var boundaries = config.boundaries();

var points = new Points<NamedValue?>();
if (values.size > 0) {
foreach (NamedValue value in values) {
var point = value_to_point(value, config, boundaries);
points.add(point);
}
}

return points as Points<NamedValue>;
}

private static Point value_to_point(NamedValue current_value, Config config, Boundaries boundaries) {
var category_length = config.categories.size;
uint8 category_pos = 0;
for (uint8 i = 0; i < category_length;i++) {
var category = config.categories.get(i);
if (category != null && category == current_value.name) {
category_pos = i;
break;
}
}
var width_between_each_points = (boundaries.x.max - boundaries.x.min) / (category_length - 1);//TODO divide by 0
var x = (boundaries.x.min + (category_pos * width_between_each_points));
var y = boundaries.y.max - (current_value.value * config.y_axis.get_ratio());
return Point() {
x = x,
y =y,
height = current_value.value * config.y_axis.get_ratio()
};
}
}
}
4 changes: 2 additions & 2 deletions src/serie.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ namespace LiveChart {

public signal void value_added(double value);

private SerieRenderer renderer;
private LiveSerieRenderer renderer;

public Serie(string name, SerieRenderer renderer = new Line()) {
public Serie(string name, LiveSerieRenderer renderer = new Line()) {
this.name = name;
this.renderer = renderer;
}
Expand Down
33 changes: 28 additions & 5 deletions src/serie_renderer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ namespace LiveChart {
width=0,
height=0
};

protected Values values;
public Values get_values() {
return this.values;
}

public abstract void draw(Context ctx, Config config);
public BoundingBox get_bounding_box() {
Expand All @@ -53,4 +48,32 @@ namespace LiveChart {
return point.x < SerieRenderer.VIRTUAL_LEFT_PADDING;
}
}

public abstract class LiveSerieRenderer : SerieRenderer {

protected PointsFactory<TimestampedValue?> points_factory;
protected Values values;
public Values get_values() {
return this.values;
}

public new abstract void draw(Context ctx, Config config);
protected LiveSerieRenderer(Values values) {
this.values = values;
this.points_factory = new TimeStampedPointsFactory(values);
}
}

public abstract class StaticSerieRenderer : SerieRenderer {
protected PointsFactory<NamedValue?> points_factory;
protected StaticValues values;
public StaticValues get_values() {
return this.values;
}
public new abstract void draw(Context ctx, Config config);
protected StaticSerieRenderer(StaticValues values) {
this.values = values;
this.points_factory = new NamedPointsFactory(values);
}
}
}
8 changes: 3 additions & 5 deletions src/smooth_line.vala
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using Cairo;

namespace LiveChart {
public class SmoothLine : SerieRenderer {

public class SmoothLine : LiveSerieRenderer {
public SmoothLine(Values values = new Values()) {
base();
this.values = values;
base(values);
}

public override void draw(Context ctx, Config config) {
if (visible) {
var points = Points.create(values, config);
var points = points_factory.create(config);
if(points.size > 0) {
this.draw_smooth_line(points, ctx, config);
ctx.stroke();
Expand Down
2 changes: 1 addition & 1 deletion src/smooth_line_area.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace LiveChart {

public override void draw(Context ctx, Config config) {
if (visible) {
var points = Points.create(values, config);
var points = points_factory.create(config);
if(points.size > 0) {
this.draw_smooth_line(points, ctx, config);
ctx.stroke_preserve();
Expand Down
4 changes: 2 additions & 2 deletions src/threshold_line.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ using Cairo;

namespace LiveChart {

public class ThresholdLine : SerieRenderer {
public class ThresholdLine : LiveSerieRenderer {

public double value { get; set; default = 0;}

public ThresholdLine(double value) {
base();
base(new Values());
this.value = value;
}

Expand Down
21 changes: 21 additions & 0 deletions src/values.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@ namespace LiveChart {
base.add(value);
}
}

public struct NamedValue {
public string name;
public double value;
}

public class StaticValues : LinkedList<NamedValue?> {

public Bounds bounds {
get; construct set;
}

public StaticValues() {
this.bounds = new Bounds();
}

public new void add(NamedValue value) {
bounds.update(value.value);
base.add(value);
}
}
}
2 changes: 1 addition & 1 deletion tests/area.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private void register_area() {
Cairo.Context context = new Cairo.Context(surface);
cairo_background(context);

var points = LiveChart.Points.create(new LiveChart.Values(), create_config());
var points = (new LiveChart.TimeStampedPointsFactory(new LiveChart.Values())).create(create_config());

var area = new LiveChart.Area(points, Gdk.RGBA() {red = 1.0, green = 0.0, blue = 0.0, alpha = 1.0 }, 1.0);

Expand Down
4 changes: 2 additions & 2 deletions tests/points.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private void register_points() {
values.add({now, 100});

//when
var points = LiveChart.Points.create(values, config);
var points =(new LiveChart.TimeStampedPointsFactory(values)).create(config);

//then
assert(points.size == values.size);
Expand All @@ -39,6 +39,6 @@ private void register_points() {
var values = new LiveChart.Values();

//when //then
LiveChart.Points.create(values, config);
(new LiveChart.TimeStampedPointsFactory(values)).create(config);
});
}

0 comments on commit e08e13f

Please sign in to comment.