legendContinuous.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.LegendContinuous = void 0;
  15. const g_1 = require("@antv/g");
  16. const gui_1 = require("@antv/gui");
  17. const scale_1 = require("@antv/scale");
  18. const d3_format_1 = require("d3-format");
  19. const array_1 = require("../utils/array");
  20. const utils_1 = require("./utils");
  21. function calculateFinalSize(size, defaultSize) {
  22. return Math.min(size, defaultSize);
  23. }
  24. function updateShapeDimensions(shape, finalSize, orientation) {
  25. shape.size = finalSize;
  26. if ((0, utils_1.isHorizontal)(orientation)) {
  27. shape.height = finalSize;
  28. }
  29. else {
  30. shape.width = finalSize;
  31. }
  32. return shape;
  33. }
  34. function inferContinuousShape(value, options, component) {
  35. const { size } = options;
  36. const shape = (0, utils_1.inferComponentShape)(value, options, component);
  37. const finalSize = calculateFinalSize(size, exports.LegendContinuous.props.defaultLegendSize);
  38. return updateShapeDimensions(shape, finalSize, shape.orientation);
  39. }
  40. function getFormatter(max) {
  41. return (value) => ({
  42. value: value / max,
  43. label: String(value),
  44. });
  45. }
  46. function getQuantizeOrQuantileConfig(shape, colorScale, min, max, range) {
  47. const thresholds = colorScale.thresholds;
  48. const formatter = getFormatter(max);
  49. return Object.assign(Object.assign({}, shape), { color: range, data: [min, ...thresholds, max].map(formatter) });
  50. }
  51. function getThresholdConfig(shape, colorScale, range) {
  52. const thresholds = colorScale.thresholds;
  53. const data = [-Infinity, ...thresholds, Infinity].map((value, index) => ({
  54. value: index,
  55. label: value,
  56. }));
  57. return Object.assign(Object.assign({}, shape), { data, color: range, labelFilter: (datum, index) => {
  58. return index > 0 && index < data.length - 1;
  59. } });
  60. }
  61. function rangeOf(scale) {
  62. const { domain } = scale.getOptions();
  63. const [min, max] = [domain[0], (0, array_1.lastOf)(domain)];
  64. return [min, max];
  65. }
  66. /**
  67. * if color scale is not defined, create a constant color scale based on default color
  68. * @param scale
  69. * @param theme
  70. */
  71. function createColorScale(scale, theme) {
  72. const { defaultColor } = theme;
  73. const options = scale.getOptions();
  74. const newScale = scale.clone();
  75. newScale.update(Object.assign(Object.assign({}, options), { range: [(0, g_1.parseColor)(defaultColor).toString()] }));
  76. return newScale;
  77. }
  78. function getLinearConfig(shape, colorScale, sizeScale, opacityScale, theme) {
  79. const { length } = shape;
  80. const scale = colorScale || createColorScale(sizeScale || opacityScale, theme);
  81. const [min, max] = rangeOf(scale);
  82. return Object.assign(Object.assign({}, shape), { data: scale.getTicks().map((value) => ({ value })), color: new Array(length).fill(0).map((d, i) => {
  83. const value = ((max - min) / (length - 1)) * i + min;
  84. const color = scale.map(value);
  85. const opacity = opacityScale ? opacityScale.map(value) : 1;
  86. return color.replace(/rgb[a]*\(([\d]{1,3}) *, *([\d]{1,3}) *, *([\d]{1,3})[\S\s]*\)/, (match, p1, p2, p3) => `rgba(${p1}, ${p2}, ${p3}, ${opacity})`);
  87. }) });
  88. }
  89. function inferContinuousConfig(scales, value, options, component, theme) {
  90. const colorScale = (0, utils_1.scaleOf)(scales, 'color');
  91. const shape = inferContinuousShape(value, options, component);
  92. if (colorScale instanceof scale_1.Threshold) {
  93. const { range } = colorScale.getOptions();
  94. const [min, max] = rangeOf(colorScale);
  95. // for quantize, quantile scale
  96. if (colorScale instanceof scale_1.Quantize || colorScale instanceof scale_1.Quantile) {
  97. return getQuantizeOrQuantileConfig(shape, colorScale, min, max, range);
  98. }
  99. // for threshold
  100. return getThresholdConfig(shape, colorScale, range);
  101. }
  102. // for linear, pow, sqrt, log, time, utc scale
  103. const sizeScale = (0, utils_1.scaleOf)(scales, 'size');
  104. const opacityScale = (0, utils_1.scaleOf)(scales, 'opacity');
  105. return getLinearConfig(shape, colorScale, sizeScale, opacityScale, theme);
  106. }
  107. /**
  108. * Guide Component for continuous color scale.
  109. * @todo Custom style.
  110. */
  111. const LegendContinuous = (options) => {
  112. const { labelFormatter, layout, order, orientation, position, size, title, style } = options, rest = __rest(options, ["labelFormatter", "layout", "order", "orientation", "position", "size", "title", "style"]);
  113. return ({ scales, value, theme }) => {
  114. const { bbox } = value;
  115. const { x, y, width, height } = bbox;
  116. const finalLayout = (0, utils_1.inferComponentLayout)(position, layout);
  117. const { continuousLegend: legendTheme = {} } = theme;
  118. const finalStyle = (0, utils_1.adaptor)(Object.assign({}, legendTheme, Object.assign(Object.assign({ titleText: (0, utils_1.titleContent)(title), titleFontSize: 12, handle: false, indicator: false, labelAlign: 'value', labelFormatter: typeof labelFormatter === 'string'
  119. ? (d) => (0, d3_format_1.format)(labelFormatter)(d.label)
  120. : labelFormatter }, inferContinuousConfig(scales, value, options, exports.LegendContinuous, theme)), style), rest));
  121. const layoutWrapper = new utils_1.G2Layout({
  122. style: Object.assign(Object.assign({ x,
  123. y,
  124. width,
  125. height }, finalLayout), {
  126. // @ts-ignore
  127. subOptions: finalStyle }),
  128. });
  129. layoutWrapper.appendChild(new gui_1.Continuous({
  130. style: finalStyle,
  131. }));
  132. return layoutWrapper;
  133. };
  134. };
  135. exports.LegendContinuous = LegendContinuous;
  136. exports.LegendContinuous.props = {
  137. defaultPosition: 'top',
  138. defaultOrientation: 'vertical',
  139. defaultOrder: 1,
  140. defaultSize: 60,
  141. defaultLength: 300,
  142. defaultLegendSize: 60,
  143. };
  144. //# sourceMappingURL=legendContinuous.js.map