transformProps.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import supercluster from 'supercluster';
  20. import { DEFAULT_POINT_RADIUS, DEFAULT_MAX_ZOOM } from './MapBox';
  21. const NOOP = () => {};
  22. export default function transformProps(chartProps) {
  23. const {
  24. width,
  25. height,
  26. formData,
  27. hooks,
  28. queryData
  29. } = chartProps;
  30. const {
  31. onError = NOOP,
  32. setControlValue = NOOP
  33. } = hooks;
  34. const {
  35. bounds,
  36. geoJSON,
  37. hasCustomMetric,
  38. mapboxApiKey
  39. } = queryData.data;
  40. const {
  41. clusteringRadius,
  42. globalOpacity,
  43. mapboxColor,
  44. mapboxStyle,
  45. pandasAggfunc,
  46. pointRadius,
  47. pointRadiusUnit,
  48. renderWhileDragging
  49. } = formData; // Validate mapbox color
  50. const rgb = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/.exec(mapboxColor);
  51. if (rgb === null) {
  52. onError("Color field must be of form 'rgb(%d, %d, %d)'");
  53. return {};
  54. }
  55. const opts = {
  56. maxZoom: DEFAULT_MAX_ZOOM,
  57. radius: clusteringRadius
  58. };
  59. if (hasCustomMetric) {
  60. opts.initial = () => ({
  61. sum: 0,
  62. squaredSum: 0,
  63. min: Infinity,
  64. max: -Infinity
  65. });
  66. opts.map = prop => ({
  67. sum: prop.metric,
  68. squaredSum: prop.metric ** 2,
  69. min: prop.metric,
  70. max: prop.metric
  71. });
  72. opts.reduce = (accu, prop) => {
  73. // Temporarily disable param-reassignment linting to work with supercluster's api
  74. /* eslint-disable no-param-reassign */
  75. accu.sum += prop.sum;
  76. accu.squaredSum += prop.squaredSum;
  77. accu.min = Math.min(accu.min, prop.min);
  78. accu.max = Math.max(accu.max, prop.max);
  79. /* eslint-enable no-param-reassign */
  80. };
  81. }
  82. const clusterer = supercluster(opts);
  83. clusterer.load(geoJSON.features);
  84. return {
  85. width,
  86. height,
  87. aggregatorName: pandasAggfunc,
  88. bounds,
  89. clusterer,
  90. globalOpacity,
  91. hasCustomMetric,
  92. mapboxApiKey,
  93. mapStyle: mapboxStyle,
  94. onViewportChange({
  95. latitude,
  96. longitude,
  97. zoom
  98. }) {
  99. setControlValue('viewport_longitude', longitude);
  100. setControlValue('viewport_latitude', latitude);
  101. setControlValue('viewport_zoom', zoom);
  102. },
  103. pointRadius: pointRadius === 'Auto' ? DEFAULT_POINT_RADIUS : pointRadius,
  104. pointRadiusUnit,
  105. renderWhileDragging,
  106. rgb
  107. };
  108. }