@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,23 @@
1
+ <script>
2
+ import D3Chart from '../d3.chart.vue';
3
+ import d3sunburst from './d3.sunburst';
4
+
5
+ export default {
6
+ name: 'D3Sunburst',
7
+ extends: D3Chart,
8
+ mounted() {
9
+ this.chart = new d3sunburst(
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
+ .chart__slice--sunburst {
20
+ stroke: white;
21
+ stroke-width: 1px;
22
+ }
23
+ </style>
@@ -0,0 +1,336 @@
1
+ import d3chart from '../d3.chart';
2
+ import { select, selectAll } from 'd3-selection';
3
+ import { scaleOrdinal, scaleLinear } from 'd3-scale';
4
+ import { min, max, extent } from 'd3-array';
5
+ import { transition } from 'd3-transition';
6
+ import * as cloud from 'd3-cloud';
7
+ import {
8
+ easeLinear,
9
+ easePolyIn,
10
+ easePolyOut,
11
+ easePoly,
12
+ easePolyInOut,
13
+ easeQuadIn,
14
+ easeQuadOut,
15
+ easeQuad,
16
+ easeQuadInOut,
17
+ easeCubicIn,
18
+ easeCubicOut,
19
+ easeCubic,
20
+ easeCubicInOut,
21
+ easeSinIn,
22
+ easeSinOut,
23
+ easeSin,
24
+ easeSinInOut,
25
+ easeExpIn,
26
+ easeExpOut,
27
+ easeExp,
28
+ easeExpInOut,
29
+ easeCircleIn,
30
+ easeCircleOut,
31
+ easeCircle,
32
+ easeCircleInOut,
33
+ easeElasticIn,
34
+ easeElastic,
35
+ easeElasticOut,
36
+ easeElasticInOut,
37
+ easeBackIn,
38
+ easeBackOut,
39
+ easeBack,
40
+ easeBackInOut,
41
+ easeBounceIn,
42
+ easeBounce,
43
+ easeBounceOut,
44
+ easeBounceInOut,
45
+ } from 'd3-ease';
46
+ import {
47
+ schemeCategory10,
48
+ schemeAccent,
49
+ schemeDark2,
50
+ schemePaired,
51
+ schemePastel1,
52
+ schemePastel2,
53
+ schemeSet1,
54
+ schemeSet2,
55
+ schemeSet3,
56
+ schemeTableau10,
57
+ } from 'd3-scale-chromatic';
58
+
59
+ const d3 = {
60
+ select,
61
+ selectAll,
62
+ scaleOrdinal,
63
+ scaleLinear,
64
+ min,
65
+ max,
66
+ extent,
67
+ transition,
68
+ cloud,
69
+ easeLinear,
70
+ easePolyIn,
71
+ easePolyOut,
72
+ easePoly,
73
+ easePolyInOut,
74
+ easeQuadIn,
75
+ easeQuadOut,
76
+ easeQuad,
77
+ easeQuadInOut,
78
+ easeCubicIn,
79
+ easeCubicOut,
80
+ easeCubic,
81
+ easeCubicInOut,
82
+ easeSinIn,
83
+ easeSinOut,
84
+ easeSin,
85
+ easeSinInOut,
86
+ easeExpIn,
87
+ easeExpOut,
88
+ easeExp,
89
+ easeExpInOut,
90
+ easeCircleIn,
91
+ easeCircleOut,
92
+ easeCircle,
93
+ easeCircleInOut,
94
+ easeElasticIn,
95
+ easeElastic,
96
+ easeElasticOut,
97
+ easeElasticInOut,
98
+ easeBackIn,
99
+ easeBackOut,
100
+ easeBack,
101
+ easeBackInOut,
102
+ easeBounceIn,
103
+ easeBounce,
104
+ easeBounceOut,
105
+ easeBounceInOut,
106
+ schemeCategory10,
107
+ schemeAccent,
108
+ schemeDark2,
109
+ schemePaired,
110
+ schemePastel1,
111
+ schemePastel2,
112
+ schemeSet1,
113
+ schemeSet2,
114
+ schemeSet3,
115
+ schemeTableau10,
116
+ };
117
+
118
+ import pluralize from 'pluralize';
119
+
120
+ /**
121
+ * D3 Words Cloud
122
+ */
123
+ class d3wordscloud extends d3chart {
124
+ constructor(selection, data, config) {
125
+ super(selection, data, config, {
126
+ margin: { top: 20, right: 20, bottom: 20, left: 20 },
127
+ key: 'word',
128
+ size: 'size',
129
+ value: 'value',
130
+ fontFamily: 'Arial',
131
+ angle: { steps: 2, start: 0, end: 90 },
132
+ color: {
133
+ key: false,
134
+ keys: false,
135
+ scheme: false,
136
+ current: '#1f77b4',
137
+ default: '#AAA',
138
+ axis: '#000',
139
+ },
140
+ tooltip: { label: false, suffix: false, suffixPlural: false },
141
+ transition: { duration: 500, ease: 'easeLinear' },
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Init chart
147
+ */
148
+ initChart() {
149
+ // Set up dimensions
150
+ this.getDimensions();
151
+ this.initChartFrame('wordscloud');
152
+
153
+ if (this.cfg.tooltip.suffix && this.cfg.tooltip.suffixPlural) {
154
+ pluralize.addIrregularRule(this.cfg.tooltip.suffix, this.cfg.tooltip.suffixPlural);
155
+ }
156
+
157
+ this.gcenter = this.g.append('g');
158
+ this.tData = [];
159
+
160
+ this.setChartDimension();
161
+ this.updateChart();
162
+ }
163
+
164
+ /**
165
+ * Compute data before operate
166
+ */
167
+ computeData() {
168
+ d3.cloud()
169
+ .size([this.cfg.width, this.cfg.height])
170
+ .words(
171
+ this.data.map(d => ({
172
+ text: d[this.cfg.key],
173
+ size: d[this.cfg.size],
174
+ value: d[this.cfg.value],
175
+ color: this.colorElement(d, 'text'),
176
+ }))
177
+ )
178
+ .rotate(() => this.wordsAngle(this.cfg.angle))
179
+ .font(this.cfg.fontFamily)
180
+ .fontSize(d => d.size * this.fontScale)
181
+ .on('end', d => {
182
+ if (this.data.length !== d.length && this.fontScale > 0) {
183
+ this.fontScale = this.fontScale - 0.1;
184
+ this.computeData();
185
+ } else {
186
+ this.renderEnd = true;
187
+ this.tData = d;
188
+ }
189
+ })
190
+ .start();
191
+ }
192
+
193
+ /**
194
+ * Set up chart dimensions (non depending on data)
195
+ */
196
+ setChartDimension() {
197
+ // Resize SVG element
198
+ this.svg
199
+ .attr(
200
+ 'viewBox',
201
+ `0 0 ${this.cfg.width + this.cfg.margin.left + this.cfg.margin.right} ${this.cfg.height +
202
+ this.cfg.margin.top +
203
+ this.cfg.margin.bottom}`
204
+ )
205
+ .attr('width', this.cfg.width + this.cfg.margin.left + this.cfg.margin.right)
206
+ .attr('height', this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom);
207
+
208
+ // Center element
209
+ this.gcenter.attr('transform', `translate(${this.cfg.width / 2}, ${this.cfg.height / 2})`);
210
+
211
+ // Font reduction scale
212
+ this.fontScale = 1;
213
+ this.renderEnd = false;
214
+ }
215
+
216
+ /**
217
+ * Bind data to main elements groups
218
+ */
219
+ bindData() {
220
+ // Set transition
221
+ this.transition = d3
222
+ .transition('t')
223
+ .duration(this.cfg.transition.duration)
224
+ .ease(d3[this.cfg.transition.ease]);
225
+
226
+ // Word group selection data
227
+ this.wordgroup = this.gcenter.selectAll('.chart__word-group').data(this.tData, d => d.text);
228
+ }
229
+
230
+ /**
231
+ * Set up scales
232
+ */
233
+ setScales() {
234
+ if (this.cfg.color.scheme instanceof Array === true) {
235
+ this.colorScale = d3.scaleOrdinal().range(this.cfg.color.scheme);
236
+ } else if (typeof this.cfg.color.scheme === 'string') {
237
+ this.colorScale = d3.scaleOrdinal(d3[this.cfg.color.scheme]);
238
+ }
239
+ }
240
+
241
+ /**
242
+ * Add new chart's elements
243
+ */
244
+ enterElements() {
245
+ if (!this.renderEnd) return;
246
+
247
+ // Elements to add
248
+ const newwords = this.wordgroup
249
+ .enter()
250
+ .append('g')
251
+ .attr('class', 'chart__word-group chart__word-group--wordscloud');
252
+
253
+ newwords
254
+ .append('text')
255
+ .style('font-size', d => d.size + 'px')
256
+ .style('font-family', d => d.font)
257
+ .attr('text-anchor', 'middle')
258
+ .attr('fill', d => d.color)
259
+ .attr('transform', d => `translate(${[d.x, d.y]})rotate(${d.rotate})`)
260
+ .on('mouseover', d => {
261
+ const label = this.cfg.tooltip.suffixPlural
262
+ ? pluralize(this.cfg.tooltip.suffix, d.value)
263
+ : this.cfg.tooltip.suffix;
264
+ const text = this.cfg.tooltip.suffix
265
+ ? `<div>${d.text}: ${d.value} ${label}</div>`
266
+ : `<div>${d.text}: ${d.value}</div>`;
267
+ this.tooltip.html(text).classed('active', true);
268
+ })
269
+ .on('mouseout', () => {
270
+ this.tooltip.classed('active', false);
271
+ })
272
+ .on('mousemove', () => {
273
+ this.tooltip
274
+ .style('left', window.event['pageX'] - 28 + 'px')
275
+ .style('top', window.event['pageY'] - 40 + 'px');
276
+ })
277
+ .text(d => d.text);
278
+ }
279
+
280
+ /**
281
+ * Update chart's elements based on data change
282
+ */
283
+ updateElements() {
284
+ if (!this.renderEnd) return;
285
+
286
+ this.wordgroup
287
+ .selectAll('text')
288
+ .data(this.tData, d => d.text)
289
+ .transition(this.transition)
290
+ .attr('fill', d => this.colorElement(d, 'text'))
291
+ .style('font-size', d => d.size + 'px')
292
+ .attr('transform', d => `translate(${[d.x, d.y]})rotate(${d.rotate})`);
293
+ }
294
+
295
+ /**
296
+ * Remove chart's elements without data
297
+ */
298
+ exitElements() {
299
+ if (!this.renderEnd) return;
300
+
301
+ this.wordgroup
302
+ .exit()
303
+ .transition(this.transition)
304
+ .style('opacity', 0)
305
+ .remove();
306
+ }
307
+
308
+ /**
309
+ * Word's angle
310
+ */
311
+ wordsAngle() {
312
+ if (typeof this.cfg.angle === 'number') {
313
+ // Config angle is fixed number
314
+ return this.cfg.angle;
315
+ } else if (typeof this.cfg.angle === 'object') {
316
+ if (this.cfg.angle instanceof Array === true) {
317
+ // Config angle is custom array
318
+ const idx = this.randomInt(0, this.cfg.angle.length - 1);
319
+ return this.cfg.angle[idx];
320
+ } else {
321
+ // Config angle is custom object
322
+ const angle = (this.cfg.angle.end - this.cfg.angle.start) / (this.cfg.angle.steps - 1);
323
+ return this.cfg.angle.start + this.randomInt(0, this.cfg.angle.steps) * angle;
324
+ }
325
+ }
326
+ return 0;
327
+ }
328
+
329
+ randomInt(min, max) {
330
+ const i = Math.ceil(min);
331
+ const j = Math.floor(max);
332
+ return Math.floor(Math.random() * (j - i + 1)) + i;
333
+ }
334
+ }
335
+
336
+ export default d3wordscloud;
@@ -0,0 +1,16 @@
1
+ <script>
2
+ import D3Chart from '../d3.chart.vue';
3
+ import d3wordscloud from './d3.wordscloud';
4
+
5
+ export default {
6
+ name: 'D3WordsCloud',
7
+ extends: D3Chart,
8
+ mounted() {
9
+ this.chart = new d3wordscloud(
10
+ this.$refs.chart,
11
+ JSON.parse(JSON.stringify(this.datum)),
12
+ this.config,
13
+ );
14
+ },
15
+ };
16
+ </script>
@@ -0,0 +1,242 @@
1
+ /* eslint-disable no-console */
2
+ import {select} from 'd3-selection';
3
+
4
+ const d3 = {select};
5
+
6
+ /**
7
+ * D3 Chart Base
8
+ */
9
+ class d3chart {
10
+ constructor(selection, data, config, cfg) {
11
+ this.selection = d3.select(selection);
12
+ this.data = data;
13
+ this.cfg = cfg;
14
+ this._setConfig(config);
15
+
16
+ // Resize listener
17
+ this.onResize = () => {this.resizeChart();};
18
+ window.addEventListener("resize", this.onResize);
19
+
20
+ this.initChart();
21
+ }
22
+
23
+ _setConfig(config){
24
+ // Set up configuration
25
+ Object.keys(config).forEach(key=> {
26
+ if(config[key] instanceof Object && config[key] instanceof Array === false){
27
+ Object.keys(config[key]).forEach(sk=> {
28
+ this.cfg[key][sk] = config[key][sk];
29
+ });
30
+ } else this.cfg[key] = config[key];
31
+ });
32
+ }
33
+
34
+ /**
35
+ * Init chart
36
+ */
37
+ initChart(){
38
+ console.error('d3chart.initChart not implemented');
39
+ }
40
+
41
+ /**
42
+ * Resize chart pipe
43
+ */
44
+ setScales(){
45
+ console.error('d3chart.setScales not implemented');
46
+ }
47
+
48
+ /**
49
+ * Set chart dimensional sizes
50
+ */
51
+ setChartDimension(){
52
+ console.error('d3chart.setChartDimension not implemented');
53
+ }
54
+
55
+ /**
56
+ * Bind data to main elements groups
57
+ */
58
+ bindData(){
59
+ console.error('d3.chart.bindData not implemented');
60
+ }
61
+
62
+ /**
63
+ * Add new chart's elements
64
+ */
65
+ enterElements(){
66
+ console.error('d3.chart.enterElements not implemented');
67
+ }
68
+
69
+ /**
70
+ * Update chart's elements based on data change
71
+ */
72
+ updateElements(){
73
+ console.error('d3.chart.updateElements not implemented');
74
+ }
75
+
76
+ /**
77
+ * Remove chart's elements without data
78
+ */
79
+ exitElements(){
80
+ console.error('d3.chart.exitElements not implemented');
81
+ }
82
+
83
+
84
+ /**
85
+ * Set up chart dimensions
86
+ */
87
+ getDimensions(){
88
+ this.cfg.width = parseInt(this.selection.node().offsetWidth) - this.cfg.margin.left - this.cfg.margin.right;
89
+ this.cfg.height = parseInt(this.selection.node().offsetHeight)- this.cfg.margin.top - this.cfg.margin.bottom;
90
+ }
91
+
92
+ /**
93
+ * Returns chart's data
94
+ */
95
+ getData(){
96
+ return this.data;
97
+ }
98
+
99
+ /**
100
+ * Add new data elements
101
+ */
102
+ enterData(data){
103
+ this.data = this.data.concat(data);
104
+ this.setScales();
105
+ this.updateChart();
106
+ }
107
+
108
+ /**
109
+ * Update existing data elements
110
+ */
111
+ updateData(data){
112
+ this.data = [...data];
113
+ this.setScales();
114
+ this.updateChart();
115
+ }
116
+
117
+ /**
118
+ * Compute data before operate
119
+ */
120
+ computeData(){
121
+ }
122
+
123
+ /**
124
+ * Remove data elements
125
+ */
126
+ exitData(filter){
127
+ this.data.forEach((d,i) => {
128
+ let c = 0;
129
+ Object.keys(filter).forEach(key => {
130
+ if(filter[key] == d[key]) c++;
131
+ });
132
+ if(c == Object.keys(filter).length){
133
+ this.data.splice(i,1);
134
+ }
135
+ });
136
+ this.setScales();
137
+ this.updateChart();
138
+ }
139
+
140
+ /**
141
+ * Init chart commons elements (div > svg > g; tooltip)
142
+ */
143
+ initChartFrame(classname='undefined'){
144
+ // Wrapper div
145
+ this.wrap = this.selection.append('div')
146
+ .attr("class", "chart__wrap chart__wrap--"+classname);
147
+
148
+ // SVG element
149
+ this.svg = this.wrap.append('svg')
150
+ .attr("class", "chart chart--"+classname);
151
+
152
+ // General group for margin convention
153
+ this.g = this.svg.append("g")
154
+ .attr("class", "chart__margin-wrap chart__margin-wrap--"+classname)
155
+ .attr("transform", `translate(${this.cfg.margin.left},${this.cfg.margin.top})`);
156
+
157
+ // Tooltip
158
+ this.selection.selectAll('.chart__tooltip').remove();
159
+ this.tooltip = this.wrap
160
+ .append('div')
161
+ .attr('class', "chart__tooltip chart__tooltip--"+classname);
162
+ }
163
+
164
+ /**
165
+ * Compute element color
166
+ */
167
+ colorElement(d, key = undefined) {
168
+ key = key ? key : this.cfg.key;
169
+
170
+ // if key is set, return own object color key
171
+ if(this.cfg.color.key) return d[this.cfg.color.key];
172
+
173
+ // base color is default one if current key is set, else current one
174
+ let baseColor = this.cfg.currentKey
175
+ ? this.cfg.color.default
176
+ : this.cfg.color.current;
177
+
178
+ // if scheme is set, base color is color scheme
179
+ if(this.cfg.color.scheme){
180
+ baseColor = this.colorScale(d[key]);
181
+ }
182
+
183
+ // if keys is an object, base color is color key if exists
184
+ if(this.cfg.color.keys
185
+ && this.cfg.color.keys instanceof Object
186
+ && this.cfg.color.keys instanceof Array === false) {
187
+ if (typeof this.cfg.color.keys[key] == 'string') {
188
+ baseColor = this.cfg.color.keys[key];
189
+ } else if (typeof this.cfg.color.keys[d[key]] == 'string') {
190
+ baseColor = this.cfg.color.keys[d[key]];
191
+ }
192
+ }
193
+
194
+ // if current key is set and key is current, base color is current
195
+ if(this.cfg.currentKey && d[this.cfg.key] === this.cfg.currentKey){
196
+ baseColor = this.cfg.color.current;
197
+ }
198
+
199
+ return baseColor;
200
+ }
201
+
202
+ /**
203
+ * Update chart methods
204
+ */
205
+ updateChart(){
206
+ this.computeData();
207
+ this.bindData();
208
+ this.setScales();
209
+ this.enterElements();
210
+ this.updateElements();
211
+ this.exitElements();
212
+ }
213
+
214
+ /**
215
+ * Resize chart methods
216
+ */
217
+ resizeChart(){
218
+ this.getDimensions();
219
+ //this.setScales();
220
+ this.setChartDimension();
221
+ this.updateChart();
222
+ }
223
+
224
+ /**
225
+ * Update chart configuration
226
+ */
227
+ updateConfig(config){
228
+ this._setConfig(config);
229
+ this.resizeChart();
230
+ }
231
+
232
+ /**
233
+ * Destroy chart methods
234
+ */
235
+ destroyChart(){
236
+ window.removeEventListener("resize", this.onResize);
237
+ }
238
+
239
+ }
240
+
241
+
242
+ export default d3chart;
@@ -0,0 +1,80 @@
1
+ <template>
2
+ <div class="chart__wrapper">
3
+ <div v-if="title" class="chart__title">{{ title }}</div>
4
+ <div ref="chart" :style="{ height: `${this.height}px` }"></div>
5
+ <div v-if="source" class="chart__source">{{ source }}</div>
6
+ <div v-if="download" class="chart__source">
7
+ <span @click="downloadSVG">{{ download }}</span>
8
+ </div>
9
+ </div>
10
+ </template>
11
+
12
+ <script>
13
+ import { saveSvgAsPng } from 'save-svg-as-png';
14
+
15
+ export default {
16
+ name: 'D3Chart',
17
+ data() {
18
+ return {
19
+ chart: {},
20
+ };
21
+ },
22
+ props: {
23
+ config: {
24
+ type: Object,
25
+ required: true,
26
+ default: () => ({}),
27
+ },
28
+ datum: {
29
+ type: Array,
30
+ required: true,
31
+ default: () => ([]),
32
+ },
33
+ title: {
34
+ type: String,
35
+ default: '',
36
+ },
37
+ source: {
38
+ type: String,
39
+ default: '',
40
+ },
41
+ height: {
42
+ type: Number,
43
+ default: 300,
44
+ },
45
+ download: {
46
+ type: String,
47
+ default: 'Descargar',
48
+ },
49
+ },
50
+ methods: {
51
+ downloadSVG() {
52
+ const node = this.$refs.chart.getElementsByClassName('chart')[0];
53
+ saveSvgAsPng(node, 'chart.png', {
54
+ backgroundColor: '#FFF',
55
+ });
56
+ },
57
+ },
58
+ watch: {
59
+ config: {
60
+ handler(val) {
61
+ this.chart.updateConfig(val);
62
+ },
63
+ deep: true,
64
+ },
65
+ datum(vals) {
66
+ this.chart.updateData([ ...vals ]);
67
+ },
68
+ height(val) {
69
+ setTimeout(() =>{ this.chart.resizeChart(); }, 10);
70
+ },
71
+ },
72
+ beforeDestroy() {
73
+ this.chart.destroyChart();
74
+ },
75
+ };
76
+ </script>
77
+
78
+ <style lang="scss">
79
+ @import './styles';
80
+ </style>