Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class canvasChart {
- constructor(canvas, data, colors=[], step) {
- this.canvas = canvas
- this.data = data
- this.colors = colors
- this.step = step
- this.ctx = this.canvas.getContext('2d')
- }
- init() {
- let maxH = Math.max.apply(null, this.data)
- this.canvas.width = this.data.length * this.step
- this.canvas.height = maxH
- this.data.forEach((element, index) => {
- this.ctx.fillStyle = this.colors[index]
- this.ctx.fillRect(index * this.step, maxH - element, this.step, element)
- })
- }
- }
- let canvas = document.getElementById('canvas')
- let cData = JSON.parse(canvas.dataset.rates)
- let cStep = JSON.parse(canvas.dataset.stepWidth)
- let cColors = JSON.parse(canvas.dataset.colors)
- let cChart = new canvasChart(canvas, cData, cColors, cStep)
- cChart.init()
Advertisement
Add Comment
Please, Sign In to add comment