2018-09-08 00:17:21 +00:00
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import moment from 'moment';
|
|
|
|
import d3TimeFormat from 'd3-time-format';
|
|
|
|
import d3Format from 'd3-format';
|
|
|
|
import d3Scale from 'd3-scale';
|
|
|
|
import d3Array from 'd3-array';
|
|
|
|
import LineChart from 'nomad-ui/components/line-chart';
|
2018-09-25 04:20:02 +00:00
|
|
|
import formatDuration from 'nomad-ui/utils/format-duration';
|
2018-09-08 00:17:21 +00:00
|
|
|
|
|
|
|
export default LineChart.extend({
|
|
|
|
xProp: 'timestamp',
|
2018-09-13 19:26:18 +00:00
|
|
|
yProp: 'percent',
|
2018-09-08 00:17:21 +00:00
|
|
|
timeseries: true,
|
|
|
|
|
|
|
|
xFormat() {
|
|
|
|
return d3TimeFormat.timeFormat('%H:%M:%S');
|
|
|
|
},
|
|
|
|
|
|
|
|
yFormat() {
|
|
|
|
return d3Format.format('.1~%');
|
|
|
|
},
|
|
|
|
|
2018-09-25 04:20:02 +00:00
|
|
|
// Specific a11y descriptors
|
|
|
|
title: 'Stats Time Series Chart',
|
|
|
|
|
|
|
|
description: computed('data.[]', 'xProp', 'yProp', function() {
|
|
|
|
const { xProp, yProp, data } = this.getProperties('data', 'xProp', 'yProp');
|
|
|
|
const yRange = d3Array.extent(data, d => d[yProp]);
|
|
|
|
const xRange = d3Array.extent(data, d => d[xProp]);
|
|
|
|
const yFormatter = this.yFormat();
|
|
|
|
|
|
|
|
const duration = formatDuration(xRange[1] - xRange[0], 'ms', true);
|
|
|
|
|
2018-10-17 14:37:45 +00:00
|
|
|
return `Time series data for the last ${duration}, with values ranging from ${yFormatter(yRange[0])} to ${yFormatter(yRange[1])}`;
|
2018-09-25 04:20:02 +00:00
|
|
|
}),
|
|
|
|
|
2018-09-08 00:17:21 +00:00
|
|
|
xScale: computed('data.[]', 'xProp', 'timeseries', 'yAxisOffset', function() {
|
|
|
|
const xProp = this.get('xProp');
|
|
|
|
const scale = this.get('timeseries') ? d3Scale.scaleTime() : d3Scale.scaleLinear();
|
2018-09-19 23:32:53 +00:00
|
|
|
const data = this.get('data');
|
2018-09-08 00:17:21 +00:00
|
|
|
|
2018-09-19 23:32:53 +00:00
|
|
|
const [low, high] = d3Array.extent(data, d => d[xProp]);
|
2018-09-08 00:17:21 +00:00
|
|
|
const minLow = moment(high)
|
|
|
|
.subtract(5, 'minutes')
|
|
|
|
.toDate();
|
2018-09-19 23:32:53 +00:00
|
|
|
|
|
|
|
const extent = data.length ? [Math.min(low, minLow), high] : [minLow, new Date()];
|
|
|
|
scale.rangeRound([10, this.get('yAxisOffset')]).domain(extent);
|
2018-09-08 00:17:21 +00:00
|
|
|
|
|
|
|
return scale;
|
|
|
|
}),
|
|
|
|
|
|
|
|
yScale: computed('data.[]', 'yProp', 'xAxisOffset', function() {
|
|
|
|
return d3Scale
|
|
|
|
.scaleLinear()
|
|
|
|
.rangeRound([this.get('xAxisOffset'), 10])
|
|
|
|
.domain([0, 1]);
|
|
|
|
}),
|
|
|
|
});
|