|
| 1 | +import { globalAgent } from 'http'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @fileOverview Bar chart component definition |
| 5 | + * |
| 6 | + * @author Brian Greig |
| 7 | + * |
| 8 | + * @requires NPM:d3:Vue |
| 9 | + * @requires src/v-chart-plugin.js |
| 10 | + */ |
| 11 | + |
| 12 | +/* eslint-env browser */ |
| 13 | +const d3 = Object.assign({}, |
| 14 | + require('d3-selection'), |
| 15 | + require('d3-scale'), |
| 16 | + require('d3-axis'), |
| 17 | + require('d3-ease'), |
| 18 | + require('d3-array')); |
| 19 | +/** |
| 20 | + * Builds a Box Plot. |
| 21 | + * @module boxPlot |
| 22 | + */ |
| 23 | + |
| 24 | +const boxPlot = function chart() { |
| 25 | + /** |
| 26 | + * The SVG that stores the chart |
| 27 | + * @member svgContainer |
| 28 | + */ |
| 29 | + const svgContainer = d3.select(`#${this.chartData.selector}`); |
| 30 | + /** |
| 31 | + * The configuration of the coordinate system |
| 32 | + * @member cs |
| 33 | + */ |
| 34 | + let cs = { |
| 35 | + palette: { |
| 36 | + fill: ['#005792', '#ffcdcd'], |
| 37 | + stroke: '#d1f4fa', |
| 38 | + }, |
| 39 | + x: { |
| 40 | + axisHeight: 10, |
| 41 | + ticks: 5, |
| 42 | + }, |
| 43 | + y: { |
| 44 | + axisWidth: 10, |
| 45 | + ticks: 5, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * Runs when a new element is added to the dataset |
| 51 | + * @member enter |
| 52 | + * @function |
| 53 | + * @param {Object} boxes (svg element) |
| 54 | + */ |
| 55 | + const enter = (boxes, lines) => { |
| 56 | + boxes.enter() |
| 57 | + .append('rect') |
| 58 | + .attr('fill', 'none') |
| 59 | + .attr('stroke', 'black') |
| 60 | + .attr('stroke-width', 3) |
| 61 | + .attr('class', this.selector) |
| 62 | + .attr('width', 50) |
| 63 | + .attr('height', d => cs.y.scale(d.thirdQrt) - cs.y.scale(d.firstQrt)) |
| 64 | + .attr('x', 50) |
| 65 | + .attr('y', d => cs.y.scale(d.firstQrt)) |
| 66 | + |
| 67 | + const l = lines.enter() |
| 68 | + |
| 69 | + l.append('line') |
| 70 | + .attr('fill', 'none') |
| 71 | + .attr('stroke', 'black') |
| 72 | + .attr('stroke-width', 3) |
| 73 | + .attr('x1', 75) |
| 74 | + .attr('y1', d => cs.y.scale(d.min)) |
| 75 | + .attr('x2', 75) |
| 76 | + .attr('y2', d => cs.y.scale(d.firstQrt)) |
| 77 | + |
| 78 | + l.append('line') |
| 79 | + .attr('fill', 'none') |
| 80 | + .attr('stroke', 'black') |
| 81 | + .attr('stroke-width', 3) |
| 82 | + .attr('x1', 75) |
| 83 | + .attr('y1', d => cs.y.scale(d.thirdQrt)) |
| 84 | + .attr('x2', 75) |
| 85 | + .attr('y2', d => cs.y.scale(d.max)) |
| 86 | + |
| 87 | + l.append('line') |
| 88 | + .attr('fill', 'none') |
| 89 | + .attr('stroke', 'black') |
| 90 | + .attr('stroke-width', 3) |
| 91 | + .attr('x1', 50) |
| 92 | + .attr('y1', d => cs.y.scale(d.median)) |
| 93 | + .attr('x2', 100) |
| 94 | + .attr('y2', d => cs.y.scale(d.median)) |
| 95 | + |
| 96 | + l.append('line') |
| 97 | + .attr('fill', 'none') |
| 98 | + .attr('stroke', 'black') |
| 99 | + .attr('stroke-width', 3) |
| 100 | + .attr('x1', 50) |
| 101 | + .attr('y1', d => cs.y.scale(d.min)) |
| 102 | + .attr('x2', 100) |
| 103 | + .attr('y2', d => cs.y.scale(d.min)) |
| 104 | + |
| 105 | + l.append('line') |
| 106 | + .attr('fill', 'none') |
| 107 | + .attr('stroke', 'black') |
| 108 | + .attr('stroke-width', 3) |
| 109 | + .attr('x1', 50) |
| 110 | + .attr('y1', d => cs.y.scale(d.max)) |
| 111 | + .attr('x2', 100) |
| 112 | + .attr('y2', d => cs.y.scale(d.max)) |
| 113 | + |
| 114 | + return boxes; |
| 115 | + }; |
| 116 | + /** |
| 117 | + * Runs when a value of an element in dataset is changed |
| 118 | + * @member transition |
| 119 | + * @function |
| 120 | + * @param {Object} boxes (svg element) |
| 121 | + */ |
| 122 | + const transition = (boxes) => { |
| 123 | + boxes.transition() |
| 124 | + return boxes; |
| 125 | + }; |
| 126 | + /** |
| 127 | + * Runs when an element is removed from the dataset |
| 128 | + * @member exit |
| 129 | + * @function |
| 130 | + * @param {Object} boxes (svg element) |
| 131 | + */ |
| 132 | + const exit = (rects) => { |
| 133 | + boxes.exit().remove(); |
| 134 | + return rects; |
| 135 | + }; |
| 136 | + /** |
| 137 | + * Builds the scales for the x and y axes |
| 138 | + * @member buildScales |
| 139 | + * @function |
| 140 | + */ |
| 141 | + const buildScales = () => { |
| 142 | + cs.y.scale = d3.scaleLinear() |
| 143 | + .domain([this.min * 0.95, this.max * 1.05]) |
| 144 | + .range([this.header, this.displayHeight]); |
| 145 | + }; |
| 146 | + /** |
| 147 | + * Draws the x and y axes on the svg |
| 148 | + * @member drawAxis |
| 149 | + * @function |
| 150 | + */ |
| 151 | + const drawAxis = () => { |
| 152 | + |
| 153 | + }; |
| 154 | + |
| 155 | + const ds = this.metricAsArray('total').sort(); |
| 156 | + const a = [{ |
| 157 | + min: this.min, |
| 158 | + median: d3.quantile(ds, 0.5), |
| 159 | + max: this.max, |
| 160 | + firstQrt: d3.quantile(ds, 0.25), |
| 161 | + thirdQrt: d3.quantile(ds, 0.75) |
| 162 | + }] |
| 163 | + |
| 164 | + const boxes = svgContainer.selectAll('rect').data(a); |
| 165 | + const lines = svgContainer.selectAll('line').data(a); |
| 166 | + |
| 167 | + cs = this.setOverrides(cs, this.chartData.overrides); |
| 168 | + console.log(a) |
| 169 | + buildScales(cs); |
| 170 | + drawAxis(cs); |
| 171 | + enter(boxes, lines); |
| 172 | + transition(boxes, lines); |
| 173 | + exit(boxes, lines); |
| 174 | + |
| 175 | + return cs; |
| 176 | +}; |
| 177 | + |
| 178 | +export default boxPlot; |
0 commit comments