@antv/g2plot 2.3.31 → 2.3.35

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 (298) hide show
  1. package/CHANGELOG.md +1270 -0
  2. package/{README.zh-CN.md → README.en-US.md} +33 -42
  3. package/README.md +43 -32
  4. package/dist/g2plot.min.js +3 -18
  5. package/dist/g2plot.min.js.map +1 -1
  6. package/esm/adaptor/common.d.ts +1 -0
  7. package/esm/adaptor/common.js +1 -0
  8. package/esm/adaptor/common.js.map +1 -1
  9. package/esm/adaptor/pattern.d.ts +10 -0
  10. package/esm/adaptor/pattern.js +54 -0
  11. package/esm/adaptor/pattern.js.map +1 -0
  12. package/esm/core/plot.d.ts +11 -1
  13. package/esm/core/plot.js +42 -1
  14. package/esm/core/plot.js.map +1 -1
  15. package/esm/index.d.ts +5 -1
  16. package/esm/index.js +5 -1
  17. package/esm/index.js.map +1 -1
  18. package/esm/plots/area/adaptor.js +2 -2
  19. package/esm/plots/area/adaptor.js.map +1 -1
  20. package/esm/plots/circle-packing/adaptor.js +4 -4
  21. package/esm/plots/circle-packing/adaptor.js.map +1 -1
  22. package/esm/plots/circle-packing/utils.d.ts +0 -5
  23. package/esm/plots/circle-packing/utils.js +1 -20
  24. package/esm/plots/circle-packing/utils.js.map +1 -1
  25. package/esm/plots/column/adaptor.js +3 -2
  26. package/esm/plots/column/adaptor.js.map +1 -1
  27. package/esm/plots/dual-axes/adaptor.js +6 -4
  28. package/esm/plots/dual-axes/adaptor.js.map +1 -1
  29. package/esm/plots/dual-axes/types.d.ts +2 -1
  30. package/esm/plots/dual-axes/types.js.map +1 -1
  31. package/esm/plots/dual-axes/util/geometry.js +3 -2
  32. package/esm/plots/dual-axes/util/geometry.js.map +1 -1
  33. package/esm/plots/dual-axes/util/legend.js +4 -1
  34. package/esm/plots/dual-axes/util/legend.js.map +1 -1
  35. package/esm/plots/funnel/geometries/basic.js +2 -7
  36. package/esm/plots/funnel/geometries/basic.js.map +1 -1
  37. package/esm/plots/heatmap/adaptor.js +2 -2
  38. package/esm/plots/heatmap/adaptor.js.map +1 -1
  39. package/esm/plots/heatmap/types.d.ts +4 -2
  40. package/esm/plots/heatmap/types.js.map +1 -1
  41. package/esm/plots/histogram/adaptor.js +2 -1
  42. package/esm/plots/histogram/adaptor.js.map +1 -1
  43. package/esm/plots/liquid/adaptor.js +2 -2
  44. package/esm/plots/liquid/adaptor.js.map +1 -1
  45. package/esm/plots/pie/adaptor.js +2 -1
  46. package/esm/plots/pie/adaptor.js.map +1 -1
  47. package/esm/plots/radial-bar/adaptor.js +2 -1
  48. package/esm/plots/radial-bar/adaptor.js.map +1 -1
  49. package/esm/plots/rose/adaptor.js +2 -2
  50. package/esm/plots/rose/adaptor.js.map +1 -1
  51. package/esm/plots/sunburst/adaptor.js +2 -2
  52. package/esm/plots/sunburst/adaptor.js.map +1 -1
  53. package/esm/plots/tiny-area/adaptor.js +2 -2
  54. package/esm/plots/tiny-area/adaptor.js.map +1 -1
  55. package/esm/plots/tiny-column/adaptor.js +2 -2
  56. package/esm/plots/tiny-column/adaptor.js.map +1 -1
  57. package/esm/plots/treemap/adaptor.js +2 -1
  58. package/esm/plots/treemap/adaptor.js.map +1 -1
  59. package/esm/plots/venn/adaptor.d.ts +23 -0
  60. package/esm/plots/venn/adaptor.js +177 -0
  61. package/esm/plots/venn/adaptor.js.map +1 -0
  62. package/esm/plots/venn/constant.d.ts +7 -0
  63. package/esm/plots/venn/constant.js +33 -0
  64. package/esm/plots/venn/constant.js.map +1 -0
  65. package/esm/plots/venn/index.d.ts +24 -0
  66. package/esm/plots/venn/index.js +47 -0
  67. package/esm/plots/venn/index.js.map +1 -0
  68. package/esm/plots/venn/interaction/action.d.ts +18 -0
  69. package/esm/plots/venn/interaction/action.js +77 -0
  70. package/esm/plots/venn/interaction/action.js.map +1 -0
  71. package/esm/plots/venn/interaction/index.d.ts +1 -0
  72. package/esm/plots/venn/interaction/index.js +15 -0
  73. package/esm/plots/venn/interaction/index.js.map +1 -0
  74. package/esm/plots/venn/label.d.ts +1 -0
  75. package/esm/plots/venn/label.js +31 -0
  76. package/esm/plots/venn/label.js.map +1 -0
  77. package/esm/plots/venn/layout/circleintersection.d.ts +29 -0
  78. package/esm/plots/venn/layout/circleintersection.js +186 -0
  79. package/esm/plots/venn/layout/circleintersection.js.map +1 -0
  80. package/esm/plots/venn/layout/diagram.d.ts +17 -0
  81. package/esm/plots/venn/layout/diagram.js +193 -0
  82. package/esm/plots/venn/layout/diagram.js.map +1 -0
  83. package/esm/plots/venn/layout/layout.d.ts +27 -0
  84. package/esm/plots/venn/layout/layout.js +593 -0
  85. package/esm/plots/venn/layout/layout.js.map +1 -0
  86. package/esm/plots/venn/shape.d.ts +1 -0
  87. package/esm/plots/venn/shape.js +42 -0
  88. package/esm/plots/venn/shape.js.map +1 -0
  89. package/esm/plots/venn/types.d.ts +27 -0
  90. package/esm/plots/venn/types.js +2 -0
  91. package/esm/plots/venn/types.js.map +1 -0
  92. package/esm/plots/venn/utils.d.ts +28 -0
  93. package/esm/plots/venn/utils.js +93 -0
  94. package/esm/plots/venn/utils.js.map +1 -0
  95. package/esm/types/annotation.d.ts +5 -1
  96. package/esm/types/annotation.js.map +1 -1
  97. package/esm/types/attr.d.ts +4 -1
  98. package/esm/types/attr.js.map +1 -1
  99. package/esm/types/common.d.ts +3 -1
  100. package/esm/types/common.js.map +1 -1
  101. package/esm/types/index.d.ts +1 -0
  102. package/esm/types/index.js +1 -0
  103. package/esm/types/index.js.map +1 -1
  104. package/esm/types/pattern.d.ts +47 -0
  105. package/esm/types/pattern.js +1 -0
  106. package/esm/types/pattern.js.map +1 -0
  107. package/esm/utils/color/blend.d.ts +20 -0
  108. package/esm/utils/color/blend.js +92 -0
  109. package/esm/utils/color/blend.js.map +1 -0
  110. package/esm/utils/padding.d.ts +5 -0
  111. package/esm/utils/padding.js +19 -0
  112. package/esm/utils/padding.js.map +1 -1
  113. package/esm/utils/pattern/dot.d.ts +32 -0
  114. package/esm/utils/pattern/dot.js +69 -0
  115. package/esm/utils/pattern/dot.js.map +1 -0
  116. package/esm/utils/pattern/index.d.ts +17 -0
  117. package/esm/utils/pattern/index.js +27 -0
  118. package/esm/utils/pattern/index.js.map +1 -0
  119. package/esm/utils/pattern/line.d.ts +25 -0
  120. package/esm/utils/pattern/line.js +56 -0
  121. package/esm/utils/pattern/line.js.map +1 -0
  122. package/esm/utils/pattern/square.d.ts +28 -0
  123. package/esm/utils/pattern/square.js +61 -0
  124. package/esm/utils/pattern/square.js.map +1 -0
  125. package/esm/utils/pattern/util.d.ts +50 -0
  126. package/esm/utils/pattern/util.js +90 -0
  127. package/esm/utils/pattern/util.js.map +1 -0
  128. package/lib/adaptor/common.d.ts +1 -0
  129. package/lib/adaptor/common.js +2 -0
  130. package/lib/adaptor/common.js.map +1 -1
  131. package/lib/adaptor/pattern.d.ts +10 -0
  132. package/lib/adaptor/pattern.js +58 -0
  133. package/lib/adaptor/pattern.js.map +1 -0
  134. package/lib/core/plot.d.ts +11 -1
  135. package/lib/core/plot.js +41 -0
  136. package/lib/core/plot.js.map +1 -1
  137. package/lib/index.d.ts +5 -1
  138. package/lib/index.js +7 -1
  139. package/lib/index.js.map +1 -1
  140. package/lib/plots/area/adaptor.js +1 -1
  141. package/lib/plots/area/adaptor.js.map +1 -1
  142. package/lib/plots/circle-packing/adaptor.js +2 -2
  143. package/lib/plots/circle-packing/adaptor.js.map +1 -1
  144. package/lib/plots/circle-packing/utils.d.ts +0 -5
  145. package/lib/plots/circle-packing/utils.js +2 -22
  146. package/lib/plots/circle-packing/utils.js.map +1 -1
  147. package/lib/plots/column/adaptor.js +3 -2
  148. package/lib/plots/column/adaptor.js.map +1 -1
  149. package/lib/plots/dual-axes/adaptor.js +6 -4
  150. package/lib/plots/dual-axes/adaptor.js.map +1 -1
  151. package/lib/plots/dual-axes/types.d.ts +2 -1
  152. package/lib/plots/dual-axes/types.js.map +1 -1
  153. package/lib/plots/dual-axes/util/geometry.js +2 -1
  154. package/lib/plots/dual-axes/util/geometry.js.map +1 -1
  155. package/lib/plots/dual-axes/util/legend.js +4 -1
  156. package/lib/plots/dual-axes/util/legend.js.map +1 -1
  157. package/lib/plots/funnel/geometries/basic.js +1 -6
  158. package/lib/plots/funnel/geometries/basic.js.map +1 -1
  159. package/lib/plots/heatmap/adaptor.js +1 -1
  160. package/lib/plots/heatmap/adaptor.js.map +1 -1
  161. package/lib/plots/heatmap/types.d.ts +4 -2
  162. package/lib/plots/heatmap/types.js.map +1 -1
  163. package/lib/plots/histogram/adaptor.js +2 -1
  164. package/lib/plots/histogram/adaptor.js.map +1 -1
  165. package/lib/plots/liquid/adaptor.js +1 -1
  166. package/lib/plots/liquid/adaptor.js.map +1 -1
  167. package/lib/plots/pie/adaptor.js +2 -1
  168. package/lib/plots/pie/adaptor.js.map +1 -1
  169. package/lib/plots/radial-bar/adaptor.js +2 -1
  170. package/lib/plots/radial-bar/adaptor.js.map +1 -1
  171. package/lib/plots/rose/adaptor.js +1 -1
  172. package/lib/plots/rose/adaptor.js.map +1 -1
  173. package/lib/plots/sunburst/adaptor.js +1 -1
  174. package/lib/plots/sunburst/adaptor.js.map +1 -1
  175. package/lib/plots/tiny-area/adaptor.js +1 -1
  176. package/lib/plots/tiny-area/adaptor.js.map +1 -1
  177. package/lib/plots/tiny-column/adaptor.js +1 -1
  178. package/lib/plots/tiny-column/adaptor.js.map +1 -1
  179. package/lib/plots/treemap/adaptor.js +2 -1
  180. package/lib/plots/treemap/adaptor.js.map +1 -1
  181. package/lib/plots/venn/adaptor.d.ts +23 -0
  182. package/lib/plots/venn/adaptor.js +183 -0
  183. package/lib/plots/venn/adaptor.js.map +1 -0
  184. package/lib/plots/venn/constant.d.ts +7 -0
  185. package/lib/plots/venn/constant.js +36 -0
  186. package/lib/plots/venn/constant.js.map +1 -0
  187. package/lib/plots/venn/index.d.ts +24 -0
  188. package/lib/plots/venn/index.js +50 -0
  189. package/lib/plots/venn/index.js.map +1 -0
  190. package/lib/plots/venn/interaction/action.d.ts +18 -0
  191. package/lib/plots/venn/interaction/action.js +80 -0
  192. package/lib/plots/venn/interaction/action.js.map +1 -0
  193. package/lib/plots/venn/interaction/index.d.ts +1 -0
  194. package/lib/plots/venn/interaction/index.js +17 -0
  195. package/lib/plots/venn/interaction/index.js.map +1 -0
  196. package/lib/plots/venn/label.d.ts +1 -0
  197. package/lib/plots/venn/label.js +33 -0
  198. package/lib/plots/venn/label.js.map +1 -0
  199. package/lib/plots/venn/layout/circleintersection.d.ts +29 -0
  200. package/lib/plots/venn/layout/circleintersection.js +196 -0
  201. package/lib/plots/venn/layout/circleintersection.js.map +1 -0
  202. package/lib/plots/venn/layout/diagram.d.ts +17 -0
  203. package/lib/plots/venn/layout/diagram.js +201 -0
  204. package/lib/plots/venn/layout/diagram.js.map +1 -0
  205. package/lib/plots/venn/layout/layout.d.ts +27 -0
  206. package/lib/plots/venn/layout/layout.js +606 -0
  207. package/lib/plots/venn/layout/layout.js.map +1 -0
  208. package/lib/plots/venn/shape.d.ts +1 -0
  209. package/lib/plots/venn/shape.js +44 -0
  210. package/lib/plots/venn/shape.js.map +1 -0
  211. package/lib/plots/venn/types.d.ts +27 -0
  212. package/lib/plots/venn/types.js +4 -0
  213. package/lib/plots/venn/types.js.map +1 -0
  214. package/lib/plots/venn/utils.d.ts +28 -0
  215. package/lib/plots/venn/utils.js +98 -0
  216. package/lib/plots/venn/utils.js.map +1 -0
  217. package/lib/types/annotation.d.ts +5 -1
  218. package/lib/types/annotation.js.map +1 -1
  219. package/lib/types/attr.d.ts +4 -1
  220. package/lib/types/attr.js.map +1 -1
  221. package/lib/types/common.d.ts +3 -1
  222. package/lib/types/common.js.map +1 -1
  223. package/lib/types/index.d.ts +1 -0
  224. package/lib/types/index.js +1 -0
  225. package/lib/types/index.js.map +1 -1
  226. package/lib/types/pattern.d.ts +47 -0
  227. package/lib/types/pattern.js +3 -0
  228. package/lib/types/pattern.js.map +1 -0
  229. package/lib/utils/color/blend.d.ts +20 -0
  230. package/lib/utils/color/blend.js +98 -0
  231. package/lib/utils/color/blend.js.map +1 -0
  232. package/lib/utils/padding.d.ts +5 -0
  233. package/lib/utils/padding.js +21 -1
  234. package/lib/utils/padding.js.map +1 -1
  235. package/lib/utils/pattern/dot.d.ts +32 -0
  236. package/lib/utils/pattern/dot.js +74 -0
  237. package/lib/utils/pattern/dot.js.map +1 -0
  238. package/lib/utils/pattern/index.d.ts +17 -0
  239. package/lib/utils/pattern/index.js +31 -0
  240. package/lib/utils/pattern/index.js.map +1 -0
  241. package/lib/utils/pattern/line.d.ts +25 -0
  242. package/lib/utils/pattern/line.js +61 -0
  243. package/lib/utils/pattern/line.js.map +1 -0
  244. package/lib/utils/pattern/square.d.ts +28 -0
  245. package/lib/utils/pattern/square.js +66 -0
  246. package/lib/utils/pattern/square.js.map +1 -0
  247. package/lib/utils/pattern/util.d.ts +50 -0
  248. package/lib/utils/pattern/util.js +99 -0
  249. package/lib/utils/pattern/util.js.map +1 -0
  250. package/package.json +10 -8
  251. package/src/adaptor/common.ts +2 -0
  252. package/src/adaptor/pattern.ts +66 -0
  253. package/src/core/plot.ts +43 -1
  254. package/src/index.ts +8 -1
  255. package/src/plots/area/adaptor.ts +3 -2
  256. package/src/plots/circle-packing/adaptor.ts +12 -3
  257. package/src/plots/circle-packing/utils.ts +1 -21
  258. package/src/plots/column/adaptor.ts +3 -1
  259. package/src/plots/dual-axes/adaptor.ts +6 -4
  260. package/src/plots/dual-axes/types.ts +11 -1
  261. package/src/plots/dual-axes/util/geometry.ts +3 -2
  262. package/src/plots/dual-axes/util/legend.ts +4 -1
  263. package/src/plots/funnel/geometries/basic.ts +2 -10
  264. package/src/plots/heatmap/adaptor.ts +12 -1
  265. package/src/plots/heatmap/types.ts +6 -2
  266. package/src/plots/histogram/adaptor.ts +13 -1
  267. package/src/plots/liquid/adaptor.ts +2 -2
  268. package/src/plots/pie/adaptor.ts +2 -0
  269. package/src/plots/radial-bar/adaptor.ts +2 -0
  270. package/src/plots/rose/adaptor.ts +2 -1
  271. package/src/plots/sunburst/adaptor.ts +2 -1
  272. package/src/plots/tiny-area/adaptor.ts +2 -2
  273. package/src/plots/tiny-column/adaptor.ts +2 -2
  274. package/src/plots/treemap/adaptor.ts +13 -1
  275. package/src/plots/venn/adaptor.ts +227 -0
  276. package/src/plots/venn/constant.ts +35 -0
  277. package/src/plots/venn/index.ts +47 -0
  278. package/src/plots/venn/interaction/action.ts +67 -0
  279. package/src/plots/venn/interaction/index.ts +17 -0
  280. package/src/plots/venn/label.ts +26 -0
  281. package/src/plots/venn/layout/circleintersection.ts +223 -0
  282. package/src/plots/venn/layout/diagram.ts +219 -0
  283. package/src/plots/venn/layout/layout.ts +734 -0
  284. package/src/plots/venn/shape.ts +53 -0
  285. package/src/plots/venn/types.ts +25 -0
  286. package/src/plots/venn/utils.ts +100 -0
  287. package/src/types/annotation.ts +3 -1
  288. package/src/types/attr.ts +7 -1
  289. package/src/types/common.ts +3 -1
  290. package/src/types/index.ts +1 -0
  291. package/src/types/pattern.ts +50 -0
  292. package/src/utils/color/blend.ts +103 -0
  293. package/src/utils/padding.ts +20 -0
  294. package/src/utils/pattern/dot.ts +86 -0
  295. package/src/utils/pattern/index.ts +45 -0
  296. package/src/utils/pattern/line.ts +70 -0
  297. package/src/utils/pattern/square.ts +78 -0
  298. package/src/utils/pattern/util.ts +104 -0
