Skip to content

Commit

Permalink
add_unaware_timestamp_collection_by_index method
Browse files Browse the repository at this point in the history
  • Loading branch information
lcallarec committed Aug 27, 2020
1 parent fee8c11 commit 8946f97
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ LiveChart uses custom [Value](https://lcallarec.github.io/live-chart/Livechart/L
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()` :
Use `Chart.add_unaware_timestamp_collection()` or `Chart.add_unaware_timestamp_collection_by_index()`:

```vala
// Your own dataset
Expand All @@ -604,7 +604,10 @@ 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);
//or
chart.add_unaware_timestamp_collection_by_index(0, unaware_timestamp_collection, timespan_between_value);
```

Et voilà !
Expand Down
5 changes: 5 additions & 0 deletions src/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace LiveChart {
});
}

public void add_unaware_timestamp_collection_by_index(int serie_index, Gee.Collection<double?> collection, int timespan_between_value) {
var serie = series.get(serie_index);
add_unaware_timestamp_collection(serie, collection, timespan_between_value);
}

public void to_png(string filename) throws Error {
var window = this.get_window();
if (window == null) {
Expand Down
32 changes: 32 additions & 0 deletions tests/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ private void register_chart() {
assert(serie.get_values().get(0).value == 100);
});


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

chart.add_serie(serie);

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_by_index(0, 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);
});

Test.add_func("/LiveChart/Chart/#ShouldNotCrashWhenRevealingAChartWithoutAnyValueAdded", () => {
//given
var chart = new LiveChart.Chart();
Expand Down

0 comments on commit 8946f97

Please sign in to comment.