Skip to content

Commit 4a690b4

Browse files
committed
working on responsive design
1 parent c95ef2b commit 4a690b4

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

src/components/PieChart.vue

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<template>
22
<div>
3-
<svg ref="svg" class="pie" width="300" height="300" />
3+
<v-card-subtitle>Wins/Losses</v-card-subtitle>
4+
<resize-observer @notify="handleResize" />
5+
<svg ref="svg" class="pie" />
46
</div>
57
</template>
68

79
<script>
810
import * as d3 from 'd3';
11+
import debounce from '../util/debounce';
912
1013
export default {
1114
name: 'PieChart',
@@ -19,39 +22,77 @@ export default {
1922
2023
data: () => ({
2124
pie: null,
25+
width: 300,
26+
height: 300,
27+
padding: {
28+
top: 20,
29+
left: 0,
30+
bottom: 20,
31+
right: 0,
32+
},
33+
svg: null,
34+
svgSelection: null,
2235
}),
2336
2437
watch: {
2538
stats(newStats) {
26-
this.drawChart(newStats);
39+
this.updateChart(newStats);
2740
},
2841
},
2942
3043
mounted() {
31-
const svg = d3.select(this.$refs.svg);
32-
const width = +svg.attr('width');
33-
const height = +svg.attr('height');
44+
this.width = this.parentWidth();
45+
this.svg = this.$refs.svg;
3446
35-
const margin = { top: 20, left: 0, bottom: 20, right: 0 };
47+
this.drawChart();
48+
},
3649
37-
const chartWidth = width - (margin.left + margin.right);
38-
const chartHeight = height - (margin.top + margin.bottom);
50+
methods: {
51+
drawChart() {
52+
this.svgSelection = d3.select(this.svg);
53+
this.svgSelection.attr('width', this.width).attr('height', this.height);
54+
const chartWidth = this.width - (this.padding.left + this.padding.right);
55+
const chartHeight = this.height - (this.padding.top + this.padding.bottom);
3956
40-
this.chartLayer = svg.append('g').attr('transform', `translate(${margin.left}, ${margin.top})`);
57+
this.chartLayer = this.svgSelection.append('g').attr('transform', `translate(${this.padding.left}, ${this.padding.top})`);
4158
42-
this.arc = d3
43-
.arc()
44-
.outerRadius(0)
45-
.innerRadius(chartHeight / 2);
59+
this.arc = d3
60+
.arc()
61+
.outerRadius(chartHeight / 2)
62+
.innerRadius(chartHeight / 4)
63+
.padAngle(0.05);
4664
47-
this.pieG = this.chartLayer.append('g').attr('transform', `translate(${chartWidth / 2}, ${chartHeight / 2})`);
65+
this.pieG = this.chartLayer.append('g').attr('transform', `translate(${chartWidth / 2}, ${chartHeight / 2})`);
4866
49-
this.drawChart(this.stats);
50-
},
67+
this.updateChart(this.stats);
68+
},
5169
52-
methods: {
53-
drawChart(stats) {
70+
parentWidth() {
71+
const { svg } = this.$refs;
72+
const { width } = svg.parentNode.getBoundingClientRect();
73+
const paddingLeft = +getComputedStyle(svg.parentNode).paddingLeft.split('px')[0];
74+
const paddingRight = +getComputedStyle(svg.parentNode).paddingRight.split('px')[0];
75+
const paddingSum = paddingLeft + paddingRight;
76+
return width - paddingSum;
77+
},
78+
79+
handleResize() {
80+
this.width = this.parentWidth();
81+
d3.select(this.svg)
82+
.transition()
83+
.attr('height', this.padding.top + this.height + this.padding.bottom)
84+
.attr('width', this.padding.left + this.width + this.padding.right);
85+
86+
debounce(function () {
87+
this.drawChart()
88+
}, 250);
89+
},
90+
91+
updateChart(stats) {
5492
d3.selectAll('path').remove();
93+
console.log(this.parentWidth());
94+
95+
const total = d3.sum(this.stats.map(stat => stat.value));
5596
5697
const arcs = d3
5798
.pie()
@@ -62,6 +103,14 @@ export default {
62103
63104
block.select('path').attr('d', this.arc);
64105
106+
console.log(this.pieG.node());
107+
108+
this.pieG.append('span').attr('id', 'info');
109+
110+
const infoBlock = d3.select('#info');
111+
112+
console.log(infoBlock);
113+
65114
const newBlock = block.join('g').classed('arc', true);
66115
67116
newBlock

src/util/debounce.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Returns a function, that, as long as it continues to be invoked, will not
2+
// be triggered. The function will be called after it stops being called for
3+
// N milliseconds. If `immediate` is passed, trigger the function on the
4+
// leading edge, instead of the trailing.
5+
export default function(func, wait, immediate) {
6+
let timeout;
7+
return function() {
8+
const context = this;
9+
const args = arguments;
10+
const later = function() {
11+
timeout = null;
12+
if (!immediate) func.apply(context, args);
13+
};
14+
const callNow = immediate && !timeout;
15+
clearTimeout(timeout);
16+
timeout = setTimeout(later, wait);
17+
if (callNow) func.apply(context, args);
18+
};
19+
}

0 commit comments

Comments
 (0)