Skip to content

Commit cfb1242

Browse files
add support for popping-out graphs
1 parent 65ca7c0 commit cfb1242

File tree

5 files changed

+636
-61
lines changed

5 files changed

+636
-61
lines changed

src/components/Plotly.vue

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Plotly from 'plotly.js'
77
import { store } from './Globals.js'
88
import * as d3 from 'd3'
9+
import { faWindowRestore } from '@fortawesome/free-solid-svg-icons'
910
1011
const Color = require('color')
1112
@@ -217,64 +218,94 @@ export default {
217218
}
218219
},
219220
methods: {
221+
popupButton () {
222+
return {
223+
name: 'Open in new window',
224+
icon: {
225+
title: 'test',
226+
name: 'iconFS',
227+
width: 600,
228+
height: 600,
229+
path: faWindowRestore.icon[4]
230+
}, // Use any icon available
231+
click: (gd) => {
232+
// const plotData = JSON.parse(JSON.stringify(gd.data))
233+
// const plotLayout = JSON.parse(JSON.stringify(gd.layout))
234+
// const plotConfig = { showLink: false, displayModeBar: true }
235+
236+
// Open a new window
237+
const newWindow = window.open(
238+
'/#/plot', '_blank',
239+
'toolbar=no,scrollbars=yes,resizable=yes,top=500,left=500,width=800,height=400,allow-scripts'
240+
)
241+
setTimeout(() => {
242+
console.log(newWindow)
243+
console.log(newWindow.setPlotData)
244+
newWindow.setPlotData(gd.data)
245+
newWindow.setPlotOptions(gd.layout)
246+
newWindow.setCssColors(this.state.cssColors)
247+
newWindow.plot()
248+
}, 1000)
249+
console.log(newWindow)
250+
}
251+
}
252+
},
220253
csvButton () {
221-
return [
222-
{
223-
name: 'downloadCsv',
224-
title: 'Download data as csv',
225-
icon: Plotly.Icons.disk,
226-
click: () => {
227-
console.log(this.gd.data)
228-
const data = this.gd.data
229-
const header = ['timestamp(ms)']
230-
for (const series of data) {
231-
header.push(series.name.split(' |')[0])
232-
}
254+
return {
255+
name: 'downloadCsv',
256+
title: 'Download data as csv',
257+
icon: Plotly.Icons.disk,
258+
click: () => {
259+
console.log(this.gd.data)
260+
const data = this.gd.data
261+
const header = ['timestamp(ms)']
262+
for (const series of data) {
263+
header.push(series.name.split(' |')[0])
264+
}
233265
234-
const indexes = []
266+
const indexes = []
235267
236-
const interval = 100
237-
let lasttime = Infinity
238-
let finaltime = 0
268+
const interval = 100
269+
let lasttime = Infinity
270+
let finaltime = 0
239271
240-
for (const series in data) {
241-
indexes.push(0)
242-
const x = data[series].x
243-
lasttime = Math.min(lasttime, x[0])
244-
finaltime = Math.max(finaltime, x[x.length - 1])
245-
}
246-
finaltime = Math.min(finaltime, this.state.timeRange[1])
247-
lasttime = Math.max(lasttime, this.state.timeRange[0])
272+
for (const series in data) {
273+
indexes.push(0)
274+
const x = data[series].x
275+
lasttime = Math.min(lasttime, x[0])
276+
finaltime = Math.max(finaltime, x[x.length - 1])
277+
}
278+
finaltime = Math.min(finaltime, this.state.timeRange[1])
279+
lasttime = Math.max(lasttime, this.state.timeRange[0])
248280
249-
const csv = [header]
250-
while (lasttime < finaltime - interval) {
251-
const line = [lasttime]
252-
for (const series in data) {
253-
let index = indexes[series]
254-
let x = data[series].x[index]
255-
while (x < lasttime) {
256-
indexes[series] += 1
257-
index = indexes[series]
258-
x = data[series].x[index]
259-
}
260-
line.push(data[series].y[index])
281+
const csv = [header]
282+
while (lasttime < finaltime - interval) {
283+
const line = [lasttime]
284+
for (const series in data) {
285+
let index = indexes[series]
286+
let x = data[series].x[index]
287+
while (x < lasttime) {
288+
indexes[series] += 1
289+
index = indexes[series]
290+
x = data[series].x[index]
261291
}
262-
csv.push(line)
263-
lasttime = lasttime + interval
292+
line.push(data[series].y[index])
264293
}
265-
const csvContent = csv.map(e => e.join(',')).join('\n')
266-
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
267-
const link = document.createElement('a')
268-
const url = URL.createObjectURL(blob)
269-
link.setAttribute('href', url)
270-
link.setAttribute('download', 'data.csv')
271-
link.style.visibility = 'hidden'
272-
document.body.appendChild(link)
273-
link.click()
274-
document.body.removeChild(link)
294+
csv.push(line)
295+
lasttime = lasttime + interval
275296
}
297+
const csvContent = csv.map(e => e.join(',')).join('\n')
298+
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
299+
const link = document.createElement('a')
300+
const url = URL.createObjectURL(blob)
301+
link.setAttribute('href', url)
302+
link.setAttribute('download', 'data.csv')
303+
link.style.visibility = 'hidden'
304+
document.body.appendChild(link)
305+
link.click()
306+
document.body.removeChild(link)
276307
}
277-
]
308+
}
278309
},
279310
resize () {
280311
Plotly.Plots.resize(this.gd)
@@ -787,7 +818,7 @@ export default {
787818
plotData,
788819
plotOptions,
789820
{
790-
modeBarButtonsToAdd: this.csvButton(),
821+
modeBarButtonsToAdd: [this.csvButton(), this.popupButton()],
791822
scrollZoom: true,
792823
editable: true,
793824
responsive: true

0 commit comments

Comments
 (0)