ahoy_captain 0.83 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -13
  3. data/Rakefile +23 -2
  4. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +20 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +371 -0
  6. data/app/assets/javascript/ahoy_captain/controllers/filter/item_controller.js +12 -0
  7. data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
  8. data/app/assets/javascript/ahoy_captain/controllers/frame_link_controller.js +20 -0
  9. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +58 -16
  10. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +5 -0
  11. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +236 -22
  12. data/app/assets/javascript/ahoy_captain/controllers/map_controller.js +47 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +1 -1
  14. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +45 -0
  16. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +4 -2
  17. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +33 -0
  18. data/app/assets/javascript/ahoy_captain/controllers/toggle_controller.js +17 -0
  19. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  20. data/app/assets/javascript/ahoy_captain/helpers/countries.js +2261 -0
  21. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  22. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  23. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  24. data/app/components/ahoy_captain/comparison_link_component.html.erb +17 -0
  25. data/app/components/ahoy_captain/comparison_link_component.rb +44 -0
  26. data/app/components/ahoy_captain/dropdown_button_component.html.erb +5 -5
  27. data/app/components/ahoy_captain/dropdown_link_component.html.erb +5 -7
  28. data/app/components/ahoy_captain/dropdown_link_component.rb +4 -0
  29. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +50 -0
  30. data/app/components/ahoy_captain/filter/dropdown_component.rb +51 -0
  31. data/app/components/ahoy_captain/filter/modal_component.html.erb +7 -5
  32. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -21
  33. data/app/components/ahoy_captain/filter/select_component.rb +24 -9
  34. data/app/components/ahoy_captain/filter/tag_component.html.erb +8 -4
  35. data/app/components/ahoy_captain/filter/tag_component.rb +6 -30
  36. data/app/components/ahoy_captain/filter/tag_container_component.html.erb +2 -3
  37. data/app/components/ahoy_captain/filter/tag_container_component.rb +1 -8
  38. data/app/components/ahoy_captain/previous_next_component.html.erb +8 -0
  39. data/app/components/ahoy_captain/previous_next_component.rb +11 -0
  40. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  41. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  42. data/app/components/ahoy_captain/stats/container_component.html.erb +15 -0
  43. data/app/components/ahoy_captain/stats/container_component.rb +26 -0
  44. data/app/components/ahoy_captain/sticky_nav_component.html.erb +28 -33
  45. data/app/components/ahoy_captain/sticky_nav_component.rb +19 -0
  46. data/app/components/ahoy_captain/table_component.html.erb +2 -2
  47. data/app/components/ahoy_captain/table_component.rb +15 -3
  48. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  49. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  50. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  51. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  52. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  53. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  54. data/app/components/ahoy_captain/tables/headers/header_component.html.erb +1 -1
  55. data/app/components/ahoy_captain/tables/headers/header_component.rb +4 -0
  56. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  57. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  58. data/app/components/ahoy_captain/tables/rows/row_component.rb +2 -3
  59. data/app/components/ahoy_captain/tile_component.html.erb +21 -10
  60. data/app/components/ahoy_captain/tile_component.rb +10 -2
  61. data/app/components/ahoy_captain/tooltip_component.html.erb +2 -2
  62. data/app/controllers/ahoy_captain/application_controller.rb +7 -16
  63. data/app/controllers/ahoy_captain/exports_controller.rb +1 -2
  64. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  65. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  66. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  67. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +1 -1
  68. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -1
  69. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +4 -4
  70. data/app/controllers/ahoy_captain/filters/sources_controller.rb +1 -1
  71. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  72. data/app/controllers/ahoy_captain/locations/cities_controller.rb +22 -0
  73. data/app/controllers/ahoy_captain/locations/countries_controller.rb +22 -0
  74. data/app/controllers/ahoy_captain/locations/maps_controller.rb +24 -0
  75. data/app/controllers/ahoy_captain/locations/regions_controller.rb +22 -0
  76. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  77. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  78. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -1
  79. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  80. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  81. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  82. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +1 -10
  83. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  84. data/app/helpers/ahoy_captain/application_helper.rb +60 -3
  85. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  86. data/app/models/ahoy_captain/filter_parser.rb +82 -0
  87. data/app/models/ahoy_captain/range_from_params.rb +78 -0
  88. data/app/models/ahoy_captain/rangeable.rb +0 -3
  89. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  90. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  91. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  92. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -54
  93. data/app/presenters/ahoy_captain/goals_presenter.rb +3 -2
  94. data/app/queries/ahoy_captain/application_query.rb +74 -10
  95. data/app/queries/ahoy_captain/event_query.rb +7 -2
  96. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  97. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  98. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  99. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  100. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  101. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  102. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  103. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  104. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +3 -3
  105. data/app/queries/ahoy_captain/visit_query.rb +1 -2
  106. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  107. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  108. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  109. data/app/views/ahoy_captain/devices/_table.html.erb +1 -4
  110. data/app/views/ahoy_captain/funnels/show.html.erb +5 -2
  111. data/app/views/ahoy_captain/goals/index.html.erb +1 -4
  112. data/app/views/ahoy_captain/layouts/application.html.erb +3 -4
  113. data/app/views/ahoy_captain/layouts/shared/_tile_loader.html.erb +12 -0
  114. data/app/views/ahoy_captain/locations/maps/show.html.erb +3 -0
  115. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  116. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  117. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  118. data/app/views/ahoy_captain/realtimes/show.html.erb +1 -1
  119. data/app/views/ahoy_captain/roots/_filters.html.erb +80 -0
  120. data/app/views/ahoy_captain/roots/show.html.erb +113 -122
  121. data/app/views/ahoy_captain/stats/base/index.html.erb +37 -8
  122. data/app/views/ahoy_captain/stats/show.html.erb +14 -56
  123. data/config/routes.rb +9 -3
  124. data/lib/ahoy_captain/ahoy/event_methods.rb +21 -14
  125. data/lib/ahoy_captain/ahoy/visit_methods.rb +1 -1
  126. data/lib/ahoy_captain/configuration.rb +18 -7
  127. data/lib/ahoy_captain/engine.rb +21 -0
  128. data/lib/ahoy_captain/filter_configuration/filter.rb +16 -0
  129. data/lib/ahoy_captain/filter_configuration/filter_collection.rb +48 -0
  130. data/lib/ahoy_captain/filters_configuration.rb +77 -0
  131. data/lib/ahoy_captain/goals.rb +1 -1
  132. data/lib/ahoy_captain/predicate_label.rb +7 -0
  133. data/lib/ahoy_captain/version.rb +1 -1
  134. data/lib/ahoy_captain.rb +8 -1
  135. data/lib/generators/ahoy_captain/templates/config.rb.tt +32 -0
  136. metadata +149 -22
  137. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +0 -15
  138. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -20
  139. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +0 -65
  140. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +0 -3
  141. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +0 -9
  142. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +0 -6
  143. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +0 -9
  144. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +0 -5
  145. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +0 -12
  146. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +0 -11
  147. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +0 -12
  148. data/app/controllers/ahoy_captain/cities_controller.rb +0 -20
  149. data/app/controllers/ahoy_captain/countries_controller.rb +0 -20
  150. data/app/controllers/ahoy_captain/regions_controller.rb +0 -20
  151. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html+details.erb +0 -0
  152. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html.erb +0 -0
  153. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html+details.erb +0 -0
  154. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html.erb +0 -0
  155. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html+details.erb +0 -0
  156. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html.erb +0 -0
