Skip to content

Commit

Permalink
Deal with your own data
Browse files Browse the repository at this point in the history
  • Loading branch information
lcallarec committed Apr 11, 2020
1 parent 7585379 commit 70370e7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [Background](#background)
- [Hidding chart parts](#hidding-chart-parts)
- [Programmatic export](#programmatic-export)
- [Deal with your own data](#deal-with-your-own-data)
- [How Livechart versions works ?](#how-livechart-versions-works)

## Getting started
Expand Down Expand Up @@ -524,7 +525,35 @@ var filename = "chart_export.png";
chart.to_png(filename);
```

## How Livechart versions works ?
## Deal with your own data

LiveChart uses custom [Value](https://lcallarec.github.io/live-chart/Livechart/LiveChart.TimestampedValue.html) enum to store recorded values.
Basically, it stores the value, as a double, and a timestamp.
If you use to store in a `Gee.Collection<double?>`, without any timestamp information - most of the time because you know the interval between each points - and need to import them in a LiveChart, don't panic, there's a solution.

Use `Chart.add_unaware_timestamp_collection()` :

```vala
// Your own dataset
var unaware_timestamp_collection = new Gee.ArrayList<double?>();
unaware_timestamp_collection.add(5);
unaware_timestamp_collection.add(10);
unaware_timestamp_collection.add(15);
var chart = new LiveChart.Chart();
var serie = new LiveChart.Serie("CPU usage");
//Potentially, you may want to clean up the existing data
serie.clear();
//You know that, in your own model, there's 2000ms between each of your points
var timespan_between_value = 2000;
chart.add_unaware_timestamp_collection(serie, unaware_timestamp_collection, timespan_between_value);
```

Et voilà !

## How LiveChart versions works ?

* For each new feature, the `minor` version number will be bumped
* For each bug fix, small improvement or documentation update, the `patch` version number will be bumped
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('live-chart', ['vala', 'c'], version: '1.3.3')
project('live-chart', ['vala', 'c'], version: '1.3.4')

cc = meson.get_compiler('c')
libm = cc.find_library('m', required: true)
Expand Down
11 changes: 11 additions & 0 deletions src/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ namespace LiveChart {
config.y_axis.update_bounds(value);
}

public void add_unaware_timestamp_collection(Serie serie, Gee.Collection<double?> collection, int timespan_between_value) {
var ts = GLib.get_real_time() / 1000 - (collection.size * timespan_between_value);
var values = serie.get_values();
collection.foreach((value) => {
ts += timespan_between_value;
values.add({ts, value});
config.y_axis.update_bounds(value);
return true;
});
}

public void to_png(string filename) throws Error {
var window = this.get_window();
if (window == null) {
Expand Down
29 changes: 29 additions & 0 deletions tests/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,33 @@ private void register_chart() {
assert(e is LiveChart.ChartError.EXPORT_ERROR);
}
});

Test.add_func("/LiveChart/Chart/add_unaware_timestamp_collection", () => {
//given
var chart = new LiveChart.Chart();
var serie = new LiveChart.Serie("TEST");

var unaware_timestamp_collection = new Gee.ArrayList<double?>();
unaware_timestamp_collection.add(5);
unaware_timestamp_collection.add(10);
unaware_timestamp_collection.add(15);

var timespan_between_value = 5000;

//when
var now = GLib.get_real_time() / 1000;
chart.add_unaware_timestamp_collection(serie, unaware_timestamp_collection, timespan_between_value);

//then
assert(serie.get_values().size == 3);
assert(serie.get_values().get(0).value == 5);
assert(serie.get_values().get(1).value == 10);
assert(serie.get_values().get(2).value == 15);
assert(serie.get_values().get(2).timestamp == now);
assert(serie.get_values().get(1).timestamp == now - 5000);
assert(serie.get_values().get(0).timestamp == now - 10000);

assert(chart.config.y_axis.get_bounds().lower == 5);
assert(chart.config.y_axis.get_bounds().upper == 15);
});
}
2 changes: 1 addition & 1 deletion tests/serie.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ private void register_serie() {

//then
assert(serie.get_values().size == 0);
});
});
}

0 comments on commit 70370e7

Please sign in to comment.