2bbee8e1ba47aa1a52f56a02f63c936ab868fc57c08160e86167be744f1e3c5417113963f89ae5773df1157a089c19247923e14b248387362f42e34f7e0b3a 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import * as zrUtil from 'zrender/lib/core/util.js';
  2. import { AllPropTypes, Dictionary } from 'zrender/lib/core/types.js';
  3. import { ColorString, BuiltinVisualProperty, VisualOptionPiecewise, VisualOptionUnit, ParsedValue } from '../util/types.js';
  4. declare type RawValue = ParsedValue;
  5. declare type VisualValue = AllPropTypes<VisualOptionUnit>;
  6. declare type NormalizedValue = number;
  7. declare type MappingMethod = 'linear' | 'piecewise' | 'category' | 'fixed';
  8. interface Normalizer {
  9. (this: VisualMapping, value?: RawValue): NormalizedValue;
  10. }
  11. interface ColorMapper {
  12. (this: VisualMapping, value: RawValue | NormalizedValue, isNormalized?: boolean, out?: number[]): ColorString | number[];
  13. }
  14. interface DoMap {
  15. (this: VisualMapping, normalzied?: NormalizedValue, value?: RawValue): VisualValue;
  16. }
  17. interface VisualValueGetter {
  18. (key: string): VisualValue;
  19. }
  20. interface VisualValueSetter {
  21. (key: string, value: VisualValue): void;
  22. }
  23. interface VisualHandler {
  24. applyVisual(this: VisualMapping, value: RawValue, getter: VisualValueGetter, setter: VisualValueSetter): void;
  25. _normalizedToVisual: {
  26. linear(this: VisualMapping, normalized: NormalizedValue): VisualValue;
  27. category(this: VisualMapping, normalized: NormalizedValue): VisualValue;
  28. piecewise(this: VisualMapping, normalzied: NormalizedValue, value: RawValue): VisualValue;
  29. fixed(this: VisualMapping): VisualValue;
  30. };
  31. /**
  32. * Get color mapping for the outside usage.
  33. * Currently only used in `color` visual.
  34. *
  35. * The last parameter out is cached color array.
  36. */
  37. getColorMapper?: (this: VisualMapping) => ColorMapper;
  38. }
  39. interface VisualMappingPiece {
  40. index?: number;
  41. value?: number | string;
  42. interval?: [number, number];
  43. close?: [0 | 1, 0 | 1];
  44. text?: string;
  45. visual?: VisualOptionPiecewise;
  46. }
  47. export interface VisualMappingOption {
  48. type?: BuiltinVisualProperty;
  49. mappingMethod?: MappingMethod;
  50. /**
  51. * required when mappingMethod is 'linear'
  52. */
  53. dataExtent?: [number, number];
  54. /**
  55. * required when mappingMethod is 'piecewise'.
  56. * Visual for only each piece can be specified
  57. * [
  58. * {value: someValue},
  59. * {interval: [min1, max1], visual: {...}},
  60. * {interval: [min2, max2]}
  61. * ],.
  62. */
  63. pieceList?: VisualMappingPiece[];
  64. /**
  65. * required when mappingMethod is 'category'. If no option.categories, categories is set as [0, 1, 2, ...].
  66. */
  67. categories?: (string | number)[];
  68. /**
  69. * Whether loop mapping when mappingMethod is 'category'.
  70. * @default false
  71. */
  72. loop?: boolean;
  73. /**
  74. * Visual data
  75. * when mappingMethod is 'category', visual data can be array or object
  76. * (like: {cate1: '#222', none: '#fff'})
  77. * or primary types (which represents default category visual), otherwise visual
  78. * can be array or primary (which will be normalized to array).
  79. */
  80. visual?: VisualValue[] | Dictionary<VisualValue> | VisualValue;
  81. }
  82. interface VisualMappingInnerPiece extends VisualMappingPiece {
  83. originIndex: number;
  84. }
  85. interface VisualMappingInnerOption extends VisualMappingOption {
  86. hasSpecialVisual: boolean;
  87. pieceList: VisualMappingInnerPiece[];
  88. /**
  89. * Map to get category index
  90. */
  91. categoryMap: Dictionary<number>;
  92. /**
  93. * Cached parsed rgba array from string to avoid parse every time.
  94. */
  95. parsedVisual: number[][];
  96. visual?: VisualValue[] | Dictionary<VisualValue>;
  97. }
  98. declare class VisualMapping {
  99. option: VisualMappingInnerOption;
  100. type: BuiltinVisualProperty;
  101. mappingMethod: MappingMethod;
  102. applyVisual: VisualHandler['applyVisual'];
  103. getColorMapper: VisualHandler['getColorMapper'];
  104. _normalizeData: Normalizer;
  105. _normalizedToVisual: DoMap;
  106. constructor(option: VisualMappingOption);
  107. mapValueToVisual(value: RawValue): VisualValue;
  108. getNormalizer(): zrUtil.Bind1<Normalizer, this>;
  109. static visualHandlers: {
  110. [key in BuiltinVisualProperty]: VisualHandler;
  111. };
  112. /**
  113. * List available visual types.
  114. *
  115. * @public
  116. * @return {Array.<string>}
  117. */
  118. static listVisualTypes(): ("symbol" | "color" | "opacity" | "decal" | "symbolSize" | "liftZ" | "colorAlpha" | "colorLightness" | "colorSaturation" | "colorHue")[];
  119. /**
  120. * @public
  121. */
  122. static isValidType(visualType: string): boolean;
  123. /**
  124. * Convinent method.
  125. * Visual can be Object or Array or primary type.
  126. */
  127. static eachVisual<Ctx, T>(visual: T | T[] | Dictionary<T>, callback: (visual: T, key?: string | number) => void, context?: Ctx): void;
  128. static mapVisual<Ctx, T>(visual: T, callback: (visual: T, key?: string | number) => T, context?: Ctx): T;
  129. static mapVisual<Ctx, T>(visual: T[], callback: (visual: T, key?: string | number) => T[], context?: Ctx): T[];
  130. static mapVisual<Ctx, T>(visual: Dictionary<T>, callback: (visual: T, key?: string | number) => Dictionary<T>, context?: Ctx): Dictionary<T>;
  131. /**
  132. * Retrieve visual properties from given object.
  133. */
  134. static retrieveVisuals(obj: Dictionary<any>): VisualOptionPiecewise;
  135. /**
  136. * Give order to visual types, considering colorSaturation, colorAlpha depends on color.
  137. *
  138. * @public
  139. * @param {(Object|Array)} visualTypes If Object, like: {color: ..., colorSaturation: ...}
  140. * IF Array, like: ['color', 'symbol', 'colorSaturation']
  141. * @return {Array.<string>} Sorted visual types.
  142. */
  143. static prepareVisualTypes(visualTypes: {
  144. [key in BuiltinVisualProperty]?: any;
  145. } | BuiltinVisualProperty[]): (keyof VisualOptionUnit)[];
  146. /**
  147. * 'color', 'colorSaturation', 'colorAlpha', ... are depends on 'color'.
  148. * Other visuals are only depends on themself.
  149. */
  150. static dependsOn(visualType1: BuiltinVisualProperty, visualType2: BuiltinVisualProperty): boolean;
  151. /**
  152. * @param value
  153. * @param pieceList [{value: ..., interval: [min, max]}, ...]
  154. * Always from small to big.
  155. * @param findClosestWhenOutside Default to be false
  156. * @return index
  157. */
  158. static findPieceIndex(value: number, pieceList: VisualMappingPiece[], findClosestWhenOutside?: boolean): number;
  159. }
  160. export default VisualMapping;