Skip to content

Commit 7b2982e

Browse files
Initsogarmyhro
andauthored
Stats Location: update geochart to use coordinates for Regions and Cities (#99142)
* Stats: add subdivisions tooltip * Update geochart to accept coordinates for Cities view * Omit coordinates property when no data exists * Handle region and city mode as markers * Filter locations with coordinates * Remove prop that is not needed for now * Refactor sample locations using coordinates * Simplify conditions * Remove unused attribute to sample locations --------- Co-authored-by: Tiago Ilieve <[email protected]>
1 parent aa2c86e commit 7b2982e

File tree

4 files changed

+120
-36
lines changed

4 files changed

+120
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const sampleLocations = [
2+
{
3+
label: 'California',
4+
countryCode: 'US',
5+
value: 2000,
6+
coordinates: { latitude: '36.7783', longitude: '-119.4179' },
7+
},
8+
{
9+
label: 'Uttar Pradesh',
10+
countryCode: 'IN',
11+
value: 1500,
12+
coordinates: { latitude: '27.1303', longitude: '80.8590' },
13+
},
14+
{
15+
label: 'England',
16+
countryCode: 'GB',
17+
value: 1200,
18+
coordinates: { latitude: '52.3555', longitude: '-1.1743' },
19+
},
20+
{
21+
label: 'Ontario',
22+
countryCode: 'CA',
23+
value: 1000,
24+
coordinates: { latitude: '51.2538', longitude: '-85.3232' },
25+
},
26+
{
27+
label: 'North Rhine-Westphalia',
28+
countryCode: 'DE',
29+
value: 900,
30+
coordinates: { latitude: '51.4332', longitude: '7.6616' },
31+
},
32+
{
33+
label: 'West Java',
34+
countryCode: 'ID',
35+
value: 800,
36+
coordinates: { latitude: '-6.8897', longitude: '107.6405' },
37+
},
38+
{
39+
label: 'Tokyo',
40+
countryCode: 'JP',
41+
value: 700,
42+
coordinates: { latitude: '35.6895', longitude: '139.6917' },
43+
},
44+
{
45+
label: 'São Paulo',
46+
countryCode: 'BR',
47+
value: 600,
48+
coordinates: { latitude: '-23.5505', longitude: '-46.6333' },
49+
},
50+
{
51+
label: 'North Holland',
52+
countryCode: 'NL',
53+
value: 500,
54+
coordinates: { latitude: '52.5200', longitude: '4.6707' },
55+
},
56+
{
57+
label: 'Andalusia',
58+
countryCode: 'ES',
59+
value: 400,
60+
coordinates: { latitude: '37.5443', longitude: '-4.7278' },
61+
},
62+
];
63+
64+
export default sampleLocations;

client/my-sites/stats/features/modules/stats-locations/stats-locations.tsx

+2-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Geochart from '../../../geochart';
2828
import StatsCardSkeleton from '../shared/stats-card-skeleton';
2929
import StatsInfoArea from '../shared/stats-info-area';
3030
import CountryFilter from './country-filter';
31+
import sampleLocations from './sample-locations';
3132

3233
import './style.scss';
3334

@@ -209,22 +210,9 @@ const StatsLocations: React.FC< StatsModuleLocationsProps > = ( { query, summary
209210
</StatsInfoArea>
210211
);
211212

212-
const fakeData = [
213-
{ label: 'United States', countryCode: 'US', value: 2000, region: '021' },
214-
{ label: 'India', countryCode: 'IN', value: 1500, region: '034' },
215-
{ label: 'United Kingdom', countryCode: 'GB', value: 1200, region: '154' },
216-
{ label: 'Canada', countryCode: 'CA', value: 1000, region: '021' },
217-
{ label: 'Germany', countryCode: 'DE', value: 900, region: '155' },
218-
{ label: 'Indonesia', countryCode: 'ID', value: 800, region: '035' },
219-
{ label: 'Japan', countryCode: 'JP', value: 700, region: '030' },
220-
{ label: 'France', countryCode: 'FR', value: 600, region: '155' },
221-
{ label: 'Netherlands', countryCode: 'NL', value: 500, region: '155' },
222-
{ label: 'Spain', countryCode: 'ES', value: 400, region: '039' },
223-
];
224-
225213
const hasLocationData = Array.isArray( data ) && data.length > 0;
226214

227-
const locationData = shouldGate ? fakeData : data;
215+
const locationData = shouldGate ? sampleLocations : data;
228216

229217
const heroElement = (
230218
<>

client/my-sites/stats/geochart/index.jsx

+53-22
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,64 @@ class StatsGeochart extends Component {
105105
}
106106
};
107107

108-
drawData = () => {
109-
const { currentUserCountryCode, data, geoMode, translate, numberLabel, customHeight } =
110-
this.props;
111-
if ( ! data || ! data.length ) {
112-
return;
113-
}
108+
/**
109+
* Prepare data for Google GeoChart.
110+
* @param {Array} data - The data to prepare.
111+
* @returns {Object} chartData - The prepared data.
112+
*/
113+
prepareChartData = ( data ) => {
114+
const { geoMode, numberLabel, translate } = this.props;
115+
const chartData = new window.google.visualization.DataTable();
114116

115-
const mapData = map( data, ( location ) => {
116-
let code = location.countryCode;
117-
if ( geoMode !== 'country' ) {
118-
code = `${ location.countryCode } ${ location.label }`;
119-
}
120-
121-
return [
122-
{
123-
v: code,
124-
f: location.label,
125-
},
126-
location.value,
127-
];
128-
} );
117+
if ( geoMode !== 'country' ) {
118+
chartData.addColumn( 'number', 'Latitude' );
119+
chartData.addColumn( 'number', 'Longitude' );
120+
chartData.addColumn( 'string', 'Location' );
121+
chartData.addColumn( 'number', numberLabel || translate( 'Views' ).toString() );
122+
123+
chartData.addRows(
124+
data.reduce( ( filteredLocations, location ) => {
125+
if ( location.coordinates ) {
126+
filteredLocations.push( [
127+
Number( location.coordinates.latitude ),
128+
Number( location.coordinates.longitude ),
129+
location.label,
130+
location.value,
131+
] );
132+
}
133+
134+
return filteredLocations;
135+
}, [] )
136+
);
129137

130-
const chartData = new window.google.visualization.DataTable();
138+
return chartData;
139+
}
140+
141+
// Default to country
131142
chartData.addColumn( 'string', translate( 'Country' ).toString() );
132143
chartData.addColumn( 'number', numberLabel || translate( 'Views' ).toString() );
133-
chartData.addRows( mapData );
144+
chartData.addRows(
145+
map( data, ( location ) => {
146+
return [
147+
{
148+
v: location.countryCode,
149+
f: location.label,
150+
},
151+
location.value,
152+
];
153+
} )
154+
);
155+
156+
return chartData;
157+
};
158+
159+
drawData = () => {
160+
const { currentUserCountryCode, data, geoMode, customHeight } = this.props;
161+
if ( ! data || ! data.length ) {
162+
return;
163+
}
134164

165+
const chartData = this.prepareChartData( data );
135166
// Note that using raw hex values here is an exception due to
136167
// IE11 and other older browser not supporting CSS custom props.
137168
// We have to set values to Google GeoChart via JS. We don't

client/state/stats/lists/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ export const normalizers = {
441441
countryCode: viewData.country_code,
442442
value: viewData.views,
443443
region: country.map_region,
444+
...( viewData.coordinates && { coordinates: viewData.coordinates } ),
444445
};
445446
} );
446447
},

0 commit comments

Comments
 (0)