@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,484 @@
1
+ import d3chart from '../d3.chart';
2
+ import { select, selectAll } from 'd3-selection';
3
+ import { scaleBand, scaleLinear, scaleOrdinal } from 'd3-scale';
4
+ import { max } from 'd3-array';
5
+ import { transition } from 'd3-transition';
6
+ import { axisBottom, axisLeft } from 'd3-axis';
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
+ scaleBand,
63
+ scaleLinear,
64
+ scaleOrdinal,
65
+ max,
66
+ transition,
67
+ axisBottom,
68
+ axisLeft,
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
+ const REFERENCE = 'Texto de referencia';
121
+ const SCANNED = 'Texto escaneado';
122
+
123
+ /**
124
+ * D3 Bar Chart
125
+ */
126
+ class d3barchart extends d3chart {
127
+ constructor(selection, data, config) {
128
+ super(selection, data, config, {
129
+ margin: { top: 10, right: 30, bottom: 20, left: 40 },
130
+ key: 'key',
131
+ currentKey: false,
132
+ values: [],
133
+ orientation: 'vertical',
134
+ labelRotation: 0,
135
+ color: {
136
+ key: false,
137
+ keys: false,
138
+ scheme: false,
139
+ current: '#1f77b4',
140
+ default: '#AAA',
141
+ axis: '#000',
142
+ },
143
+ axis: {
144
+ yTitle: false,
145
+ xTitle: false,
146
+ yFormat: '.0f',
147
+ xFormat: '.0f',
148
+ yTicks: 10,
149
+ xTicks: 10,
150
+ },
151
+ tooltip: { label: false, suffix: false, suffixPlural: false },
152
+ keys: { [SCANNED]: SCANNED, [REFERENCE]: REFERENCE },
153
+ transition: { duration: 350, ease: 'easeLinear' },
154
+ });
155
+ }
156
+
157
+ /**
158
+ * Init chart
159
+ */
160
+ initChart() {
161
+ // Set up dimensions
162
+ this.getDimensions();
163
+ this.initChartFrame('barchart');
164
+
165
+ if (this.cfg.tooltip.suffix && this.cfg.tooltip.suffixPlural) {
166
+ pluralize.addIrregularRule(this.cfg.tooltip.suffix, this.cfg.tooltip.suffixPlural);
167
+ }
168
+
169
+ // Set up scales
170
+ this.xScale = d3.scaleBand();
171
+ this.xScaleInn = d3.scaleBand();
172
+ this.yScale = d3.scaleLinear();
173
+
174
+ // Axis group
175
+ this.axisg = this.g.append('g').attr('class', 'chart__axis chart__axis--barchart');
176
+
177
+ // Horizontal grid
178
+ this.yGrid = this.axisg
179
+ .append('g')
180
+ .attr('class', 'chart__grid chart__grid--y chart__grid--barchart');
181
+
182
+ // Bottom axis
183
+ this.xAxis = this.axisg.append('g').attr('class', 'chart__axis-x chart__axis-x--barchart');
184
+
185
+ // Vertical axis title
186
+ if (this.cfg.axis.yTitle)
187
+ this.yAxisTitle = this.axisg
188
+ .append('text')
189
+ .attr('class', 'chart__axis-title chart__axis-title--barchart')
190
+ .attr('transform', 'rotate(-90)')
191
+ .style('text-anchor', 'middle');
192
+
193
+ this.setChartDimension();
194
+ this.updateChart();
195
+ }
196
+
197
+ /**
198
+ * Resize chart pipe
199
+ */
200
+ setScales() {
201
+ this.xScale
202
+ .rangeRound(
203
+ this.cfg.orientation !== 'horizontal' ? [0, this.cfg.width] : [0, this.cfg.height]
204
+ )
205
+ .paddingInner(0.1)
206
+ .domain(this.data.map((d) => d[this.cfg.key]));
207
+
208
+ this.xScaleInn
209
+ .domain(this.cfg.values)
210
+ .rangeRound([0, this.xScale.bandwidth()])
211
+ .paddingInner(0.05);
212
+
213
+ const yMax = this.calculateBiggestNumber();
214
+ this.yScale
215
+ .rangeRound(
216
+ this.cfg.orientation !== 'horizontal' ? [0, this.cfg.height] : [this.cfg.width, 0]
217
+ )
218
+ .domain([yMax, 0]);
219
+
220
+ if (this.cfg.color.scheme instanceof Array === true) {
221
+ this.colorScale = d3.scaleOrdinal().range(this.cfg.color.scheme);
222
+ } else if (typeof this.cfg.color.scheme === 'string') {
223
+ this.colorScale = d3.scaleOrdinal(d3[this.cfg.color.scheme]);
224
+ }
225
+
226
+ const yGridFunction =
227
+ this.cfg.orientation !== 'horizontal'
228
+ ? d3
229
+ .axisLeft(this.yScale)
230
+ .tickSize(-this.cfg.width)
231
+ .ticks(this.cfg.axis.yTicks, this.cfg.axis.yFormat)
232
+ : d3
233
+ .axisBottom(this.yScale)
234
+ .tickSize(-this.cfg.height)
235
+ .ticks(this.cfg.axis.yTicks, this.cfg.axis.yFormat);
236
+
237
+ const xAxisFunction =
238
+ this.cfg.orientation !== 'horizontal' ? d3.axisBottom(this.xScale) : d3.axisLeft(this.xScale);
239
+
240
+ // Horizontal grid
241
+ this.yGrid
242
+ .attr(
243
+ 'transform',
244
+ this.cfg.orientation !== 'horizontal' ? 'translate(0,0)' : `translate(0,${this.cfg.height})`
245
+ )
246
+ .transition(this.transition)
247
+ .call(yGridFunction);
248
+
249
+ // Bottom axis
250
+ this.xAxis
251
+ .attr(
252
+ 'transform',
253
+ this.cfg.orientation !== 'horizontal' ? `translate(0,${this.cfg.height})` : 'translate(0,0)'
254
+ )
255
+ .call(xAxisFunction);
256
+ }
257
+
258
+ /**
259
+ * Set chart dimensional sizes
260
+ */
261
+ setChartDimension() {
262
+ // SVG element
263
+ this.svg
264
+ .attr(
265
+ 'viewBox',
266
+ `0 0 ${this.cfg.width + this.cfg.margin.left + this.cfg.margin.right} ${
267
+ this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom
268
+ }`
269
+ )
270
+ .attr('width', this.cfg.width + this.cfg.margin.left + this.cfg.margin.right)
271
+ .attr('height', this.cfg.height + this.cfg.margin.top + this.cfg.margin.bottom);
272
+
273
+ // Vertical axis title
274
+ if (this.cfg.axis.yTitle)
275
+ this.yAxisTitle
276
+ .attr('y', -this.cfg.margin.left + 10)
277
+ .attr('x', -this.cfg.height / 2)
278
+ .text(this.cfg.axis.yTitle);
279
+
280
+ // Bottom axis label rotation
281
+ if (this.cfg.labelRotation !== 0)
282
+ this.xAxis
283
+ .selectAll('text')
284
+ .attr('y', Math.cos((this.cfg.labelRotation * Math.PI) / 180) * 9)
285
+ .attr('x', Math.sin((this.cfg.labelRotation * Math.PI) / 180) * 9)
286
+ .attr('dy', '.35em')
287
+ .attr('transform', `rotate(${this.cfg.labelRotation})`)
288
+ .style('text-anchor', 'start');
289
+ }
290
+
291
+ /**
292
+ * Bind data to main elements groups
293
+ */
294
+ bindData() {
295
+ // Set transition
296
+ this.transition = d3
297
+ .transition('t')
298
+ .duration(this.cfg.transition.duration)
299
+ .ease(d3[this.cfg.transition.ease]);
300
+
301
+ // Bars groups
302
+ this.g.selectAll('.chart__bar-group').remove();
303
+ this.itemg = this.g.selectAll('.chart__bar-group').data(this.data, (d) => d[this.cfg.key]);
304
+ }
305
+
306
+ hasComparissionData() {
307
+ return this.data.some((item) => item[REFERENCE] > 0);
308
+ }
309
+
310
+ calculateTotals() {
311
+ const totalCompared = this.data.reduce((total, item) => {
312
+ return total + item[REFERENCE];
313
+ }, 0);
314
+ const totalOriginal = this.data.reduce((total, item) => {
315
+ return total + item[SCANNED];
316
+ }, 0);
317
+ return { [REFERENCE]: totalCompared, [SCANNED]: totalOriginal };
318
+ }
319
+
320
+ calculateMaximums() {
321
+ const maxCompared = this.data.reduce(
322
+ (max, item) => this.getBiggerNumber(max, item[REFERENCE]),
323
+ 0
324
+ );
325
+ const maxOriginal = this.data.reduce(
326
+ (max, item) => this.getBiggerNumber(max, item[SCANNED]),
327
+ 0
328
+ );
329
+ return { [REFERENCE]: maxCompared, [SCANNED]: maxOriginal };
330
+ }
331
+
332
+ getBiggerNumber(a, b) {
333
+ if (a > b) {
334
+ return a;
335
+ }
336
+ return b;
337
+ }
338
+
339
+ calculateBiggestNumber() {
340
+ const totals = this.calculateTotals();
341
+ const maximums = this.calculateMaximums();
342
+
343
+ const referenceMax = maximums[REFERENCE];
344
+ const referenceTotal = totals[REFERENCE];
345
+
346
+ const scannedMax = maximums[SCANNED];
347
+ const scannedTotal = totals[SCANNED];
348
+
349
+ const referencePercentage = this.calculatePercentage(referenceMax, referenceTotal);
350
+ const scannedPercentage = this.calculatePercentage(scannedMax, scannedTotal);
351
+
352
+ const biggest = this.getBiggerNumber(referencePercentage, scannedPercentage);
353
+ return this.roundToNextStep(biggest);
354
+ }
355
+
356
+ roundToNextStep(x) {
357
+ return Math.ceil(x / 10) * 10;
358
+ }
359
+
360
+ calculatePercentage(value, total) {
361
+ return (value * 100) / total;
362
+ }
363
+
364
+ /**
365
+ * Add new chart's elements
366
+ */
367
+ enterElements() {
368
+ const totals = this.calculateTotals();
369
+
370
+ const newbars = this.itemg
371
+ .enter()
372
+ .append('g')
373
+ .attr('class', 'chart__bar-group chart__bar-group--barchart')
374
+ .attr('transform', (d) => {
375
+ if (this.cfg.orientation !== 'horizontal') {
376
+ return `translate(${this.xScale(d[this.cfg.key])},0)`;
377
+ }
378
+ return `translate(0,${this.xScale(d[this.cfg.key])})`;
379
+ });
380
+
381
+ const rects = newbars
382
+ .selectAll('.chart__bar')
383
+ .data((d) =>
384
+ this.cfg.values.map((v) => {
385
+ const dat = { ...d };
386
+ dat[this.cfg.key] = d[this.cfg.key];
387
+ return dat;
388
+ })
389
+ )
390
+ .enter()
391
+ .append('rect')
392
+ .attr('class', 'chart__bar chart__bar--barchart')
393
+ .classed('chart__bar--current', (d) => {
394
+ return this.cfg.currentKey && d[this.cfg.key] === this.cfg.currentKey;
395
+ })
396
+ .attr('x', (d, i) => {
397
+ return this.cfg.orientation !== 'horizontal'
398
+ ? this.xScaleInn(this.cfg.values[i % this.cfg.values.length])
399
+ : 0;
400
+ })
401
+ .attr('y', (d, i) => {
402
+ return this.cfg.orientation !== 'horizontal'
403
+ ? this.cfg.height
404
+ : this.xScaleInn(this.cfg.values[i % this.cfg.values.length]);
405
+ })
406
+ .attr('height', 0)
407
+ .attr('width', 0)
408
+ .on('mouseover', (event, d) => {
409
+ const bars = d3.selectAll('.chart__bar--barchart').nodes();
410
+ const index = bars.indexOf(event.currentTarget);
411
+ const key = this.cfg.values[index % this.cfg.values.length];
412
+ const label = this.cfg.tooltip.suffixPlural
413
+ ? pluralize(this.cfg.tooltip.suffix, d[key])
414
+ : this.cfg.tooltip.suffix;
415
+
416
+ const labelKey = this.cfg.keys[key];
417
+
418
+ const percentage = this.calculatePercentage(d[key], totals[key]).toFixed(2);
419
+
420
+ const text = `<div>${labelKey}: ${percentage}%</div>`;
421
+ this.tooltip.html(text).classed('active', true);
422
+ })
423
+ .on('mouseout', () => {
424
+ this.tooltip.classed('active', false);
425
+ })
426
+ .on('mousemove', (event) => {
427
+ this.tooltip
428
+ .style('left', event['pageX'] - 28 + 'px')
429
+ .style('top', event['pageY'] - 40 + 'px');
430
+ });
431
+ }
432
+
433
+ /**
434
+ * Update chart's elements based on data change
435
+ */
436
+ updateElements() {
437
+ const totals = this.calculateTotals();
438
+
439
+ // Bars groups
440
+ this.itemg.transition(this.transition).attr('transform', (d) => {
441
+ return this.cfg.orientation !== 'horizontal'
442
+ ? `translate(${this.xScale(d[this.cfg.key])},0)`
443
+ : `translate(0,${this.xScale(d[this.cfg.key])})`;
444
+ });
445
+ // Bars
446
+ this.g
447
+ .selectAll('.chart__bar')
448
+ .transition(this.transition)
449
+ .attr('fill', (d, i) => this.colorElement(d, this.cfg.values[i % this.cfg.values.length]))
450
+ .attr('opacity', (d, i) => (i % 2 == 1 && this.hasComparissionData() ? 0.6 : 1))
451
+ .attr('x', (d, i) => {
452
+ return this.cfg.orientation !== 'horizontal'
453
+ ? this.xScaleInn(this.cfg.values[i % this.cfg.values.length])
454
+ : 0;
455
+ })
456
+ .attr('y', (d, i) => {
457
+ return this.cfg.orientation !== 'horizontal'
458
+ ? this.yScale(+d[this.cfg.values[i % this.cfg.values.length]])
459
+ : this.xScaleInn(this.cfg.values[i % this.cfg.values.length]);
460
+ })
461
+ .attr('width', (d, i) => {
462
+ const key = this.cfg.values[i % this.cfg.values.length];
463
+ const percentage = this.calculatePercentage(d[key], totals[key]);
464
+
465
+ return this.cfg.orientation !== 'horizontal'
466
+ ? this.xScaleInn.bandwidth()
467
+ : this.yScale(percentage);
468
+ })
469
+ .attr('height', (d, i) => {
470
+ return this.cfg.orientation !== 'horizontal'
471
+ ? this.cfg.height - this.yScale(+d[this.cfg.values[i % this.cfg.values.length]])
472
+ : this.xScaleInn.bandwidth();
473
+ });
474
+ }
475
+
476
+ /**
477
+ * Remove chart's elements without data
478
+ */
479
+ exitElements() {
480
+ this.itemg.exit().transition(this.transition).style('opacity', 0).remove();
481
+ }
482
+ }
483
+
484
+ export default d3barchart;
@@ -0,0 +1,16 @@
1
+ <script>
2
+ import D3Chart from '../d3.chart.vue';
3
+ import d3barchart from './d3.barchart';
4
+
5
+ export default {
6
+ name: 'D3BarChart',
7
+ extends: D3Chart,
8
+ mounted() {
9
+ this.chart = new d3barchart(
10
+ this.$refs.chart,
11
+ JSON.parse(JSON.stringify(this.datum)),
12
+ this.config,
13
+ );
14
+ },
15
+ };
16
+ </script>