@antv/g2 5.0.6 → 5.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/dist/g2-lite.min.js +4 -4
  2. package/dist/g2.min.js +4 -4
  3. package/esm/animation/morphing.js +4 -0
  4. package/esm/animation/morphing.js.map +1 -1
  5. package/esm/api/chart.d.ts +4 -7
  6. package/esm/api/chart.js +22 -90
  7. package/esm/api/chart.js.map +1 -1
  8. package/esm/api/mark/index.d.ts +3 -1
  9. package/esm/api/mark/index.js +2 -1
  10. package/esm/api/mark/index.js.map +1 -1
  11. package/esm/api/mark/mark.d.ts +7 -1
  12. package/esm/api/mark/mark.js +9 -0
  13. package/esm/api/mark/mark.js.map +1 -1
  14. package/esm/api/utils.d.ts +10 -0
  15. package/esm/api/utils.js +71 -0
  16. package/esm/api/utils.js.map +1 -0
  17. package/esm/component/axis.js +17 -19
  18. package/esm/component/axis.js.map +1 -1
  19. package/esm/component/axisX.js +3 -1
  20. package/esm/component/axisX.js.map +1 -1
  21. package/esm/component/axisY.js +1 -1
  22. package/esm/component/axisY.js.map +1 -1
  23. package/esm/component/constant.js +6 -0
  24. package/esm/component/constant.js.map +1 -1
  25. package/esm/component/legendCategory.js +8 -5
  26. package/esm/component/legendCategory.js.map +1 -1
  27. package/esm/component/legendContinuous.js +91 -33
  28. package/esm/component/legendContinuous.js.map +1 -1
  29. package/esm/component/utils.d.ts +9 -5
  30. package/esm/component/utils.js +14 -14
  31. package/esm/component/utils.js.map +1 -1
  32. package/esm/composition/mark.js +2 -1
  33. package/esm/composition/mark.js.map +1 -1
  34. package/esm/data/index.d.ts +2 -0
  35. package/esm/data/index.js +1 -0
  36. package/esm/data/index.js.map +1 -1
  37. package/esm/data/kde.d.ts +10 -0
  38. package/esm/data/kde.js +30 -0
  39. package/esm/data/kde.js.map +1 -0
  40. package/esm/interaction/brushFilter.js +2 -6
  41. package/esm/interaction/brushFilter.js.map +1 -1
  42. package/esm/interaction/brushHighlight.d.ts +3 -2
  43. package/esm/interaction/brushHighlight.js +202 -28
  44. package/esm/interaction/brushHighlight.js.map +1 -1
  45. package/esm/interaction/brushXHighlight.js +1 -1
  46. package/esm/interaction/brushXHighlight.js.map +1 -1
  47. package/esm/interaction/brushYHighlight.js +1 -1
  48. package/esm/interaction/brushYHighlight.js.map +1 -1
  49. package/esm/interaction/event.d.ts +1 -0
  50. package/esm/interaction/event.js +14 -12
  51. package/esm/interaction/event.js.map +1 -1
  52. package/esm/interaction/tooltip.d.ts +4 -4
  53. package/esm/interaction/tooltip.js +103 -31
  54. package/esm/interaction/tooltip.js.map +1 -1
  55. package/esm/mark/density.d.ts +4 -0
  56. package/esm/mark/density.js +61 -0
  57. package/esm/mark/density.js.map +1 -0
  58. package/esm/mark/gauge.d.ts +7 -0
  59. package/esm/mark/gauge.js +4 -6
  60. package/esm/mark/gauge.js.map +1 -1
  61. package/esm/mark/index.d.ts +2 -0
  62. package/esm/mark/index.js +1 -0
  63. package/esm/mark/index.js.map +1 -1
  64. package/esm/runtime/component.d.ts +4 -2
  65. package/esm/runtime/component.js +164 -12
  66. package/esm/runtime/component.js.map +1 -1
  67. package/esm/runtime/layout.d.ts +3 -3
  68. package/esm/runtime/layout.js +40 -21
  69. package/esm/runtime/layout.js.map +1 -1
  70. package/esm/runtime/plot.js +10 -7
  71. package/esm/runtime/plot.js.map +1 -1
  72. package/esm/runtime/scale.js +2 -2
  73. package/esm/runtime/scale.js.map +1 -1
  74. package/esm/runtime/transform.js +4 -1
  75. package/esm/runtime/transform.js.map +1 -1
  76. package/esm/runtime/types/component.d.ts +1 -0
  77. package/esm/shape/area/area.js +1 -1
  78. package/esm/shape/area/area.js.map +1 -1
  79. package/esm/shape/area/curve.js +1 -1
  80. package/esm/shape/area/curve.js.map +1 -1
  81. package/esm/shape/box/violin.d.ts +3 -0
  82. package/esm/shape/box/violin.js +85 -0
  83. package/esm/shape/box/violin.js.map +1 -0
  84. package/esm/shape/connector/connector.js +1 -1
  85. package/esm/shape/density/density.d.ts +8 -0
  86. package/esm/shape/density/density.js +50 -0
  87. package/esm/shape/density/density.js.map +1 -0
  88. package/esm/shape/index.d.ts +4 -0
  89. package/esm/shape/index.js +2 -0
  90. package/esm/shape/index.js.map +1 -1
  91. package/esm/shape/line/curve.js +1 -1
  92. package/esm/shape/line/curve.js.map +1 -1
  93. package/esm/shape/text/advance.d.ts +1 -1
  94. package/esm/shape/text/advance.js +1 -1
  95. package/esm/shape/text/advance.js.map +1 -1
  96. package/esm/shape/text/badge.js +1 -1
  97. package/esm/shape/utils.js +1 -1
  98. package/esm/shape/utils.js.map +1 -1
  99. package/esm/spec/composition.d.ts +22 -19
  100. package/esm/spec/dataTransform.d.ts +34 -2
  101. package/esm/spec/interaction.d.ts +3 -1
  102. package/esm/spec/mark.d.ts +9 -8
  103. package/esm/spec/transform.d.ts +1 -1
  104. package/esm/spec/utils.d.ts +1 -0
  105. package/esm/stdlib/index.js +5 -5
  106. package/esm/stdlib/index.js.map +1 -1
  107. package/esm/theme/classic.js +3 -0
  108. package/esm/theme/classic.js.map +1 -1
  109. package/esm/theme/classicDark.js +1 -0
  110. package/esm/theme/classicDark.js.map +1 -1
  111. package/esm/transform/filter.js +6 -2
  112. package/esm/transform/filter.js.map +1 -1
  113. package/esm/{shape → utils}/createElement.d.ts +1 -1
  114. package/esm/{shape → utils}/createElement.js.map +1 -1
  115. package/esm/utils/helper.d.ts +3 -0
  116. package/esm/utils/helper.js +3 -0
  117. package/esm/utils/helper.js.map +1 -1
  118. package/esm/utils/scale.js +5 -1
  119. package/esm/utils/scale.js.map +1 -1
  120. package/esm/utils/size.js +7 -4
  121. package/esm/utils/size.js.map +1 -1
  122. package/lib/animation/morphing.js +4 -0
  123. package/lib/animation/morphing.js.map +1 -1
  124. package/lib/api/chart.d.ts +4 -7
  125. package/lib/api/chart.js +27 -97
  126. package/lib/api/chart.js.map +1 -1
  127. package/lib/api/mark/index.d.ts +3 -1
  128. package/lib/api/mark/index.js +1 -0
  129. package/lib/api/mark/index.js.map +1 -1
  130. package/lib/api/mark/mark.d.ts +7 -1
  131. package/lib/api/mark/mark.js +10 -1
  132. package/lib/api/mark/mark.js.map +1 -1
  133. package/lib/api/utils.d.ts +10 -0
  134. package/lib/api/utils.js +80 -0
  135. package/lib/api/utils.js.map +1 -0
  136. package/lib/component/axis.js +17 -19
  137. package/lib/component/axis.js.map +1 -1
  138. package/lib/component/axisX.js +3 -1
  139. package/lib/component/axisX.js.map +1 -1
  140. package/lib/component/axisY.js +1 -1
  141. package/lib/component/axisY.js.map +1 -1
  142. package/lib/component/constant.js +6 -0
  143. package/lib/component/constant.js.map +1 -1
  144. package/lib/component/legendCategory.js +7 -4
  145. package/lib/component/legendCategory.js.map +1 -1
  146. package/lib/component/legendContinuous.js +90 -32
  147. package/lib/component/legendContinuous.js.map +1 -1
  148. package/lib/component/utils.d.ts +9 -5
  149. package/lib/component/utils.js +16 -15
  150. package/lib/component/utils.js.map +1 -1
  151. package/lib/composition/mark.js +2 -1
  152. package/lib/composition/mark.js.map +1 -1
  153. package/lib/data/index.d.ts +2 -0
  154. package/lib/data/index.js +3 -1
  155. package/lib/data/index.js.map +1 -1
  156. package/lib/data/kde.d.ts +10 -0
  157. package/lib/data/kde.js +38 -0
  158. package/lib/data/kde.js.map +1 -0
  159. package/lib/interaction/brushFilter.js +2 -6
  160. package/lib/interaction/brushFilter.js.map +1 -1
  161. package/lib/interaction/brushHighlight.d.ts +3 -2
  162. package/lib/interaction/brushHighlight.js +201 -27
  163. package/lib/interaction/brushHighlight.js.map +1 -1
  164. package/lib/interaction/brushXHighlight.js +1 -1
  165. package/lib/interaction/brushXHighlight.js.map +1 -1
  166. package/lib/interaction/brushYHighlight.js +1 -1
  167. package/lib/interaction/brushYHighlight.js.map +1 -1
  168. package/lib/interaction/event.d.ts +1 -0
  169. package/lib/interaction/event.js +15 -12
  170. package/lib/interaction/event.js.map +1 -1
  171. package/lib/interaction/tooltip.d.ts +4 -4
  172. package/lib/interaction/tooltip.js +102 -30
  173. package/lib/interaction/tooltip.js.map +1 -1
  174. package/lib/mark/density.d.ts +4 -0
  175. package/lib/mark/density.js +65 -0
  176. package/lib/mark/density.js.map +1 -0
  177. package/lib/mark/gauge.d.ts +7 -0
  178. package/lib/mark/gauge.js +4 -6
  179. package/lib/mark/gauge.js.map +1 -1
  180. package/lib/mark/index.d.ts +2 -0
  181. package/lib/mark/index.js +3 -1
  182. package/lib/mark/index.js.map +1 -1
  183. package/lib/runtime/component.d.ts +4 -2
  184. package/lib/runtime/component.js +163 -10
  185. package/lib/runtime/component.js.map +1 -1
  186. package/lib/runtime/layout.d.ts +3 -3
  187. package/lib/runtime/layout.js +40 -21
  188. package/lib/runtime/layout.js.map +1 -1
  189. package/lib/runtime/plot.js +10 -7
  190. package/lib/runtime/plot.js.map +1 -1
  191. package/lib/runtime/scale.js +2 -2
  192. package/lib/runtime/scale.js.map +1 -1
  193. package/lib/runtime/transform.js +4 -1
  194. package/lib/runtime/transform.js.map +1 -1
  195. package/lib/runtime/types/component.d.ts +1 -0
  196. package/lib/shape/area/area.js +1 -1
  197. package/lib/shape/area/area.js.map +1 -1
  198. package/lib/shape/area/curve.js +1 -1
  199. package/lib/shape/area/curve.js.map +1 -1
  200. package/lib/shape/box/violin.d.ts +3 -0
  201. package/lib/shape/box/violin.js +89 -0
  202. package/lib/shape/box/violin.js.map +1 -0
  203. package/lib/shape/connector/connector.js +1 -1
  204. package/lib/shape/density/density.d.ts +8 -0
  205. package/lib/shape/density/density.js +54 -0
  206. package/lib/shape/density/density.js.map +1 -0
  207. package/lib/shape/index.d.ts +4 -0
  208. package/lib/shape/index.js +6 -1
  209. package/lib/shape/index.js.map +1 -1
  210. package/lib/shape/line/curve.js +1 -1
  211. package/lib/shape/line/curve.js.map +1 -1
  212. package/lib/shape/text/advance.d.ts +1 -1
  213. package/lib/shape/text/advance.js +1 -1
  214. package/lib/shape/text/advance.js.map +1 -1
  215. package/lib/shape/text/badge.js +1 -1
  216. package/lib/shape/utils.js +1 -1
  217. package/lib/shape/utils.js.map +1 -1
  218. package/lib/spec/composition.d.ts +22 -19
  219. package/lib/spec/dataTransform.d.ts +34 -2
  220. package/lib/spec/interaction.d.ts +3 -1
  221. package/lib/spec/mark.d.ts +9 -8
  222. package/lib/spec/transform.d.ts +1 -1
  223. package/lib/spec/utils.d.ts +1 -0
  224. package/lib/stdlib/index.js +2 -2
  225. package/lib/stdlib/index.js.map +1 -1
  226. package/lib/theme/classic.js +3 -0
  227. package/lib/theme/classic.js.map +1 -1
  228. package/lib/theme/classicDark.js +1 -0
  229. package/lib/theme/classicDark.js.map +1 -1
  230. package/lib/transform/filter.js +6 -2
  231. package/lib/transform/filter.js.map +1 -1
  232. package/lib/{shape → utils}/createElement.d.ts +1 -1
  233. package/lib/{shape → utils}/createElement.js.map +1 -1
  234. package/lib/utils/helper.d.ts +3 -0
  235. package/lib/utils/helper.js +5 -1
  236. package/lib/utils/helper.js.map +1 -1
  237. package/lib/utils/scale.js +5 -1
  238. package/lib/utils/scale.js.map +1 -1
  239. package/lib/utils/size.js +7 -4
  240. package/lib/utils/size.js.map +1 -1
  241. package/package.json +5 -3
  242. package/src/animation/morphing.ts +5 -0
  243. package/src/api/chart.ts +30 -113
  244. package/src/api/mark/index.ts +3 -0
  245. package/src/api/mark/mark.ts +12 -0
  246. package/src/api/utils.ts +79 -0
  247. package/src/component/axis.ts +22 -18
  248. package/src/component/axisX.ts +3 -1
  249. package/src/component/axisY.ts +2 -1
  250. package/src/component/constant.ts +6 -0
  251. package/src/component/legendCategory.ts +19 -6
  252. package/src/component/legendContinuous.ts +185 -74
  253. package/src/component/utils.ts +14 -3
  254. package/src/composition/mark.ts +2 -0
  255. package/src/data/index.ts +2 -0
  256. package/src/data/kde.ts +53 -0
  257. package/src/interaction/brushFilter.ts +8 -6
  258. package/src/interaction/brushHighlight.ts +258 -20
  259. package/src/interaction/brushXHighlight.ts +1 -0
  260. package/src/interaction/brushYHighlight.ts +1 -0
  261. package/src/interaction/event.ts +19 -13
  262. package/src/interaction/tooltip.ts +154 -25
  263. package/src/mark/density.ts +81 -0
  264. package/src/mark/gauge.ts +13 -7
  265. package/src/mark/index.ts +2 -0
  266. package/src/runtime/component.ts +210 -12
  267. package/src/runtime/layout.ts +74 -19
  268. package/src/runtime/plot.ts +8 -6
  269. package/src/runtime/scale.ts +2 -2
  270. package/src/runtime/transform.ts +2 -1
  271. package/src/runtime/types/component.ts +1 -0
  272. package/src/shape/area/area.ts +1 -1
  273. package/src/shape/area/curve.ts +1 -1
  274. package/src/shape/box/violin.ts +119 -0
  275. package/src/shape/connector/connector.ts +1 -1
  276. package/src/shape/density/density.ts +52 -0
  277. package/src/shape/index.ts +4 -0
  278. package/src/shape/line/curve.ts +1 -1
  279. package/src/shape/text/advance.ts +1 -1
  280. package/src/shape/text/badge.ts +1 -1
  281. package/src/shape/utils.ts +1 -1
  282. package/src/spec/composition.ts +22 -19
  283. package/src/spec/dataTransform.ts +35 -0
  284. package/src/spec/interaction.ts +3 -2
  285. package/src/spec/mark.ts +10 -6
  286. package/src/spec/transform.ts +1 -0
  287. package/src/spec/utils.ts +2 -0
  288. package/src/stdlib/index.ts +8 -0
  289. package/src/theme/classic.ts +3 -0
  290. package/src/theme/classicDark.ts +1 -0
  291. package/src/transform/filter.ts +6 -2
  292. package/src/{shape → utils}/createElement.ts +1 -1
  293. package/src/utils/helper.ts +12 -1
  294. package/src/utils/scale.ts +5 -1
  295. package/src/utils/size.ts +10 -4
  296. /package/esm/{shape → utils}/createElement.js +0 -0
  297. /package/lib/{shape → utils}/createElement.js +0 -0
