Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class svgChart {
- constructor(element, data, colors=[], step) {
- this.element = element
- this.data = data
- this.colors = colors
- this.step = step
- }
- init() {
- let maxH = Math.max.apply(null, this.data)
- let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
- svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
- svg.setAttribute('viewBox', `0 0 ${this.data.length * this.step} ${maxH}`)
- svg.setAttribute('width', this.data.length * this.step)
- svg.setAttribute('height', maxH)
- this.data.forEach((element, index) => {
- let child = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
- child.setAttribute('width', `${this.step}px`)
- child.setAttribute('height', `${element}px`)
- child.setAttribute('x', index * this.step)
- child.setAttribute('y', `${maxH - element}px`)
- child.setAttribute('fill', this.colors[index])
- svg.appendChild(child)
- })
- this.element.appendChild(svg)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment