@politicalwatch/tipi-uikit 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/.browserslistrc +2 -0
  2. package/.editorconfig +7 -0
  3. package/.eslintrc.js +11 -0
  4. package/.postcssrc.json +5 -0
  5. package/.prettierrc.js +7 -0
  6. package/.stylelintrc +24 -0
  7. package/README.md +21 -0
  8. package/package.json +45 -0
  9. package/src/assets/.gitkeep +0 -0
  10. package/src/assets/birthday_bg.png +0 -0
  11. package/src/assets/party_logos/bildu.png +0 -0
  12. package/src/assets/party_logos/bng.png +0 -0
  13. package/src/assets/party_logos/ca.jpeg +0 -0
  14. package/src/assets/party_logos/ciudadanos.png +0 -0
  15. package/src/assets/party_logos/compromis.jpg +0 -0
  16. package/src/assets/party_logos/cup.png +0 -0
  17. package/src/assets/party_logos/eajpnv.png +0 -0
  18. package/src/assets/party_logos/encomupodem.jpg +0 -0
  19. package/src/assets/party_logos/erc.png +0 -0
  20. package/src/assets/party_logos/fac.png +0 -0
  21. package/src/assets/party_logos/icon/bng.svg +4 -0
  22. package/src/assets/party_logos/icon/coalicioncanaria.svg +7 -0
  23. package/src/assets/party_logos/icon/compromis.svg +3 -0
  24. package/src/assets/party_logos/icon/cs.svg +3 -0
  25. package/src/assets/party_logos/icon/cup.svg +4 -0
  26. package/src/assets/party_logos/icon/ehbildu.svg +4 -0
  27. package/src/assets/party_logos/icon/encomu.svg +3 -0
  28. package/src/assets/party_logos/icon/erc.svg +5 -0
  29. package/src/assets/party_logos/icon/foroasturias.svg +4 -0
  30. package/src/assets/party_logos/icon/iu.svg +3 -0
  31. package/src/assets/party_logos/icon/jxcat.svg +3 -0
  32. package/src/assets/party_logos/icon/maspais.svg +3 -0
  33. package/src/assets/party_logos/icon/nuevacanaria.svg +3 -0
  34. package/src/assets/party_logos/icon/pdecat.svg +3 -0
  35. package/src/assets/party_logos/icon/pnv.svg +4 -0
  36. package/src/assets/party_logos/icon/podemos.svg +3 -0
  37. package/src/assets/party_logos/icon/pp.svg +4 -0
  38. package/src/assets/party_logos/icon/prc.svg +5 -0
  39. package/src/assets/party_logos/icon/psoe.svg +3 -0
  40. package/src/assets/party_logos/icon/sumar.svg +73 -0
  41. package/src/assets/party_logos/icon/teruelexiste.svg +3 -0
  42. package/src/assets/party_logos/icon/unidaspodemos.svg +27 -0
  43. package/src/assets/party_logos/icon/upn.svg +5 -0
  44. package/src/assets/party_logos/icon/vox.svg +4 -0
  45. package/src/assets/party_logos/iu.png +0 -0
  46. package/src/assets/party_logos/jxcat.png +0 -0
  47. package/src/assets/party_logos/mas-pais.jpg +0 -0
  48. package/src/assets/party_logos/nueva_canarias.png +0 -0
  49. package/src/assets/party_logos/pdecat.png +0 -0
  50. package/src/assets/party_logos/podemos.jpg +0 -0
  51. package/src/assets/party_logos/pp.png +0 -0
  52. package/src/assets/party_logos/prc.png +0 -0
  53. package/src/assets/party_logos/psc.png +0 -0
  54. package/src/assets/party_logos/pse-ee.png +0 -0
  55. package/src/assets/party_logos/psoe.png +0 -0
  56. package/src/assets/party_logos/teruelexiste.svg +4 -0
  57. package/src/assets/party_logos/unidaspodemos.png +0 -0
  58. package/src/assets/party_logos/upn.jpg +0 -0
  59. package/src/assets/party_logos/vox.png +0 -0
  60. package/src/assets/svg/icon-advanced.svg +3 -0
  61. package/src/assets/svg/icon-bell.svg +3 -0
  62. package/src/assets/svg/icon-birthday.svg +18 -0
  63. package/src/assets/svg/icon-building.svg +3 -0
  64. package/src/assets/svg/icon-calendar.svg +4 -0
  65. package/src/assets/svg/icon-chevron-down.svg +3 -0
  66. package/src/assets/svg/icon-chevron-left.svg +3 -0
  67. package/src/assets/svg/icon-chevron-right.svg +3 -0
  68. package/src/assets/svg/icon-chevron-up.svg +3 -0
  69. package/src/assets/svg/icon-clock.svg +4 -0
  70. package/src/assets/svg/icon-close-menu.svg +3 -0
  71. package/src/assets/svg/icon-close.svg +3 -0
  72. package/src/assets/svg/icon-conversation.svg +3 -0
  73. package/src/assets/svg/icon-denied.svg +3 -0
  74. package/src/assets/svg/icon-document.svg +4 -0
  75. package/src/assets/svg/icon-download.svg +3 -0
  76. package/src/assets/svg/icon-error.svg +3 -0
  77. package/src/assets/svg/icon-github.svg +9 -0
  78. package/src/assets/svg/icon-hamburger.svg +1 -0
  79. package/src/assets/svg/icon-info.svg +3 -0
  80. package/src/assets/svg/icon-less.svg +3 -0
  81. package/src/assets/svg/icon-location.svg +3 -0
  82. package/src/assets/svg/icon-mail.svg +4 -0
  83. package/src/assets/svg/icon-more.svg +5 -0
  84. package/src/assets/svg/icon-open-blank.svg +3 -0
  85. package/src/assets/svg/icon-plus.svg +3 -0
  86. package/src/assets/svg/icon-search.svg +4 -0
  87. package/src/assets/svg/icon-success.svg +3 -0
  88. package/src/assets/svg/icon-twitter.svg +3 -0
  89. package/src/assets/svg/icon-upload.svg +3 -0
  90. package/src/components/CSVDownload/CSVDownload.vue +77 -0
  91. package/src/components/Charts/Barchart/d3.barchart.js +488 -0
  92. package/src/components/Charts/Barchart/d3.barchart.vue +16 -0
  93. package/src/components/Charts/Sliceschart/d3.sliceschart.js +272 -0
  94. package/src/components/Charts/Sliceschart/d3.sliceschart.vue +20 -0
  95. package/src/components/Charts/Sunburst/d3.sunburst.js +416 -0
  96. package/src/components/Charts/Sunburst/d3.sunburst.vue +23 -0
  97. package/src/components/Charts/WordsCloud/d3.wordscloud.js +336 -0
  98. package/src/components/Charts/WordsCloud/d3.wordscloud.vue +16 -0
  99. package/src/components/Charts/d3.chart.js +242 -0
  100. package/src/components/Charts/d3.chart.vue +80 -0
  101. package/src/components/Charts/styles.scss +92 -0
  102. package/src/components/CongressLink/CongressLink.vue +17 -0
  103. package/src/components/Deputy/Deputy.vue +119 -0
  104. package/src/components/Header/Header.vue +15 -0
  105. package/src/components/Icon/Icon.vue +33 -0
  106. package/src/components/InitiativeCard/InitiativeCard.vue +98 -0
  107. package/src/components/InitiativeMeta/InitiativeMeta.vue +85 -0
  108. package/src/components/Loader/Loader.vue +27 -0
  109. package/src/components/Message/Message.vue +29 -0
  110. package/src/components/Navbar/Navbar.vue +96 -0
  111. package/src/components/Neuron/Neuron.vue +209 -0
  112. package/src/components/PartyLogo/PartyLogo.vue +59 -0
  113. package/src/components/PartyLogo/PartyLogoIcon.vue +229 -0
  114. package/src/components/Results/Results.vue +109 -0
  115. package/src/components/Splash/Splash.vue +42 -0
  116. package/src/components/Text/Text.vue +59 -0
  117. package/src/components/TipiCharts/TipiBarchart.vue +204 -0
  118. package/src/components/TopicCard/TopicCard.vue +40 -0
  119. package/src/components/TopicLink/TopicLink.vue +33 -0
  120. package/src/components/TopicPill/TopicPill.vue +69 -0
  121. package/src/components/Topics/Topics.vue +84 -0
  122. package/src/components/TwoCircles/TwoCircles.vue +138 -0
  123. package/src/components/index.js +53 -0
  124. package/src/main.js +11 -0
  125. package/src/styles/01_settings/_settings__breakpoints.scss +28 -0
  126. package/src/styles/01_settings/_settings__colors.scss +30 -0
  127. package/src/styles/01_settings/_settings__fonts.scss +3 -0
  128. package/src/styles/01_settings/_settings__icons.scss +0 -0
  129. package/src/styles/01_settings/_settings__variables.scss +45 -0
  130. package/src/styles/02_tools/_tools__functions.scss +19 -0
  131. package/src/styles/02_tools/_tools__mixins.scss +321 -0
  132. package/src/styles/03_generic/_generic__box-sizing.scss +12 -0
  133. package/src/styles/03_generic/_generic__global.scss +20 -0
  134. package/src/styles/03_generic/_generic__normalize.scss +371 -0
  135. package/src/styles/04_base/_base__anchors.scss +8 -0
  136. package/src/styles/04_base/_base__blockquotes.scss +8 -0
  137. package/src/styles/04_base/_base__forms.scss +32 -0
  138. package/src/styles/04_base/_base__headings.scss +23 -0
  139. package/src/styles/04_base/_base__images.scss +4 -0
  140. package/src/styles/04_base/_base__lists.scss +66 -0
  141. package/src/styles/04_base/_base__paragraphs.scss +11 -0
  142. package/src/styles/04_base/_base__tables.scss +95 -0
  143. package/src/styles/05_objects/_objects__columns.scss +29 -0
  144. package/src/styles/05_objects/_objects__container.scss +19 -0
  145. package/src/styles/05_objects/_objects__grid.scss +57 -0
  146. package/src/styles/05_objects/_objects__masonry.scss +11 -0
  147. package/src/styles/05_objects/_objects__media.scss +24 -0
  148. package/src/styles/05_objects/_objects__section.scss +21 -0
  149. package/src/styles/06_components/_components__button.scss +178 -0
  150. package/src/styles/06_components/_components__congress-link.scss +12 -0
  151. package/src/styles/06_components/_components__decorator.scss +10 -0
  152. package/src/styles/06_components/_components__deputy.scss +167 -0
  153. package/src/styles/06_components/_components__disclaimer.scss +24 -0
  154. package/src/styles/06_components/_components__forms.scss +454 -0
  155. package/src/styles/06_components/_components__header.scss +36 -0
  156. package/src/styles/06_components/_components__icon.scss +17 -0
  157. package/src/styles/06_components/_components__initiative-card.scss +49 -0
  158. package/src/styles/06_components/_components__initiative-meta.scss +91 -0
  159. package/src/styles/06_components/_components__loader.scss +125 -0
  160. package/src/styles/06_components/_components__menu.scss +104 -0
  161. package/src/styles/06_components/_components__message.scss +66 -0
  162. package/src/styles/06_components/_components__navbar.scss +114 -0
  163. package/src/styles/06_components/_components__party-logo.scss +21 -0
  164. package/src/styles/06_components/_components__splash.scss +29 -0
  165. package/src/styles/06_components/_components__text.scss +69 -0
  166. package/src/styles/06_components/_components__topic-card.scss +36 -0
  167. package/src/styles/06_components/_components__topic-link.scss +37 -0
  168. package/src/styles/06_components/_components__topics.scss +107 -0
  169. package/src/styles/07_trumps/_trumps__borders.scss +1 -0
  170. package/src/styles/07_trumps/_trumps__colors.scss +3 -0
  171. package/src/styles/07_trumps/_trumps__grid.scss +1 -0
  172. package/src/styles/07_trumps/_trumps__helpers.scss +17 -0
  173. package/src/styles/07_trumps/_trumps__texts.scss +39 -0
  174. package/src/styles/07_trumps/_trumps__visibility.scss +56 -0
  175. package/src/styles/main.scss +54 -0
  176. package/src/utils/index.js +26 -0
  177. package/svgo.config.js +16 -0
  178. package/vite.config.js +57 -0
@@ -0,0 +1,272 @@
1
+ import d3chart from '../d3.chart';
2
+ import {select, selectAll} from 'd3-selection';
3
+ import {scaleLinear, scaleOrdinal} from 'd3-scale';
4
+ import {max, min, sum} from 'd3-array';
5
+ import {pie, arc} from 'd3-shape';
6
+ import {transition} from 'd3-transition';
7
+ import {interpolate} from 'd3-interpolate';
8
+ import {easeLinear, easePolyIn, easePolyOut, easePoly,
9
+ easePolyInOut, easeQuadIn, easeQuadOut, easeQuad, easeQuadInOut,
10
+ easeCubicIn, easeCubicOut, easeCubic, easeCubicInOut, easeSinIn,
11
+ easeSinOut, easeSin, easeSinInOut, easeExpIn, easeExpOut, easeExp,
12
+ easeExpInOut, easeCircleIn, easeCircleOut, easeCircle, easeCircleInOut,
13
+ easeElasticIn, easeElastic, easeElasticOut, easeElasticInOut, easeBackIn,
14
+ easeBackOut, easeBack, easeBackInOut, easeBounceIn, easeBounce,
15
+ easeBounceOut, easeBounceInOut} from 'd3-ease';
16
+ import {schemeCategory10, schemeAccent, schemeDark2, schemePaired,
17
+ schemePastel1, schemePastel2, schemeSet1, schemeSet2, schemeSet3,
18
+ schemeTableau10} from 'd3-scale-chromatic';
19
+
20
+ const d3 = {select, selectAll, scaleLinear, scaleOrdinal, max, min, sum, transition, pie, arc, interpolate,
21
+ easeLinear, easePolyIn, easePolyOut, easePoly, easePolyInOut, easeQuadIn, easeQuadOut,
22
+ easeQuad, easeQuadInOut, easeCubicIn, easeCubicOut, easeCubic, easeCubicInOut,
23
+ easeSinIn, easeSinOut, easeSin, easeSinInOut, easeExpIn, easeExpOut, easeExp,
24
+ easeExpInOut, easeCircleIn, easeCircleOut, easeCircle, easeCircleInOut,
25
+ easeElasticIn, easeElastic, easeElasticOut, easeElasticInOut, easeBackIn,
26
+ easeBackOut, easeBack, easeBackInOut, easeBounceIn, easeBounce, easeBounceOut,
27
+ easeBounceInOut, schemeCategory10, schemeAccent, schemeDark2, schemePaired,
28
+ schemePastel1, schemePastel2, schemeSet1, schemeSet2, schemeSet3,
29
+ schemeTableau10};
30
+
31
+ /**
32
+ * D3 Slices Chart
33
+ */
34
+ class d3sliceschart extends d3chart{
35
+
36
+ constructor(selection, data, config) {
37
+ super(selection, data, config, {
38
+ margin: {top: 40, right: 20, bottom: 40, left: 20},
39
+ key: '',
40
+ value: 'value',
41
+ color : {key: false, keys: false, scheme: false, current: '#1f77b4', default: '#AAA', axis: '#000'},
42
+ radius: {inner: false, outter: false, padding: 0, round: 0},
43
+ transition: {duration: 350, ease: 'easeLinear'},
44
+ });
45
+ }
46
+
47
+ /**
48
+ * Init chart
49
+ */
50
+ initChart() {
51
+
52
+ // Set up dimensions
53
+ this.getDimensions();
54
+ this.initChartFrame('sliceschart');
55
+
56
+ this.cScale = d3.scaleOrdinal();
57
+ this.rScale = d3.scaleLinear();
58
+ this.arc = d3.arc();
59
+ this.pie = d3.pie()
60
+ .sort(null)
61
+ .value(() => 1)
62
+ .padAngle(this.cfg.radius.padding);
63
+
64
+ if(this.cfg.radius && this.cfg.radius.inner){
65
+ const outRadius = this.cfg.radius.outter ? this.cfg.radius.outter : Math.min(this.cfg.width, this.cfg.height) / 2;
66
+ this.cfg.radius.relation = this.cfg.radius.inner
67
+ ? this.cfg.radius.inner / outRadius
68
+ : 0;
69
+ }
70
+
71
+ this.gcenter = this.g.append('g');
72
+ this.setChartDimension();
73
+ this.updateChart();
74
+
75
+ this.centerText = this.g.append('text')
76
+ .attr('x', this.cfg.width / 2)
77
+ .attr('y', (this.cfg.height / 2) + 10)
78
+ .attr('text-anchor', 'middle');
79
+ }
80
+
81
+ /**
82
+ * Set up chart dimensions (non depending on data)
83
+ */
84
+ setChartDimension(){
85
+ // SVG element
86
+ this.svg
87
+ .attr("viewBox", `0 0 ${this.cfg.width+this.cfg.margin.left+this.cfg.margin.right} ${this.cfg.height+this.cfg.margin.top+this.cfg.margin.bottom}`)
88
+ .attr("width", this.cfg.width + this.cfg.margin.left + this.cfg.margin.right)
89
+ .attr("height", this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom);
90
+
91
+ // Center element
92
+ this.gcenter
93
+ .attr('transform', `translate(${this.cfg.width/2}, ${this.cfg.height/2})`);
94
+ }
95
+
96
+ /**
97
+ * Bind data to main elements groups
98
+ */
99
+ bindData(){
100
+ this.itemg = this.gcenter.selectAll('.chart__slice-group')
101
+ .data(this.pie(this.data), d => d.data[this.cfg.key]);
102
+
103
+ // Set transition
104
+ this.transition = d3.transition('t')
105
+ .duration(this.cfg.transition.duration)
106
+ .ease(d3[this.cfg.transition.ease]);
107
+ }
108
+
109
+ /**
110
+ * Set up scales
111
+ */
112
+ setScales(){
113
+
114
+ // Set up radius
115
+ this.cfg.radius.outter = this.cfg.radius && this.cfg.radius.outter ? this.cfg.radius.outter : Math.min(this.cfg.width, this.cfg.height) / 2;
116
+ this.inRadius = this.cfg.radius && this.cfg.radius.inner ? this.cfg.radius.inner : 0;
117
+ if(this.cfg.radius.relation){
118
+ this.inRadius = this.cfg.radius.outter * this.cfg.radius.relation;
119
+ }
120
+
121
+ // Set up arcs
122
+ this.arc = d3.arc()
123
+ .outerRadius(this.cfg.radius.outter)
124
+ .innerRadius(this.inRadius)
125
+ .cornerRadius(this.cfg.radius.round);
126
+
127
+ let max = d3.max(this.data, d => d[this.cfg.value]);
128
+ if (max == 0) {
129
+ max = 1;
130
+ }
131
+
132
+ this.rScale
133
+ .range([this.inRadius, this.cfg.radius.outter])
134
+ .domain([0, max]);
135
+
136
+ // Set up color scheme
137
+ if(this.cfg.color.scheme){
138
+ if(this.cfg.color.scheme instanceof Array === true){
139
+ this.colorScale = d3.scaleOrdinal()
140
+ .domain(this.data.map(d=>d[this.cfg.key]))
141
+ .range(this.cfg.color.scheme);
142
+ }else{
143
+ this.colorScale = d3.scaleOrdinal(d3[this.cfg.color.scheme])
144
+ .domain(this.data.map(d=>d[this.cfg.key]));
145
+ }
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Add new chart's elements
151
+ */
152
+ enterElements(){
153
+
154
+ const newg = this.itemg
155
+ .enter().append('g')
156
+ .attr("class", "chart__slice-group chart__slice-group--sliceschart");
157
+
158
+ // BACKGROUNDS
159
+ newg.append("path")
160
+ .attr("class", "chart__slice chart__slice--sliceschart")
161
+ //.on('mouseover', (d, i) => {
162
+ // const key = d.data[this.cfg.key];
163
+ // const value = d.data[this.cfg.value];
164
+ // this.tooltip.html(() => {
165
+ // return `<div>${key}: ${value}</div>`;
166
+ // })
167
+ // .classed('active', true);
168
+ //})
169
+ //.on('mouseout', () => {
170
+ // this.tooltip.classed('active', false);
171
+ //})
172
+ //.on('mousemove', () => {
173
+ // this.tooltip
174
+ // .style('left', window.event['pageX'] - 28 + 'px')
175
+ // .style('top', window.event['pageY'] - 40 + 'px');
176
+ //})
177
+ .on('mouseover', (d, i) => {
178
+ const total = d3.sum(this.data, d => d.value);
179
+ const value = d.data.value > 0 && total > 0
180
+ ? Math.round((d.data.value / total)*100)
181
+ : 0;
182
+ const key = d.data[this.cfg.key];
183
+ this.centerText.text(`${key}: ${value}%`);
184
+ })
185
+ .on('mouseout', () => {
186
+ this.centerText.text('');
187
+ })
188
+ .transition(this.transition)
189
+ .delay((d,i) => i * this.cfg.transition.duration)
190
+ .attrTween('d', d => {
191
+ const i = d3.interpolate(d.startAngle+0.1, d.endAngle);
192
+ return t => {
193
+ d.endAngle = i(t);
194
+ return this.arc(d);
195
+ };
196
+ })
197
+ .style("fill", d=> this.cfg.color.default)
198
+ .style('opacity', 1);
199
+
200
+ // FILLS
201
+ newg.append("path")
202
+ .attr("class", "chart__slice chart__slice--sliceschart")
203
+ .transition(this.transition)
204
+ .delay((d,i) => i * this.cfg.transition.duration)
205
+ .attrTween('d', d => {
206
+ const i = d3.interpolate(d.startAngle+0.1, d.endAngle);
207
+ const arc = d3.arc()
208
+ .outerRadius(this.rScale(d.data[this.cfg.value]))
209
+ .innerRadius(this.inRadius)
210
+ .cornerRadius(this.cfg.radius.round);
211
+ return t => {
212
+ d.endAngle = i(t);
213
+ return arc(d);
214
+ };
215
+ })
216
+ .style("fill", d=> this.colorElement(d.data))
217
+ .style('pointer-events', 'none')
218
+ .style('opacity', 1);
219
+
220
+ }
221
+
222
+ /**
223
+ * Update chart's elements based on data change
224
+ */
225
+ updateElements(){
226
+ /*
227
+ // PATHS
228
+ this.itemg.selectAll(".chart__slice")
229
+ .style('opacity', 0)
230
+ .data(this.pie(this.data), d => d.data[this.cfg.key])
231
+ .transition(this.transition)
232
+ .delay((d,i) => i * this.cfg.transition.duration)
233
+ .attrTween('d', d => {
234
+ const i = d3.interpolate(d.startAngle+0.1, d.endAngle);
235
+ return t => {
236
+ d.endAngle = i(t);
237
+ return this.arc(d)
238
+ }
239
+ })
240
+ .style("fill", this.cfg.color.default)
241
+ .style('opacity', 1);
242
+ */
243
+ }
244
+
245
+ /**
246
+ * Remove chart's elements without data
247
+ */
248
+ exitElements(){
249
+ this.itemg.exit()
250
+ .transition(this.transition)
251
+ .style("opacity", 0)
252
+ .remove();
253
+ }
254
+
255
+ midAngle(d){
256
+ return d.startAngle + (d.endAngle - d.startAngle)/2;
257
+ }
258
+
259
+ /**
260
+ * Store the displayed angles in _current.
261
+ * Then, interpolate from _current to the new angles.
262
+ * During the transition, _current is updated in-place by d3.interpolate.
263
+ */
264
+ arcTween(a) {
265
+ var i = d3.interpolate(this._current, a);
266
+ this._current = i(0);
267
+ return (t) => this.arc(i(t)) ;
268
+ }
269
+
270
+ }
271
+
272
+ export default d3sliceschart;
@@ -0,0 +1,20 @@
1
+ <script>
2
+ import D3Chart from '../d3.chart.vue';
3
+ import d3sliceschart from './d3.sliceschart';
4
+
5
+ export default {
6
+ name: 'D3SlicesChart',
7
+ extends: D3Chart,
8
+ mounted() {
9
+ this.chart = new d3sliceschart(
10
+ this.$refs.chart,
11
+ JSON.parse(JSON.stringify(this.datum)),
12
+ this.config,
13
+ );
14
+ },
15
+ };
16
+ </script>
17
+
18
+ <style lang="scss">
19
+ @import '../styles';
20
+ </style>
@@ -0,0 +1,416 @@
1
+ import d3chart from '../d3.chart';
2
+ import { select, selectAll } from 'd3-selection';
3
+ import { scaleLinear, scaleOrdinal, scaleSqrt } from 'd3-scale';
4
+ import { hierarchy, partition } from 'd3-hierarchy';
5
+ import { arc } from 'd3-shape';
6
+ import { path } from 'd3-path';
7
+ import { transition } from 'd3-transition';
8
+ import { interpolate } from 'd3-interpolate';
9
+ import {
10
+ easeLinear,
11
+ easePolyIn,
12
+ easePolyOut,
13
+ easePoly,
14
+ easePolyInOut,
15
+ easeQuadIn,
16
+ easeQuadOut,
17
+ easeQuad,
18
+ easeQuadInOut,
19
+ easeCubicIn,
20
+ easeCubicOut,
21
+ easeCubic,
22
+ easeCubicInOut,
23
+ easeSinIn,
24
+ easeSinOut,
25
+ easeSin,
26
+ easeSinInOut,
27
+ easeExpIn,
28
+ easeExpOut,
29
+ easeExp,
30
+ easeExpInOut,
31
+ easeCircleIn,
32
+ easeCircleOut,
33
+ easeCircle,
34
+ easeCircleInOut,
35
+ easeElasticIn,
36
+ easeElastic,
37
+ easeElasticOut,
38
+ easeElasticInOut,
39
+ easeBackIn,
40
+ easeBackOut,
41
+ easeBack,
42
+ easeBackInOut,
43
+ easeBounceIn,
44
+ easeBounce,
45
+ easeBounceOut,
46
+ easeBounceInOut,
47
+ } from 'd3-ease';
48
+ import {
49
+ schemeCategory10,
50
+ schemeAccent,
51
+ schemeDark2,
52
+ schemePaired,
53
+ schemePastel1,
54
+ schemePastel2,
55
+ schemeSet1,
56
+ schemeSet2,
57
+ schemeSet3,
58
+ schemeTableau10,
59
+ } from 'd3-scale-chromatic';
60
+
61
+ const d3 = {
62
+ select,
63
+ selectAll,
64
+ scaleLinear,
65
+ scaleOrdinal,
66
+ scaleSqrt,
67
+ hierarchy,
68
+ partition,
69
+ arc,
70
+ path,
71
+ transition,
72
+ interpolate,
73
+ easeLinear,
74
+ easePolyIn,
75
+ easePolyOut,
76
+ easePoly,
77
+ easePolyInOut,
78
+ easeQuadIn,
79
+ easeQuadOut,
80
+ easeQuad,
81
+ easeQuadInOut,
82
+ easeCubicIn,
83
+ easeCubicOut,
84
+ easeCubic,
85
+ easeCubicInOut,
86
+ easeSinIn,
87
+ easeSinOut,
88
+ easeSin,
89
+ easeSinInOut,
90
+ easeExpIn,
91
+ easeExpOut,
92
+ easeExp,
93
+ easeExpInOut,
94
+ easeCircleIn,
95
+ easeCircleOut,
96
+ easeCircle,
97
+ easeCircleInOut,
98
+ easeElasticIn,
99
+ easeElastic,
100
+ easeElasticOut,
101
+ easeElasticInOut,
102
+ easeBackIn,
103
+ easeBackOut,
104
+ easeBack,
105
+ easeBackInOut,
106
+ easeBounceIn,
107
+ easeBounce,
108
+ easeBounceOut,
109
+ easeBounceInOut,
110
+ schemeCategory10,
111
+ schemeAccent,
112
+ schemeDark2,
113
+ schemePaired,
114
+ schemePastel1,
115
+ schemePastel2,
116
+ schemeSet1,
117
+ schemeSet2,
118
+ schemeSet3,
119
+ schemeTableau10,
120
+ };
121
+
122
+ import pluralize from 'pluralize';
123
+
124
+ /**
125
+ * D3 Sunburst
126
+ */
127
+ class d3sunburst extends d3chart {
128
+ constructor(selection, data, config) {
129
+ super(selection, data, config, {
130
+ margin: { top: 20, right: 20, bottom: 20, left: 20 },
131
+ key: '',
132
+ value: '',
133
+ color: {
134
+ key: false,
135
+ keys: false,
136
+ scheme: false,
137
+ current: '#1f77b4',
138
+ default: '#AAA',
139
+ axis: '#000',
140
+ },
141
+ tooltip: { label: false, suffix: false, suffixPlural: false },
142
+ transition: { duration: 350, ease: 'easeLinear' },
143
+ charSpace: 8,
144
+ });
145
+ }
146
+
147
+ /**
148
+ * Init chart
149
+ */
150
+ initChart() {
151
+ // Set up dimensions
152
+ this.getDimensions();
153
+ this.initChartFrame('sunburst');
154
+
155
+ if (this.cfg.tooltip.suffix && this.cfg.tooltip.suffixPlural) {
156
+ pluralize.addIrregularRule(this.cfg.tooltip.suffix, this.cfg.tooltip.suffixPlural);
157
+ }
158
+
159
+ // Center group
160
+ this.gcenter = this.g.append('g');
161
+
162
+ this.setChartDimension();
163
+ this.updateChart();
164
+ }
165
+
166
+ /**
167
+ * Set up chart dimensions (non depending on data)
168
+ */
169
+ setChartDimension() {
170
+ // SVG element
171
+ this.svg
172
+ .attr(
173
+ 'viewBox',
174
+ `0 0 ${this.cfg.width + this.cfg.margin.left + this.cfg.margin.right} ${this.cfg.height +
175
+ this.cfg.margin.top +
176
+ this.cfg.margin.bottom}`
177
+ )
178
+ .attr('width', this.cfg.width + this.cfg.margin.left + this.cfg.margin.right)
179
+ .attr('height', this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom);
180
+
181
+ // Center group
182
+ this.gcenter.attr('transform', `translate(${this.cfg.width / 2}, ${this.cfg.height / 2})`);
183
+ }
184
+
185
+ /**
186
+ * Bind data to main elements groups
187
+ */
188
+ bindData() {
189
+ const partition = data => {
190
+ const root = d3
191
+ .hierarchy(data)
192
+ .sum(d => d[this.cfg.value])
193
+ .sort((a, b) => b.name - a.name);
194
+ return d3.partition()(root);
195
+ };
196
+
197
+ this.hData = partition(this.data[0]).descendants();
198
+
199
+ this.itemg = this.gcenter
200
+ .selectAll('.chart__slice-group')
201
+ .data(this.hData, d => d.data[this.cfg.key]);
202
+
203
+ // Set transition
204
+ this.transition = d3
205
+ .transition('t')
206
+ .duration(this.cfg.transition.duration)
207
+ .ease(d3[this.cfg.transition.ease]);
208
+ }
209
+
210
+ /**
211
+ * Set up scales
212
+ */
213
+ setScales() {
214
+ this.radius = Math.min(this.cfg.width, this.cfg.height) / 2;
215
+
216
+ this.xScale = d3
217
+ .scaleLinear()
218
+ .range([0, 2 * Math.PI])
219
+ .clamp(true);
220
+
221
+ this.yScale = d3.scaleSqrt().range([this.radius * 0.1, this.radius]);
222
+
223
+ this.arc = d3
224
+ .arc()
225
+ .startAngle(d => this.xScale(d.x0))
226
+ .endAngle(d => this.xScale(d.x1))
227
+ .innerRadius(d => Math.max(0, this.yScale(d.y0)))
228
+ .outerRadius(d => Math.max(0, this.yScale(d.y1)));
229
+
230
+ // Set up color scheme
231
+ if (this.cfg.color.scheme) {
232
+ if (this.cfg.color.scheme instanceof Array === true) {
233
+ this.colorScale = d3.scaleOrdinal().range(this.cfg.color.scheme);
234
+ } else {
235
+ this.colorScale = d3.scaleOrdinal(d3[this.cfg.color.scheme]);
236
+ }
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Add new chart's elements
242
+ */
243
+ enterElements() {
244
+ const newg = this.itemg
245
+ .enter()
246
+ .append('g')
247
+ .attr('class', 'chart__slice-group chart__slice-group--sunburst clickable')
248
+ .on('click', d => {
249
+ window.event.stopPropagation();
250
+ this.focusOn(d);
251
+ });
252
+
253
+ // PATHS
254
+ newg
255
+ .append('path')
256
+ .attr('class', 'chart__slice chart__slice--sunburst')
257
+ .style('fill', d => this.colorElement(d.data))
258
+ .on('mouseover', d => {
259
+ const label = this.cfg.tooltip.suffixPlural
260
+ ? pluralize(this.cfg.tooltip.suffix, d.value)
261
+ : this.cfg.tooltip.suffix;
262
+ const text = this.cfg.tooltip.suffix
263
+ ? `<div>${d.data[this.cfg.key]}: ${d.value} ${label}</div>`
264
+ : `<div>${d.data[this.cfg.key]}: ${d.value}</div>`;
265
+ this.tooltip.html(text).classed('active', true);
266
+ })
267
+ .on('mouseout', () => {
268
+ this.tooltip.classed('active', false);
269
+ })
270
+ .on('mousemove', () => {
271
+ this.tooltip
272
+ .style('left', window.event['pageX'] - 28 + 'px')
273
+ .style('top', window.event['pageY'] - 40 + 'px');
274
+ })
275
+ .transition(this.transition)
276
+ .attrTween('d', d => {
277
+ const iy0 = d3.interpolate(0, d.y0);
278
+ const iy1 = d3.interpolate(d.y0, d.y1);
279
+ const ix0 = d3.interpolate(0, d.x0);
280
+ const ix1 = d3.interpolate(0, d.x1);
281
+ return t => {
282
+ d.y0 = iy0(t);
283
+ d.y1 = iy1(t);
284
+ d.x0 = ix0(t);
285
+ d.x1 = ix1(t);
286
+ return this.arc(d);
287
+ };
288
+ });
289
+
290
+ // LABELS
291
+ newg
292
+ .append('path')
293
+ .attr('class', 'chart__line--hidden')
294
+ .attr('id', (_, i) => `hiddenArc${i}`)
295
+ .attr('d', d => this.middleArcLine(d));
296
+
297
+ this.labels = newg
298
+ .append('text')
299
+ .attr('class', 'chart__labels')
300
+ .attr('display', 'none')
301
+ .attr('text-anchor', 'middle')
302
+ .attr('dy', 2)
303
+ .attr('font-size', '10px')
304
+ .style('opacity', 0);
305
+
306
+ this.labels
307
+ .append('textPath')
308
+ .attr('startOffset', '50%')
309
+ .attr('xlink:href', (_, i) => `#hiddenArc${i}`)
310
+ .text(d => d.data.name);
311
+ }
312
+
313
+ /**
314
+ * Update chart's elements based on data change
315
+ */
316
+ updateElements() {
317
+ this.itemg
318
+ .selectAll('.chart__slice')
319
+ .transition(this.transition)
320
+ .attrTween('d', d => {
321
+ const d2 = this.hData.filter(j => j.data.name === d.data.name)[0];
322
+ const iy0 = d3.interpolate(d.y0, d2.y0);
323
+ const iy1 = d3.interpolate(d.y1, d2.y1);
324
+ const ix0 = d3.interpolate(d.x0, d2.x0);
325
+ const ix1 = d3.interpolate(d.x1, d2.x1);
326
+ return t => {
327
+ d2.y0 = iy0(t);
328
+ d2.y1 = iy1(t);
329
+ d2.x0 = ix0(t);
330
+ d2.x1 = ix1(t);
331
+ return this.arc(d2);
332
+ };
333
+ })
334
+ .style('fill', d => this.colorElement(d.data));
335
+
336
+ this.labels
337
+ .attr('display', d => (this.textFits(d) ? null : 'none'))
338
+ .transition(this.transition)
339
+ .duration(this.cfg.transition.duration / 2)
340
+ .delay(this.cfg.transition.duration / 2)
341
+ .style('opacity', 1);
342
+ }
343
+
344
+ /**
345
+ * Remove chart's elements without data
346
+ */
347
+ exitElements() {
348
+ this.itemg
349
+ .exit()
350
+ .transition(this.transition)
351
+ .style('opacity', 0)
352
+ .remove();
353
+ }
354
+
355
+ /**
356
+ * Check if text fits in available space
357
+ */
358
+ textFits(d) {
359
+ if (!d.parent) return false;
360
+ const deltaAngle = this.xScale(d.x1) - this.xScale(d.x0);
361
+ const r = Math.max(0, (this.yScale(d.y0) + this.yScale(d.y1)) / 2);
362
+ return d.data[this.cfg.key].length * this.cfg.charSpace < r * deltaAngle;
363
+ }
364
+
365
+ /**
366
+ * Transition slice on focus
367
+ */
368
+ focusOn(d) {
369
+ const d2 = this.hData.filter(j => j.data.name === d.data.name)[0];
370
+ const transition = this.svg
371
+ .transition()
372
+ .duration(this.cfg.transition.duration)
373
+ .ease(d3[this.cfg.transition.ease])
374
+ .tween('scale', () => {
375
+ const xd = d3.interpolate(this.xScale.domain(), [d2.x0, d2.x1]);
376
+ const yd = d3.interpolate(this.yScale.domain(), [d2.y0, 1]);
377
+ return t => {
378
+ this.xScale.domain(xd(t));
379
+ this.yScale.domain(yd(t));
380
+ };
381
+ });
382
+
383
+ transition.selectAll('.chart__slice').attrTween('d', d => () => {
384
+ const d3 = this.hData.filter(j => j.data.name === d.data.name)[0];
385
+ return this.arc(d3);
386
+ });
387
+
388
+ transition.selectAll('.chart__line--hidden').attrTween('d', da => () => {
389
+ const d2 = this.hData.filter(j => j.data.name === da.data.name)[0];
390
+ return this.middleArcLine(d2);
391
+ });
392
+
393
+ transition.selectAll('.chart__labels').attrTween('display', da => () => {
394
+ const d2 = this.hData.filter(j => j.data.name === da.data.name)[0];
395
+ return this.textFits(d2) ? null : 'none';
396
+ });
397
+ }
398
+
399
+ middleArcLine(d) {
400
+ const halfPi = Math.PI / 2;
401
+ const angles = [this.xScale(d.x0) - halfPi, this.xScale(d.x1) - halfPi];
402
+ const r = Math.max(0, (this.yScale(d.y0) + this.yScale(d.y1)) / 2);
403
+
404
+ const middleAngle = (angles[1] + angles[0]) / 2;
405
+ const invertDirection = middleAngle > 0 && middleAngle < Math.PI;
406
+ if (invertDirection) {
407
+ angles.reverse();
408
+ }
409
+
410
+ const path = d3.path();
411
+ path.arc(0, 0, r, angles[0], angles[1], invertDirection);
412
+ return path.toString();
413
+ }
414
+ }
415
+
416
+ export default d3sunburst;