|
| 1 | +/** |
| 2 | + * @fileOverview Line Graph component definition |
| 3 | + * |
| 4 | + * @author Brian Greig |
| 5 | + * |
| 6 | + * @requires NPM:d3:Vue |
| 7 | + * @requires src/v-chart-plugin.js |
| 8 | + */ |
| 9 | +const d3 = Object.assign({}, |
| 10 | + require('d3-selection'), |
| 11 | + require('d3-scale'), |
| 12 | + require('d3-axis'), |
| 13 | + require('d3-shape')); |
| 14 | +/** |
| 15 | + * Builds a Line Graph. |
| 16 | + * @module lineGraph |
| 17 | + */ |
| 18 | + |
| 19 | +const lineGraph = function chart(mode) { |
| 20 | + /** |
| 21 | + * The SVG that stores the chart |
| 22 | + * @member svgContainer |
| 23 | + */ |
| 24 | + const svgContainer = d3.select(`#${this.chartData.selector}`); |
| 25 | + /** |
| 26 | + * The configuration of the coordinate system |
| 27 | + * @member cs |
| 28 | + */ |
| 29 | + let cs = { |
| 30 | + palette: { |
| 31 | + pointFill: '#005792', |
| 32 | + pointStroke: '#d1f4fa', |
| 33 | + }, |
| 34 | + x: { |
| 35 | + domain: [], |
| 36 | + range: [], |
| 37 | + axisHeight: 20, |
| 38 | + }, |
| 39 | + y: { |
| 40 | + axisWidth: 30, |
| 41 | + ticks: 5, |
| 42 | + }, |
| 43 | + r: { |
| 44 | + |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + /** |
| 49 | + * Runs when a new element is added to the dataset |
| 50 | + * @member enter |
| 51 | + * @function |
| 52 | + * @param {Object} points (svg element) |
| 53 | + */ |
| 54 | + const enter = (points) => { |
| 55 | + points.enter() |
| 56 | + .append('circle') |
| 57 | + .attr('class', this.selector) |
| 58 | + .attr('r', d => cs.r.scale(d.metric[2])) |
| 59 | + .on('mouseover', (d) => { |
| 60 | + this.addTooltip(d, window.event); |
| 61 | + }) |
| 62 | + .on('mouseout', (d) => { |
| 63 | + this.removeTooltip(d); |
| 64 | + }) |
| 65 | + .attr('cx', d => cs.x.scale(d.metric[0]) + cs.y.axisWidth + 5) |
| 66 | + .attr('cy', d => cs.y.scale(d.metric[1])); |
| 67 | + return points; |
| 68 | + }; |
| 69 | + /** |
| 70 | + * Runs when a value of an element in dataset is changed |
| 71 | + * @member transition |
| 72 | + * @function |
| 73 | + * @param {Object} points (svg element) |
| 74 | + */ |
| 75 | + const transition = (points) => { |
| 76 | + points.transition() |
| 77 | + .attr('r', d => cs.r.scale(d.metric[2])) |
| 78 | + .attr('cx', d => cs.x.scale(d.dim) + cs.y.axisWidth + 5) |
| 79 | + .attr('cy', d => cs.y.scale(d.metric[0])) |
| 80 | + .attr('cx', d => cs.x.scale(d.dim) + cs.y.axisWidth + 5) |
| 81 | + .attr('cy', d => cs.y.scale(d.metric[1])); |
| 82 | + return points; |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * Runs when an element is removed from the dataset |
| 87 | + * @member exit |
| 88 | + * @function |
| 89 | + * @param {Object} points (svg element) |
| 90 | + */ |
| 91 | + const exit = (points) => { |
| 92 | + points.exit().remove(); |
| 93 | + return points; |
| 94 | + }; |
| 95 | + |
| 96 | + /** |
| 97 | + * Builds the scales for the x and y axes |
| 98 | + * @member buildScales |
| 99 | + * @function |
| 100 | + */ |
| 101 | + const buildScales = cs => { |
| 102 | + cs.y.scale = d3.scaleLinear() |
| 103 | + .domain([this.min, this.max]) |
| 104 | + .range([this.displayHeight - cs.x.axisHeight, this.header]); |
| 105 | + cs.x.scale = d3.scaleLinear() |
| 106 | + .domain([this.min, this.max]) |
| 107 | + .range([0, this.width]); |
| 108 | + cs.r.scale = d3.scaleLinear() |
| 109 | + .domain([0, 2]) |
| 110 | + .range([0, 20]); |
| 111 | + }; |
| 112 | + /** |
| 113 | + * Draws the x and y axes on the svg |
| 114 | + * @member drawAxis |
| 115 | + * @function |
| 116 | + */ |
| 117 | + const drawAxis = cs => { |
| 118 | + cs.x.axis = d3.axisBottom().scale(cs.x.scale); |
| 119 | + cs.x.xOffset = cs.y.axisWidth + 5; |
| 120 | + cs.x.yOffset = this.displayHeight - cs.x.axisHeight; |
| 121 | + cs.y.axis = d3.axisLeft().ticks(cs.y.ticks, 's').scale(cs.y.scale); |
| 122 | + cs.y.xOffset = cs.y.axisWidth; |
| 123 | + cs.y.yOffset = 0; |
| 124 | + svgContainer.append('g').attr('class', 'axis').attr('transform', `translate(${cs.x.xOffset}, ${cs.x.yOffset})`) |
| 125 | + .call(cs.x.axis); |
| 126 | + svgContainer.append('g').attr('class', 'axis').attr('transform', `translate(${cs.y.xOffset},${cs.y.yOffset})`) |
| 127 | + .call(cs.y.axis); |
| 128 | + }; |
| 129 | + |
| 130 | + const points = svgContainer.selectAll('circle').data(this.ds); |
| 131 | + |
| 132 | + cs = this.setOverrides(cs, this.chartData.overrides); |
| 133 | + |
| 134 | + buildScales(cs); |
| 135 | + drawAxis(cs); |
| 136 | + enter(points); |
| 137 | + transition(points); |
| 138 | + exit(points); |
| 139 | + |
| 140 | + return cs; |
| 141 | +}; |
| 142 | + |
| 143 | +export default lineGraph; |
0 commit comments