@@ -3,32 +3,38 @@ import {
3
3
React ,
4
4
type AllWidgetProps ,
5
5
DataSourceComponent ,
6
- type FeatureLayerDataSource
6
+ type FeatureLayerDataSource ,
7
+ FormattedMessage ,
8
+ hooks
7
9
} from 'jimu-core'
8
10
import { type FeatureDataRecord } from 'jimu-arcgis'
9
11
import * as echarts from 'echarts'
12
+
13
+ /**********************************LOCAL IMPORTS*********************************/
10
14
import { type IMConfig } from '../config'
15
+ import defaultI18nMessages from './translations/default'
11
16
12
- // DataSource: https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2
17
+ // DataSource FeatureLayer: https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2
18
+ // DataSource WebMap: 019fa672af30459e95ae088c650d745b
13
19
14
20
/******************************WIDGET FUNCTION********************************/
15
21
const Widget = ( props : AllWidgetProps < IMConfig > ) => {
16
22
/******************************LOCAL STATE********************************/
17
- // Extract the useDataSources and id from the widget's props
18
- const { useDataSources, id } = props
19
- // Create a ref for the chart div
20
- const chartRef = React . useRef < HTMLDivElement > ( null )
23
+ const { useDataSources, id, config } = props // Extract the useDataSources and id from the widget's props
24
+ const chartRef = React . useRef < HTMLDivElement > ( null ) // Create a ref for the chart div
21
25
// Create state variables for the data source, chart, series array, fips array, and countyState array
22
26
const [ dataSource , setDataSource ] = React . useState < FeatureLayerDataSource > ( )
23
27
const [ chart , setChart ] = React . useState < echarts . ECharts > ( )
24
28
const [ seriesArray , setSeriesArray ] = React . useState <
25
- Array < { name : string , type : string , stack : string , data : number [ ] } >
29
+ Array < { name : string , type : string , stack : string , data : number [ ] , emphasize : { focus : string } } >
26
30
> ( [ ] )
27
31
const [ fipsArray , setFipsArray ] = React . useState < string [ ] > ( [ ] )
28
32
const [ countyStateArray , setCountyStateArray ] = React . useState < string [ ] > ( [ ] )
29
33
30
- /******************************DATASOURCE FUNCTIONS********************************/
34
+ // USE THE TRANSLATION HOOK
35
+ const t = hooks . useTranslation ( defaultI18nMessages )
31
36
37
+ /******************************DATASOURCE FUNCTIONS********************************/
32
38
/**
33
39
* Set the data source once it is created
34
40
* @param dataSource - The data source set in builder configuration
@@ -54,40 +60,68 @@ const Widget = (props: AllWidgetProps<IMConfig>) => {
54
60
const createChart = ( ) => {
55
61
const option = {
56
62
title : {
57
- text : 'Sample Census county data'
63
+ text : `${ config . sampleCensusCountyData } ` ,
64
+ textAlign : 'left' ,
65
+ textVerticalAlign : 'top'
58
66
} ,
59
67
tooltip : {
60
- trigger : 'axis'
68
+ trigger : 'axis' ,
69
+ axisPointer : {
70
+ type : 'shadow'
71
+ }
61
72
} ,
62
73
legend : {
63
74
data : seriesArray . map ( ( x ) => x . name ) ,
64
75
type : 'scroll' ,
65
76
orient : 'horizontal' ,
66
- top : 'bottom'
77
+ itemGap : 5 ,
78
+ width : 500 ,
79
+ left : 'center' ,
80
+ bottom : 0
67
81
} ,
68
82
dataZoom : [
69
83
{
70
- show : true ,
71
- realtime : true ,
72
- start : 65 ,
73
- end : 85
84
+ type : 'slider' ,
85
+ xAxisIndex : 0 ,
86
+ bottom : 40 ,
87
+ start : 0 ,
88
+ end : 10 ,
89
+ minSpan : 10 ,
90
+ height : 24 ,
91
+ fillerColor : '#d467a7' ,
92
+ moveHandleSize : 8 ,
93
+ moveHandleStyle : { color : '#c90076' } ,
94
+ dataBackground : { areaStyle : { color : '#d467a7' } } ,
95
+ selectedDataBackground : '#b30369'
96
+ } ,
97
+ {
98
+ type : 'inside' ,
99
+ xAxisIndex : 0 ,
100
+ start : 0 ,
101
+ end : 100 ,
102
+ minSpan : 10
74
103
} ,
75
104
{
76
105
type : 'inside' ,
77
- realtime : true ,
78
- start : 65 ,
79
- end : 85
106
+ yAxisIndex : 0 ,
107
+ start : 0 ,
108
+ end : 100 ,
109
+ minSpan : 10
80
110
}
81
111
] ,
82
112
grid : {
83
113
left : '3%' ,
84
114
right : '4%' ,
85
- bottom : '3%' ,
115
+ bottom : '10%' ,
116
+ top : '10%' ,
86
117
containLabel : true
87
118
} ,
88
119
toolbox : {
120
+ show : true ,
89
121
feature : {
90
- saveAsImage : { }
122
+ mark : { show : true } ,
123
+ restore : { show : true } ,
124
+ saveAsImage : { show : true }
91
125
}
92
126
} ,
93
127
xAxis : {
@@ -126,12 +160,11 @@ const Widget = (props: AllWidgetProps<IMConfig>) => {
126
160
. catch ( ( err ) => {
127
161
console . error ( err )
128
162
} )
129
- // Get the max record count (page size) for the data source
130
- const pageSize = dataSource . restLayer . layerDefinition . maxRecordCount
131
- let allRecords : FeatureDataRecord [ ] = [ ]
132
- let page = 1
163
+ const pageSize = dataSource . restLayer . layerDefinition . maxRecordCount // Get the max record count (page size) for the data source
164
+ let allRecords : FeatureDataRecord [ ] = [ ] // Create an array to hold all records
165
+ let page = 1 // Set the initial page to 1
133
166
134
- // Fetch all records from the data source using pagination
167
+ // Fetch all records from the data source using pagination while the allRecords array is less than the record count
135
168
while ( allRecords . length < recordCount ) {
136
169
await dataSource
137
170
. load (
@@ -153,7 +186,7 @@ const Widget = (props: AllWidgetProps<IMConfig>) => {
153
186
} )
154
187
}
155
188
156
- // Create arrays for the chart
189
+ // Create arrays for the chart | TODO: Refactor this to be more dynamic
157
190
const ageUnder5 = allRecords . map ( ( x ) => x . feature . attributes . AGE_UNDER5 )
158
191
const ageFive17 = allRecords . map ( ( x ) => x . feature . attributes . AGE_5_17 )
159
192
const ageEighteen21 = allRecords . map ( ( x ) => x . feature . attributes . AGE_18_21 )
@@ -175,49 +208,91 @@ const Widget = (props: AllWidgetProps<IMConfig>) => {
175
208
name : 'Age under 5' ,
176
209
type : 'bar' ,
177
210
stack : 'Total' ,
178
- data : ageUnder5
211
+ data : ageUnder5 ,
212
+ emphasize : {
213
+ focus : 'series'
214
+ }
179
215
} ,
180
216
{
181
217
name : 'Age 5-17' ,
182
218
type : 'bar' ,
183
219
stack : 'Total' ,
184
- data : ageFive17
220
+ data : ageFive17 ,
221
+ emphasize : {
222
+ focus : 'series'
223
+ }
185
224
} ,
186
225
{
187
226
name : 'Age 18-21' ,
188
227
type : 'bar' ,
189
228
stack : 'Total' ,
190
- data : ageEighteen21
229
+ data : ageEighteen21 ,
230
+ emphasize : {
231
+ focus : 'series'
232
+ }
191
233
} ,
192
234
{
193
235
name : 'Age 22-29' ,
194
236
type : 'bar' ,
195
237
stack : 'Total' ,
196
- data : ageTwentyTwo29
238
+ data : ageTwentyTwo29 ,
239
+ emphasize : {
240
+ focus : 'series'
241
+ }
197
242
} ,
198
243
{
199
244
name : 'Age 30-39' ,
200
245
type : 'bar' ,
201
246
stack : 'Total' ,
202
- data : ageThirty39
247
+ data : ageThirty39 ,
248
+ emphasize : {
249
+ focus : 'series'
250
+ }
203
251
} ,
204
252
{
205
253
name : 'Age 40-49' ,
206
254
type : 'bar' ,
207
255
stack : 'Total' ,
208
- data : ageForty49
256
+ data : ageForty49 ,
257
+ emphasize : {
258
+ focus : 'series'
259
+ }
209
260
} ,
210
261
{
211
262
name : 'Age 50-64' ,
212
263
type : 'bar' ,
213
264
stack : 'Total' ,
214
- data : ageFifty64
265
+ data : ageFifty64 ,
266
+ emphasize : {
267
+ focus : 'series'
268
+ }
215
269
} ,
216
270
{
217
271
name : 'Age 65 and up' ,
218
272
type : 'bar' ,
219
273
stack : 'Total' ,
220
- data : ageSixty5up
274
+ data : ageSixty5up ,
275
+ emphasize : {
276
+ focus : 'series'
277
+ }
278
+ } ,
279
+ {
280
+ name : '65+' ,
281
+ type : 'bar' ,
282
+ stack : 'Total' ,
283
+ data : ageSixty5up ,
284
+ emphasize : {
285
+ focus : 'series'
286
+ }
287
+ } ,
288
+ {
289
+ name : 'Under 5' ,
290
+ type : 'bar' ,
291
+ stack : 'Total' ,
292
+ data : ageUnder5 ,
293
+ emphasize : {
294
+ focus : 'series'
295
+ }
221
296
}
222
297
]
223
298
setCountyStateArray ( countyState )
@@ -259,10 +334,15 @@ const Widget = (props: AllWidgetProps<IMConfig>) => {
259
334
}
260
335
} , [ chart , seriesArray , countyStateArray ] )
261
336
262
- /****************************** MARK-UP********************************/
337
+ /****************************MARK-UP** ********************************/
263
338
// If there is no data source, display a message to select a data source
264
339
if ( ! useDataSources ?. [ 0 ] ) {
265
- return < div > Please select a data source</ div >
340
+ return (
341
+ < FormattedMessage
342
+ id = 'selectDataSource'
343
+ defaultMessage = { t ( 'pleaseSelectDataSource' ) }
344
+ />
345
+ )
266
346
} else {
267
347
// Return the chart and data source component
268
348
return (
0 commit comments