infoscreens/elements/heatmap.js

104 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-11-28 21:57:05 +01:00
import { withComponent, props } from 'skatejs';
import { Timeseries } from '../lib/timeseries';
2017-11-30 21:50:33 +01:00
import injectCss from '../lib/plotly-shadowdom';
2017-11-30 23:01:46 +01:00
import { colors } from '../lib/colors';
2017-11-28 21:57:05 +01:00
const Component = withComponent();
class Heatmap extends Component {
static props = {
timeseries: props.string,
2017-11-28 22:46:49 +01:00
days: props.number,
2017-11-28 21:57:05 +01:00
interpolate: props.boolean,
accumulate: props.boolean,
2017-11-30 16:09:18 +01:00
data: props.array,
2017-11-30 21:50:33 +01:00
width: props.number,
height: props.number,
2017-11-28 21:57:05 +01:00
};
2018-12-09 22:07:02 +01:00
2017-11-28 21:57:05 +01:00
connected() {
2017-11-30 16:09:18 +01:00
if (!this.timeseries) {
return;
}
const daySlots = this.days || 7;
this.ts = new Timeseries(this.timeseries, new Date(), daySlots);
this.ts.getData({
interpolate: this.interpolate,
accumulate: this.accumulate,
usePreviousValue: false,
})
.then((values) => {
this.data = values;
});
2017-11-28 21:57:05 +01:00
}
2018-12-09 22:07:02 +01:00
2017-11-28 21:57:05 +01:00
renderer(renderRoot, render) {
const root = renderRoot;
2017-11-30 21:50:33 +01:00
while (root.firstChild) {
root.removeChild(root.firstChild);
}
2017-11-28 21:57:05 +01:00
root.appendChild(render());
2017-11-30 21:50:33 +01:00
injectCss(root);
2017-11-28 21:57:05 +01:00
}
2017-11-30 21:50:33 +01:00
render({ data, ts, width, height }) {
2017-11-28 21:57:05 +01:00
const el = document.createElement('div');
2017-11-30 16:09:18 +01:00
if (!data || !data.length || !ts) {
// No data yet
2017-11-28 21:57:05 +01:00
return el;
}
2017-11-30 21:50:33 +01:00
const graphWidth = Math.floor((window.innerWidth / 100) * (width || 40));
const graphHeight = Math.floor((window.innerHeight / 100) * (height || 50));
2017-11-30 16:09:18 +01:00
const graphData = [{
2017-11-28 21:57:05 +01:00
x: ts.getSlotLabels(),
y: ts.getDayLabels(),
2017-11-30 16:09:18 +01:00
z: data,
2017-11-28 21:57:05 +01:00
type: 'heatmap',
colorscale: [
['0.0', 'rgb(0, 0, 0)'],
2017-11-30 23:01:46 +01:00
['1.0', colors[2]],
2017-11-28 21:57:05 +01:00
],
showlegend: false,
showscale: false,
}];
const layout = {
2017-11-30 21:50:33 +01:00
autosize: false,
width: graphWidth,
height: graphHeight,
2017-11-28 21:57:05 +01:00
yaxis: {
autorange: 'reversed',
tickfont: {
family: 'Source Code Pro',
},
2017-11-30 23:01:46 +01:00
tickcolor: '#204a87',
gridcolor: '#204a87',
2017-11-28 21:57:05 +01:00
},
xaxis: {
type: 'category',
tickfont: {
family: 'Source Code Pro',
},
2017-11-30 23:01:46 +01:00
tickcolor: '#204a87',
gridcolor: '#204a87',
},
font: {
family: ['Source Code Pro', 'sans-serif'],
size: 16,
color: '#fff',
outlineColor: 'transparent',
2017-11-28 21:57:05 +01:00
},
paper_bgcolor: 'transparent',
2017-11-30 15:45:16 +01:00
margin: {
r: 0,
t: 0,
},
2017-11-28 21:57:05 +01:00
};
2017-11-30 16:09:18 +01:00
Plotly.newPlot(el, graphData, layout, {
staticPlot: true,
});
2017-11-28 21:57:05 +01:00
return el;
}
}
customElements.define('cbase-heatmap', Heatmap);