@@ -1,14 +1,26 @@
1
1
  import { Controller } from '@hotwired/stimulus';
2
2
  import 'chartjs-plugin-datalabels';
3
+ import { getCSS, externalTooltipHandler } from "helpers/chart_utils";
4
+
5
+ const calculatePercentageDifference = function(oldValue, newValue) {
6
+ if(!oldValue) { return false }
7
+ if (oldValue == 0 && newValue > 0) {
8
+ return 100
9
+ } else if (oldValue == 0 && newValue == 0) {
10
+ return 0
11
+ } else {
12
+ return Math.round((newValue - oldValue) / oldValue * 100)
13
+ }
14
+ }
3
15
 
4
16
  export default class extends Controller {
5
17
  connect() {
6
- const funnel = JSON.parse(this.element.dataset.data);
18
+ this.funnel = JSON.parse(this.element.dataset.data);
7
19
 
8
20
  const fontFamily = 'ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
9
- const labels = funnel.steps.map((step) => step.name);
10
- const stepData = funnel.steps.map((step) => step.total_events);
11
- const dropOffData = funnel.steps.map((step) => step.drop_off * 100);
21
+ const labels = this.funnel.steps.map((step) => step.name);
22
+ const stepData = this.funnel.steps.map((step) => step.total_events);
23
+ const dropOffData = this.funnel.steps.map((step) => step.drop_off * 100);
12
24
 
13
25
  const data = {
14
26
  labels,
@@ -17,13 +29,19 @@ export default class extends Controller {
17
29
  label: 'Visitors',
18
30
  data: stepData,
19
31
  borderRadius: 4,
32
+ color: getCSS('--ac'),
33
+ backgroundColor: getCSS('--p'),
20
34
  stack: 'Stack 0',
35
+ yAxisID: 'y',
21
36
  },
22
37
  {
23
38
  label: 'Dropoff',
24
39
  data: dropOffData,
25
40
  borderRadius: 4,
26
41
  stack: 'Stack 0',
42
+ color: getCSS('--ac'),
43
+ backgroundColor: getCSS('--a'),
44
+ yAxisID: 'yComparison',
27
45
  },
28
46
  ],
29
47
  };
@@ -39,13 +57,11 @@ export default class extends Controller {
39
57
  padding: 100,
40
58
  },
41
59
  plugins: {
42
- legend: {
43
- display: false,
44
- },
60
+ legend: false,
45
61
  tooltip: {
46
- mode: 'index',
47
- intersect: true,
48
- position: 'average',
62
+ enabled: false,
63
+ position: 'nearest',
64
+ external: externalTooltipHandler(this)
49
65
  },
50
66
  datalabels: {
51
67
  anchor: 'end',
@@ -54,9 +70,7 @@ export default class extends Controller {
54
70
  padding: {
55
71
  top: 8, bottom: 8, right: 8, left: 8,
56
72
  },
57
- font: {
58
- size: 12, weight: 'normal', lineHeight: 1.6, family: fontFamily,
59
- },
73
+ color: getCSS('--pc'),
60
74
  textAlign: 'center',
61
75
  },
62
76
  },
@@ -69,8 +83,6 @@ export default class extends Controller {
69
83
  grid: { drawBorder: false, display: false },
70
84
  ticks: {
71
85
  padding: 8,
72
- font: { weight: 'bold', family: fontFamily, size: 14 },
73
- color: 'rgb(228, 228, 231)',
74
86
  },
75
87
  },
76
88
  },
@@ -79,9 +91,39 @@ export default class extends Controller {
79
91
 
80
92
  const visitorsData = [];
81
93
 
82
- new Chart(
94
+ this.chart = new Chart(
83
95
  this.element,
84
96
  config,
85
97
  );
86
98
  }
99
+
100
+ formatLabel(label) {
101
+ return label
102
+ }
103
+
104
+ formatMetric(metric) {
105
+ return metric
106
+ }
107
+
108
+
109
+ extractTooltipData(tooltip) {
110
+ const data = this.funnel.steps.find(step => step.name === tooltip.title[0]);
111
+
112
+ const value = data.total_events;
113
+ const label = "Visitors"
114
+ let comparisonLabel = "Dropoff"
115
+ let comparisonValue = data.unique_visits;
116
+
117
+ return {
118
+ comparison: true,
119
+ comparisonDifference: false,
120
+ metric: tooltip.title[0],
121
+ label: this.formatLabel(label),
122
+ labelBackgroundColor: getCSS('--bc'),
123
+ formattedValue: value,
124
+ comparisonLabel: comparisonLabel,
125
+ comparisonLabelBackgroundColor: "",
126
+ formattedComparisonValue: comparisonValue,
127
+ }
128
+ }
87
129
  }
@@ -6,5 +6,10 @@ export default class extends Controller {
6
6
  const interval = event.target.value;
7
7
  url.searchParams.set('interval', interval);
8
8
  event.target.closest('turbo-frame').src = url.href;
9
+ document.querySelectorAll('a[data-turbo-frame="chart"]').forEach(el => {
10
+ const url = new URL(el.href);
11
+ url.searchParams.set('interval', interval);
12
+ el.href = url.href
13
+ })
9
14
  }
10
15
  }
