-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.html
1152 lines (995 loc) · 39.2 KB
/
filter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>Filters</title>
<style>
/* aloglia autocomplete.js styles */
.algolia-autocomplete {
width: 100%;
}
.algolia-autocomplete .aa-input, .algolia-autocomplete .aa-hint {
width: 100%;
}
.algolia-autocomplete .aa-hint {
color: #999;
}
.algolia-autocomplete .aa-dropdown-menu {
width: 100%;
background-color: #fff;
border: 1px solid #999;
border-top: none;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion {
cursor: pointer;
padding: 5px 4px;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor {
background-color: #B2D7FF;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion em {
font-weight: bold;
font-style: normal;
}
/* main styles */
body {
display: flex;
font-family: "Avenir Next W01", "Avenir Next", Avenir, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
}
html,
body,
#viewDiv {
height: 100vh;
width: 100%;
padding: 0;
margin: 0;
flex: auto;
overflow: hidden;
}
#sidebar {
width: 325px;
min-width: 325px;
height: 100%;
padding: 10px;
flex: auto;
overflow: auto;
}
#sidebarItems {
display: flex;
flex-direction: column;
}
#sidebarItems > * {
flex: auto;
padding-bottom: 10px;
}
#sidebarItems:first-child {
padding-top: 10px;
}
.sidebarItemHeader {
font-weight: bold;
}
#datasetName {
font-size: 24px;
}
#recordCount {
margin-left: 12px;
color: #828282;
}
#widget {
/* height: 200px; */
}
.histogramWidget {
height: 200px;
margin-bottom: 16px;
margin-left: 8px;
margin-top: 8px;
}
#widgetMessage {
font-size: 14px;
font-style: italic;
display: none;
}
.timesliderWidget {
min-width: 315px !important; /* override to allow slightly narrower time slider */
margin-bottom: 16px;
}
.valueListWidget label {
display: block;
padding: 4px;
}
.valueListWidget label > .subText {
/* display: none; */
margin-left: 8px;
color: #828282;
font-size: 14px;
}
.valueListWidget label:hover .subText {
/* display: inline; */
}
.valueListSearchBox {
font-family: "Avenir Next W01", "Avenir Next", Avenir, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
margin-top: 4px;
margin-left: 4px;
width: calc(100% - 4px);
}
.valueListSideLink {
display: none;
float: right;
color: #005e95; /* calcite dark-blue */
}
.valueListWidget *:hover > .valueListSideLink {
display: inline;
}
*:focus {
outline: none !important; /* this looks better but I believe has accessibility issues to consider */
}
.miniHistogramWidget {
position: absolute;
top: 13px;
width: 100%;
height: 40px;
}
#zoomToData {
padding: 6px;
background-color: #f8f8f8;
}
/* calcite-tab {
margin-left: 8px;
margin-top: 8px;
}
*/
</style>
<script src="https://unpkg.com/[email protected]/lodash.js"></script>
<script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
<!-- <link rel="stylesheet" href="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.2.5/css/calcite-web.min.css"> -->
<!-- <script src="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.2.5/js/calcite-web.min.js"></script> -->
<!-- calcite components -->
<script
type="module"
src="https://unpkg.com/@esri/[email protected]/dist/calcite/calcite.esm.js"
></script>
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/@esri/[email protected]/dist/calcite/calcite.css"
/>
<script type="module">
import { loadModules, setDefaultOptions } from 'https://unpkg.com/esri-loader/dist/esm/esri-loader.js';
(async () => {
setDefaultOptions({
css: true,
// url: 'http://localhost:8000/buildOutput/init.js',
// version: 'next',
});
const [
Map,
MapView,
FeatureLayer,
generateHistogram,
Histogram,
HistogramRangeSlider,
TimeSlider,
uniqueValues,
webMercatorUtils
] = await loadModules([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/renderers/smartMapping/statistics/histogram",
"esri/widgets/Histogram",
"esri/widgets/HistogramRangeSlider",
"esri/widgets/TimeSlider",
"esri/renderers/smartMapping/statistics/uniqueValues",
"esri/geometry/support/webMercatorUtils"
]);
// URL params
const params = new URLSearchParams(window.location.search);
const datasetId = params.get('dataset');
const datasetSlug = params.get('slug');
const env = params.get('env') || 'prod';
const { layer, dataset } = await loadDataset({ datasetId, datasetSlug, env });
const map = new Map({
basemap: "dark-gray-vector",
layers: [layer]
});
const view = new MapView({
container: "viewDiv",
map: map,
extent: getDatasetExtent(dataset)
});
const layerView = await view.whenLayerView(layer);
// update layerview filter based on histogram widget, debounced
const updateLayerViewWithHistogram = _.throttle(
(layerView, fieldName, histogramWidget) => {
updateLayerViewEffect(layerView, { where: histogramWidget.generateWhereClause(fieldName), updateExtent: zoomToDataCheckbox.checked });
},
50
);
// Dataset info
document.querySelector('#datasetName').innerHTML = dataset.attributes.name;
document.querySelector('#orgName').innerHTML = dataset.attributes.orgName || '';
document.querySelector('#recordCount').innerHTML = `${dataset.attributes.recordCount} records`;
const attributeList = updateAttributeList(dataset);
// widget state tracking
let timeSlider;
// add event listener to dropdown, to create visualizations
attributeList.addEventListener('calciteDropdownItemSelected', async event => {
const fieldName = event.target.getAttribute('data-field');
const field = getDatasetField(dataset, fieldName);
document.querySelector('#attributeListButton').innerHTML = fieldName;
// Reset UI state
// stop timeSlider playback
if (timeSlider && timeSlider.widget) {
timeSlider.widget.stop();
timeSlider = null;
}
// resetTabs(document.getElementById('widget'));
document.getElementById('widget').innerHTML = ''; // clear previous widget
document.getElementById('widgetMessage').innerHTML = ''; // clear previous message
// clear previous filters
updateLayerViewEffect(layerView, { where: null, updateExtent: zoomToDataCheckbox.checked });
// Numeric fields - histogram
if (field.simpleType === 'numeric') {// || field.simpleType === 'date') {
// Histogram
const container = document.createElement('div');
container.classList.add('histogramWidget');
document.getElementById('widget').appendChild(container);
// addTab('Graph', container, document.getElementById('widget'));
const histogramSlider = await createHistogram({ dataset, fieldName, layer, layerView, container, slider: true });
if (histogramSlider.widget) {
if (histogramSlider.coverage < 1) {
widgetMessage.innerText = 'Graph values are approximate';
}
histogramSlider.widget.on(["thumb-change", "thumb-drag", "segment-drag"], event => {
updateLayerViewWithHistogram(layerView, fieldName, histogramSlider.widget);
});
}
}
// Date fields - time slider
if (field.simpleType === 'date') {
// Time slider
const container = document.createElement('div');
container.classList.add('timesliderWidget');
document.getElementById('widget').appendChild(container);
// addTab('Graph', container, document.getElementById('widget'));
timeSlider = await createTimeSlider({ dataset, fieldName, layerView, container });
// set widget state
if (timeSlider.widget) {
// add a nested histogram
const histogramContainer = document.createElement('div');
histogramContainer.classList.add('miniHistogramWidget');
container.getElementsByClassName("esri-slider__track")[0].after(histogramContainer);
const miniHistogram = await createHistogram ({ dataset, fieldName, layer, layerView, container: histogramContainer });
timeSlider.widget.watch("timeExtent", function(value){
// convert Date to unix time stamp with unary + operator
const where = `${fieldName} BETWEEN ${+new Date(value.start)} AND ${+new Date(value.end)}`;
// update layer view filter to reflect current timeExtent
updateLayerViewEffect(layerView, { where });
});
}
}
// All fields - value list
// if (field.simpleType === 'numeric' || field.simpleType === 'string') {
// Value list
const listContainer = document.createElement('div');
listContainer.classList.add('valueListWidget');
// Build filter/where clause and update layer
const onCheckboxChange = ({ checkboxes }) => {
let checked = checkboxes.filter(c => c.checked).map(c => JSON.parse(c.value));
let where = '1=1';
if (checked.length > 0) {
const hasNull = checked.find(c => c.value == null) ? true : false;
checked = checked.filter(c => c.value != null);
let whereVals;
if (field.simpleType === 'date') {
whereVals = checked.map(c => +new Date(c.value));
where = whereVals.map(v => `${fieldName} = ${v}`).join(' OR ');
} else {
whereVals = checked.map(c => {
if (typeof c.value === 'string') {
return `'${c.value}'`
} else {
return c.value;
}
});
where = `${fieldName} IN (${whereVals.join(', ')})`;
}
if (hasNull) {
where = whereVals ? `${where} OR ` : '';
where = `${fieldName} IS NULL`; // need special SQL handling for null vales
}
}
updateLayerViewEffect(layerView, { where, updateExtent: zoomToDataCheckbox.checked });
};
const { fieldStats } = await createValueList({ dataset, fieldName, layer, container: listContainer, onUpdateValues: onCheckboxChange });
if (field.simpleType !== 'numeric' || // always add for strings and dates
fieldStats.topValues.length > 0) { // only add for numerics if there are "categorical" values
document.getElementById('widget').appendChild(listContainer);
// addTab('Values', listContainer, document.getElementById('widget'));
}
// }
});
// On-map UI widgets
view.ui.move('zoom', 'top-right');
view.ui.add('zoomToData', 'bottom-right');
const zoomToDataCheckbox = document.querySelector('#zoomToData calcite-checkbox');
zoomToDataCheckbox.addEventListener('calciteCheckboxChange', () => {
updateLayerViewEffect(layerView, { updateExtent: zoomToDataCheckbox.checked });
});
// put vars on window for debugging
Object.assign(window, { view, map, dataset, layer, layerView, getDatasetField, getDatasetFieldUniqueValues, /*histogram, histogramValues,*/ generateHistogram, HistogramRangeSlider, uniqueValues });
async function loadDataset ({ datasetId, datasetSlug, env }) {
let dataset = {};
if (datasetId) { // dataset id provided directly
// https://opendataqa.arcgis.com/api/v3/datasets/97a641ac39904f349fb5fc25b94207f6
const datasetURL = `https://opendata${env === 'qa' ? 'qa' : ''}.arcgis.com/api/v3/datasets/${datasetId}`;
try {
dataset = (await fetch(datasetURL).then(r => r.json())).data;
} catch(e) { console.log('failed to load dataset from id', datasetId, e); }
} else if (datasetSlug) { // dataset slug provided as alternate
// https://opendata.arcgis.com/api/v3/datasets?filter%5Bslug%5D=kingcounty%3A%3Aphoto-centers-for-2010-king-county-orthoimagery-project-ortho-image10-point
const filter = `${encodeURIComponent('filter[slug]')}=${encodeURIComponent(datasetSlug)}`
const datasetURL = `https://opendata${env === 'qa' ? 'qa' : ''}.arcgis.com/api/v3/datasets?${filter}`;
try {
dataset = (await fetch(datasetURL).then(r => r.json())).data[0];
} catch(e) { console.log('failed to load dataset from slug', datasetSlug, e); }
}
// let symbol = {
// color: [51, 51, 204, 0.9],
// outline: {
// color: 'white',
// width: 0.5
// }
// }
// if (geometryType === 'point') {
// symbol = { ...symbol, type: 'simple-marker', size: '8px' };
// }
// else if (geometryType === 'polyline') {
// symbol = { ...symbol, type: 'simple-line', width: '4px' };
// }
// else if (geometryType === 'polygon') {
// symbol = { ...symbol, type: 'simple-fill' };
// }
const layer = new FeatureLayer({
// renderer: { type: 'simple', symbol },
url: dataset.attributes.url
});
layer.minScale = 0; // draw at all scales
layer.outFields = ["*"]; // get all fields (easier for prototyping, optimize by managing for necessary fields)
return { dataset, layer };
}
function getDatasetExtent (dataset) {
const extent = dataset.attributes.extent;
return {
xmin: extent.coordinates[0][0],
ymin: extent.coordinates[0][1],
xmax: extent.coordinates[1][0],
ymax: extent.coordinates[1][1],
spatialReference: extent.spatialReference
};
}
function getDatasetField (dataset, fieldName) {
fieldName = fieldName.toLowerCase();
const field = dataset.attributes.fields.find(f => f.name.toLowerCase() === fieldName);
const stats = [...Object.entries(dataset.attributes.statistics).values()].find(([, fields]) => fields[fieldName]);
// add "simple type" (numeric, date, string) and stats into rest of field definition
return {
...field,
simpleType: stats && stats[0],
statistics: stats && stats[1][fieldName].statistics
}
}
const DATASET_FIELD_UNIQUE_VALUES = {}; // cache by field name
async function getDatasetFieldUniqueValues (dataset, fieldName, layer) {
if (!DATASET_FIELD_UNIQUE_VALUES[fieldName]) {
const field = getDatasetField(dataset, fieldName);
let stats;
if (field.statistics && field.statistics.uniqueCount) {
stats = { ...field.statistics };
} else {
const uniqueValueInfos = (await uniqueValues({ layer, field: fieldName }))
.uniqueValueInfos
.sort((a, b) => a.count > b.count ? -1 : 1);
const count = uniqueValueInfos.reduce((count, f) => count + f.count, 0);
stats = {
count,
uniqueCount: uniqueValueInfos.length,
values: uniqueValueInfos
}
}
// add percent of records
stats.values = stats.values
// .filter(v => v.value != null && (typeof v.value !== 'string' || v.value.trim() !== ''))
.map(v => ({ ...v, pct: v.count / stats.count }));
// get top values
const maxTopValCount = 12;
// stats.topValues = stats.values.slice(0, maxTopValCount);
stats.topValues = [];
if (stats.uniqueCount < maxTopValCount) {
stats.topValues = stats.values;
} else {
let coverage = 0;
for (let i=0, coverage=0; i < stats.values.length; i++) {
// let stat = { ...stats.values[i], pct: stats.values[i].count / recordCount };
const stat = stats.values[i];
// if (coverage >= 0.80 && stat.pct < 0.05 && stats.topValues.length >= maxTopValCount) break;
if (stat.pct < 0.015 || stats.topValues.length >= maxTopValCount) break;
stats.topValues.push(stat);
coverage += stat.pct;
}
}
DATASET_FIELD_UNIQUE_VALUES[fieldName] = stats;
}
return DATASET_FIELD_UNIQUE_VALUES[fieldName];
}
// Determine if field is an integer
async function datasetFieldIsInteger (field) {
if (field.type.toLowerCase().includes('integer')) { // explicit integer type
return true;
} else { // or check the known values to see if they're all integers
const stats = await getDatasetFieldUniqueValues(dataset, field.name, layer);
return stats.values.every(v => v.value == null || Number.isInteger(v.value));
}
}
// function valueIsNullish (value) {
// // '<Null>'.match(/[^a-zA-Z0-9]*(.+)[^a-zA-Z0-9]*/)
// return value == null ||
// (typeof value === 'string' &&
// value.trim() === '' || value.toLowerCase().
// }
function updateAttributeList (dataset) {
// Attribute dropdown (numeric attributes only right now)
// create <calcite-dropdown-item> for each attribute
const attributeList = document.querySelector('#attributeList > calcite-dropdown-group');
const attributes = [
...Object.entries(dataset.attributes.statistics.numeric || {}),
...Object.entries(dataset.attributes.statistics.date || {}),
...Object.entries(dataset.attributes.statistics.string || {})
];
attributes
.map(([fieldName, { statistics: fieldStats }]) => [fieldName, fieldStats]) // grab stats
.filter(([fieldName, fieldStats]) => { // exclude fields with one value
return !fieldStats ||
!fieldStats.values ||
fieldStats.uniqueCount > 1 || // unique count reported as 0 for sampled data
fieldStats.values.min !== fieldStats.values.max
})
.forEach(([fieldName, fieldStats]) => {
// dataset.attributes.fieldNames
// .map(fieldName => [fieldName, getDatasetField(dataset, fieldName)])
// .filter(([fieldName, field]) => !field.statistics || field.statistics.values.min !== field.statistics.values.max)
// .forEach(([fieldName, field]) => {
const field = getDatasetField(dataset, fieldName);
// const fieldStats = field.statistics.values;
const item = document.createElement('calcite-dropdown-item');
item.innerHTML = `${field.alias || fieldName}`;
if (fieldStats && fieldStats.values && fieldStats.values.min != null && fieldStats.values.max != null) {
if (field.simpleType === 'numeric') {
// TODO: vary precision based on value range
item.innerHTML += ` (${fieldStats.values.min.toFixed(2)} to ${fieldStats.values.max.toFixed(2)})`;
} else if (field.simpleType === 'date') {
item.innerHTML += ` (${formatDate(fieldStats.values.min)} to ${formatDate(fieldStats.values.max)})`;
}
} else if (fieldStats && fieldStats.uniqueCount && field.simpleType === 'string') {
item.innerHTML += ` (${fieldStats.uniqueCount} values)`;
}
// add icon for field type
if (field.simpleType === 'numeric') {
item.iconEnd = 'number';
} else if (field.simpleType === 'string') {
item.iconEnd = 'description';
} else if (field.simpleType === 'date') {
item.iconEnd = 'calendar';
}
item.setAttribute('data-field', field.name);
attributeList.appendChild(item);
});
return attributeList;
}
async function updateLayerViewEffect(layerView, { where = undefined, updateExtent = false } = {}) {
layerView.filter = null;
if (where !== undefined) {
if (where === null) {
layerView.effect = null;
} else {
layerView.effect = {
filter: {
where,
// geometry: layerView.view.extent.clone().expand(0.5) // testing limiting query by geom/viewport
},
excludedEffect: 'grayscale(100%) opacity(15%)'
};
}
}
// adjust view extent (in or out) to fit all filtered data
if (updateExtent) {
try {
let featureExtent;
const queriedExtent = await layer.queryExtent({
where: (layerView.effect && layerView.effect.filter && layerView.effect.filter.where) || '1=1',
outSpatialReference: layerView.view.spatialReference
});
if (queriedExtent.count > 0) {
featureExtent = queriedExtent.extent.expand(1.10);
} else {
return;
}
// const extent = webMercatorUtils.project(featureExtent.extent, layerView.view.spatialReference);
// view.extent =
// console.log(layerView.view.extent.contains(featureExtent), (featureExtent.width * featureExtent.height) / (layerView.view.extent.width * layerView.view.extent.height) < 0.20);
if (!layerView.view.extent.contains(featureExtent) ||
(featureExtent.width * featureExtent.height) / (layerView.view.extent.width * layerView.view.extent.height) < 0.20) {
layerView.view.goTo(featureExtent, { duration: 350 });
}
} catch(e) {
console.log('could not query or project feature extent to update viewport', e);
}
}
}
async function createHistogram ({dataset, fieldName, layer, layerView, container, slider = false }) {
// wrap in another container to handle height without fighting w/JSAPI and rest of sidebar
const parentContainer = container;
container = document.createElement('div');
parentContainer.appendChild(container);
try {
const params = {
layer: layer,
field: fieldName,
numBins: 30
};
let values, source, coverage;
try {
values = await generateHistogram(params);
source = 'widget';
coverage = 1;
} catch(e) {
try {
// histogram generation failed with automated server call, try using features from server query
console.log('histogram generation failed with automated server call, try using features from server query');
params.features = (await layer.queryFeatures()).features;
const featureCount = await layer.queryFeatureCount();
values = await generateHistogram(params);
source = 'layerQuery';
coverage = params.features.length / featureCount;
} catch(e) {
// histogram generation failed with server call, try using features in layer view
console.log('histogram generation failed with server call, try using features in layer view');
params.features = (await layerView.queryFeatures()).features;
const featureCount = await layer.queryFeatureCount();
values = await generateHistogram(params);
source = 'layerView';
coverage = params.features.length / featureCount;
}
}
// Histogram widget (graph only, no range slider)
// const histogram = Histogram.fromHistogramResult(values);
// histogram.container = container;
// Determine if field is an integer
const field = getDatasetField(dataset, fieldName);
const integer = await datasetFieldIsInteger(field);
let widget;
if (slider) {
// Histogram range slider widget
widget = new HistogramRangeSlider({
bins: values.bins,
min: values.minValue,
max: values.maxValue,
values: [values.minValue, values.maxValue],
precision: integer ? 0 : 2,
container: container,
excludedBarColor: "#dddddd",
rangeType: "between",
labelFormatFunction: (value, type) => {
// apply date formatting to histogram
if (field.simpleType == 'date') {
return formatDate(value);
}
return value;
}
});
} else {
// plain histogram, for miniHistogram nested in timeSlider
widget = new Histogram({
bins: values.bins,
min: values.minValue,
max: values.maxValue,
container: container,
rangeType: "between",
});
}
return { widget, values, source, coverage };
}
catch(e) {
console.log('histogram generation failed', e);
return {};
}
}
async function createTimeSlider ({ dataset, fieldName, layerView, container }) {
try {
const field = getDatasetField(dataset, fieldName);
// let {min: startDate, max: endDate } = dataset.attributes.statistics.date[fieldname.toLowerCase()].statistics.values;
const startDate = new Date(field.statistics.values.min);
const endDate = new Date(field.statistics.values.max);
const widget = new TimeSlider({
container: container,
// view: view,
mode: "time-window",
fullTimeExtent: {
start: startDate,
end: endDate,
},
values: [
startDate,
endDate
],
});
// handle play button behavior
let selectionWasFullExtent;
widget.watch('viewModel.state', function(state){
if (state == "playing") {
// check values (date selection) against fullTimeExtent
// convert to numeric values with unary + operator to check equivalence (with a 10% tolerance)
if ( +this.values[0] == +this.fullTimeExtent.start &&
Math.abs(+this.values[1] - +this.fullTimeExtent.end) <
(+new Date(this.fullTimeExtent.end) - +new Date(this.fullTimeExtent.start))/ 10 ) {
// make a note
selectionWasFullExtent = true;
// set new selection end to 10% through the date range
this.values[1] = new Date(+new Date(this.fullTimeExtent.start) + (+new Date(this.fullTimeExtent.end) - +new Date(this.fullTimeExtent.start)) / 10);
}
}
else if (state == "ready" && selectionWasFullExtent) {
// reset note
selectionWasFullExtent = false;
this.values = [this.fullTimeExtent.start, this.fullTimeExtent.end];
}
});
return { widget };
}
catch(e) {
console.log(e);
return {};
}
}
async function createValueList ({ dataset, fieldName, layer, container, onUpdateValues }) {
// <label>
// <calcite-checkbox checked="true"></calcite-checkbox> Switch is on
// </label>
const list = document.createElement('div');
const header = document.createElement('div');
header.innerText = 'Values';
header.classList.add('sidebarItemHeader');
list.appendChild(header);
const checkboxList = document.createElement('div');
list.appendChild(checkboxList);
const field = getDatasetField(dataset, fieldName);
const stats = await getDatasetFieldUniqueValues(dataset, fieldName, layer);
// if (!stats.topValues || stats.topValues.length === 0) {
// return {};
// }
let checkboxListenerDisabled = false;
function addValueListCheckbox (value, checkboxes) {
const checkbox = document.createElement('calcite-checkbox');
checkbox.value = JSON.stringify(value);
// const labelText = document.createTextNode(`${value.value} (${(value.pct * 100).toFixed(2)}% of records)`);
const labelText = document.createElement('span');
// handle null-ish, date, and other field formatting
if (value.value == null || (typeof value.value === 'string' && value.value.trim() === '')) {
labelText.innerHTML = '<span style="color: gray">No value</span>';
} else if (field.simpleType === 'date') {
labelText.innerHTML = formatDate(value.value);
} else {
labelText.innerHTML = value.value;
}
const labelSubText = document.createElement('span');
labelSubText.classList.add('subText');
labelSubText.innerText = value.pct != null ? `${(value.pct * 100).toFixed(2)}%` : '';
const onlyLink = document.createElement('a');
onlyLink.classList.add('valueListSideLink');
onlyLink.href = '#';
onlyLink.innerText = 'only';
const label = document.createElement('label');
label.classList.add('valueListCheckbox');
label.appendChild(checkbox);
label.appendChild(labelText);
label.appendChild(labelSubText);
label.appendChild(onlyLink);
checkbox.addEventListener('calciteCheckboxChange', (event) => {
if (!checkboxListenerDisabled) {
onUpdateValues({ checkboxes, event });
}
});
onlyLink.addEventListener('click', event => {
// disable change listener to keep it from firing as all checkboxes are updated
checkboxListenerDisabled = true;
// check selected box and un-check all others
checkboxes.forEach(c => {
c.checked = c === checkbox ? true : false;
});
// re-enable listener and invoke update handler just once
checkboxListenerDisabled = false;
onUpdateValues({ checkboxes, event });
});
checkboxList.appendChild(label);
return checkbox;
}
// clear all link
const clearLink = document.createElement('a');
clearLink.classList.add('valueListSideLink');
clearLink.href = '#';
clearLink.innerText = 'clear';
header.appendChild(clearLink);
clearLink.addEventListener('click', event => {
// disable change listener to keep it from firing as all checkboxes are updated
checkboxListenerDisabled = true;
// un-check all checkboxes
checkboxes.forEach(c => c.checked = false);
// re-enable listener and invoke update handler just once
checkboxListenerDisabled = false;
onUpdateValues({ checkboxes, event });
});
const checkboxes = [];
checkboxes.push(...stats.topValues.map(value => addValueListCheckbox(value, checkboxes)));
container.appendChild(list);
// search box
if (stats.uniqueCount > stats.topValues.length) {
const searchBox = document.createElement('input');
searchBox.classList.add('valueListSearchBox');
searchBox.type = 'text';
searchBox.placeholder = `Search ${fieldName} values...`;
list.appendChild(searchBox);
function searchSource(params) {
return async function doSearch(query, callback) {
const where = field.simpleType === 'date' ?
`CAST(${fieldName} AS VARCHAR(256)) LIKE lower('%${query}%')` : // convert dates to strings
`lower(${fieldName}) LIKE lower('%${query}%')`
const { features } = await layer.queryFeatures({
// where: `lower(${fieldName}) LIKE lower('%${query}%')`,
// where: `CAST(${fieldName} AS VARCHAR(256)) LIKE lower('%${query}%')`,
where,
orderByFields: [fieldName],
outFields: [fieldName],
returnDistinctValues: true,
num: 10
});
// de-dupe results (by turning into set)
const vals = new Set(features
.map(f => f.attributes[fieldName])
.map(v => typeof v === 'string' ? v.trim() : v)
.filter(value => {
// exclude any results already selected in checkboxes
return !checkboxes
.filter(c => c.checked)
.map(c => JSON.parse(c.value))
.map(v => v.value)
.includes(value);
})
);
// return values
callback([...vals].map(value => ({
value,
label: field.simpleType === 'date' ? formatDate(value) : value
})));
};
}
autocomplete(searchBox, { hint: false, clearOnSelected: true, }, [{
source: searchSource({ hitsPerPage: 5 }),
displayKey: 'label',
templates: {
suggestion: function(suggestion) {
return suggestion.label;
}
}
}]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
// console.log(event, suggestion, dataset, context);
let checkbox = checkboxes
.filter(c => !c.checked)
.find(c => JSON.parse(c.value).value === suggestion.value);
if (!checkbox) {
checkbox = addValueListCheckbox(suggestion, checkboxes);
checkboxes.push(checkbox);
}
checkbox.checked = true;
// TODO: why is this necessary? calcite event listener doesn't fire when checkbox added and set to checked in this flow
onUpdateValues({ checkboxes });
});
}
return { checkboxes, fieldStats: stats };
}
function resetTabs (container) {
// const widget = container.getElementById('widget');
const template = document.querySelector('#widgetTemplate').content.cloneNode(true);
container.innerHTML = '';
container.appendChild(template);
}
function addTab (title, content, container) {
const navContainer = container.querySelector('calcite-tab-nav');
const navItem = document.createElement('calcite-tab-title');
navItem.innerHTML = title;
navContainer.appendChild(navItem);
const contentContainer = container.querySelector('calcite-tabs');
const contentItem = document.createElement('calcite-tab');
contentItem.appendChild(content);
contentContainer.appendChild(contentItem);
}
function createSlider (field, layer, container) {
// <calcite-slider
// min="1"
// max="100"
// minValue="50"
// maxValue="85"
// step="1"
// min-label="Temperature (lower)"
// max-label="Temperature (upper)"
// ></calcite-slider>
}
function formatDate (timestamp) {
const date = new Date(timestamp);
return `${date.getMonth()+1}/${date.getDate()}/${date.getFullYear()}`;
}
// from ember-arcgis-layout-cards/addon/utils/esri/renderer.js
const POINT_RENDERER = {
type: 'simple',
label: '',
description: '',
symbol: {
color: [49, 130, 189, 0.9],
size: 6,
angle: 0,
xoffset: 0,
yoffset: 0,
type: 'simple-marker',
style: 'circle',
outline: {
color: [220, 220, 220, 1],
width: 0.6,
type: 'simple-line',
style: 'solid'
}
}
};