Fix chart update on (auto)refresh

This commit is contained in:
Bartek Fabiszewski 2020-04-14 19:54:28 +02:00
parent 6ef2453709
commit 42bdf5aab0
2 changed files with 49 additions and 12 deletions

View File

@ -21,6 +21,7 @@ import { lang as $ } from './initializer.js';
import Chartist from 'chartist' import Chartist from 'chartist'
import ViewModel from './viewmodel.js'; import ViewModel from './viewmodel.js';
import ctAxisTitle from 'chartist-plugin-axistitle'; import ctAxisTitle from 'chartist-plugin-axistitle';
import uObserve from './observe.js';
import uUtils from './utils.js'; import uUtils from './utils.js';
/** /**
@ -116,8 +117,12 @@ export default class ChartViewModel extends ViewModel {
setObservers() { setObservers() {
this.state.onChanged('currentTrack', (track) => { this.state.onChanged('currentTrack', (track) => {
this.render(); if (track) {
this.model.buttonVisible = !!track && track.hasPlotData; uObserve.observe(track, 'positions', () => {
this.onTrackUpdate(track, true);
});
}
this.onTrackUpdate(track);
}); });
this.onChanged('buttonVisible', (visible) => this.renderButton(visible)); this.onChanged('buttonVisible', (visible) => this.renderButton(visible));
this.onChanged('chartVisible', (visible) => this.renderContainer(visible)); this.onChanged('chartVisible', (visible) => this.renderContainer(visible));
@ -129,13 +134,22 @@ export default class ChartViewModel extends ViewModel {
}; };
} }
/**
* @param {?uTrack} track
* @param {boolean=} update
*/
onTrackUpdate(track, update = false) {
this.render(track, update);
this.model.buttonVisible = !!track && track.hasPlotData;
}
/** /**
* @param {boolean} isVisible * @param {boolean} isVisible
*/ */
renderContainer(isVisible) { renderContainer(isVisible) {
if (isVisible) { if (isVisible) {
this.chartContainer.style.display = 'block'; this.chartContainer.style.display = 'block';
this.render(); this.render(this.state.currentTrack);
} else { } else {
this.chartContainer.style.display = 'none'; this.chartContainer.style.display = 'none';
} }
@ -152,15 +166,19 @@ export default class ChartViewModel extends ViewModel {
} }
} }
render() { /**
* @param {?uTrack} track
* @param {boolean=} update
*/
render(track, update = false) {
let data = []; let data = [];
if (this.state.currentTrack && this.state.currentTrack.hasPlotData && this.model.chartVisible) { if (track && track.hasPlotData && this.model.chartVisible) {
data = this.state.currentTrack.plotData; data = track.plotData;
} else { } else {
this.model.chartVisible = false; this.model.chartVisible = false;
} }
if (this.data !== data) { if (update || this.data !== data) {
console.log(`Chart update (${data.length})`); console.log(`Chart${update ? ' forced' : ''} update (${data.length})`);
this.data = data; this.data = data;
const options = { const options = {
lineSmooth: (data.length <= LARGE_DATA) lineSmooth: (data.length <= LARGE_DATA)

View File

@ -256,7 +256,7 @@ describe('ChartViewModel tests', () => {
vm.data = null; vm.data = null;
vm.chartSetup(); vm.chartSetup();
// when // when
vm.render(); vm.render(state.currentTrack);
// then // then
expect(mockChart.update).toHaveBeenCalledTimes(1); expect(mockChart.update).toHaveBeenCalledTimes(1);
expect(mockChart.update.calls.mostRecent().args[0].series[0]).toBe(track.plotData); expect(mockChart.update.calls.mostRecent().args[0].series[0]).toBe(track.plotData);
@ -275,12 +275,31 @@ describe('ChartViewModel tests', () => {
vm.data = track.plotData; vm.data = track.plotData;
vm.chartSetup(); vm.chartSetup();
// when // when
vm.render(); vm.render(state.currentTrack);
// then // then
expect(mockChart.update).not.toHaveBeenCalled(); expect(mockChart.update).not.toHaveBeenCalled();
expect(vm.data).toBe(track.plotData); expect(vm.data).toBe(track.plotData);
}); });
it('should render chart on same track and chart visible with update requested', () => {
// given
const positions = [
TrackFactory.getPosition({ id: 1, latitude: 2, longitude: 3, altitude: 4 }),
TrackFactory.getPosition({ id: 2, latitude: 3, longitude: 4, altitude: 5 })
];
const track = TrackFactory.getTrack(positions);
state.currentTrack = track;
vm.model.chartVisible = true;
vm.data = track.plotData;
vm.chartSetup();
// when
vm.render(state.currentTrack, true);
// then
expect(mockChart.update).toHaveBeenCalledTimes(1);
expect(mockChart.update.calls.mostRecent().args[0].series[0]).toBe(track.plotData);
expect(vm.data).toBe(track.plotData);
});
it('should render empty chart on empty track and hide chart container', () => { it('should render empty chart on empty track and hide chart container', () => {
// given // given
const track = TrackFactory.getTrack(0); const track = TrackFactory.getTrack(0);
@ -289,7 +308,7 @@ describe('ChartViewModel tests', () => {
vm.data = chartData; vm.data = chartData;
vm.chartSetup(); vm.chartSetup();
// when // when
vm.render(); vm.render(state.currentTrack);
// then // then
expect(mockChart.update).toHaveBeenCalledTimes(1); expect(mockChart.update).toHaveBeenCalledTimes(1);
expect(mockChart.update.calls.mostRecent().args[0].series[0]).toEqual(track.plotData); expect(mockChart.update.calls.mostRecent().args[0].series[0]).toEqual(track.plotData);
@ -304,7 +323,7 @@ describe('ChartViewModel tests', () => {
vm.data = chartData; vm.data = chartData;
vm.chartSetup(); vm.chartSetup();
// when // when
vm.render(); vm.render(state.currentTrack);
// then // then
expect(mockChart.update).toHaveBeenCalledTimes(1); expect(mockChart.update).toHaveBeenCalledTimes(1);
expect(mockChart.update.calls.mostRecent().args[0].series[0]).toEqual([]); expect(mockChart.update.calls.mostRecent().args[0].series[0]).toEqual([]);