Skip to content

Commit 71e294f

Browse files
Merge pull request #153 from ignoreintuition/development
Development
2 parents ea41f39 + 2b25379 commit 71e294f

File tree

3 files changed

+194
-14
lines changed

3 files changed

+194
-14
lines changed

src/components/chartExample.vue

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div class="col-6 col-md-8">
1818
<div class="row">
1919
<div class="col-12">
20-
<v-chart v-bind:chartData="bubbleChartData"></v-chart>
20+
<v-chart v-bind:chartData="boxPlotData"></v-chart>
2121
</div>
2222
<div class="col-12 col-lg-6">
2323
<v-chart v-bind:chartData="areaChartData"></v-chart>
@@ -75,25 +75,16 @@ export default {
7575
width: 50
7676
}
7777
},
78-
bubbleChartData: {
79-
chartType: "bubbleChart",
78+
boxPlotData: {
79+
chartType: "boxPlot",
8080
selector: "chart",
81-
title: "Bubble Plot",
81+
title: "Box Plot",
8282
subtitle: "Sales by month",
8383
width: 600,
8484
height: 500,
85-
metric: ["total", "forecast", "yoy"],
85+
metric: ['total'],
8686
data: sales,
8787
goal: 500,
88-
legends: {
89-
enabled: true,
90-
height: 25,
91-
width: 50
92-
},
93-
grid: {
94-
enabled: true,
95-
gridTicks: 25
96-
}
9788
},
9889
lineGraphData: {
9990
chartType: "lineGraph",

src/import/boxPlot.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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;

src/v-chart-plugin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import scatterPlot from './import/scatterPlot';
1414
import pieChart from './import/pieChart';
1515
import areaChart from './import/areaChart';
1616
import bubbleChart from './import/bubbleChart';
17+
import boxPlot from './import/boxPlot';
1718

1819
const d3 = Object.assign({},
1920
require('d3-selection'));
@@ -209,6 +210,15 @@ const Chart = {
209210
.style('stroke', '#708090')
210211
.style('stroke-width', 1)
211212
},
213+
/**
214+
* get the values of a metric as an array
215+
* @memberOf Chart
216+
* @returns {Array} metric values
217+
*/
218+
metricAsArray(metric) {
219+
metric = this.chartData.data.map(d => d[metric]);
220+
return metric;
221+
},
212222

213223
...((typeof barChart !== 'undefined') && { barChart }),
214224
...((typeof vBarChart !== 'undefined') && { vBarChart }),
@@ -217,6 +227,7 @@ const Chart = {
217227
...((typeof areaChart !== 'undefined') && { areaChart }),
218228
...((typeof lineGraph !== 'undefined') && { lineGraph }),
219229
...((typeof bubbleChart !== 'undefined') && { bubbleChart }),
230+
...((typeof boxPlot !== 'undefined') && { boxPlot }),
220231
},
221232
computed: {
222233
/**

0 commit comments

Comments
 (0)