@@ -0,0 +1,53 @@
1
+ import { IGroup } from '@antv/g-base';
2
+ import { registerShape, Types, Util } from '@antv/g2';
3
+ import { parsePathString } from '@antv/path-util';
4
+ import { deepAssign } from '../../utils';
5
+ import { Datum, Point } from '../../types';
6
+ import { CustomInfo } from './types';
7
+ import { PATH_FIELD } from './constant';
8
+
9
+ /**
10
+ * 获取填充属性
11
+ * @param cfg 图形绘制数据
12
+ */
13
+ function getFillAttrs(cfg: Types.ShapeInfo) {
14
+ // style.fill 优先级更高
15
+ return deepAssign({}, cfg.defaultStyle, { fill: cfg.color }, cfg.style);
16
+ }
17
+
18
+ registerShape('schema', 'venn', {
19
+ draw(cfg: Types.ShapeInfo & { points: Point[]; nextPoints: Point[] }, container: IGroup) {
20
+ const data = cfg.data as Datum;
21
+ const segments = parsePathString(data[PATH_FIELD]);
22
+ const fillAttrs = getFillAttrs(cfg);
23
+
24
+ const group = container.addGroup({ name: 'venn-shape' });
25
+
26
+ group.addShape('path', {
27
+ attrs: {
28
+ ...fillAttrs,
29
+ path: segments,
30
+ },
31
+ name: 'venn-path',
32
+ });
33
+
34
+ const { offsetX, offsetY } = cfg.customInfo as CustomInfo;
35
+
36
+ const matrix = Util.transform(null, [['t', offsetX, offsetY]]);
37
+ group.setMatrix(matrix);
38
+
39
+ return group;
40
+ },
41
+ getMarker(markerCfg: Types.ShapeMarkerCfg) {
42
+ const { color } = markerCfg;
43
+ return {
44
+ symbol: 'circle',
45
+ style: {
46
+ lineWidth: 0,
47
+ stroke: color,
48
+ fill: color,
49
+ r: 4,
50
+ },
51
+ };
52
+ },
53
+ });
@@ -0,0 +1,25 @@
1
+ import { Types } from '@antv/g2';
2
+ import { Options, StyleAttr } from '../../types';
3
+ import { ID_FIELD, PATH_FIELD } from './constant';
4
+
5
+ export type VennData = (Types.Datum & { sets: string[]; [PATH_FIELD]: string; [ID_FIELD]: string })[];
6
+
7
+ /** 配置类型定义 */
8
+ export interface VennOptions extends Options {
9
+ /** 韦恩图 数据 */
10
+ readonly data: Types.Datum[];
11
+ /** 集合字段 */
12
+ readonly setsField: string;
13
+ /** 大小字段 */
14
+ readonly sizeField: string;
15
+
16
+ // 韦恩图 样式
17
+ /** color */
18
+ readonly color?: Options['color'];
19
+ /** 并集合的颜色混合方式, 可选项: 参考 https://gka.github.io/chroma.js/#chroma-blend, 默认: multiply */
20
+ readonly blendMode?: string;
21
+ /** point 样式 */
22
+ readonly pointStyle?: StyleAttr;
23
+ }
24
+
25
+ export type CustomInfo = { offsetY: number; offsetX: number } & Pick<VennOptions, 'label'>;
@@ -0,0 +1,100 @@
1
+ import { assign, memoize } from '@antv/util';
2
+ import { blend } from '../../utils/color/blend';
3
+ import { log, LEVEL } from '../../utils';
4
+ import { venn, scaleSolution } from './layout/layout';
5
+ import { intersectionAreaPath, computeTextCentres } from './layout/diagram';
6
+ import { ID_FIELD, PATH_FIELD } from './constant';
7
+ import { VennData, VennOptions } from './types';
8
+
9
+ /**
10
+ * 获取 颜色映射
11
+ * @usage colorMap.get(id) => color
12
+ *
13
+ * @returns Map<string, string>
14
+ */
15
+ export const getColorMap = memoize(
16
+ (colorPalette: string[], data: VennData, options: VennOptions) => {
17
+ const { blendMode, setsField } = options;
18
+ const colorMap = new Map<string /** id */, string /** color */>();
19
+ const colorPaletteLen = colorPalette.length;
20
+ data.forEach((d, idx) => {
21
+ if (d[setsField].length === 1) {
22
+ colorMap.set(d[ID_FIELD], colorPalette[(idx + colorPaletteLen) % colorPaletteLen]);
23
+ } else {
24
+ /** 一般都是可以获取到颜色的,如果不正确 就是输入了非法数据 */
25
+ const colorArr = d[setsField].map((id) => colorMap.get(id));
26
+ colorMap.set(
27
+ d[ID_FIELD],
28
+ colorArr.slice(1).reduce((a, b) => blend(a, b, blendMode), colorArr[0])
29
+ );
30
+ }
31
+ });
32
+
33
+ return colorMap;
34
+ },
35
+ (...params) => JSON.stringify(params)
36
+ );
37
+
38
+ /**
39
+ * 给韦恩图数据进行布局
40
+ *
41
+ * @param data
42
+ * @param width
43
+ * @param height
44
+ * @param padding
45
+ * @returns 韦恩图数据
46
+ */
47
+ export function layoutVennData(options: VennOptions, width: number, height: number, padding: number = 0): VennData {
48
+ const { data, setsField, sizeField } = options;
49
+
50
+ // 处理空数据的情况
51
+ if (data.length === 0) {
52
+ log(LEVEL.WARN, false, 'warn: %s', '数据不能为空');
53
+ return [];
54
+ }
55
+
56
+ const vennData: VennData = data.map((d) => ({
57
+ ...d,
58
+ sets: d[setsField] || [],
59
+ size: d[sizeField],
60
+ [PATH_FIELD]: '',
61
+ [ID_FIELD]: '',
62
+ }));
63
+ // 1. 进行排序,避免图形元素遮挡
64
+ vennData.sort((a, b) => a.sets.length - b.sets.length);
65
+ // todo 2. 可以在这里处理下非法数据输入,避免直接 crash
66
+
67
+ const solution = venn(vennData);
68
+ const circles = scaleSolution(solution, width, height, padding);
69
+ const textCenters = computeTextCentres(circles, vennData);
70
+ vennData.forEach((row) => {
71
+ const sets = row.sets;
72
+ const id = sets.join(',');
73
+ row[ID_FIELD] = id;
74
+ // 保留 vennText 布局方法
75
+ const setCircles = sets.map((set) => circles[set]);
76
+ let path = intersectionAreaPath(setCircles);
77
+ if (!/[zZ]$/.test(path)) {
78
+ path += ' Z';
79
+ }
80
+ row[PATH_FIELD] = path;
81
+ const center = textCenters[id] || { x: 0, y: 0 };
82
+ assign(row, center);
83
+ });
84
+ return vennData;
85
+ }
86
+
87
+ /**
88
+ * 检查是否存在 非法元素
89
+ * @param legalArr 合法集合:['A', 'B']
90
+ * @param testArr 检查集合:['A', 'B', 'C'] or ['A', 'C'](存在非法 'C')
91
+ * @return boolean
92
+ */
93
+ export function islegalSets(legalArr: any[], testArr: any[]): boolean {
94
+ for (let i = 0; i < testArr.length; i++) {
95
+ if (!legalArr.includes(testArr[i])) {
96
+ return false;
97
+ }
98
+ }
99
+ return true;
100
+ }
@@ -1,6 +1,6 @@
1
1
  import { Types } from '@antv/g2';
