-
Notifications
You must be signed in to change notification settings - Fork 2
/
overview-chart.component.ts
196 lines (165 loc) 路 5.42 KB
/
overview-chart.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Component, OnInit } from "@angular/core";
import {TeamService} from "../../services/team.service";
import {SharedModule} from "../../shared-helpers.module";
declare const d3, tippy;
@Component({
selector: "overview-chart",
templateUrl: "./components/overview-chart/overview-chart.html",
styleUrls: ["./components/overview-chart/overview-chart.css"]
})
export class OverviewChartComponent implements OnInit {
rawData: any = {}; // The returned, un-formatted team data
chartVisible: boolean; // If true chart will show
loading: boolean = true;
constructor(
private teamService: TeamService,
private sharedModule: SharedModule
){}
/**
* On chart initialised, show loader, fetch data, call to render
*/
ngOnInit() {
this.loading = true;
this.teamService.sentimentDataUpdated.subscribe(
(teamSentimentData) => {
this.rawData = teamSentimentData;
this.updateChart();
}
);
}
/**
* Called whenever data is modified, re-renders the chart with the new data
* @param rawData
*/
updateChart(rawData = this.rawData){
const chartData = this.makeChartData(rawData);
this.chartVisible = this.isThereEnoughData(chartData);
this.loading = false;
if(this.chartVisible){
this.renderChart(this.makeChartData(rawData));
}
else{
this.removeOldChart();
}
}
/**
* Determines if there is enough data to make showing chart worthwhile
* If not enough data, a message will show instead
* @param chartData
* @returns {boolean}
*/
private isThereEnoughData(chartData){
return chartData.length > 0;
}
/**
* Generates the chart data, from the raw data that was in mongo
* @param rawData
* @returns {Array}
*/
private makeChartData(rawData){
let chartData = [];
const sentimentCount = this.sharedModule.getOverallSentimentCount(rawData);
Object.keys(sentimentCount).forEach((sentimentName)=>{
chartData.push({name: sentimentName, value: sentimentCount[sentimentName]})
});
return chartData;
}
/**
* You'll never guess.... removeOldChart() removes the old chart
*/
private removeOldChart(){
d3.select("#overview-chart").select('svg').remove();
}
/**
* The actual D3 code to render the pie chart!
* @param chartData
*/
private renderChart(chartData){
/* Reset the SVG */
this.removeOldChart(); // Remove old SVG
let parent = d3.select("#overview-chart"); // Get parent
if(parent == null) return; // Okay, somethings gone very wrong, lets not even bother going any further with this...
/* Dimensions */
let margin = {top: 10, right: 10, bottom: 10, left: 10};
let width = parseInt(parent.style("width")) - margin.left - margin.right;
if(isNaN(width) || width > 300 || width < 0 ){ width = 300; } // width must be valid int, between 0 and 300. Default 300.
let height = width - margin.top - margin.bottom;
/* Create the new SVG */
let svg = d3.select("#overview-chart")
.append('svg')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("class", "center-block")
.append("g")
.attr("transform", "translate(" + ((width/2)+margin.left) + "," + ((height/2)+margin.top) + ")");
let radius = Math.min(width, height) / 2;
let color = d3.scaleOrdinal()
.domain(['good', 'average', 'bad'])
.range(["#4DC54E", "#D3D030", "#BB5337"]);
let arc = d3.arc()
.outerRadius(radius)
.innerRadius(radius - 35);
let pie = d3.pie()
.sort(null)
.startAngle(1.1*Math.PI)
.endAngle(3.1*Math.PI)
.value(function(d) { return d.value; });
let g = svg.selectAll(".arc")
.data(pie(chartData))
.enter().append("g")
.attr("class", "arc")
.attr('title', (d)=> makeTitle(d.data))
.on('mouseover', function (d, i ) {
tippy(this, {arrow: false, followCursor: true, position: 'bottom', delay: [0, 300]});
});
g.append("path")
.attr("fill", (d)=> { return color(d.data.name); })
.transition()
.duration(1000)
.ease(d3.easeBounce)
.attrTween("d", (b)=>{
let i = d3.interpolate({startAngle: 1.1*Math.PI, endAngle: 1.1*Math.PI}, b);
return function(t) { return arc(i(t)); };
} );
function makeTitle(d){
return `<b style="color: ${color(d.name)}">${d.name}</b> (${d.value} votes)`
}
}
/**
* Updates the chart with a different set of date data
* @param xDays
*/
private showLastXDays(xDays){
this.updateChart(this.sharedModule.showLastXDays(this.rawData, xDays));
}
/**
* Just shows todays data in the chart
*/
showByToday(){
this.showLastXDays(0);
}
/**
* Just shows the last weeks data in the chart
*/
showByWeek(){
this.showLastXDays(7);
}
/**
* Just shows the last months data in the chart
*/
showByMonth(){
this.showLastXDays(30);
}
/**
* Work-around to make the chart work on all (most) screen sizes
* @param event
*/
public onWindowResize(event){
let resizeTimer = undefined;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
return resizeTimer = setTimeout((() =>
this.updateChart() ), 250);
});
}
}