Skip to content

Commit

Permalink
update chart instead of creating a new one every time
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisMoschina committed Nov 14, 2024
1 parent 660226d commit f83b1c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
10 changes: 10 additions & 0 deletions open_earable/lib/sensor_data_tab/sensor_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ class _EarableDataChartState extends State<EarableDataChart> {
data: _data,
),
];
webSeriesList = [
ChartSeries(
id: '${widget.chartTitle}${_data.isNotEmpty ? " (${_units[widget.sensorName]})" : ""}',
label: '${widget.chartTitle}${_data.isNotEmpty ? " (${_units[widget.sensorName]})" : ""}',
getDomainFn: (SensorData data, _) => data.timestamp,
getMeasureFn: (SensorData data, _) => data.values[0],
getColorFn: (_, __) => colors[0],
data: _data,
),
];
}

print("Created series list for ${widget.sensorName}: $webSeriesList");
Expand Down
37 changes: 20 additions & 17 deletions open_earable/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,28 @@
}

if (canvas.chartInitialized === true) {
console.log('Updating existing chart');
// Update the existing chart
canvas.chart.destroy();
}

canvas.chart = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: datasets,
},
options: {
responsive: true,
animation: {
duration: 0,
canvas.chart.data.labels = labels;
canvas.chart.data.datasets = datasets;
canvas.chart.update();
} else {
console.log('Creating new chart');
canvas.chart = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: datasets,
},
},
});

canvas.chartInitialized = true;
options: {
responsive: true,
animation: {
duration: 0,
},
},
});
canvas.chartInitialized = true;
}
}
</script>
</head>
Expand Down

0 comments on commit f83b1c1

Please sign in to comment.