overview
1 year ago
campaign_builder_controller.js
1 year ago
chart_controller.js
1 year ago
chart_interval_controller.js
2 years ago
clipboard_controller.js
1 year ago
copy_report_controller.js
2 years ago
create_report_controller.js
1 year ago
delete_data_controller.js
1 year ago
delete_report_controller.js
2 years ago
easepick_controller.js
2 years ago
export_overview_controller.js
1 year ago
export_reports_controller.js
2 years ago
filters_controller.js
1 year ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
map_controller.js
1 year ago
migration_redirect_controller.js
2 years ago
modal_controller.js
2 years ago
pie_chart_controller.js
1 year ago
plugin_group_options_controller.js
1 year ago
pruner_controller.js
1 year ago
quick_stats_controller.js
1 year ago
real_time_controller.js
1 year ago
refresh_overview_controller.js
1 year ago
rename_report_controller.js
2 years ago
report_controller.js
1 year ago
reset_analytics_controller.js
1 year ago
reset_overview_controller.js
1 year ago
save_report_controller.js
2 years ago
select_input_controller.js
2 years ago
set_favorite_report_controller.js
2 years ago
sort_controller.js
2 years ago
sortable_reports_controller.js
2 years ago
table_columns_controller.js
2 years ago
woocommerce_settings_controller.js
1 year ago
map_controller.js
129 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | import {isDarkMode} from "../utils/appearance"; |
| 3 | |
| 4 | export default class extends Controller { |
| 5 | static targets = ["chart"] |
| 6 | static values = { |
| 7 | data: Array, |
| 8 | flagsUrl: String, |
| 9 | locale: String |
| 10 | } |
| 11 | |
| 12 | mapInstance = null |
| 13 | |
| 14 | connect() { |
| 15 | this.resizeObserver = new ResizeObserver(() => { |
| 16 | this.drawChart() |
| 17 | }); |
| 18 | |
| 19 | google.charts.load('current', { |
| 20 | 'packages': ['geochart'], |
| 21 | }) |
| 22 | |
| 23 | google.charts.setOnLoadCallback(() => { |
| 24 | this.drawChart() |
| 25 | this.resizeObserver.observe(this.element); |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | disconnect() { |
| 30 | this.resizeObserver.disconnect(); |
| 31 | window.mapInstances = window.mapInstances.filter(mapInstance => { |
| 32 | return mapInstance !== this.mapInstance |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | drawChart = () => { |
| 37 | // Empty the element to fix glitch where chart height wouldn't be resized |
| 38 | this.chartTarget.innerHTML = ''; |
| 39 | |
| 40 | // Remove the map |
| 41 | if(this.mapInstance) { |
| 42 | const index = window.mapInstances.indexOf(this.mapInstance); |
| 43 | |
| 44 | if(index > -1) { |
| 45 | window.mapInstances.splice(index, 1) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | let dataTable = new google.visualization.DataTable(); |
| 50 | |
| 51 | dataTable.addColumn('string', 'country'); |
| 52 | dataTable.addColumn('number', 'views'); |
| 53 | dataTable.addColumn({ |
| 54 | 'type': 'string', |
| 55 | 'role': 'tooltip', |
| 56 | 'p': {'html': true} |
| 57 | }) |
| 58 | |
| 59 | dataTable.addRows( |
| 60 | this.convertCountryDataToRows(this.dataValue) |
| 61 | ); |
| 62 | |
| 63 | const options = { |
| 64 | displayMode: 'regions', |
| 65 | tooltip: { |
| 66 | isHtml: true, |
| 67 | showTitle: false |
| 68 | }, |
| 69 | backgroundColor: isDarkMode() ? '#373040' : '#FFFFFF', |
| 70 | datalessRegionColor: isDarkMode() ? '#695C7A' : undefined, |
| 71 | colorAxis: { |
| 72 | colors: isDarkMode() ? ['#AC9CC9', '#9E66FF'] : ['#C4ABED', '#5223A0'] |
| 73 | }, |
| 74 | legend: { |
| 75 | numberFormat: `${iawpText.views}: #`, |
| 76 | textStyle: { |
| 77 | color: isDarkMode() ? '#FFFFFF' : '#000000', |
| 78 | strokeWidth: 0 |
| 79 | } |
| 80 | }, |
| 81 | } |
| 82 | |
| 83 | const map = new google.visualization.GeoChart(this.chartTarget) |
| 84 | |
| 85 | map.draw(dataTable, options) |
| 86 | |
| 87 | if(!window.mapInstances) { |
| 88 | window.mapInstances = [] |
| 89 | } |
| 90 | |
| 91 | this.mapInstance = map |
| 92 | window.mapInstances.push(map) |
| 93 | } |
| 94 | |
| 95 | convertCountryDataToRows(countryData) { |
| 96 | return countryData.map(country => { |
| 97 | return [ |
| 98 | country.country_code, |
| 99 | country.views, |
| 100 | this.getTooltipMarkup(country) |
| 101 | ] |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | getTooltipMarkup(country) { |
| 106 | const formatted_views = this.formatNumber(country['views']) |
| 107 | const formatted_visitors = this.formatNumber(country['visitors']) |
| 108 | const formatted_sessions = this.formatNumber(country['sessions']) |
| 109 | const flagUrl = this.flagsUrlValue + '/' + country['country_code'].toLowerCase() + '.svg' |
| 110 | |
| 111 | return ` |
| 112 | <div class="iawp-geo-chart-tooltip"> |
| 113 | <img src="${flagUrl}" alt="Country flag"/> |
| 114 | <h1>${country['country']}</h1> |
| 115 | <p><span>${iawpText.views}: </span>${formatted_views}</p> |
| 116 | <p><span>${iawpText.visitors}: </span>${formatted_visitors}</p> |
| 117 | <p><span>${iawpText.sessions}: </span>${formatted_sessions}</p> |
| 118 | </div> |
| 119 | `; |
| 120 | |
| 121 | return title.outerHTML |
| 122 | } |
| 123 | |
| 124 | formatNumber(number) { |
| 125 | return new Intl.NumberFormat(this.localeValue, { |
| 126 | maximumFractionDigits: 0 |
| 127 | }).format(number); |
| 128 | } |
| 129 | } |