@@ -1,37 +1,251 @@
1
1
  import { Controller } from '@hotwired/stimulus';
2
+ import { getCSS, externalTooltipHandler, dateFormatter, metricFormatter } from 'helpers/chart_utils';
3
+
4
+ const calculatePercentageDifference = function(oldValue, newValue) {
5
+ if(!oldValue) { return false }
6
+ if (oldValue == 0 && newValue > 0) {
7
+ return 100
8
+ } else if (oldValue == 0 && newValue == 0) {
9
+ return 0
10
+ } else {
11
+ return Math.round((newValue - oldValue) / oldValue * 100)
12
+ }
13
+ }
14
+
15
+ const footer = (tooltipItems) => {
16
+ let sum = 0;
17
+
18
+ tooltipItems.forEach(function(tooltipItem) {
19
+ sum += tooltipItem.parsed.y;
20
+ });
21
+ return 'Sum: ' + sum;
22
+ };
2
23
 
3
24
  export default class extends Controller {
4
25
  static values = {
5
- data: Object,
6
- label: String
26
+ current: Object,
27
+ comparedTo: Object,
28
+ interval: String,
29
+ label: String,
30
+ metric: String,
31
+ comparison: String
7
32
  }
33
+
8
34
  connect() {
9
- const getCSS = (varname) => {
10
- return `hsl(${getComputedStyle(document.documentElement).getPropertyValue(varname)})`
11
- }
12
- Chart.register(Chart.Colors)
35
+ const onClick = (e) => {
36
+ if(drag) { return }
37
+ const element = this.chart.getElementsAtEventForMode(e, 'index', { intersect: false })[0];
38
+ const searchParams = new URLSearchParams(window.location.search);
39
+
40
+ searchParams.delete('period')
41
+ searchParams.delete('start_date')
42
+ searchParams.delete('end_date')
43
+ searchParams.delete('compare_to_start_date')
44
+ searchParams.delete('compare_to_end_date')
45
+ searchParams.set('date', Object.keys(this.currentValue)[element.index])
13
46
 
14
- new Chart(this.element,
47
+ Turbo.visit(window.location.pathname + "?" + searchParams.toString())
48
+ }
49
+ const datasets = [
15
50
  {
51
+ label: Object.keys(this.currentValue),
52
+ data: Object.values(this.currentValue),
53
+ borderColor: getCSS('--a'),
54
+ backgroundColor: getCSS('--a'),
55
+ color: getCSS('--bc'),
56
+ yAxisID: 'y',
57
+ }
58
+ ]
59
+
60
+ if(this.hasComparedToValue) {
61
+ datasets.push({
62
+ label: Object.keys(this.comparedToValue),
63
+ data: Object.values(this.comparedToValue),
64
+ borderColor: getCSS('--s', 0.8),
65
+ backgroundColor: getCSS('--s', 0.8),
66
+ color: getCSS('--bc'),
67
+ yAxisID: 'yComparison',
68
+ })
69
+ }
70
+
71
+ const calculateMaximumY = function(dataset) {
72
+ if (dataset) {
73
+ return Math.max(Object.values(dataset))
74
+ } else {
75
+ return 1
76
+ }
77
+ }
78
+
79
+ const typeForDate = this.comparisonValue === 'year' ? 'long' : "short"
80
+ const options = {
16
81
  type: 'line',
17
82
  data: {
18
- labels: Object.keys(this.dataValue),
19
- datasets: [
20
- {
21
- label: this.labelValue,
22
- data: Object.values(this.dataValue),
23
- borderColor: getCSS('--p'),
24
- backgroundColor: getCSS('--af'),
25
- color: getCSS('--pc')
26
- }
27
- ]
83
+ labels: Object.keys(this.currentValue),
84
+ datasets: datasets
28
85
  },
29
- plugins: {
30
- colors: {
31
- forceOverride: true
86
+ options: {
87
+ onClick: onClick,
88
+ responsive: true,
89
+ maintainAspectRatio: false,
90
+ interaction: {
91
+ intersect: false,
92
+ mode: 'index',
93
+ },
94
+ plugins: {
95
+ legend: false,
96
+ tooltip: {
97
+ enabled: false,
98
+ position: 'nearest',
99
+ external: externalTooltipHandler(this)
100
+ }
101
+ },
102
+ scales: {
103
+ y: {
104
+ min: 0,
105
+ suggestedMax: calculateMaximumY(this.currentValue),
106
+ ticks: {
107
+ },
108
+ grid: {
109
+ zeroLineColor: 'transparent',
110
+ drawBorder: false,
111
+ }
112
+ },
113
+ yComparison: {
114
+ min: 0,
115
+ suggestedMax: calculateMaximumY(this.currentValue),
116
+ display: false,
117
+ grid: { display: false },
118
+ },
119
+ x: {
120
+ ticks: {
121
+ grid: { display: false },
122
+
123
+ color: getCSS('--bc'),
124
+ callback: (val, idx) => {
125
+ if(idx % 2 == 0) {
126
+ const date = Object.keys(this.currentValue)[val];
127
+ return dateFormatter[this.intervalValue](date, typeForDate)
128
+ } else {
129
+ return ""
130
+ }
131
+
132
+ }
133
+ }
134
+ }
135
+ },
136
+ elements: {
137
+ point: {
138
+ radius: 0
139
+ }
32
140
  }
33
141
  }
34
- },
35
- )
142
+ }
143
+ this.chart = new Chart(this.element, options);
144
+
145
+
146
+ var canvas = this.element;
147
+ var overlay = document.getElementById('overlay');
148
+ var startIndex = 0;
149
+ overlay.width = canvas.width;
150
+ overlay.height = canvas.height;
151
+ var selectionContext = overlay.getContext('2d');
152
+ var selectionRect = {
153
+ w: 0,
154
+ startX: 0,
155
+ startY: 0
156
+ };
157
+ var drag = false;
158
+ canvas.addEventListener('pointerdown', evt => {
159
+ const points = this.chart.getElementsAtEventForMode(evt, 'index', {
160
+ intersect: false
161
+ });
162
+
163
+ startIndex = points[0].index;
164
+ const rect = canvas.getBoundingClientRect();
165
+ selectionRect.startX = evt.clientX - rect.left;
166
+ selectionRect.startY = this.chart.chartArea.top;
167
+ drag = true;
168
+ });
169
+ canvas.addEventListener('pointermove', evt => {
170
+ const rect = canvas.getBoundingClientRect();
171
+ if (drag) {
172
+
173
+ const rect = canvas.getBoundingClientRect();
174
+ selectionContext.fillStyle = getCSS('--p')
175
+ selectionRect.w = (evt.clientX - rect.left) - selectionRect.startX;
176
+ selectionContext.globalAlpha = 0.5;
177
+ selectionContext.clearRect(0, 0, canvas.width, canvas.height);
178
+ selectionContext.fillRect(selectionRect.startX,
179
+ selectionRect.startY,
180
+ selectionRect.w,
181
+ this.chart.chartArea.bottom - this.chart.chartArea.top);
182
+ } else {
183
+ selectionContext.clearRect(0, 0, canvas.width, canvas.height);
184
+ var x = evt.clientX - rect.left;
185
+ if (x > this.chart.chartArea.left) {
186
+ selectionContext.fillStyle = getCSS('--p')
187
+ selectionContext.fillRect(x,
188
+ this.chart.chartArea.top,
189
+ 1,
190
+ this.chart.chartArea.bottom - this.chart.chartArea.top);
191
+ }
192
+ }
193
+ });
194
+ canvas.addEventListener('pointerup', evt => {
195
+ const points = this.chart.getElementsAtEventForMode(evt, 'index', {
196
+ intersect: false
197
+ });
198
+ const dates = [options.data.labels[startIndex], options.data.labels[points[0].index]].sort()
199
+ const searchParams = new URLSearchParams(window.location.search);
200
+ searchParams.set('start_date', dates[0])
201
+ searchParams.set('end_date', dates[1])
202
+ searchParams.delete('period')
203
+ searchParams.delete('compare_to_start_date')
204
+ searchParams.delete('compare_to_end_date')
205
+ Turbo.visit(window.location.pathname + "?" + searchParams.toString())
206
+ });
207
+ }
208
+
209
+ formatLabel(label) {
210
+ return dateFormatter[this.intervalValue](label, 'long')
211
+ }
212
+
213
+ formatMetric(value) {
214
+ return metricFormatter[this.metricValue](value)
215
+ }
216
+
217
+ resize() { this.chart.resize() };
218
+
219
+ extractTooltipData(tooltip) {
220
+ const data = this.chart.config.data.datasets.find((set) => set.yAxisID == "y")
221
+ const comparisonData = this.chart.config.data.datasets.find((set) => set.yAxisID == "yComparison");
222
+ const dataIndex = this.chart.config.data.datasets.indexOf(data)
223
+ const comparisonDataIndex = this.chart.config.data.datasets.indexOf(comparisonData);
224
+
225
+ const tooltipData = tooltip.dataPoints.find((dataPoint) => dataPoint.datasetIndex == dataIndex)
226
+ const label = data.label[tooltipData.dataIndex];
227
+ let comparisonLabel = false
228
+ let comparisonValue = false
229
+ let comparisonLabelBackgroundColor = false
230
+ if(this.hasComparedToValue) {
231
+ const tooltipComparisonData = tooltip.dataPoints.find((dataPoint) => dataPoint.datasetIndex == comparisonDataIndex);
232
+ comparisonLabel = comparisonData.label[tooltipComparisonData.dataIndex];
233
+ comparisonValue = tooltip.dataPoints.find((dataPoint) => dataPoint.datasetIndex == comparisonDataIndex)?.raw || 0
234
+ comparisonLabelBackgroundColor = comparisonData.backgroundColor
235
+ }
236
+
237
+ const value = tooltip.dataPoints.find((dataPoint) => dataPoint.datasetIndex == dataIndex)?.raw || 0
238
+
239
+ return {
240
+ comparison: this.hasComparedToValue,
241
+ comparisonDifference: calculatePercentageDifference(comparisonValue, value),
242
+ metric: this.labelValue,
243
+ label: this.formatLabel(label),
244
+ labelBackgroundColor: data.backgroundColor,
245
+ formattedValue: this.formatMetric(value),
246
+ comparisonLabel: this.formatLabel(comparisonLabel),
247
+ comparisonLabelBackgroundColor: comparisonLabelBackgroundColor,
248
+ formattedComparisonValue: this.formatMetric(comparisonValue)
249
+ }
36
250
  }
37
251
  }
@@ -0,0 +1,47 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+ import "chartjs-chart-geo";
3
+ import CountryMap from "helpers/countries"
4
+
5
+ export default class extends Controller {
6
+ static values = {
7
+ data: Object
8
+ }
9
+ connect() {
10
+ fetch('https://unpkg.com/world-atlas/countries-50m.json').then((r) => r.json()).then((data) => {
11
+ const countries = ChartGeo.topojson.feature(data, data.objects.countries).features;
12
+ const numericToCode = {};
13
+ Object.keys(CountryMap).forEach(key => { numericToCode[CountryMap[key]['Numeric code']] = key })
14
+
15
+ countries.forEach(country => {
16
+ const abbrev = numericToCode[country.id];
17
+ country.value = this.dataValue[abbrev]
18
+ })
19
+
20
+ const chart = new Chart(this.element.getContext("2d"), {
21
+ type: 'choropleth',
22
+ data: {
23
+ labels: countries.map((d) => d.properties.name),
24
+ datasets: [{
25
+ label: 'Countries',
26
+ data: countries.map((d) => ({feature: d, value: d.value || 0 })),
27
+ }]
28
+ },
29
+ options: {
30
+ showOutline: false,
31
+ showGraticule: false,
32
+ plugins: {
33
+ legend: {
34
+ display: false
35
+ },
36
+ },
37
+ scales: {
38
+ projection: {
39
+ axis: 'x',
40
+ projection: 'equalEarth'
41
+ }
42
+ }
43
+ }
44
+ });
45
+ });
46
+ }
47
+ }
@@ -1,10 +1,10 @@
1
1
  import {Controller} from "@hotwired/stimulus"
2
- import SlimSelect from 'slim-select'
3
2
 
4
3
  export default class extends Controller {
5
4
  static targets = ["select"]
6
5
 
7
6
  handleChange(event) {
7
+ this.selectTarget.dataset.predicate = event.target.value
8
8
  this.selectTarget.name = event.target.value
9
9
  }
10
10
  }
@@ -0,0 +1,8 @@
1
+ import {Controller} from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+
5
+ handleChange(event) {
6
+ document.querySelector('turbo-frame#goals').src = event.target.value
7
+ }
8
+ }
@@ -0,0 +1,45 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ export default class extends Controller {
4
+ static targets = ['name', 'value'];
5
+
6
+ connect() {
7
+ this.init = this.init.bind(this)
8
+
9
+ const interval = setInterval(() => {
10
+ if(window.comboboxConnected === 2) {
11
+ clearInterval(interval);
12
+ this.init()
13
+ }
14
+ }, 100)
15
+ }
16
+
17
+ init() {
18
+
19
+ if(this.nameTarget.value) {
20
+ this.valueTarget.combobox.element.dataset.comboboxQueryValue = `q[properties.${this.nameTarget.value}_i_cont]`
21
+ }
22
+
23
+ this.nameTarget.addEventListener("change", (event) => {
24
+ if(event.target.value) {
25
+ event.target.dataset.column = event.target.value;
26
+ this.valueTarget.combobox.element.dataset.comboboxQueryValue = `q[properties.${event.target.value}_i_cont]`
27
+ this.valueTarget.combobox.setDisabled(false)
28
+ this.valueTarget.combobox.isOpenValue = false
29
+ } else {
30
+ this.valueTarget.combobox.setSelected([])
31
+ this.valueTarget.combobox.setDisabled(true)
32
+ }
33
+ })
34
+ this.valueTarget.addEventListener("change", (event) => {
35
+ if(event.target.value.length > 0) {
36
+ this.valueTarget.name = `q[properties.${this.nameTarget.dataset.column}_in]`
37
+ } else {
38
+ this.valueTarget.name = null
39
+ this.valueTarget.combobox.element.dataset.comboboxQueryValue = ""
40
+ }
41
+ })
42
+
43
+ }
44
+
45
+ }
@@ -2,12 +2,14 @@ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  export default class extends Controller {
4
4
  static targets = ['label'];
5
-
5
+ static values = {
6
+ interval: Number
7
+ }
6
8
  connect() {
7
9
  this.reload = this.reload.bind(this);
8
10
  this.setLabel = this.setLabel.bind(this);
9
11
  this.labelCount = 0;
10
- this.reloadInterval = setInterval(this.reload, 1000 * 30);
12
+ this.reloadInterval = setInterval(this.reload, 1000 * this.intervalValue);
11
13
  this.labelInterval = setInterval(this.setLabel, 1000);
12
14
  }
13
15
 
@@ -0,0 +1,33 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ export default class extends Controller {
4
+ static targets = ["title"]
5
+ connect() {
6
+ this.frame = this.element.querySelector('turbo-frame');
7
+ const targetNode = this.frame;
8
+ const config = { attributes: true };
9
+
10
+ const callback = (mutationList, observer) => {
11
+ for (const mutation of mutationList) {
12
+ if (mutation.type === "attributes") {
13
+ this.handleFrameLoad(mutation.attributeName)
14
+ }
15
+ }
16
+ };
17
+
18
+ const observer = new MutationObserver(callback);
19
+ observer.observe(targetNode, config);
20
+ }
21
+
22
+ handleFrameLoad(status) {
23
+ if(!this.frame.hasAttribute('skeleton')) {
24
+ if(status === 'busy') {
25
+ this.frame.innerHTML = document.querySelector('#tile-loader-template').innerHTML;
26
+ }
27
+ }
28
+ }
29
+
30
+ setTitle(event) {
31
+ this.titleTarget.innerHTML = event.target.title || event.target.text
32
+ }
33
+ }
@@ -0,0 +1,17 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // Connects to data-controller="toggle"
4
+ export default class extends Controller {
5
+ static targets = ['toggleable'];
6
+ static values = {
7
+ enable: Boolean
8
+ }
9
+
10
+ trigger() {
11
+ if (this.enableValue) {
12
+ this.toggleableTargets.forEach(element => {
13
+ element.classList.toggle('hidden');
14
+ });
15
+ }
16
+ }
17
+ }