Skip to content

Commit

Permalink
Merge pull request #4661 from hashicorp/f-ui-line-chart
Browse files Browse the repository at this point in the history
UI: line chart
  • Loading branch information
DingoEatingFuzz authored Sep 13, 2018
2 parents 1002eeb + efb1301 commit 5e2edf8
Show file tree
Hide file tree
Showing 17 changed files with 999 additions and 25 deletions.
62 changes: 62 additions & 0 deletions ui/app/components/freestyle/sg-line-chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import d3TimeFormat from 'd3-time-format';

export default Component.extend({
timerTicks: 0,

startTimer: function() {
this.set(
'timer',
setInterval(() => {
this.incrementProperty('timerTicks');

const ref = this.get('lineChartLive');
ref.addObject({ ts: Date.now(), val: Math.random() * 30 + 20 });
if (ref.length > 60) {
ref.splice(0, ref.length - 60);
}
}, 500)
);
}.on('init'),

willDestroy() {
clearInterval(this.get('timer'));
},

lineChartData: computed(() => {
return [
{ year: 2010, value: 10 },
{ year: 2011, value: 10 },
{ year: 2012, value: 20 },
{ year: 2013, value: 30 },
{ year: 2014, value: 50 },
{ year: 2015, value: 80 },
{ year: 2016, value: 130 },
{ year: 2017, value: 210 },
{ year: 2018, value: 340 },
];
}),

lineChartMild: computed(() => {
return [
{ year: 2010, value: 100 },
{ year: 2011, value: 90 },
{ year: 2012, value: 120 },
{ year: 2013, value: 130 },
{ year: 2014, value: 115 },
{ year: 2015, value: 105 },
{ year: 2016, value: 90 },
{ year: 2017, value: 85 },
{ year: 2018, value: 90 },
];
}),

lineChartLive: computed(() => {
return [];
}),

secondsFormat() {
return d3TimeFormat.timeFormat('%H:%M:%S');
},
});
76 changes: 76 additions & 0 deletions ui/app/components/freestyle/sg-stats-time-series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import d3TimeFormat from 'd3-time-format';
import moment from 'moment';

export default Component.extend({
timerTicks: 0,

startTimer: function() {
this.set(
'timer',
setInterval(() => {
const metricsHigh = this.get('metricsHigh');
const prev = metricsHigh.length ? metricsHigh[metricsHigh.length - 1].value : 0.9;
this.appendTSValue(
metricsHigh,
Math.min(Math.max(prev + Math.random() * 0.05 - 0.025, 0.5), 1)
);

const metricsLow = this.get('metricsLow');
const prev2 = metricsLow.length ? metricsLow[metricsLow.length - 1].value : 0.1;
this.appendTSValue(
metricsLow,
Math.min(Math.max(prev2 + Math.random() * 0.05 - 0.025, 0), 0.5)
);
}, 1000)
);
}.on('init'),

appendTSValue(array, value, maxLength = 300) {
array.addObject({
timestamp: Date.now(),
value,
});

if (array.length > maxLength) {
array.splice(0, array.length - maxLength);
}
},

willDestroy() {
clearInterval(this.get('timer'));
},

metricsHigh: computed(() => {
return [];
}),

metricsLow: computed(() => {
return [];
}),

staticMetrics: computed(() => {
const ts = offset =>
moment()
.subtract(offset, 'm')
.toDate();
return [
{ timestamp: ts(20), value: 0.5 },
{ timestamp: ts(18), value: 0.5 },
{ timestamp: ts(16), value: 0.4 },
{ timestamp: ts(14), value: 0.3 },
{ timestamp: ts(12), value: 0.9 },
{ timestamp: ts(10), value: 0.3 },
{ timestamp: ts(8), value: 0.3 },
{ timestamp: ts(6), value: 0.4 },
{ timestamp: ts(4), value: 0.5 },
{ timestamp: ts(2), value: 0.6 },
{ timestamp: ts(0), value: 0.6 },
];
}),

secondsFormat() {
return d3TimeFormat.timeFormat('%H:%M:%S');
},
});
Loading

0 comments on commit 5e2edf8

Please sign in to comment.