2
2
 
3
- export type Annotation =
3
+ type AnnotationOption =
4
4
  | Types.ArcOption
5
5
  | Types.ImageOption
6
6
  | Types.LineOption
@@ -11,3 +11,5 @@ export type Annotation =
11
11
  | Types.DataRegionOption
12
12
  | Types.ShapeAnnotationOption
13
13
  | Types.HtmlAnnotationOption;
14
+
15
+ export type Annotation = { id?: string } & AnnotationOption;
package/src/types/attr.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import { ShapeAttrs } from '@antv/g-base';
2
+ import { PatternOption } from '../utils/pattern/index';
2
3
  import { Datum } from './common';
3
4
 
4
5
  /** 图形属性 */
5
6
  export type ShapeStyle = ShapeAttrs;
6
7
 
7
8
  /** 颜色映射 */
8
- export type ColorAttr = string | string[] | ((datum: Datum) => string);
9
+ export type ColorAttr = string | string[] | ((datum: Datum) => string) | object;
10
+ /** pattern 映射*/
11
+ export type PatternAttr =
12
+ | CanvasPattern
13
+ | PatternOption
14
+ | ((datum: Datum, color: string /** inherit color */) => PatternOption | CanvasPattern);
9
15
  /** 尺寸大小映射 */
10
16
  export type SizeAttr = number | [number, number] | ((datum: Datum) => number);
11
17
  /** 图形 shape 映射 */
@@ -9,7 +9,7 @@ import { Annotation } from './annotation';
9
9
  import { State } from './state';
10
10
  import { Slider } from './slider';
11
11
  import { Scrollbar } from './scrollbar';
12
- import { ColorAttr } from './attr';
12
+ import { ColorAttr, PatternAttr } from './attr';
13
13
  import { Meta } from './meta';
14
14
 
15
15
  /** annotation position */
@@ -109,6 +109,8 @@ export type Options = {
109
109
  readonly theme?: string | object;
110
110
  /** 颜色色板 */
111
111
  readonly color?: ColorAttr;
112
+ /** pattern 配置 */
113
+ readonly pattern?: PatternAttr;
112
114
  /** xAxis 的配置项 */
113
115
  readonly xAxis?: Axis;
114
116
  /** yAxis 的配置项 */
@@ -6,6 +6,7 @@ export * from './statistic';
6
6
  export * from './meta';
7
7
  export * from './axis';
8
8
  export * from './interaction';
9
+ export * from './annotation';
9
10
  export * from './locale';
10
11
  export * from './button';
11
12
 
@@ -0,0 +1,50 @@
1
+ export type PatternCfg = {
2
+ /** pattern background color. Default: inherit (默认: 继承图形元素颜色) */
3
+ backgroundColor?: string;
4
+ /** pattern fill color. 贴图图案填充色 */
5
+ fill?: string;
6
+ /** 填充透明度 */
7
+ fillOpacity?: number;
8
+ /** pattern stroke color. 贴图图案描边色 */
9
+ stroke?: string;
10
+ /** 描边透明度 */
11
+ strokeOpacity?: number;
12
+ /** lines thickness. 描边粗细 */
13
+ lineWidth?: number;
14
+ /** 整个pattern 透明度 */
15
+ opacity?: number;
16
+ /** 整个pattern 的旋转角度 */
17
+ rotation?: number;
18
+ };
19
+
20
+ /**
21
+ * dot pattern
22
+ */
23
+ export type DotPatternCfg = PatternCfg & {
24
+ /** 点的大小, 默认: 4 */
25
+ size?: number;
26
+ /** padding between dots, 默认: 4 */
27
+ padding?: number;
28
+ /** 是否交错,默认: true. 即 staggered dots. */
29
+ isStagger?: boolean;
30
+ };
31
+
32
+ /**
33
+ * line pattern
34
+ */
35
+ export type LinePatternCfg = PatternCfg & {
36
+ /** pacing between lines. 线之间的距离 */
37
+ spacing?: number;
38
+ };
39
+
40
+ /**
41
+ * square pattern
42
+ */
43
+ export type SquarePatternCfg = PatternCfg & {
44
+ /** 矩形的大小 */
45
+ size?: number;
46
+ /** 矩形之间的间隔 */
47
+ padding?: number;
48
+ /** 是否交错,默认: true. 即 staggered squares. */
49
+ isStagger?: boolean;
50
+ };
@@ -0,0 +1,103 @@
1
+ import colorUtil from '@antv/color-util';
2
+ /*
3
+ * interpolates between a set of colors uzing a bezier spline
4
+ * blend mode formulas taken from http://www.venture-ware.com/kevin/coding/lets-learn-math-photoshop-blend-modes/
5
+ */
6
+
7
+ const each =
8
+ (f) =>
9
+ (c0: number[], c1: number[]): number[] => {
10
+ const out = [];
11
+ out[0] = f(c0[0], c1[0]);
12
+ out[1] = f(c0[1], c1[1]);
13
+ out[2] = f(c0[2], c1[2]);
14
+ return out;
15
+ };
16
+
17
+ /**
18
+ * 混合方法集合
19
+ */
20
+ const blendObject = {
21
+ normal: (a: number) => a,
22
+ multiply: (a: number, b: number) => (a * b) / 255,
23
+ screen: (a: number, b: number) => 255 * (1 - (1 - a / 255) * (1 - b / 255)),
24
+ overlay: (a: number, b: number) => (b < 128 ? (2 * a * b) / 255 : 255 * (1 - 2 * (1 - a / 255) * (1 - b / 255))),
25
+ darken: (a: number, b: number) => (a > b ? b : a),
26
+ lighten: (a: number, b: number) => (a > b ? a : b),
27
+ dodge: (a: number, b: number) => {
28
+ if (a === 255) return 255;
29
+ a = (255 * (b / 255)) / (1 - a / 255);
30
+ return a > 255 ? 255 : a;
31
+ },
32
+ burn: (a: number, b: number) => {
33
+ // 参考 w3c 的写法,考虑除数为 0 的情况
34
+ if (b === 255) return 255;
35
+ else if (a === 0) return 0;
36
+ else return 255 * (1 - Math.min(1, (1 - b / 255) / (a / 255)));
37
+ },
38
+ };
39
+
40
+ /**
41
+ * 获取混合方法
42
+ */
43
+ export const innerBlend = (mode: string) => {
44
+ if (!blendObject[mode]) {
45
+ throw new Error('unknown blend mode ' + mode);
46
+ }
47
+ return blendObject[mode];
48
+ };
49
+
50
+ /**
51
+ * 混合颜色,并处理透明度情况
52
+ * 参考:https://www.w3.org/TR/compositing/#blending
53
+ * @param c0
54
+ * @param c1
55
+ * @param mode 混合模式
56
+ * @return rbga
57
+ */
58
+ export function blend(c0: string, c1: string, mode = 'normal') {
59
+ // blendRgbArr: 生成不考虑透明度的 blend color: [r, g, b]
60
+ const blendRgbArr = each(innerBlend(mode))(colorToArr(c0), colorToArr(c1));
61
+
62
+ const [r0, g0, b0, a0] = colorToArr(c0);
63
+ const [r1, g1, b1, a1] = colorToArr(c1);
64
+
65
+ const a = Number((a0 + a1 * (1 - a0)).toFixed(2));
66
+
67
+ const r = Math.round(
68
+ ((a0 * (1 - a1) * (r0 / 255) + a0 * a1 * (blendRgbArr[0] / 255) + (1 - a0) * a1 * (r1 / 255)) / a) * 255
69
+ );
70
+ const g = Math.round(
71
+ ((a0 * (1 - a1) * (g0 / 255) + a0 * a1 * (blendRgbArr[1] / 255) + (1 - a0) * a1 * (g1 / 255)) / a) * 255
72
+ );
73
+ const b = Math.round(
74
+ ((a0 * (1 - a1) * (b0 / 255) + a0 * a1 * (blendRgbArr[2] / 255) + (1 - a0) * a1 * (b1 / 255)) / a) * 255
75
+ );
76
+
77
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
78
+ }
79
+
80
+ /**
81
+ * 统一颜色输入的格式 [r, g, b, a]
82
+ * 参考:https://www.w3.org/TR/compositing/#blending
83
+ * @param c color
84
+ * @return [r, g, b, a]
85
+ */
86
+ export function colorToArr(c: string): number[] {
87
+ const color = c.replace('/s+/g', ''); // 去除所有空格
88
+ let rgbaArr: any[];
89
+
90
+ // 'red' -> [r, g, b, 1]
91
+ if (typeof color === 'string' && !color.startsWith('rgba') && !color.startsWith('#')) {
92
+ return (rgbaArr = colorUtil.rgb2arr(colorUtil.toRGB(color)).concat([1]));
93
+ }
94
+
95
+ // rgba(255, 200, 125, 0.5) -> [r, g, b, a]
96
+ if (color.startsWith('rgba')) rgbaArr = color.replace('rgba(', '').replace(')', '').split(',');
97
+
98
+ // '#fff000' -> [r, g, b, 1]
99
+ if (color.startsWith('#')) rgbaArr = colorUtil.rgb2arr(color).concat([1]); // 如果是 16 进制(6 位数),默认透明度 1
100
+
101
+ // [r, g, b, a] 前三位取整
102
+ return rgbaArr.map((item, index) => (index === 3 ? Number(item) : item | 0));
103
+ }
@@ -48,3 +48,23 @@ export function getAdjustAppendPadding(padding: Types.ViewAppendPadding, positio
48
48
  currentAppendPadding[3] + PADDING[3],
49
49
  ];
50
50
  }
51
+
52
+ /**
53
+ * 根据图表的 padding 和 appendPadding 计算出图表的最终 padding
54
+ * @param array
55
+ */
56
+ export function resolveAllPadding(paddings: Types.ViewPadding[]) {
57
+ // 先把数组里的 padding 全部转换成 normal
58
+ const normalPaddings = paddings.map((item) => normalPadding(item));
59
+ let finalPadding = [0, 0, 0, 0];
60
+ if (normalPaddings.length > 0) {
61
+ finalPadding = finalPadding.map((item, index) => {
62
+ // 有几个 padding 数组就遍历几次,累加
63
+ normalPaddings.forEach((d, i) => {
64
+ item += normalPaddings[i][index];
65
+ });
66
+ return item;
67
+ });
68
+ }
69
+ return finalPadding;
70
+ }
@@ -0,0 +1,86 @@
1
+ import { DotPatternCfg } from '../../types/pattern';
2
+ import { deepAssign } from '../../utils';
3
+ import {
4
+ getUnitPatternSize,
5
+ initCanvas,
6
+ drawBackground,
7
+ getSymbolsPosition,
8
+ transformMatrix,
9
+ getPixelRatio,
10
+ } from './util';
11
+
12
+ /**
13
+ * dotPattern的默认配置
14
+ */
15
+ export const defaultDotPatternCfg = {
16
+ size: 6,
17
+ padding: 2,
18
+ backgroundColor: 'transparent',
19
+ opacity: 1,
20
+ rotation: 0,
21
+ fill: '#fff',
22
+ fillOpacity: 0.5,
23
+ stroke: 'transparent',
24
+ lineWidth: 0,
25
+ isStagger: true,
26
+ };
27
+
28
+ /**
29
+ * 绘制圆点
30
+ *
31
+ * @param context
32
+ * @param cfg
33
+ * @param x 圆点中心坐标x
34
+ * @param y 圆点中心坐标y
35
+ */
36
+ export function drawDot(context: CanvasRenderingContext2D, cfg: DotPatternCfg, x: number, y: number) {
37
+ const { size, fill, lineWidth, stroke, fillOpacity } = cfg;
38
+
39
+ context.beginPath();
40
+ context.globalAlpha = fillOpacity;
41
+ context.fillStyle = fill;
42
+ context.strokeStyle = stroke;
43
+ context.lineWidth = lineWidth;
44
+ context.arc(x, y, size / 2, 0, 2 * Math.PI, false);
45
+ context.fill();
46
+ if (lineWidth) {
47
+ context.stroke();
48
+ }
49
+ context.closePath();
50
+ }
51
+
52
+ /**
53
+ * 创建 dot pattern,返回 HTMLCanvasElement
54
+ *
55
+ * @param cfg
56
+ * @returns HTMLCanvasElement
57
+ */
58
+ export function createDotPattern(cfg?: DotPatternCfg): CanvasPattern {
59
+ const dotCfg = deepAssign({}, defaultDotPatternCfg, cfg);
60
+
61
+ const { size, padding, isStagger, rotation } = dotCfg;
62
+
63
+ // 计算 画布大小,dots的位置
64
+ const unitSize = getUnitPatternSize(size, padding, isStagger);
65
+ const dots = getSymbolsPosition(unitSize, isStagger);
66
+
67
+ // 初始化 patternCanvas
68
+ const canvas = initCanvas(unitSize, unitSize);
69
+ const ctx = canvas.getContext('2d');
70
+
71
+ // 绘制 background,dots
72
+ drawBackground(ctx, dotCfg, unitSize);
73
+ for (const [x, y] of dots) {
74
+ drawDot(ctx, dotCfg, x, y);
75
+ }
76
+
77
+ const pattern = ctx.createPattern(canvas, 'repeat');
78
+
79
+ if (pattern) {
80
+ const dpr = getPixelRatio();
81
+ const matrix = transformMatrix(dpr, rotation);
82
+ pattern.setTransform(matrix);
83
+ }
84
+
85
+ return pattern;
86
+ }
@@ -0,0 +1,45 @@
1
+ import { DotPatternCfg, LinePatternCfg, SquarePatternCfg } from '../../types/pattern';
2
+ import { createDotPattern } from './dot';
3
+ import { createLinePattern } from './line';
4
+ import { createSquarePattern } from './square';
5
+
6
+ export type PatternOption =
7
+ | {
8
+ type: 'dot';
9
+ cfg?: DotPatternCfg;
10
+ }
11
+ | {
12
+ type: 'line';
13
+ cfg?: LinePatternCfg;
14
+ }
15
+ | {
16
+ type: 'square';
17
+ cfg?: SquarePatternCfg;
18
+ };
19
+
20
+ /**
21
+ * 获取内置的 CanvasPattern 方法
22
+ * @param options
23
+ * @returns
24
+ */
25
+ export function getCanvasPattern(options: PatternOption): CanvasPattern | undefined {
26
+ const { type, cfg } = options;
27
+
28
+ let pattern;
29
+
30
+ switch (type) {
31
+ case 'dot':
32
+ pattern = createDotPattern(cfg);
33
+ break;
34
+ case 'line':
35
+ pattern = createLinePattern(cfg);
36
+ break;
37
+ case 'square':
38
+ pattern = createSquarePattern(cfg);
39
+ break;
40
+ default:
41
+ break;
42
+ }
43
+
44
+ return pattern;
45
+ }
@@ -0,0 +1,70 @@
1
+ import { LinePatternCfg } from '../../types/pattern';
2
+ import { deepAssign } from '../../utils';
3
+ import { initCanvas, drawBackground, transformMatrix, getPixelRatio } from './util';
4
+
5
+ /**
6
+ * linePattern 的 默认配置
7
+ */
8
+ export const defaultLinePatternCfg = {
9
+ rotation: 45,
10
+ spacing: 5,
11
+ opacity: 1,
12
+ backgroundColor: 'transparent',
13
+ strokeOpacity: 0.5,
14
+ stroke: '#fff',
15
+ lineWidth: 2,
16
+ };
17
+
18
+ /**
19
+ * 绘制line
20
+ *
21
+ * @param context canvasContext
22
+ * @param cfg linePattern 的配置
23
+ * @param d 绘制 path 所需的 d
24
+ */
25
+ export function drawLine(context: CanvasRenderingContext2D, cfg: LinePatternCfg, d: string) {
26
+ const { stroke, lineWidth, strokeOpacity } = cfg;
27
+ const path = new Path2D(d);
28
+
29
+ context.globalAlpha = strokeOpacity;
30
+ context.lineCap = 'square';
31
+ context.strokeStyle = lineWidth ? stroke : 'transparent';
32
+ context.lineWidth = lineWidth;
33
+ context.stroke(path);
34
+ }
35
+
36
+ /**
37
+ * 创建 linePattern
38
+ */
39
+ export function createLinePattern(cfg?: LinePatternCfg): CanvasPattern {
40
+ const lineCfg = deepAssign({}, defaultLinePatternCfg, cfg);
41
+
42
+ const { spacing, rotation, lineWidth } = lineCfg;
43
+
44
+ // 计算 pattern 画布的大小, path 所需的 d
45
+ const width = spacing + lineWidth || 1;
46
+ const height = spacing + lineWidth || 1;
47
+ const d = `
48
+ M 0 0 L ${width} 0
49
+ M 0 ${height} L ${width} ${height}
50
+ `;
51
+
52
+ // 初始化 patternCanvas
53
+ const canvas = initCanvas(width, height);
54
+ const ctx = canvas.getContext('2d');
55
+
56
+ // 绘制 background,line
57
+ drawBackground(ctx, lineCfg, width, height);
58
+ drawLine(ctx, lineCfg, d);
59
+
60
+ const pattern = ctx.createPattern(canvas, 'repeat');
61
+
62
+ if (pattern) {
63
+ const dpr = getPixelRatio();
64
+ const matrix = transformMatrix(dpr, rotation);
65
+ pattern.setTransform(matrix);
66
+ }
67
+
68
+ // 返回 Pattern 对象
69
+ return pattern;
70
+ }