|
| 1 | +import Controller from '@ember/controller'; |
| 2 | +import { later } from '@ember/runloop'; |
| 3 | +import { action, computed } from '@ember-decorators/object'; |
| 4 | + |
| 5 | +import getNormalDistPDF from 'dummy/utils/get-normal-dist-pdf'; |
| 6 | + |
| 7 | +import * as debug from 'debug'; |
| 8 | +const log = debug('ember-cli-plotly:dummy:live-color'); |
| 9 | + |
| 10 | +//const interval = 0; // (go as fast as the plot can update) |
| 11 | +const interval = 200; |
| 12 | + |
| 13 | +const activeColor = '#01a0e1'; |
| 14 | +const passiveColor = '#959595'; |
| 15 | +const highlightColor = '#d474ff'; |
| 16 | + |
| 17 | +// Data to plot |
| 18 | +const n = 101; |
| 19 | +const x = new Array(n).fill(0).map((z,i) => 5*(2*i/(n-1) - 1)); // [-5, ..., 5] |
| 20 | +const sourceData = [{ |
| 21 | + name: "Group 1 (0,1)", |
| 22 | + mu: 0, |
| 23 | + sigma: 1, |
| 24 | + traces: 4, |
| 25 | + noiseFunction: y => y + 0.2*(2*(Math.random() ** 10) - 1), |
| 26 | + scaleFactor: 50 |
| 27 | +}, { |
| 28 | + name: "Group 2 (2,2)", |
| 29 | + mu: 2, |
| 30 | + sigma: 2, |
| 31 | + traces: 10, |
| 32 | + noiseFunction: (y, i) => y + 0.1*Math.sin(2*Math.PI*50*Math.random()*i/n), |
| 33 | + scaleFactor: 25 |
| 34 | +}].map(({ name, mu, sigma, traces, noiseFunction, scaleFactor }) => { |
| 35 | + const array = []; |
| 36 | + for (let i=0; i < traces; i++) { |
| 37 | + array.push({ |
| 38 | + name: `${(i+1)} (${name})`, |
| 39 | + x, |
| 40 | + y: x.map(getNormalDistPDF(mu, sigma)) |
| 41 | + .map(y => scaleFactor*y) |
| 42 | + .map(noiseFunction), |
| 43 | + mode: 'lines' |
| 44 | + }); |
| 45 | + } |
| 46 | + return array; |
| 47 | +}).reduce((array, set) => { |
| 48 | + set.forEach(s => array.push(s)); |
| 49 | + return array; |
| 50 | +}, []); |
| 51 | + |
| 52 | +export default class ExamplesLiveColorsController extends Controller { |
| 53 | + _isHighlighted = sourceData.map(() => false); |
| 54 | + plotlyEvents = ['plotly_legenddoubleclick']; |
| 55 | + currentTrace = 1; // Start on second trace |
| 56 | + currentIndex = 0; |
| 57 | + _updating = false; // Don't start the timer until user clicks button |
| 58 | + _revision = 0; |
| 59 | + |
| 60 | + _triggerUpdate() { |
| 61 | + log(`_triggerUpdate incrementing _revision (${this._revision})`); |
| 62 | + //this.set('chartData.triggerUpdate', (this.get('chartData.triggerUpdate') || 0) + 1); |
| 63 | + this.incrementProperty('_revision'); |
| 64 | + } |
| 65 | + |
| 66 | + update() { |
| 67 | + log('update firing'); |
| 68 | + if (!this.get('_updating')) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const currentTrace = this.get('currentTrace'); |
| 73 | + const currentIndex = this.get('currentIndex'); |
| 74 | + log(`Update called: currentTrace=${currentTrace}, currentIndex=${currentIndex}`, this.get(`chartData`)); |
| 75 | + |
| 76 | + // Prepare to do next point |
| 77 | + if (currentIndex >= sourceData[currentTrace].x.length) { |
| 78 | + // Stop when we've plotted all the data |
| 79 | + if (currentTrace >= sourceData.length) { |
| 80 | + log(`currentIndex (${currentIndex}) >= sourceData[currentTrace].x.length (${currentIndex >= sourceData[currentTrace].x.length})`); |
| 81 | + this.set('_updating', false); |
| 82 | + } |
| 83 | + else { |
| 84 | + this.set('currentTrace', currentTrace + 1); |
| 85 | + this.set('currentIndex', 0); |
| 86 | + } |
| 87 | + } |
| 88 | + else { |
| 89 | + this.set('currentIndex', 1 + currentIndex); |
| 90 | + } |
| 91 | + |
| 92 | + this._triggerUpdate(); |
| 93 | + if (this.get('_updating')) { |
| 94 | + later(this, 'update', interval); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + _toggleHighlighting(curveNumber) { |
| 99 | + log(`_toggleHighlighting(${curveNumber}) changing ${this._isHighlighted[curveNumber]} -> ${!this._isHighlighted[curveNumber]}`); |
| 100 | + this._isHighlighted[curveNumber] = !this._isHighlighted[curveNumber]; |
| 101 | + this._triggerUpdate(); |
| 102 | + } |
| 103 | + |
| 104 | + @computed('currentTrace', 'currentIndex', '_isHighlighted.[]', '_revision') |
| 105 | + get chartData() { |
| 106 | + const currentTrace = (() => { |
| 107 | + // FIXME: Shouldn't need to sanity check this |
| 108 | + let ct = this.get('currentTrace'); |
| 109 | + if (ct >= sourceData.length) { |
| 110 | + ct = sourceData.length - 1; |
| 111 | + } |
| 112 | + return ct; |
| 113 | + })(); |
| 114 | + const currentIndex = this.get('currentIndex'); |
| 115 | + log(`Computing chartData (currentTrace=${currentTrace}, currentIndex=${currentIndex})`); |
| 116 | + // We're going to copy sourceData (don't modify it!) into our own var here where we can set colors, slice to animate, etc. |
| 117 | + // For improved performance we could maintain this state instead of rebuilding it every time |
| 118 | + const chartData = JSON.parse(JSON.stringify(sourceData)).slice(0, currentTrace + 1); // FIXME: sloppy |
| 119 | + chartData[currentTrace].x = chartData[currentTrace].x.slice(0, currentIndex + 1); |
| 120 | + chartData[currentTrace].y = chartData[currentTrace].y.slice(0, currentIndex + 1); |
| 121 | + |
| 122 | + // Apply styling |
| 123 | + chartData.forEach((trace, i, array) => { |
| 124 | + trace.line = trace.line || {}; |
| 125 | + |
| 126 | + // Active/passive coloring |
| 127 | + if (i === array.length - 1) { |
| 128 | + // Last trace (= active trace) |
| 129 | + trace.line.color = activeColor; |
| 130 | + } |
| 131 | + else { |
| 132 | + trace.line.color = passiveColor; |
| 133 | + } |
| 134 | + |
| 135 | + // Highlight selected traces |
| 136 | + if (this._isHighlighted[i]) { |
| 137 | + trace.line.color = highlightColor; |
| 138 | + //trace.line.width = 3; |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + // TODO: See if there's a cleaner way to update than by manually re-triggering when chartData is computed |
| 143 | + chartData.triggerUpdate = this._revision; |
| 144 | + |
| 145 | + return chartData; |
| 146 | + } |
| 147 | + |
| 148 | + @action |
| 149 | + clear() { |
| 150 | + log(`Clear clicked`); |
| 151 | + this.setProperties({ |
| 152 | + currentTrace: 0, |
| 153 | + currentIndex: 0 |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + @action |
| 158 | + start() { |
| 159 | + log(`Start clicked`, this.get('_updating')); |
| 160 | + if (this.get('_updating') === false) { |
| 161 | + this.set('_updating', true); |
| 162 | + later(this, 'update', interval); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @action |
| 167 | + stop() { |
| 168 | + log(`Stop clicked`); |
| 169 | + this.set('_updating', false); |
| 170 | + } |
| 171 | + |
| 172 | + @action |
| 173 | + onPlotlyEvent(eventName, eventData) { |
| 174 | + log(`onPlotlyEvent got ${eventName} -->`, eventData); |
| 175 | + if (typeof this.plotlyEventHandlers[eventName] === 'function') { |
| 176 | + return this.plotlyEventHandlers[eventName].call(this, eventData); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + plotlyEventHandlers = { |
| 181 | + plotly_legenddoubleclick(eventData) { |
| 182 | + this._toggleHighlighting(eventData.curveNumber); |
| 183 | + return false; // prevent default behavior (hiding all other traces) |
| 184 | + } |
| 185 | + }; |
| 186 | +} |
0 commit comments