简介

G2 提供了一系列的全局配置项,一旦更改这些配置项会影响全局的图表展示,这里展示所有的全局配置项。

所有配置项

  1. const DEFAULT_COLOR = '#1890FF';
  2. const COLOR_PLATE_8 = [
  3. '#1890FF',
  4. '#2FC25B',
  5. '#FACC14',
  6. '#223273',
  7. '#8543E0',
  8. '#13C2C2',
  9. '#3436C7',
  10. '#F04864'
  11. ];
  12. const COLOR_PLATE_16 = [
  13. '#1890FF',
  14. '#41D9C7',
  15. '#2FC25B',
  16. '#FACC14',
  17. '#E6965C',
  18. '#223273',
  19. '#7564CC',
  20. '#8543E0',
  21. '#5C8EE6',
  22. '#13C2C2',
  23. '#5CA3E6',
  24. '#3436C7',
  25. '#B381E6',
  26. '#F04864',
  27. '#D598D9'
  28. ];
  29. const COLOR_PLATE_24 = [ '#1890FF',
  30. '#66B5FF',
  31. '#41D9C7',
  32. '#2FC25B',
  33. '#6EDB8F',
  34. '#9AE65C',
  35. '#FACC14',
  36. '#E6965C',
  37. '#57AD71',
  38. '#223273',
  39. '#738AE6',
  40. '#7564CC',
  41. '#8543E0',
  42. '#A877ED',
  43. '#5C8EE6',
  44. '#13C2C2',
  45. '#70E0E0',
  46. '#5CA3E6',
  47. '#3436C7',
  48. '#8082FF',
  49. '#DD81E6',
  50. '#F04864',
  51. '#FA7D92',
  52. '#D598D9' ];
  53. const COLOR_PIE = [
  54. '#1890FF',
  55. '#13C2C2',
  56. '#2FC25B',
  57. '#FACC14',
  58. '#F04864',
  59. '#8543E0',
  60. '#3436C7',
  61. '#223273' ];
  62. const COLOR_PIE_16 = [
  63. '#1890FF',
  64. '#73C9E6',
  65. '#13C2C2',
  66. '#6CD9B3',
  67. '#2FC25B',
  68. '#9DD96C',
  69. '#FACC14',
  70. '#E6965C',
  71. '#F04864',
  72. '#D66BCA',
  73. '#8543E0',
  74. '#8E77ED',
  75. '#3436C7',
  76. '#737EE6',
  77. '#223273',
  78. '#7EA2E6'
  79. ];
  80. const FONT_FAMILY = '"-apple-system", BlinkMacSystemFont, "Segoe UI", Roboto,"Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",SimSun, "sans-serif"';
  81. // tooltip 相关 dom 的 css 类名
  82. const TOOLTIP_CONTAINER_CLASS = 'g2-tooltip';
  83. const TOOLTIP_TITLE_CLASS = 'g2-tooltip-title';
  84. const TOOLTIP_LIST_CLASS = 'g2-tooltip-list';
  85. const TOOLTIP_LIST_ITEM_CLASS = 'g2-tooltip-list-item';
  86. const TOOLTIP_MARKER_CLASS = 'g2-tooltip-marker';
  87. const TOOLTIP_VALUE_CLASS = 'g2-tooltip-value';
  88. // html 渲染的 legend 相关 dom 的 css 类型
  89. const LEGEND_CONTAINER_CLASS = 'g2-legend';
  90. const LEGEND_TITLE_CLASS = 'g2-legend-title';
  91. const LEGEND_LIST_CLASS = 'g2-legend-list';
  92. const LEGEND_LIST_ITEM_CLASS = 'g2-legend-list-item';
  93. const LEGEND_MARKER_CLASS = 'g2-legend-marker';
  94. const Theme = {
  95. defaultColor: DEFAULT_COLOR, // 默认主题色
  96. plotCfg: {
  97. padding: [ 20, 20, 95, 80 ] // 默认边框
  98. },
  99. fontFamily: FONT_FAMILY,
  100. defaultLegendPosition: 'bottom', // 默认图例的展示位置
  101. colors: COLOR_PLATE_8,
  102. colors_16: COLOR_PLATE_16,
  103. colors_24: COLOR_PLATE_24,
  104. colors_pie: COLOR_PIE,
  105. colors_pie_16: COLOR_PIE_16,
  106. shapes: {
  107. point: [ 'hollowCircle', 'hollowSquare', 'hollowDiamond', 'hollowBowtie', 'hollowTriangle',
  108. 'hollowHexagon', 'cross', 'tick', 'plus', 'hyphen', 'line' ],
  109. line: [ 'line', 'dash', 'dot' ],
  110. area: [ 'area' ]
  111. },
  112. sizes: [ 1, 10 ],
  113. opacities: [ 0.1, 0.9 ],
  114. axis: { // 坐标轴
  115. top: {
  116. // zIndex: 1, // 默认上下方向的坐标轴位于左右坐标轴的上方
  117. position: 'top',
  118. title: null,
  119. label: {
  120. offset: 16,
  121. textStyle: {
  122. fill: '#545454',
  123. fontSize: 12,
  124. lineHeight: 16,
  125. textBaseline: 'middle',
  126. fontFamily: FONT_FAMILY
  127. },
  128. autoRotate: true
  129. },
  130. line: {
  131. lineWidth: 1,
  132. stroke: '#BFBFBF'
  133. },
  134. tickLine: {
  135. lineWidth: 1,
  136. stroke: '#BFBFBF',
  137. length: 4,
  138. alignWithLabel: true
  139. }
  140. },
  141. bottom: {
  142. position: 'bottom',
  143. title: null,
  144. label: {
  145. offset: 16,
  146. autoRotate: true,
  147. textStyle: {
  148. fill: '#545454',
  149. fontSize: 12,
  150. lineHeight: 16,
  151. textBaseline: 'middle',
  152. fontFamily: FONT_FAMILY
  153. }
  154. },
  155. line: {
  156. lineWidth: 1,
  157. stroke: '#BFBFBF'
  158. },
  159. tickLine: {
  160. lineWidth: 1,
  161. stroke: '#BFBFBF',
  162. length: 4,
  163. alignWithLabel: true
  164. }
  165. },
  166. left: {
  167. position: 'left',
  168. title: null,
  169. label: {
  170. offset: 8,
  171. autoRotate: true,
  172. textStyle: {
  173. fill: '#545454',
  174. fontSize: 12,
  175. lineHeight: 16,
  176. textBaseline: 'middle',
  177. fontFamily: FONT_FAMILY
  178. }
  179. },
  180. line: null,
  181. tickLine: null,
  182. grid: {
  183. zIndex: -1,
  184. lineStyle: {
  185. stroke: '#E9E9E9',
  186. lineWidth: 1,
  187. lineDash: [ 3, 3 ]
  188. },
  189. hideFirstLine: true
  190. }
  191. },
  192. right: {
  193. position: 'right',
  194. title: null,
  195. label: {
  196. offset: 8,
  197. autoRotate: true,
  198. textStyle: {
  199. fill: '#545454',
  200. fontSize: 12,
  201. lineHeight: 16,
  202. textBaseline: 'middle',
  203. fontFamily: FONT_FAMILY
  204. }
  205. },
  206. line: null,
  207. tickLine: null,
  208. grid: {
  209. lineStyle: {
  210. stroke: '#E9E9E9',
  211. lineWidth: 1,
  212. lineDash: [ 3, 3 ]
  213. },
  214. hideFirstLine: true
  215. }
  216. },
  217. circle: {
  218. zIndex: 1,
  219. title: null,
  220. label: {
  221. offset: 8,
  222. textStyle: {
  223. fill: '#545454',
  224. fontSize: 12,
  225. lineHeight: 16,
  226. fontFamily: FONT_FAMILY
  227. }
  228. },
  229. line: {
  230. lineWidth: 1,
  231. stroke: '#BFBFBF'
  232. },
  233. tickLine: {
  234. lineWidth: 1,
  235. stroke: '#BFBFBF',
  236. length: 4,
  237. alignWithLabel: true
  238. },
  239. grid: {
  240. lineStyle: {
  241. stroke: '#E9E9E9',
  242. lineWidth: 1,
  243. lineDash: [ 3, 3 ]
  244. },
  245. hideFirstLine: true
  246. }
  247. },
  248. radius: {
  249. zIndex: 0,
  250. label: {
  251. offset: 12,
  252. textStyle: {
  253. fill: '#545454',
  254. fontSize: 12,
  255. textBaseline: 'middle',
  256. lineHeight: 16,
  257. fontFamily: FONT_FAMILY
  258. }
  259. },
  260. line: {
  261. lineWidth: 1,
  262. stroke: '#BFBFBF'
  263. },
  264. tickLine: {
  265. lineWidth: 1,
  266. stroke: '#BFBFBF',
  267. length: 4,
  268. alignWithLabel: true
  269. },
  270. grid: {
  271. lineStyle: {
  272. stroke: '#E9E9E9',
  273. lineWidth: 1,
  274. lineDash: [ 3, 3 ]
  275. },
  276. type: 'circle'
  277. }
  278. },
  279. helix: {
  280. grid: null,
  281. label: null,
  282. title: null,
  283. line: {
  284. lineWidth: 1,
  285. stroke: '#BFBFBF'
  286. },
  287. tickLine: {
  288. lineWidth: 1,
  289. length: 4,
  290. stroke: '#BFBFBF',
  291. alignWithLabel: true
  292. }
  293. }
  294. },
  295. // 默认 label 上的配置项
  296. label: {
  297. offset: 20,
  298. textStyle: {
  299. fill: '#545454',
  300. fontSize: 12,
  301. textBaseline: 'middle',
  302. fontFamily: FONT_FAMILY
  303. }
  304. },
  305. treemapLabels: {
  306. offset: 10,
  307. textStyle: {
  308. fill: '#fff',
  309. fontSize: 12,
  310. textBaseline: 'top',
  311. fontStyle: 'bold',
  312. fontFamily: FONT_FAMILY
  313. }
  314. },
  315. innerLabels: {
  316. textStyle: {
  317. fill: '#fff',
  318. fontSize: 12,
  319. textBaseline: 'middle',
  320. fontFamily: FONT_FAMILY
  321. }
  322. },
  323. // 在theta坐标系下的饼图文本内部的样式
  324. thetaLabels: {
  325. labelHeight: 14,
  326. offset: 30
  327. // 在theta坐标系下的饼图文本的样式
  328. },
  329. // 图例
  330. legend: {
  331. right: {
  332. position: 'right',
  333. layout: 'vertical',
  334. itemMarginBottom: 8, // layout 为 vertical 时各个图例项的间距
  335. width: 16,
  336. height: 156,
  337. title: null,
  338. legendStyle: {
  339. LIST_CLASS: {
  340. textAlign: 'left'
  341. }
  342. },
  343. textStyle: {
  344. fill: '#8C8C8C',
  345. fontSize: 12,
  346. textAlign: 'start',
  347. textBaseline: 'middle',
  348. lineHeight: 0,
  349. fontFamily: FONT_FAMILY
  350. }, // 图例项文本的样式
  351. unCheckColor: '#bfbfbf'
  352. },
  353. left: {
  354. position: 'left',
  355. layout: 'vertical',
  356. itemMarginBottom: 8,
  357. width: 16,
  358. height: 156,
  359. title: null,
  360. textStyle: {
  361. fill: '#8C8C8C',
  362. fontSize: 12,
  363. textAlign: 'start',
  364. textBaseline: 'middle',
  365. lineHeight: 20,
  366. fontFamily: FONT_FAMILY
  367. }, // 图例项文本的样式
  368. unCheckColor: '#bfbfbf'
  369. },
  370. top: {
  371. position: 'top',
  372. offset: [ 0, 6 ],
  373. layout: 'horizontal',
  374. title: null,
  375. itemGap: 10,
  376. width: 156,
  377. height: 16,
  378. textStyle: {
  379. fill: '#8C8C8C',
  380. fontSize: 12,
  381. textAlign: 'start',
  382. textBaseline: 'middle',
  383. lineHeight: 20,
  384. fontFamily: FONT_FAMILY
  385. }, // 图例项文本的样式
  386. unCheckColor: '#bfbfbf'
  387. },
  388. bottom: {
  389. position: 'bottom',
  390. offset: [ 0, 6 ],
  391. layout: 'horizontal',
  392. title: null,
  393. itemGap: 10,
  394. width: 156,
  395. height: 16,
  396. textStyle: {
  397. fill: '#8C8C8C',
  398. fontSize: 12,
  399. textAlign: 'start',
  400. textBaseline: 'middle',
  401. lineHeight: 20,
  402. fontFamily: FONT_FAMILY
  403. }, // 图例项文本的样式
  404. unCheckColor: '#bfbfbf'
  405. },
  406. // 定义 html 渲染图例的样式
  407. html: {
  408. [`${LEGEND_CONTAINER_CLASS}`]: {
  409. height: 'auto',
  410. width: 'auto',
  411. position: 'absolute',
  412. overflow: 'auto',
  413. fontSize: '12px',
  414. fontFamily: FONT_FAMILY,
  415. lineHeight: '20px',
  416. color: '#8C8C8C'
  417. },
  418. [`${LEGEND_TITLE_CLASS}`]: {
  419. marginBottom: '4px'
  420. },
  421. [`${LEGEND_LIST_CLASS}`]: {
  422. listStyleType: 'none',
  423. margin: 0,
  424. padding: 0
  425. },
  426. [`${LEGEND_LIST_ITEM_CLASS}`]: {
  427. cursor: 'pointer',
  428. marginBottom: '5px',
  429. marginRight: '24px'
  430. },
  431. [`${LEGEND_MARKER_CLASS}`]: {
  432. width: '9px',
  433. height: '9px',
  434. borderRadius: '50%',
  435. display: 'inline-block',
  436. marginRight: '8px',
  437. verticalAlign: 'middle'
  438. }
  439. },
  440. // 不能滑动的连续图例样式
  441. gradient: {
  442. textStyle: {
  443. fill: '#8C8C8C',
  444. fontSize: 12,
  445. textAlign: 'center',
  446. textBaseline: 'middle',
  447. lineHeight: 20,
  448. fontFamily: FONT_FAMILY
  449. }, // 图例项文本的样式
  450. lineStyle: {
  451. lineWidth: 1,
  452. stroke: '#fff'
  453. },
  454. unCheckColor: '#bfbfbf'
  455. },
  456. margin: [ 0, 5, 24, 5 ], // 图例跟四个边的坐标轴、绘图区域的间距
  457. legendMargin: 24 // 图例之间的间距
  458. },
  459. // tooltip 默认的配置项
  460. tooltip: {
  461. useHtml: true,
  462. crosshairs: false,
  463. offset: 15,
  464. // css style for tooltip
  465. [`${TOOLTIP_CONTAINER_CLASS}`]: {
  466. position: 'absolute',
  467. visibility: 'hidden',
  468. // @2018-07-25 by blue.lb 这里去掉浮动,火狐上存在样式错位
  469. // whiteSpace: 'nowrap',
  470. zIndex: 8,
  471. transition: 'visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1), left 0.4s cubic-bezier(0.23, 1, 0.32, 1), top 0.4s cubic-bezier(0.23, 1, 0.32, 1)',
  472. backgroundColor: 'rgba(255, 255, 255, 0.9)',
  473. boxShadow: '0px 0px 10px #aeaeae',
  474. borderRadius: '3px',
  475. color: 'rgb(87, 87, 87)',
  476. fontSize: '12px',
  477. fontFamily: FONT_FAMILY,
  478. lineHeight: '20px',
  479. padding: '10px 10px 6px 10px'
  480. },
  481. [`${TOOLTIP_TITLE_CLASS}`]: {
  482. marginBottom: '4px'
  483. },
  484. [`${TOOLTIP_LIST_CLASS}`]: {
  485. margin: 0,
  486. listStyleType: 'none',
  487. padding: 0
  488. },
  489. [`${TOOLTIP_LIST_ITEM_CLASS}`]: {
  490. marginBottom: '4px'
  491. },
  492. [`${TOOLTIP_MARKER_CLASS}`]: {
  493. width: '5px',
  494. height: '5px',
  495. borderRadius: '50%',
  496. display: 'inline-block',
  497. marginRight: '8px'
  498. },
  499. [`${TOOLTIP_VALUE_CLASS}`]: {
  500. display: 'inline-block',
  501. float: 'right',
  502. marginLeft: '30px'
  503. }
  504. },
  505. // 折线图显示 tooltip 时的 marker
  506. tooltipMarker: {
  507. symbol: (x, y, r) => {
  508. return [
  509. [ 'M', x, y ],
  510. [ 'm', -r, 0 ],
  511. [ 'a', r, r, 0, 1, 0, r * 2, 0 ],
  512. [ 'a', r, r, 0, 1, 0, -r * 2, 0 ]
  513. ];
  514. },
  515. stroke: '#fff',
  516. shadowBlur: 10,
  517. shadowOffsetX: 0,
  518. shadowOffSetY: 0,
  519. shadowColor: 'rgba(0,0,0,0.09)',
  520. lineWidth: 2,
  521. radius: 4
  522. }, // 提示信息在折线图、区域图上形成点的样式
  523. tooltipCrosshairsRect: {
  524. type: 'rect',
  525. rectStyle: {
  526. fill: '#CCD6EC',
  527. opacity: 0.3
  528. }
  529. }, // tooltip 辅助背景框样式
  530. tooltipCrosshairsLine: {
  531. lineStyle: {
  532. stroke: 'rgba(0, 0, 0, 0.25)',
  533. lineWidth: 1
  534. }
  535. },
  536. // 各种 geometry 默认的样式
  537. shape: {
  538. point: {
  539. lineWidth: 1,
  540. fill: DEFAULT_COLOR,
  541. radius: 4
  542. },
  543. hollowPoint: {
  544. fill: '#fff',
  545. lineWidth: 1,
  546. stroke: DEFAULT_COLOR,
  547. radius: 3
  548. },
  549. interval: {
  550. lineWidth: 0,
  551. fill: DEFAULT_COLOR,
  552. fillOpacity: 0.85
  553. },
  554. hollowInterval: {
  555. fill: '#fff',
  556. stroke: DEFAULT_COLOR,
  557. fillOpacity: 0,
  558. lineWidth: 2
  559. },
  560. area: {
  561. lineWidth: 0,
  562. fill: DEFAULT_COLOR,
  563. fillOpacity: 0.6
  564. },
  565. polygon: {
  566. lineWidth: 0,
  567. fill: DEFAULT_COLOR,
  568. fillOpacity: 1
  569. },
  570. hollowPolygon: {
  571. fill: '#fff',
  572. stroke: DEFAULT_COLOR,
  573. fillOpacity: 0,
  574. lineWidth: 2
  575. },
  576. hollowArea: {
  577. fill: '#fff',
  578. stroke: DEFAULT_COLOR,
  579. fillOpacity: 0,
  580. lineWidth: 2
  581. },
  582. line: {
  583. stroke: DEFAULT_COLOR,
  584. lineWidth: 2,
  585. fill: null
  586. },
  587. edge: {
  588. stroke: DEFAULT_COLOR,
  589. lineWidth: 1,
  590. fill: null
  591. },
  592. schema: {
  593. stroke: DEFAULT_COLOR,
  594. lineWidth: 1,
  595. fill: null
  596. }
  597. },
  598. // 辅助元素的样式
  599. guide: {
  600. line: {
  601. lineStyle: {
  602. stroke: 'rgba(0, 0, 0, .65)',
  603. lineDash: [ 2, 2 ],
  604. lineWidth: 1
  605. },
  606. text: {
  607. position: 'start',
  608. autoRotate: true,
  609. style: {
  610. fill: 'rgba(0, 0, 0, .45)',
  611. fontSize: 12,
  612. textAlign: 'start',
  613. fontFamily: FONT_FAMILY,
  614. textBaseline: 'bottom'
  615. }
  616. }
  617. },
  618. text: {
  619. style: {
  620. fill: 'rgba(0,0,0,.5)',
  621. fontSize: 12,
  622. textBaseline: 'middle',
  623. textAlign: 'start',
  624. fontFamily: FONT_FAMILY
  625. }
  626. },
  627. region: {
  628. style: {
  629. lineWidth: 0, // 辅助框的边框宽度
  630. fill: '#000', // 辅助框填充的颜色
  631. fillOpacity: 0.04 // 辅助框的背景透明度
  632. } // 辅助框的图形样式属性
  633. },
  634. html: {
  635. alignX: 'middle',
  636. alignY: 'middle'
  637. },
  638. dataRegion: {
  639. style: {
  640. region: {
  641. lineWidth: 0,
  642. fill: '#000000',
  643. opacity: 0.04
  644. },
  645. text: {
  646. textAlign: 'center',
  647. textBaseline: 'bottom',
  648. fontSize: 12,
  649. fill: 'rgba(0, 0, 0, .65)'
  650. }
  651. }
  652. },
  653. dataMarker: {
  654. top: true,
  655. style: {
  656. point: {
  657. r: 3,
  658. fill: '#FFFFFF',
  659. stroke: '#1890FF',
  660. lineWidth: 2
  661. },
  662. line: {
  663. stroke: '#A3B1BF',
  664. lineWidth: 1
  665. },
  666. text: {
  667. fill: 'rgba(0, 0, 0, .65)',
  668. opacity: 1,
  669. fontSize: 12,
  670. textAlign: 'start'
  671. }
  672. },
  673. display: {
  674. point: true,
  675. line: true,
  676. text: true
  677. },
  678. lineLength: 20,
  679. direction: 'upward',
  680. autoAdjust: true
  681. }
  682. },
  683. pixelRatio: null
  684. };

其他

还有其他几个配置项:

  1. // 默认使用 canvas
  2. renderer: 'canvas',
  3. // 全局的配置项开关
  4. animate: true,
  5. widthRatio: { // 宽度所占的分类的比例
  6. column: 1 / 2, // 一般的柱状图占比 1/2
  7. rose: 0.9999999, // 玫瑰图柱状占比 1
  8. multiplePie: 1 / 1.3 // 多层的饼图、环图
  9. },
  10. // 折线图、区域图、path 当只有一个数据时,是否显示成点
  11. showSinglePoint: false,
  12. connectNulls: false, // 折线图存在 null 时是否忽视

使用

G2 全局的配置项有两种方式可以更改:

  • 通过 theme 更改全局样式
  • 单独调整单个样式 ```javascript G2.widthRatio.column = 2 / 3;

G2.legend.bottom = {} // 整体更改显示在下面的图例的配置项 ```