-
Notifications
You must be signed in to change notification settings - Fork 0
/
th-highcharts-pie.html
270 lines (242 loc) · 8.93 KB
/
th-highcharts-pie.html
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../th-d3-chart/th-d3-chart.html">
<link rel="import" href="th-highcharts.html">
<!--
A component that sets WSJ default settings for the Highcharts bar chart
##### Example
<th-highcharts-pie></th-highcharts-pie>
@element th-highcharts-pie
@blurb A component that sets WSJ default settings for the Highcharts bar chart
@status alpha
@author Thelma team
@homepage http://thelmanews.github.io/th-highcharts-pie
-->
<polymer-element extends="th-d3-chart" name="th-highcharts-pie" attributes="chartTitle chartSubtitle credits creditsURL legend legendTitle valuePrefix valueSuffix valueDecimals thousandsSep textColor backgroundColor gridLineColor">
<template>
<style>
:host {
display: inline-block;
width: 100%;
height: 100%;
position: relative;
}
</style>
<th-highcharts id="chart"
chartData="{{chartData}}"
chartType="{{chartType}}"
chartWidth="{{chartWidth}}"
chartHeight="{{chartHeight}}"
chartTitle="{{chartTitle}}"
chartSubtitle="{{chartSubtitle}}"
thousandsSep="{{thousandsSep}}"
valueDecimals="{{valueDecimals}}"
valuePrefix="{{valuePrefix}}"
valueSuffix="{{valueSuffix}}"
legend="{{legend}}"
legendTitle="{{legendTitle}}"
textColor="{{textColor}}"
backgroundColor="{{backgroundColor}}"
gridLineColor="{{gridLineColor}}"
credits="{{credits}}"
creditsURL="{{creditsURL}}"
>
</th-highcharts>
</template>
<script>
Polymer('th-highcharts-pie', {
/**
* 'chartData' allows for up to 3 series of data. It must follow this format with keys being: label, series1, series2, series3
* @type {Array}
*/
chartData: [
{"label": "Germany", "value": "100000", "color": "" },
{"label": "France", "value": "80000", "color": "" },
{"label": "Italy", "value": "40000", "color": "" }
],
/**
* 'valueDecimals' is the number of decimal places to display in values in the chart
* @type {Number}
*/
valueDecimals: 0,
/**
* 'legend' is a switch display or hide the legend
* @type {Boolean}
*/
legend: false,
/**
* 'chartType' is initialized as bar by default
* @type {String}
*/
chartType: 'pie',
init: function(){
// It's quicker to get dimensions here vs. in th-highcharts which is one level deeper into shadowDom
this.chartHeight = this.computeHeight();
this.chartWidth = this.computeWidth();
//clone options to avoid shared state
this.$.chart.chartOptions = this.$.chart.clone(this.wsjOptions);
this._formatTooltipValues();
this.$.chart._setupChart();
this.$.chart._buildChart();
},
animate: function(){
this.$.chart.animate();
},
reset: function(){
this.$.chart.reset();
},
resize: function(){
this.$.chart.resize();
},
/**
* 'wsjOptions' are the defaults defined in the WSJ highcharts repo https://github.com/dowjones/custom-data-charts/blob/master/_js/defaults.types.bar.js
*
* UPDATING INSTRUCTIONS:
* 1) Copy/paste the object defined in defaults.types.bar.js into the wsjOptions object
* 2) Replace jQuery with vanilla JavaScript
*
* @type {Object}
*/
wsjOptions: {
colors: ['#0098db', '#ce3139', '#edc639', '#727272', '#63bc51', '#bfbfbf'],
chart: {
height: 400,
marginBottom: 50,
style: {
maxWidth: '1275px',
color: '#000000',
fontFamily: "'Whitney SSm', 'Helvetica Neue', Helvetica, Arial, sans-serif",
fontWeight: 'normal',
fontStyle: 'normal',
fontSize: 13
},
type: "pie",
events: {
load: function(e){
// make the tick that has the 0 value black
var chart = e.target;
var index = chart.yAxis[0].tickPositions.indexOf(0);
index = (index === 0) ? chart.yAxis[0].tickPositions.length - 1: index;
// $chart.find('.highcharts-axis:eq(1) path:eq('+ index +')').attr('stroke', '#000');
}
},
marginLeft: 0,
},
exporting: {
enabled: false
},
tooltip: {
enabled: true,
borderWidth: 1,
borderColor: '#ccc',
useHTML: true,
shared: true,
style: {
opacity: 1,
},
formatter: function(){
// console.log(this);
var points = this.point;
var html = '<h4 class="tooltip"><span style="color: ' + points.color + '">'+this.key+': </span>';
var value = points.y;
var num_decimals = this.series.chart.options.yAxisDecimal || null;
var num_decimals_base = 0;
value = Highcharts.numberFormat(value, num_decimals, '.', '');
console.log(value);
if(this.series.chart.options.yAxisUnitPlace1 == 'prefix'){
value = this.series.chart.options.yAxisUnit1 + value;
}
if(this.series.chart.options.yAxisUnitPlace2 == 'suffix'){
value = value + this.series.chart.options.yAxisUnit2;
}
console.log(value);
html += value + '</h4>';
return html;
}
},
plotOptions: {
pie: {
allowPointSelect: true,
series: {
point: {
events: {
select: function() {
newColor = prompt('Color?');
this.update({
color:{
fillColor:newColor,
lineColor:'red',
}
})
}
}
}
},
dataLabels: {
softConnector: false,
}
}
},
xAxis: {
type: 'category',
// x: ($(window).width() > 500) ? 0 : -16,
// align: ($(window).width() > 500) ? 'right' : 'left',
// verticalAlign: ($(window).width() > 500) ? 'middle' : 'top',
// layout: ($(window).width() > 500) ? 'vertical' : 'horizontal',
// floating: ($(window).width() > 500) ? false : true,
title: {
style: {
fontFamily: "'Whitney SSm', 'Helvetica Neue', Helvetica, Arial, sans-serif",
fontSize: '12px',
fontWeight: 800
}
},
itemStyle: {
fontSize: 12,
fontFamily: "'Whitney SSm', 'Helvetica Neue', Helvetica, Arial, sans-serif",
fontWeight: 400,
fontStyle: 'normal',
color: '#231F20'
}
},
position: {
align: 'left',
x: 10
}
},
/**
* 'formatTooltipValues' reformats the tooltips that are initially formatted in 'wsjChartOptions'.
* This is necessary because number formats (valueDecimals, valuePrefix, valueSuffix) for the tooltip need to be
* passed into the function and a reference to self (the component) needs to be established in order to do so.
*
* UPDATING INSTRUCTIONS:
* 1) Define a reference to self
* 2) Redefine this.wsjOptions.tooltip.formatter by first copy/pasting the original
* 3) Replace num_decimals with self.valueDecimals
* 4) Replace jQuery loops with vanilla JavaScript forEach loops
* 5) Add valuePrefix and valueSuffix to the value
*/
_formatTooltipValues: function(){
// 1) Define reference to component
var self = this;
// 2) Copy/paste tooltip formatter function from wsjOptions
this.$.chart.chartOptions.tooltip.formatter = function(){
var points = this.point;
var html = '<h4 class="tooltip"><span style="color: ' + points.color + '; height: 32px;">'+this.key+': </span>';
var value = points.y;
// 3) Redefine number of decimal places with component attribute
var num_decimals = self.valueDecimals|| null;
var num_decimals_base = 0;
value = Highcharts.numberFormat(value, num_decimals, '.', '');
if(self.valuePrefix){
value = self.valuePrefix + value;
}
if(self.valueSuffix){
value = value + self.valueSuffix
}
html += value + '</h4>';
return html;
}
}
});
</script>
</polymer-element>