@politicalwatch/tipi-uikit 1.0.0-dev

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 (238) 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/.storybook/main.js +19 -0
  7. package/.storybook/preview.js +17 -0
  8. package/.stylelintrc +24 -0
  9. package/README.md +21 -0
  10. package/dist/bng-890acecf.mjs +29 -0
  11. package/dist/coalicioncanaria-6f4c1db4.mjs +18 -0
  12. package/dist/compromis-5ea8e904.mjs +23 -0
  13. package/dist/cs-de2f22f8.mjs +21 -0
  14. package/dist/cup-83f9d201.mjs +25 -0
  15. package/dist/ehbildu-23275d48.mjs +25 -0
  16. package/dist/encomu-90f0f0ee.mjs +23 -0
  17. package/dist/erc-83cebd9f.mjs +33 -0
  18. package/dist/foroasturias-c86ed9f1.mjs +27 -0
  19. package/dist/iu-0d57eb1a.mjs +23 -0
  20. package/dist/jxcat-69202004.mjs +23 -0
  21. package/dist/maspais-a61a53ef.mjs +21 -0
  22. package/dist/nuevacanaria-559edad7.mjs +21 -0
  23. package/dist/pdecat-0ec3db5a.mjs +21 -0
  24. package/dist/pnv-7f6f4b34.mjs +29 -0
  25. package/dist/podemos-de793077.mjs +21 -0
  26. package/dist/pp-13bed712.mjs +25 -0
  27. package/dist/prc-8a3d42c1.mjs +29 -0
  28. package/dist/psoe-97af3ccf.mjs +21 -0
  29. package/dist/style.css +1 -0
  30. package/dist/sumar-7dfecef0.mjs +29 -0
  31. package/dist/teruelexiste-7d4a17ab.mjs +21 -0
  32. package/dist/tipi-uikit.es.js +26196 -0
  33. package/dist/tipi-uikit.umd.js +134 -0
  34. package/dist/unidaspodemos-43ae78cc.mjs +18 -0
  35. package/dist/upn-e7eb332f.mjs +35 -0
  36. package/dist/vox-64380b40.mjs +25 -0
  37. package/package.json +57 -0
  38. package/src/assets/.gitkeep +0 -0
  39. package/src/assets/birthday_bg.png +0 -0
  40. package/src/assets/index.js +64 -0
  41. package/src/assets/party_logos/bildu.png +0 -0
  42. package/src/assets/party_logos/bng.png +0 -0
  43. package/src/assets/party_logos/ca.jpeg +0 -0
  44. package/src/assets/party_logos/ciudadanos.png +0 -0
  45. package/src/assets/party_logos/compromis.jpg +0 -0
  46. package/src/assets/party_logos/cup.png +0 -0
  47. package/src/assets/party_logos/eajpnv.png +0 -0
  48. package/src/assets/party_logos/encomupodem.jpg +0 -0
  49. package/src/assets/party_logos/erc.png +0 -0
  50. package/src/assets/party_logos/fac.png +0 -0
  51. package/src/assets/party_logos/icon/bng.svg +4 -0
  52. package/src/assets/party_logos/icon/coalicioncanaria.svg +7 -0
  53. package/src/assets/party_logos/icon/compromis.svg +3 -0
  54. package/src/assets/party_logos/icon/cs.svg +3 -0
  55. package/src/assets/party_logos/icon/cup.svg +4 -0
  56. package/src/assets/party_logos/icon/ehbildu.svg +4 -0
  57. package/src/assets/party_logos/icon/encomu.svg +3 -0
  58. package/src/assets/party_logos/icon/erc.svg +5 -0
  59. package/src/assets/party_logos/icon/foroasturias.svg +4 -0
  60. package/src/assets/party_logos/icon/iu.svg +3 -0
  61. package/src/assets/party_logos/icon/jxcat.svg +3 -0
  62. package/src/assets/party_logos/icon/maspais.svg +3 -0
  63. package/src/assets/party_logos/icon/nuevacanaria.svg +3 -0
  64. package/src/assets/party_logos/icon/party-error.svg +3 -0
  65. package/src/assets/party_logos/icon/pdecat.svg +3 -0
  66. package/src/assets/party_logos/icon/pnv.svg +4 -0
  67. package/src/assets/party_logos/icon/podemos.svg +3 -0
  68. package/src/assets/party_logos/icon/pp.svg +4 -0
  69. package/src/assets/party_logos/icon/prc.svg +5 -0
  70. package/src/assets/party_logos/icon/psoe.svg +3 -0
  71. package/src/assets/party_logos/icon/sumar.svg +73 -0
  72. package/src/assets/party_logos/icon/teruelexiste.svg +3 -0
  73. package/src/assets/party_logos/icon/unidaspodemos.svg +27 -0
  74. package/src/assets/party_logos/icon/upn.svg +5 -0
  75. package/src/assets/party_logos/icon/vox.svg +4 -0
  76. package/src/assets/party_logos/iu.png +0 -0
  77. package/src/assets/party_logos/jxcat.png +0 -0
  78. package/src/assets/party_logos/mas-pais.jpg +0 -0
  79. package/src/assets/party_logos/nueva_canarias.png +0 -0
  80. package/src/assets/party_logos/pdecat.png +0 -0
  81. package/src/assets/party_logos/podemos.jpg +0 -0
  82. package/src/assets/party_logos/pp.png +0 -0
  83. package/src/assets/party_logos/prc.png +0 -0
  84. package/src/assets/party_logos/psc.png +0 -0
  85. package/src/assets/party_logos/pse-ee.png +0 -0
  86. package/src/assets/party_logos/psoe.png +0 -0
  87. package/src/assets/party_logos/teruelexiste.svg +4 -0
  88. package/src/assets/party_logos/unidaspodemos.png +0 -0
  89. package/src/assets/party_logos/upn.jpg +0 -0
  90. package/src/assets/party_logos/vox.png +0 -0
  91. package/src/assets/svg/icon-advanced.svg +3 -0
  92. package/src/assets/svg/icon-bell.svg +3 -0
  93. package/src/assets/svg/icon-birthday.svg +18 -0
  94. package/src/assets/svg/icon-building.svg +3 -0
  95. package/src/assets/svg/icon-calendar.svg +4 -0
  96. package/src/assets/svg/icon-chevron-down.svg +3 -0
  97. package/src/assets/svg/icon-chevron-left.svg +3 -0
  98. package/src/assets/svg/icon-chevron-right.svg +3 -0
  99. package/src/assets/svg/icon-chevron-up.svg +3 -0
  100. package/src/assets/svg/icon-clock.svg +4 -0
  101. package/src/assets/svg/icon-close-menu.svg +3 -0
  102. package/src/assets/svg/icon-close.svg +3 -0
  103. package/src/assets/svg/icon-conversation.svg +3 -0
  104. package/src/assets/svg/icon-denied.svg +3 -0
  105. package/src/assets/svg/icon-document.svg +4 -0
  106. package/src/assets/svg/icon-download.svg +3 -0
  107. package/src/assets/svg/icon-error.svg +3 -0
  108. package/src/assets/svg/icon-footprint.svg +3 -0
  109. package/src/assets/svg/icon-github.svg +9 -0
  110. package/src/assets/svg/icon-hamburger.svg +1 -0
  111. package/src/assets/svg/icon-info.svg +3 -0
  112. package/src/assets/svg/icon-less.svg +3 -0
  113. package/src/assets/svg/icon-location.svg +3 -0
  114. package/src/assets/svg/icon-logout.svg +13 -0
  115. package/src/assets/svg/icon-mail.svg +4 -0
  116. package/src/assets/svg/icon-more.svg +5 -0
  117. package/src/assets/svg/icon-open-blank.svg +3 -0
  118. package/src/assets/svg/icon-plus.svg +3 -0
  119. package/src/assets/svg/icon-search.svg +4 -0
  120. package/src/assets/svg/icon-success.svg +3 -0
  121. package/src/assets/svg/icon-twitter.svg +3 -0
  122. package/src/assets/svg/icon-upload.svg +3 -0
  123. package/src/assets/svg/icons.js +67 -0
  124. package/src/components/CSVDownload/CSVDownload.vue +146 -0
  125. package/src/components/Charts/Barchart/d3.barchart.js +484 -0
  126. package/src/components/Charts/Barchart/d3.barchart.vue +16 -0
  127. package/src/components/Charts/Sliceschart/d3.sliceschart.js +372 -0
  128. package/src/components/Charts/Sliceschart/d3.sliceschart.vue +20 -0
  129. package/src/components/Charts/Sunburst/d3.sunburst.js +412 -0
  130. package/src/components/Charts/Sunburst/d3.sunburst.vue +23 -0
  131. package/src/components/Charts/WordsCloud/d3.wordscloud.js +332 -0
  132. package/src/components/Charts/WordsCloud/d3.wordscloud.vue +16 -0
  133. package/src/components/Charts/d3.chart.js +242 -0
  134. package/src/components/Charts/d3.chart.vue +81 -0
  135. package/src/components/Charts/styles.scss +92 -0
  136. package/src/components/CongressLink/CongressLink.vue +19 -0
  137. package/src/components/Deputy/Deputy.vue +106 -0
  138. package/src/components/Header/Header.vue +19 -0
  139. package/src/components/Icon/Icon.vue +30 -0
  140. package/src/components/InitiativeCard/InitiativeCard.vue +94 -0
  141. package/src/components/InitiativeMeta/InitiativeMeta.vue +79 -0
  142. package/src/components/Loader/Loader.vue +23 -0
  143. package/src/components/Message/Message.vue +27 -0
  144. package/src/components/Navbar/Navbar.vue +117 -0
  145. package/src/components/Neuron/Neuron.vue +211 -0
  146. package/src/components/PartyLogo/PartyLogo.vue +58 -0
  147. package/src/components/PartyLogo/PartyLogoIcon.vue +239 -0
  148. package/src/components/Results/Results.vue +114 -0
  149. package/src/components/Splash/Splash.vue +43 -0
  150. package/src/components/Text/Text.vue +69 -0
  151. package/src/components/TipiCharts/TipiBarchart.vue +204 -0
  152. package/src/components/TopicCard/TopicCard.vue +52 -0
  153. package/src/components/TopicLink/TopicLink.vue +37 -0
  154. package/src/components/TopicPill/TopicPill.vue +67 -0
  155. package/src/components/Topics/Topics.vue +79 -0
  156. package/src/components/TwoCircles/TwoCircles.vue +134 -0
  157. package/src/components/index.js +57 -0
  158. package/src/main.js +11 -0
  159. package/src/stories/CSVDownload.stories.js +245 -0
  160. package/src/stories/CongressLink.stories.js +21 -0
  161. package/src/stories/D3BarChart.stories.js +40 -0
  162. package/src/stories/D3SlicesChart.stories.js +64 -0
  163. package/src/stories/D3Sunburst.stories.js +293 -0
  164. package/src/stories/D3WordsCloud.stories.js +64 -0
  165. package/src/stories/Deputy.stories.js +168 -0
  166. package/src/stories/Header.stories.js +26 -0
  167. package/src/stories/Icon.stories.js +25 -0
  168. package/src/stories/InitiativeCard.stories.js +58 -0
  169. package/src/stories/InitiativeMeta.stories.js +37 -0
  170. package/src/stories/Loader.stories.js +25 -0
  171. package/src/stories/Message.stories.js +49 -0
  172. package/src/stories/Navbar.stories.js +85 -0
  173. package/src/stories/Neuron.stories.js +210 -0
  174. package/src/stories/PartyLogo.stories.js +21 -0
  175. package/src/stories/PartyLogoIcon.stories.js +21 -0
  176. package/src/stories/Results.stories.js +350 -0
  177. package/src/stories/Splash.stories.js +25 -0
  178. package/src/stories/Text.stories.js +46 -0
  179. package/src/stories/TopicCard.stories.js +775 -0
  180. package/src/stories/TopicLink.stories.js +778 -0
  181. package/src/stories/TopicPill.stories.js +46 -0
  182. package/src/stories/Topics.stories.js +80 -0
  183. package/src/stories/TwoCircles.stories.js +144 -0
  184. package/src/stories/mocks.js +153 -0
  185. package/src/styles/01_settings/_settings__breakpoints.scss +28 -0
  186. package/src/styles/01_settings/_settings__colors.scss +30 -0
  187. package/src/styles/01_settings/_settings__fonts.scss +3 -0
  188. package/src/styles/01_settings/_settings__icons.scss +0 -0
  189. package/src/styles/01_settings/_settings__variables.scss +45 -0
  190. package/src/styles/02_tools/_tools__functions.scss +19 -0
  191. package/src/styles/02_tools/_tools__mixins.scss +321 -0
  192. package/src/styles/03_generic/_generic__box-sizing.scss +12 -0
  193. package/src/styles/03_generic/_generic__global.scss +20 -0
  194. package/src/styles/03_generic/_generic__normalize.scss +371 -0
  195. package/src/styles/04_base/_base__anchors.scss +8 -0
  196. package/src/styles/04_base/_base__blockquotes.scss +10 -0
  197. package/src/styles/04_base/_base__forms.scss +32 -0
  198. package/src/styles/04_base/_base__headings.scss +23 -0
  199. package/src/styles/04_base/_base__images.scss +4 -0
  200. package/src/styles/04_base/_base__lists.scss +66 -0
  201. package/src/styles/04_base/_base__paragraphs.scss +11 -0
  202. package/src/styles/04_base/_base__tables.scss +95 -0
  203. package/src/styles/05_objects/_objects__columns.scss +29 -0
  204. package/src/styles/05_objects/_objects__container.scss +19 -0
  205. package/src/styles/05_objects/_objects__grid.scss +57 -0
  206. package/src/styles/05_objects/_objects__masonry.scss +11 -0
  207. package/src/styles/05_objects/_objects__media.scss +24 -0
  208. package/src/styles/05_objects/_objects__section.scss +21 -0
  209. package/src/styles/06_components/_components__button.scss +178 -0
  210. package/src/styles/06_components/_components__congress-link.scss +12 -0
  211. package/src/styles/06_components/_components__decorator.scss +10 -0
  212. package/src/styles/06_components/_components__deputy.scss +177 -0
  213. package/src/styles/06_components/_components__disclaimer.scss +24 -0
  214. package/src/styles/06_components/_components__forms.scss +454 -0
  215. package/src/styles/06_components/_components__header.scss +37 -0
  216. package/src/styles/06_components/_components__icon.scss +27 -0
  217. package/src/styles/06_components/_components__initiative-card.scss +49 -0
  218. package/src/styles/06_components/_components__initiative-meta.scss +91 -0
  219. package/src/styles/06_components/_components__loader.scss +125 -0
  220. package/src/styles/06_components/_components__menu.scss +110 -0
  221. package/src/styles/06_components/_components__message.scss +66 -0
  222. package/src/styles/06_components/_components__navbar.scss +114 -0
  223. package/src/styles/06_components/_components__party-logo.scss +21 -0
  224. package/src/styles/06_components/_components__splash.scss +29 -0
  225. package/src/styles/06_components/_components__text.scss +69 -0
  226. package/src/styles/06_components/_components__topic-card.scss +40 -0
  227. package/src/styles/06_components/_components__topic-link.scss +37 -0
  228. package/src/styles/06_components/_components__topics.scss +107 -0
  229. package/src/styles/07_trumps/_trumps__borders.scss +1 -0
  230. package/src/styles/07_trumps/_trumps__colors.scss +3 -0
  231. package/src/styles/07_trumps/_trumps__grid.scss +1 -0
  232. package/src/styles/07_trumps/_trumps__helpers.scss +17 -0
  233. package/src/styles/07_trumps/_trumps__texts.scss +39 -0
  234. package/src/styles/07_trumps/_trumps__visibility.scss +56 -0
  235. package/src/styles/main.scss +54 -0
  236. package/src/utils/index.js +26 -0
  237. package/svgo.config.js +16 -0
  238. package/vite.config.js +54 -0
@@ -0,0 +1,332 @@
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 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
+ 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} ${
202
+ this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom
203
+ }`
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', (event, 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', (event) => {
273
+ this.tooltip
274
+ .style('left', event['pageX'] - 28 + 'px')
275
+ .style('top', 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.exit().transition(this.transition).style('opacity', 0).remove();
302
+ }
303
+
304
+ /**
305
+ * Word's angle
306
+ */
307
+ wordsAngle() {
308
+ if (typeof this.cfg.angle === 'number') {
309
+ // Config angle is fixed number
310
+ return this.cfg.angle;
311
+ } else if (typeof this.cfg.angle === 'object') {
312
+ if (this.cfg.angle instanceof Array === true) {
313
+ // Config angle is custom array
314
+ const idx = this.randomInt(0, this.cfg.angle.length - 1);
315
+ return this.cfg.angle[idx];
316
+ } else {
317
+ // Config angle is custom object
318
+ const angle = (this.cfg.angle.end - this.cfg.angle.start) / (this.cfg.angle.steps - 1);
319
+ return this.cfg.angle.start + this.randomInt(0, this.cfg.angle.steps) * angle;
320
+ }
321
+ }
322
+ return 0;
323
+ }
324
+
325
+ randomInt(min, max) {
326
+ const i = Math.ceil(min);
327
+ const j = Math.floor(max);
328
+ return Math.floor(Math.random() * (j - i + 1)) + i;
329
+ }
330
+ }
331
+
332
+ 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,81 @@
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
+ },
48
+ },
49
+ methods: {
50
+ downloadSVG() {
51
+ const node = this.$refs.chart.getElementsByClassName('chart')[0];
52
+ saveSvgAsPng(node, 'chart.png', {
53
+ backgroundColor: '#FFF',
54
+ });
55
+ },
56
+ },
57
+ watch: {
58
+ config: {
59
+ handler(val) {
60
+ this.chart.updateConfig(val);
61
+ },
62
+ deep: true,
63
+ },
64
+ datum(vals) {
65
+ this.chart.updateData([...vals]);
66
+ },
67
+ height(val) {
68
+ setTimeout(() => {
69
+ this.chart.resizeChart();
70
+ }, 10);
71
+ },
72
+ },
73
+ beforeDestroy() {
74
+ this.chart.destroyChart();
75
+ },
76
+ };
77
+ </script>
78
+
79
+ <style lang="scss">
80
+ @import './styles';
81
+ </style>