transformProps.js 3.1 KB

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