transformProps.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = transformProps;
  4. /**
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. */
  22. function transformProps(chartProps) {
  23. const {
  24. width,
  25. height,
  26. datasource = {},
  27. formData,
  28. queryData
  29. } = chartProps;
  30. const {
  31. verboseMap = {}
  32. } = datasource;
  33. const {
  34. colorScheme,
  35. groupby,
  36. metrics
  37. } = formData;
  38. const data = queryData.data.map(({
  39. label,
  40. values
  41. }) => ({
  42. label,
  43. min: values.whisker_low,
  44. max: values.whisker_high,
  45. firstQuartile: values.Q1,
  46. median: values.Q2,
  47. thirdQuartile: values.Q3,
  48. outliers: values.outliers
  49. }));
  50. const xAxisLabel = groupby.join('/');
  51. const yAxisLabel = metrics.length > 0 ? verboseMap[metrics[0]] || metrics[0] : '';
  52. const boxPlotValues = data.reduce((r, e) => {
  53. r.push(e.min, e.max, ...e.outliers);
  54. return r;
  55. }, []);
  56. const minBoxPlotValue = Math.min(...boxPlotValues);
  57. const maxBoxPlotValue = Math.max(...boxPlotValues);
  58. const valueDomain = [minBoxPlotValue - 0.1 * Math.abs(minBoxPlotValue), maxBoxPlotValue + 0.1 * Math.abs(maxBoxPlotValue)];
  59. return {
  60. data,
  61. width,
  62. height,
  63. encoding: {
  64. x: {
  65. field: 'label',
  66. type: 'nominal',
  67. scale: {
  68. type: 'band',
  69. paddingInner: 0.15,
  70. paddingOuter: 0.3
  71. },
  72. axis: {
  73. title: xAxisLabel
  74. }
  75. },
  76. y: {
  77. field: 'value',
  78. type: 'quantitative',
  79. scale: {
  80. type: 'linear',
  81. domain: valueDomain
  82. },
  83. axis: {
  84. title: yAxisLabel,
  85. numTicks: 5,
  86. format: 'SMART_NUMBER'
  87. }
  88. },
  89. color: {
  90. field: 'label',
  91. type: 'nominal',
  92. scale: {
  93. scheme: colorScheme
  94. },
  95. legend: false
  96. }
  97. }
  98. };
  99. }