Skip to content

Commit 92a78ee

Browse files
added scatter plot
1 parent 78537df commit 92a78ee

File tree

3 files changed

+149
-5
lines changed

3 files changed

+149
-5
lines changed

src/components/chartExample.vue

Lines changed: 4 additions & 5 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="barChartData"></v-chart>
20+
<v-chart v-bind:chartData="bubbleChartData"></v-chart>
2121
</div>
2222
<div class="col-12 col-lg-6">
2323
<v-chart v-bind:chartData="areaChartData"></v-chart>
@@ -75,15 +75,14 @@ export default {
7575
width: 50
7676
},
7777
},
78-
barChartData: {
79-
chartType: "barChart",
78+
bubbleChartData: {
79+
chartType: "bubbleChart",
8080
selector: "chart",
8181
title: "Bar Chart",
8282
subtitle: "Sales by month",
8383
width: 600,
8484
height: 500,
85-
metric: ["total", "forecast"],
86-
dim: "month",
85+
metric: ["total", "forecast", "yoy"],
8786
data: sales,
8887
goal: 500,
8988
legends: {

src/import/bubbleChart.js

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

src/v-chart-plugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import lineGraph from './import/lineGraph';
1313
import scatterPlot from './import/scatterPlot';
1414
import pieChart from './import/pieChart';
1515
import areaChart from './import/areaChart';
16+
import bubbleChart from './import/bubbleChart';
1617

1718
const d3 = Object.assign({},
1819
require('d3-selection'));
@@ -189,6 +190,7 @@ const Chart = {
189190
...((typeof pieChart !== 'undefined') && { pieChart }),
190191
...((typeof areaChart !== 'undefined') && { areaChart }),
191192
...((typeof lineGraph !== 'undefined') && { lineGraph }),
193+
...((typeof bubbleChart !== 'undefined') && { bubbleChart }),
192194
},
193195
computed: {
194196
/**

0 commit comments

Comments
 (0)