infoscreens/elements/polar.js

114 lines
2.8 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:45:25 +01:00
import { colors } from '../lib/colors';
import injectCss from '../lib/plotly-shadowdom';
2017-11-28 21:57:05 +01:00
const Component = withComponent();
class Polar 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,
percentage: props.boolean,
2017-11-30 16:09:18 +01:00
data: props.array,
2017-11-30 21:45:25 +01:00
size: props.number,
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
}
renderer(renderRoot, render) {
const root = renderRoot;
2017-11-30 21:45:25 +01:00
while (root.firstChild) {
root.removeChild(root.firstChild);
}
2017-11-28 21:57:05 +01:00
root.appendChild(render());
2017-11-30 21:45:25 +01:00
injectCss(root);
2017-11-28 21:57:05 +01:00
}
2017-11-30 21:45:25 +01:00
render({ percentage, data, ts, size }) {
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:45:25 +01:00
const vmin = Math.min(window.innerWidth, window.innerHeight);
const graphSize = Math.floor((vmin / 100) * (size || 40));
2017-11-28 21:57:05 +01:00
const layout = {
orientation: 270,
direction: 'clockwise',
angularaxis: {
type: 'category',
tickcolor: '#204a87',
},
radialaxis: {
ticksuffix: percentage ? '%' : '',
tickcolor: '#204a87',
},
font: {
family: ['Source Code Pro', 'sans-serif'],
size: 16,
color: '#fff',
outlineColor: 'transparent',
},
showlegend: false,
paper_bgcolor: 'transparent',
2017-11-30 21:45:25 +01:00
autosize: false,
width: graphSize,
height: graphSize,
2017-11-28 21:57:05 +01:00
};
2017-11-30 16:09:18 +01:00
let graphData = [];
2017-11-28 21:57:05 +01:00
const dayLabels = ts.getDayLabels();
ts.prepareSlots().forEach((day, dayIdx) => {
2017-11-30 16:09:18 +01:00
graphData.push({
2017-11-28 21:57:05 +01:00
r: day,
t: ts.getSlotLabels(),
name: dayLabels[dayIdx],
type: 'area',
marker: {
2017-11-30 21:45:25 +01:00
color: colors[2],
2017-11-28 21:57:05 +01:00
},
opacity: 0.8,
showlegend: false,
});
});
2017-11-30 16:09:18 +01:00
if (percentage) {
graphData = graphData.slice(0);
const result = ts.getSlotLabels().map(() => 0);
data.forEach((day) => {
day.forEach((val, idx) => {
result[idx] += val;
2017-11-28 21:57:05 +01:00
});
});
2017-11-30 16:09:18 +01:00
graphData[0].r = result.map(r => (r / ts.days) * 100);
} else {
data.forEach((day, dayIdx) => {
day.forEach((val, idx) => {
graphData[dayIdx].r[idx] = val;
});
});
}
Plotly.newPlot(el, graphData, layout, {
staticPlot: true,
});
2017-11-28 21:57:05 +01:00
return el;
}
}
customElements.define('cbase-polar', Polar);