mt-polygon-simplification/benchmarking/src/loadChart.js

44 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-08-06 14:03:05 +02:00
import Chart from 'chart.js'
2019-08-07 15:34:24 +02:00
import 'file-drop-element'
import { saveAs } from 'file-saver'
2019-08-06 14:03:05 +02:00
const fileDrop = document.getElementById('file-drop')
2019-08-07 15:34:24 +02:00
const dlButton = document.getElementById('download')
2019-08-06 14:03:05 +02:00
const canvas = document.getElementById('myChart')
const ctx = canvas.getContext('2d')
2019-08-07 15:34:24 +02:00
let filename
let chart = new Chart(ctx)
const trimExtension = filename =>
filename
.split('.')
.slice(0, -1)
.join('.')
2019-08-06 14:03:05 +02:00
async function readFile(file) {
return new Promise(resolve => {
let reader = new FileReader()
reader.readAsText(file)
reader.onload = function() {
resolve(this.result)
}
})
}
2019-08-07 15:34:24 +02:00
function displayDownLoadButton() {
dlButton.style.display = 'block'
}
2019-08-06 14:03:05 +02:00
fileDrop.addEventListener('filedrop', e => {
2019-08-07 15:34:24 +02:00
filename = e.files[0].name
if (chart) chart.destroy()
2019-08-06 14:03:05 +02:00
readFile(e.files[0])
.then(JSON.parse)
2019-08-07 15:34:24 +02:00
.then(config => (chart = new Chart(ctx, config)))
.then(displayDownLoadButton)
})
dlButton.addEventListener('click', () => {
canvas.toBlob(blob => saveAs(blob, trimExtension(filename) + '.png'))
2019-08-06 14:03:05 +02:00
})