@@ -2,7 +2,7 @@ import { Coordinate } from '@antv/coord';
2
2
  import type { DisplayObject } from '@antv/g';
3
3
  import { Axis as AxisComponent } from '@antv/gui';
4
4
  import { Linear as LinearScale } from '@antv/scale';
5
- import { deepMix } from '@antv/util';
5
+ import { deepMix, has } from '@antv/util';
6
6
  import { extent } from 'd3-array';
7
7
  import { format } from 'd3-format';
8
8
  import {
@@ -193,34 +193,31 @@ function inferGridLength(position: GCP, coordinate: Coordinate) {
193
193
  }
194
194
 
195
195
  function inferLabelOverlap(transform = [], style: Record<string, any>) {
196
- const {
197
- labelAutoRotate = true,
198
- labelAutoHide = false,
199
- labelAutoEllipsis = false,
200
- } = style;
196
+ if (transform.length > 0) return transform;
197
+ const { labelAutoRotate, labelAutoHide, labelAutoEllipsis, labelAutoWrap } =
198
+ style;
201
199
 
202
- const finalTransforms = [...transform];
200
+ const finalTransforms = [];
203
201
 
204
- const mutateLabelOverlap = (overlap, state) => {
202
+ const addToTransforms = (overlap, state) => {
205
203
  if (state) {
206
- const index = finalTransforms.findIndex((t) => t.type === overlap.type);
207
- if (index !== -1) finalTransforms[index] = overlap;
208
- else finalTransforms.push(overlap);
209
- } else {
210
- const index = finalTransforms.findIndex((t) => t.type === overlap.type);
211
- if (index !== -1) finalTransforms.splice(index, 1);
204
+ finalTransforms.push(overlap);
212
205
  }
213
206
  };
214
207
 
215
- mutateLabelOverlap(
208
+ addToTransforms(
216
209
  {
217
210
  type: 'rotate',
218
211
  optionalAngles: [0, 15, 30, 45, 60, 90],
219
212
  },
220
213
  labelAutoRotate,
221
214
  );
222
- mutateLabelOverlap({ type: 'ellipsis', minLength: 20 }, labelAutoEllipsis);
223
- mutateLabelOverlap({ type: 'hide' }, labelAutoHide);
215
+ addToTransforms({ type: 'ellipsis', minLength: 20 }, labelAutoEllipsis);
216
+ addToTransforms({ type: 'hide' }, labelAutoHide);
217
+ addToTransforms(
218
+ { type: 'wrap', wordWrapWidth: 100, maxLines: 3, recoveryWhenFail: true },
219
+ labelAutoWrap,
220
+ );
224
221
  return finalTransforms;
225
222
  }
226
223
 
@@ -480,6 +477,7 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
480
477
  tickFilter,
481
478
  tickMethod,
482
479
  transform,
480
+ indexBBox,
483
481
  ...userDefinitions
484
482
  } = options;
485
483
 
@@ -503,7 +501,6 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
503
501
  };
504
502
 
505
503
  const gridLength = inferGridLength(position, coordinate);
506
-
507
504
  const overrideStyle = inferAxisLinearOverrideStyle(
508
505
  position,
509
506
  orientation,
@@ -523,16 +520,23 @@ const LinearAxisComponent: GCC<AxisOptions> = (options) => {
523
520
  position,
524
521
  coordinate,
525
522
  ),
523
+ crossSize: size,
526
524
  titleText: titleContent(title),
527
525
  labelOverlap: inferLabelOverlap(transform, internalAxisStyle),
528
526
  grid: inferGrid(internalAxisStyle.grid, coordinate, scale),
529
527
  gridLength,
530
528
  // Always showLine, make title could align the end of axis.
531
529
  line: true,
530
+ indexBBox,
532
531
  ...(!internalAxisStyle.line ? { lineOpacity: 0 } : null),
533
532
  ...overrideStyle,
534
533
  ...important,
535
534
  };
535
+
536
+ // For hide overlap, do not set crossSize.
537
+ const hasHide = finalAxisStyle.labelOverlap.find((d) => d.type === 'hide');
538
+ if (hasHide) finalAxisStyle.crossSize = false;
539
+
536
540
  return new AxisComponent({
537
541
  className: 'axis',
538
542
  style: adaptor(finalAxisStyle),
@@ -7,7 +7,9 @@ export type AxisXOptions = AxisOptions;
7
7
  * LinearAxis component bind to x scale.
8
8
  */
9
9
  export const AxisX: GCC<AxisXOptions> = (options) => {
10
- return LinearAxis(options);
10
+ return (...args) =>
11
+ // empirical value for crossPadding
12
+ LinearAxis(Object.assign({}, { crossPadding: 50 }, options))(...args);
11
13
  };
12
14
 
13
15
  AxisX.props = {
@@ -7,7 +7,8 @@ export type AxisYOptions = AxisOptions;
7
7
  * LinearAxis component bind to y scale.
8
8
  */
9
9
  export const AxisY: GCC<AxisYOptions> = (options) => {
10
- return LinearAxis(options);
10
+ return (...args) =>
11
+ LinearAxis(Object.assign({}, { crossPadding: 10 }, options))(...args);
11
12
  };
12
13
 
13
14
  AxisY.props = {
@@ -198,6 +198,11 @@ export const LEGEND_INFER_STRATEGIES: InferStrategy[] = [
198
198
  ['color', 'constant'],
199
199
  ['size', 'continuous'],
200
200
  ],
201
+ [
202
+ ['size', 'continuous'],
203
+ ['opacity', 'continuous'],
204
+ ],
205
+ [['size', 'continuous']],
201
206
  ],
202
207
  ],
203
208
  [
@@ -232,6 +237,7 @@ export const LEGEND_INFER_STRATEGIES: InferStrategy[] = [
232
237
  ['opacity', 'continuous'],
233
238
  ],
234
239
  [['color', 'continuous']],
240
+ [['opacity', 'continuous']],
235
241
  ],
236
242
  ],
237
243
  ];
@@ -19,7 +19,7 @@ import { useMarker } from '../utils/marker';
19
19
  import {
20
20
  adaptor,
21
21
  domainOf,
22
- G2Layout,
22
+ LegendCategoryLayout,
23
23
  inferComponentLayout,
24
24
  inferComponentShape,
25
25
  scaleOf,
@@ -124,9 +124,15 @@ function inferItemMarker(
124
124
  }
125
125
  if (typeof itemMarker === 'function') return itemMarker;
126
126
  return (d, i) =>
127
- createShape(itemMarker || shapes[i], library, coordinate, theme, {
128
- color: d.color,
129
- });
127
+ createShape(
128
+ itemMarker || `${namespace}.${shapes[i]}`,
129
+ library,
130
+ coordinate,
131
+ theme,
132
+ {
133
+ color: d.color,
134
+ },
135
+ );
130
136
  }
131
137
 
132
138
  function inferItemMarkerOpacity(scales: Scale[]) {
@@ -238,20 +244,27 @@ export const LegendCategory: GCC<LegendCategoryOptions> = (options) => {
238
244
  };
239
245
 
240
246
  const { legend: legendTheme = {} } = theme;
241
- const layoutWrapper = new G2Layout({
247
+
248
+ const categoryStyle = adaptor(
249
+ Object.assign({}, legendTheme, legendStyle, style),
250
+ );
251
+
252
+ const layoutWrapper = new LegendCategoryLayout({
242
253
  style: {
243
254
  x: bbox.x,
244
255
  y: bbox.y,
245
256
  width: bbox.width,
246
257
  height: bbox.height,
247
258
  ...finalLayout,
259
+ // @ts-ignore
260
+ subOptions: categoryStyle,
248
261
  },
249
262
  });
250
263
 
251
264
  layoutWrapper.appendChild(
252
265
  new Category({
253
266
  className: 'legend-category',
254
- style: adaptor(Object.assign({}, legendTheme, legendStyle, style)),
267
+ style: categoryStyle,
255
268
  }),
256
269
  );
257
270
  return layoutWrapper;
@@ -1,17 +1,22 @@
1
+ import { parseColor } from '@antv/g';
1
2
  import { Continuous } from '@antv/gui';
2
3
  import { Quantile, Quantize, Threshold } from '@antv/scale';
3
4
  import { format } from 'd3-format';
4
5
  import type {
5
6
  FlexLayout,
7
+ G2Theme,
6
8
  GuideComponentComponent as GCC,
9
+ GuideComponentOrientation as GCO,
7
10
  GuideComponentPosition as GCP,
8
11
  Scale,
9
12
  } from '../runtime';
13
+ import { lastOf } from '../utils/array';
10
14
  import {
11
- adaptor,
12
15
  G2Layout,
16
+ adaptor,
13
17
  inferComponentLayout,
14
18
  inferComponentShape,
19
+ isHorizontal,
15
20
  scaleOf,
16
21
  titleContent,
17
22
  } from './utils';
@@ -23,68 +28,171 @@ export type LegendContinuousOptions = {
23
28
  [key: string]: any;
24
29
  };
25
30
 
26
- function inferContinuousConfig(
27
- scales: Scale[],
31
+ type Shape = {
32
+ orientation: string;
33
+ width: number;
34
+ height: number;
35
+ length: number;
36
+ size: number;
37
+ };
38
+
39
+ type Config = {
40
+ orientation: string;
41
+ width: number;
42
+ height: number;
43
+ color: string[];
44
+ data: any[];
45
+ labelFilter?: (datum: any, index: number) => boolean;
46
+ };
47
+
48
+ function calculateFinalSize(size: number, defaultSize: number): number {
49
+ return Math.min(size, defaultSize);
50
+ }
51
+
52
+ function updateShapeDimensions(
53
+ shape: Shape,
54
+ finalSize: number,
55
+ orientation: Exclude<GCO, number>,
56
+ ): Shape {
57
+ shape.size = finalSize;
58
+ if (isHorizontal(orientation)) {
59
+ shape.height = finalSize;
60
+ } else {
61
+ shape.width = finalSize;
62
+ }
63
+ return shape;
64
+ }
65
+
66
+ function inferContinuousShape(
28
67
  value: Record<string, any>,
29
68
  options: LegendContinuousOptions,
30
69
  component: GCC,
31
- ) {
32
- const colorScale = scaleOf(scales, 'color');
33
- const { domain, range } = colorScale.getOptions();
34
- const [min, max] = [domain[0], domain.slice(-1)[0]];
35
- const { orientation, width, height, length } = inferComponentShape(
36
- value,
37
- options,
38
- component,
70
+ ): Shape {
71
+ const { size } = options;
72
+ const shape = inferComponentShape(value, options, component);
73
+ const finalSize = calculateFinalSize(
74
+ size,
75
+ LegendContinuous.props.defaultLegendSize,
39
76
  );
77
+ return updateShapeDimensions(shape, finalSize, shape.orientation);
78
+ }
40
79
 
41
- const shape = { orientation, width, height };
80
+ function getFormatter(max: number) {
81
+ return (value: number) => ({
82
+ value: value / max,
83
+ label: String(value),
84
+ });
85
+ }
42
86
 
43
- if (colorScale instanceof Threshold) {
44
- const thresholds = (colorScale as any).thresholds as number[];
45
- // for quantize, quantile scale
46
- if (colorScale instanceof Quantize || colorScale instanceof Quantile) {
47
- return {
48
- color: range,
49
- ...shape,
50
- data: [min, ...thresholds, max].map((value, index) => ({
51
- value: value / max,
52
- label: value,
53
- })),
54
- };
55
- }
56
- // for threshhold
57
- const data = [-Infinity, ...thresholds, Infinity].map((value, index) => ({
58
- value: index,
59
- label: value,
60
- }));
61
- return {
62
- data,
63
- ...shape,
64
- color: range,
65
- labelFilter: (datum, index) => {
66
- return index > 0 && index < data.length - 1;
67
- },
68
- };
69
- }
87
+ function getQuantizeOrQuantileConfig(
88
+ shape: Shape,
89
+ colorScale: Threshold,
90
+ min: number,
91
+ max: number,
92
+ range: string[],
93
+ ): Config {
94
+ const thresholds = (colorScale as any).thresholds as number[];
95
+ const formatter = getFormatter(max);
96
+ return {
97
+ ...shape,
98
+ color: range,
99
+ data: [min, ...thresholds, max].map(formatter),
100
+ };
101
+ }
70
102
 
71
- // for linear, pow, sqrt, log, time, utc scale
72
- const opacityScale = scaleOf(scales, 'opacity');
103
+ function getThresholdConfig(
104
+ shape: Shape,
105
+ colorScale: Threshold,
106
+ range: string[],
107
+ ): Config {
108
+ const thresholds = (colorScale as any).thresholds as number[];
109
+ const data = [-Infinity, ...thresholds, Infinity].map((value, index) => ({
110
+ value: index,
111
+ label: value,
112
+ }));
73
113
  return {
74
114
  ...shape,
75
- data: colorScale.getTicks().map((value) => ({ value })),
115
+ data,
116
+ color: range,
117
+ labelFilter: (datum, index) => {
118
+ return index > 0 && index < data.length - 1;
119
+ },
120
+ };
121
+ }
122
+
123
+ function rangeOf(scale: Scale) {
124
+ const { domain } = scale.getOptions();
125
+ const [min, max] = [domain[0], lastOf(domain)];
126
+ return [min, max];
127
+ }
128
+
129
+ /**
130
+ * if color scale is not defined, create a constant color scale based on default color
131
+ * @param scale
132
+ * @param theme
133
+ */
134
+ function createColorScale(scale: Scale, theme: G2Theme): Scale {
135
+ const { defaultColor } = theme;
136
+ const options = scale.getOptions();
137
+ const newScale = scale.clone();
138
+ newScale.update({ ...options, range: [parseColor(defaultColor).toString()] });
139
+ return newScale;
140
+ }
141
+
142
+ function getLinearConfig(
143
+ shape: Shape,
144
+ colorScale: Scale,
145
+ sizeScale: Scale,
146
+ opacityScale: Scale,
147
+ theme: G2Theme,
148
+ ): Config {
149
+ const { length } = shape;
150
+ const scale =
151
+ colorScale || createColorScale(sizeScale || opacityScale, theme);
152
+ const [min, max] = rangeOf(scale);
153
+
154
+ return {
155
+ ...shape,
156
+ data: scale.getTicks().map((value) => ({ value })),
76
157
  color: new Array(length).fill(0).map((d, i) => {
77
158
  const value = ((max - min) / (length - 1)) * i + min;
78
- const color = colorScale.map(value);
159
+ const color = scale.map(value);
79
160
  const opacity = opacityScale ? opacityScale.map(value) : 1;
80
161
  return color.replace(
81
- /rgb[a]*\(([\d]{1,3}), ([\d]{1,3}), ([\d]{1,3})[\S\s]*\)/,
162
+ /rgb[a]*\(([\d]{1,3}) *, *([\d]{1,3}) *, *([\d]{1,3})[\S\s]*\)/,
82
163
  (match, p1, p2, p3) => `rgba(${p1}, ${p2}, ${p3}, ${opacity})`,
83
164
  );
84
165
  }),
85
166
  };
86
167
  }
87
168
 
169
+ function inferContinuousConfig(
170
+ scales: Scale[],
171
+ value: Record<string, any>,
172
+ options: LegendContinuousOptions,
173
+ component: GCC,
174
+ theme: G2Theme,
175
+ ): Config {
176
+ const colorScale = scaleOf(scales, 'color');
177
+ const shape = inferContinuousShape(value, options, component);
178
+
179
+ if (colorScale instanceof Threshold) {
180
+ const { range } = colorScale.getOptions();
181
+ const [min, max] = rangeOf(colorScale);
182
+ // for quantize, quantile scale
183
+ if (colorScale instanceof Quantize || colorScale instanceof Quantile) {
184
+ return getQuantizeOrQuantileConfig(shape, colorScale, min, max, range);
185
+ }
186
+ // for threshold
187
+ return getThresholdConfig(shape, colorScale, range);
188
+ }
189
+
190
+ // for linear, pow, sqrt, log, time, utc scale
191
+ const sizeScale = scaleOf(scales, 'size');
192
+ const opacityScale = scaleOf(scales, 'opacity');
193
+ return getLinearConfig(shape, colorScale, sizeScale, opacityScale, theme);
194
+ }
195
+
88
196
  /**
89
197
  * Guide Component for continuous color scale.
90
198
  * @todo Custom style.
@@ -101,14 +209,40 @@ export const LegendContinuous: GCC<LegendContinuousOptions> = (options) => {
101
209
  style,
102
210
  ...rest
103
211
  } = options;
212
+
104
213
  return ({ scales, value, theme }) => {
105
214
  const { bbox } = value;
106
215
  const { x, y, width, height } = bbox;
216
+ const finalLayout = inferComponentLayout(position, layout);
107
217
 
108
- const finalLayout = inferComponentLayout(
109
- position,
110
- value.scales?.[0]?.guide?.layout,
218
+ const { continuousLegend: legendTheme = {} } = theme;
219
+ const finalStyle = adaptor(
220
+ Object.assign(
221
+ {},
222
+ legendTheme,
223
+ {
224
+ titleText: titleContent(title),
225
+ titleFontSize: 12,
226
+ handle: false,
227
+ indicator: false,
228
+ labelAlign: 'value',
229
+ labelFormatter:
230
+ typeof labelFormatter === 'string'
231
+ ? (d) => format(labelFormatter)(d.label)
232
+ : labelFormatter,
233
+ ...inferContinuousConfig(
234
+ scales,
235
+ value,
236
+ options,
237
+ LegendContinuous,
238
+ theme,
239
+ ),
240
+ ...style,
241
+ },
242
+ rest,
243
+ ),
111
244
  );
245
+
112
246
  const layoutWrapper = new G2Layout({
113
247
  style: {
114
248
  x,
@@ -116,38 +250,14 @@ export const LegendContinuous: GCC<LegendContinuousOptions> = (options) => {
116
250
  width,
117
251
  height,
118
252
  ...finalLayout,
253
+ // @ts-ignore
254
+ subOptions: finalStyle,
119
255
  },
120
256
  });
121
257
 
122
- const { continuousLegend: legendTheme = {} } = theme;
123
-
124
258
  layoutWrapper.appendChild(
125
259
  new Continuous({
126
- style: adaptor(
127
- Object.assign(
128
- {},
129
- legendTheme,
130
- {
131
- titleText: titleContent(title),
132
- titleFontSize: 12,
133
- handle: false,
134
- indicator: false,
135
- labelAlign: 'value',
136
- labelFormatter:
137
- typeof labelFormatter === 'string'
138
- ? (d) => format(labelFormatter)(d.label)
139
- : labelFormatter,
140
- ...inferContinuousConfig(
141
- scales,
142
- value,
143
- options,
144
- LegendContinuous,
145
- ),
146
- ...style,
147
- },
148
- rest,
149
- ),
150
- ),
260
+ style: finalStyle,
151
261
  }),
152
262
  );
153
263
 
@@ -161,4 +271,5 @@ LegendContinuous.props = {
161
271
  defaultOrder: 1,
162
272
  defaultSize: 60,
163
273
  defaultLength: 300,
274
+ defaultLegendSize: 60,
164
275
  };
@@ -83,10 +83,21 @@ export function inferComponentLayout(
83
83
  }
84
84
 
85
85
  export class G2Layout extends Layout {
86
+ get child() {
87
+ return this.children?.[0] as any;
88
+ }
89
+
86
90
  update(options: any) {
87
91
  this.attr(options);
88
- const { width, height, ...restOptions } = options;
89
- (this.children?.[0] as any)?.update(restOptions);
92
+ const { subOptions } = options;
93
+ this.child?.update(subOptions);
94
+ }
95
+ }
96
+
97
+ export class LegendCategoryLayout extends G2Layout {
98
+ update(options: any) {
99
+ const { subOptions } = options;
100
+ this.child?.update(subOptions);
90
101
  }
91
102
  }
92
103
 
@@ -122,7 +133,7 @@ export function inferComponentShape(
122
133
  const length = userDefinedLength || defaultLength || bboxLength;
123
134
  const orientation = isHorizontal ? 'horizontal' : 'vertical';
124
135
  const [width, height] = isHorizontal ? [length, size] : [size, length];
125
- return { orientation, width, height, size, length };
136
+ return { orientation, width, height, size, length } as const;
126
137
  }
127
138
 
128
139
  export function domainOf(scales: Scale[]): any[] {
@@ -37,6 +37,7 @@ export const Mark: CC<MarkOptions> = () => {
37
37
  labelTransform,
38
38
  parentKey,
39
39
  clip,
40
+ viewStyle,
40
41
  ...mark
41
42
  } = options;
42
43
 
@@ -72,6 +73,7 @@ export const Mark: CC<MarkOptions> = () => {
72
73
  marginRight,
73
74
  parentKey,
74
75
  clip,
76
+ style: viewStyle,
75
77
  marks: [{ ...mark, key: `${key}-0`, data }],
76
78
  },
77
79
  ];
package/src/data/index.ts CHANGED
@@ -15,6 +15,7 @@ export { Arc } from './arc';
15
15
  export { WordCloud } from './wordCloud';
16
16
  export { Join } from './join';
17
17
  export { Slice } from './slice';
18
+ export { KDE } from './kde';
18
19
 
19
20
  export type { FetchOptions } from './fetch';
20
21
  export type { FoldOptions } from './fold';
@@ -33,3 +34,4 @@ export type { ArcOptions } from './arc';
33
34
  export type { WordCloudOptions } from './wordCloud';
34
35
  export type { JoinOptions } from './join';
35
36
  export type { SliceOptions } from './slice';
37
+ export type { KDEOptions } from './kde';
@@ -0,0 +1,53 @@
1
+ import pdf from 'pdfast';
2
+ import { group } from 'd3-array';
3
+ import { DataComponent as DC } from '../runtime';
4
+ import { KDEDataTransform } from '../spec';
5
+
6
+ export type KDEOptions = Omit<KDEDataTransform, 'type'>;
7
+
8
+ export function defined(d: any): boolean {
9
+ return d !== undefined && d !== null && !Number.isNaN(d);
10
+ }
11
+
12
+ /**
13
+ * Kernel Density Estimation base on [pdfast](https://www.npmjs.com/package/pdfast),
14
+ * generating probability density function (pdf) using triangular kernel,
15
+ * optimized to run in O(N + K).
16
+ */
17
+ export const KDE: DC<KDEOptions> = (options) => {
18
+ const {
19
+ field,
20
+ groupBy,
21
+ as = ['y', 'size'],
22
+ min,
23
+ max,
24
+ size = 10,
25
+ width,
26
+ } = options;
27
+ const [yField, sizeField] = as;
28
+ return (data) => {
29
+ const gs = Array.from(
30
+ group(data, (d) => groupBy.map((gb) => d[gb]).join('-')).values(),
31
+ );
32
+ return gs.map((g) => {
33
+ const pdfResult = pdf.create(
34
+ g.map((i) => i[field]),
35
+ {
36
+ min,
37
+ max,
38
+ size,
39
+ width,
40
+ },
41
+ );
42
+ const _y = pdfResult.map((result) => result.x);
43
+ const _size = pdfResult.map((result) => result.y);
44
+ return {
45
+ ...(g[0] as Record<string, unknown>),
46
+ [yField]: _y,
47
+ [sizeField]: _size,
48
+ };
49
+ });
50
+ };
51
+ };
52
+
53
+ KDE.props = {};
@@ -131,9 +131,10 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
131
131
  );
132
132
 
133
133
  // Emit event.
134
- event.data = event.data || {};
135
- event.data.selection = [domainX, domainY];
136
- emitter.emit('brush:filter', event);
134
+ emitter.emit('brush:filter', {
135
+ ...event,
136
+ data: { selection: [domainX, domainY] },
137
+ });
137
138
 
138
139
  // Rerender and update view.
139
140
  const newOptions = {
@@ -154,9 +155,10 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
154
155
  const { x: scaleX, y: scaleY } = scale;
155
156
  const domainX = scaleX.getOptions().domain;
156
157
  const domainY = scaleY.getOptions().domain;
157
- event.data = event.data || {};
158
- event.data.selection = [domainX, domainY];
159
- emitter.emit('brush:filter', event);
158
+ emitter.emit('brush:filter', {
159
+ ...event,
160
+ data: { selection: [domainX, domainY] },
161
+ });
160
162
 
161
163
  filtered = false;
162
164
  newView = view;