2017-11-28 21:57:05 +01:00
|
|
|
import { withComponent, props } from 'skatejs';
|
|
|
|
import { Timeseries } from '../lib/timeseries';
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
connected() {
|
|
|
|
this.enableFetch = true;
|
|
|
|
}
|
|
|
|
renderer(renderRoot, render) {
|
|
|
|
const root = renderRoot;
|
|
|
|
root.innerHtml = '';
|
|
|
|
root.appendChild(render());
|
|
|
|
}
|
|
|
|
|
|
|
|
render({ timeseries, interpolate, accumulate, days }) {
|
2017-11-28 22:46:49 +01:00
|
|
|
let daySlots = days;
|
|
|
|
if (!daySlots) {
|
|
|
|
daySlots = 7;
|
|
|
|
}
|
2017-11-28 21:57:05 +01:00
|
|
|
const el = document.createElement('div');
|
|
|
|
if (!this.enableFetch) {
|
|
|
|
// Not yet connected
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
if (this.ts) {
|
|
|
|
// We're re-rendering, cancel previous
|
|
|
|
this.ts.canceled = true;
|
|
|
|
}
|
2017-11-28 22:46:49 +01:00
|
|
|
const ts = new Timeseries(timeseries, new Date(), daySlots);
|
2017-11-28 21:57:05 +01:00
|
|
|
const data = [{
|
|
|
|
x: ts.getSlotLabels(),
|
|
|
|
y: ts.getDayLabels(),
|
|
|
|
z: ts.prepareSlots(),
|
|
|
|
type: 'heatmap',
|
|
|
|
colorscale: [
|
|
|
|
['0.0', 'rgb(0, 0, 0)'],
|
|
|
|
['0.9', 'rgb(255, 0, 0)'],
|
|
|
|
['1.0', 'rgb(128, 0, 0)'],
|
|
|
|
],
|
|
|
|
showlegend: false,
|
|
|
|
showscale: false,
|
|
|
|
}];
|
|
|
|
const layout = {
|
|
|
|
yaxis: {
|
|
|
|
autorange: 'reversed',
|
|
|
|
tickfont: {
|
|
|
|
family: 'Source Code Pro',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
xaxis: {
|
|
|
|
type: 'category',
|
|
|
|
tickfont: {
|
|
|
|
family: 'Source Code Pro',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
paper_bgcolor: 'transparent',
|
2017-11-30 15:45:16 +01:00
|
|
|
margin: {
|
|
|
|
r: 0,
|
|
|
|
t: 0,
|
|
|
|
b: 0,
|
|
|
|
},
|
2017-11-28 21:57:05 +01:00
|
|
|
};
|
|
|
|
this.ts = ts;
|
|
|
|
ts.getData({
|
|
|
|
interpolate,
|
|
|
|
accumulate,
|
|
|
|
})
|
|
|
|
.then((values) => {
|
|
|
|
if (ts.canceled) {
|
|
|
|
el.innerHTML = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data[0].z = values;
|
|
|
|
Plotly.newPlot(el, data, layout, {
|
|
|
|
staticPlot: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('cbase-heatmap', Heatmap);
|