1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 2× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /** * @fileOverview Bar chart component definition * * @author Brian Greig * * @requires NPM:d3:Vue * @requires src/v-chart-plugin.js */ /* eslint-env browser */ const d3 = Object.assign({}, require('d3-selection'), require('d3-scale'), require('d3-axis'), require('d3-ease')); /** * Builds a Bar Chart. * @module barChart */ const barChart = function chart() { /** * The SVG that stores the chart * @member svgContainer */ const svgContainer = d3.select(`#${this.chartData.selector}`); /** * The configuration of the coordinate system * @member cs */ let cs = { palette: { fill: '#005792', stroke: '#d1f4fa', }, bar: { hPadding: 8, vPadding: 5, }, x: { axisHeight: 10, ticks: 5, }, y: { domain: [], range: [], axisWidth: null, }, }; /** * Returns width of the bar * @member getWidth * @function * @param {Object} d (svg element) */ const getWidth = d => cs.x.scale(d.metric); /** * Returns height of the bar * @member getHeight * @function */ const getHeight = () => ( this.displayHeight - cs.x.axisHeight - this.header - cs.bar.vPadding) / this.ds.length - 1; /** * Returns y axis co-ordinate of the bar * @member getYCoord * @function * @param {Object} d (svg element) * @param {Object} i (svg element) */ const getYCoord = (d, i) => i * ( this.displayHeight - cs.x.axisHeight - this.header) / this.ds.length + 1 + this.header; /** * Adds a tooltip on mouse over * @member mouseOver * @function * @param {Object} d (svg element) */ const mouseOver = (d) => { this.addTooltip(d, window.event); }; /** * Removes tooltip on mouse out * @member mouseOut * @function * @param {Object} d (svg element) */ const mouseOut = (d) => { this.removeTooltip(d); }; /** * Runs when a new element is added to the dataset * @member enter * @function * @param {Object} rects (svg element) */ const enter = (rects) => { rects.enter() .append('rect') .attr('fill', cs.palette.fill) .attr('stroke', cs.palette.stroke) .attr('class', this.selector) .attr('width', getWidth) .attr('height', getHeight) .attr('y', getYCoord) .attr('x', cs.y.axisWidth + cs.bar.hPadding) .on('mouseover', mouseOver) .on('mouseout', mouseOut); return rects; }; /** * Runs when a value of an element in dataset is changed * @member transition * @function * @param {Object} rects (svg element) */ const transition = (rects) => { rects.transition() .attr('width', getWidth) .attr('height', getHeight) .attr('y', getYCoord) .attr('x', cs.y.axisWidth + cs.bar.hPadding); return rects; }; /** * Runs when an element is removed from the dataset * @member exit * @function * @param {Object} rect (svg element) */ const exit = (rects) => { rects.exit().remove(); return rects; }; /** * Builds the scales for the x and y axes * @member buildScales * @function */ const buildScales = () => { cs.x.scale = d3.scaleLinear() .domain([0, this.max]) .range([0, this.width - cs.bar.hPadding - cs.y.axisWidth]); this.ds.forEach(t => cs.y.domain.push(t.dim)); this.ds.forEach((t, i) => cs.y.range.push((( this.displayHeight - cs.x.axisHeight - this.header + cs.bar.vPadding) * i) / this.ds.length)); cs.y.scale = d3.scaleOrdinal().domain(cs.y.domain).range(cs.y.range); }; /** * Draws the x and y axes on the svg * @member drawAxis * @function */ const drawAxis = () => { cs.x.axis = d3.axisBottom().ticks(cs.x.ticks, 's').scale(cs.x.scale); cs.y.axis = d3.axisLeft().scale(cs.y.scale); cs.x.yOffset = this.displayHeight - cs.x.axisHeight; cs.x.xOffset = cs.bar.hPadding + cs.y.axisWidth; cs.y.yOffset = cs.bar.vPadding + this.header - 1; cs.y.xOffset = cs.y.axisWidth; Iif (this.ds[0].dim) svgContainer.append('g').attr('class', 'axis').attr('transform', `translate(${cs.y.xOffset}, ${cs.y.yOffset})`).call(cs.y.axis); svgContainer.append('g').attr('class', 'axis').attr('transform', `translate(${cs.x.xOffset}, ${cs.x.yOffset})`).call(cs.x.axis); }; /** * Get the maximum dimension length * @member getMaxDimLength * @function * @param {number} accumulator * @param {number} currentValue */ const getMaxDimLength = (accumulator, currentValue) => { return (currentValue.dim.length > accumulator) ? currentValue.dim.length : accumulator; } const rects = svgContainer.selectAll('rect').data(this.ds); cs = this.setOverrides(cs, this.chartData.overrides); Iif (this.ds[0].dim) cs.y.axisWidth = cs.y.axisWidth || (this.ds.reduce(getMaxDimLength, 0)) * 10; buildScales(cs); drawAxis(cs); enter(rects); transition(rects); exit(rects); return cs; }; export default barChart; |