katerinka28

Untitled

Dec 11th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class svgChart {
  2.       constructor(element, data, colors=[], step) {
  3.         this.element = element
  4.         this.data = data
  5.         this.colors = colors
  6.         this.step = step
  7.       }
  8.       init() {
  9.         let maxH = Math.max.apply(null, this.data)
  10.         let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  11.         svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
  12.         svg.setAttribute('viewBox', `0 0 ${this.data.length * this.step} ${maxH}`)
  13.         svg.setAttribute('width', this.data.length * this.step)
  14.         svg.setAttribute('height', maxH)
  15.         this.data.forEach((element, index) => {
  16.           let child = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
  17.           child.setAttribute('width', `${this.step}px`)
  18.           child.setAttribute('height', `${element}px`)
  19.           child.setAttribute('x', index * this.step)
  20.           child.setAttribute('y', `${maxH - element}px`)
  21.           child.setAttribute('fill', this.colors[index])
  22.           svg.appendChild(child)
  23.         })
  24.         this.element.appendChild(svg)
  25.       }
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment