获取方式: G2.Global

G2的全局配置信息,用于主题等的设置。

属性

G2.Global 中属性有两种类型:

  • 度量相关
  • 皮肤主题相关

皮肤相关的属性如下:

  1. var DEFAULT_COLOR = '#4E7CCC';
  2. var FONT_FAMILY = '"Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", SimSun, "sans-serif"';
  3. var Theme = {
  4. defaultColor: DEFAULT_COLOR, // 默认主题色
  5. plotCfg: {
  6. margin: [20, 80, 60, 80]
  7. },
  8. facetCfg: {
  9. type: 'rect',
  10. margin: 10,
  11. facetTitle: {
  12. titleOffset: 16,
  13. colDimTitle: {
  14. title: {
  15. fontSize: 14,
  16. textAlign: 'center',
  17. fill: '#999'
  18. }
  19. },
  20. colTitle: {
  21. title: {
  22. fontSize: 12,
  23. textAlign: 'center',
  24. fill: '#999'
  25. }
  26. },
  27. rowTitle: {
  28. title: {
  29. fontSize: 12,
  30. textAlign: 'center',
  31. rotate: 90,
  32. fill: '#999'
  33. }
  34. },
  35. rowDimTitle: {
  36. title: {
  37. fontSize: 12,
  38. textAlign: 'center',
  39. rotate: 90,
  40. fill: '#999'
  41. }
  42. }
  43. }
  44. },
  45. binWidth: 0.03, // bin 统计的默认值
  46. fontFamily: FONT_FAMILY,
  47. colors: {
  48. 'default': ['#4E7CCC', '#36B3C3', '#4ECDA5', '#94E08A', '#E2F194', '#EDCC72', '#F8AB60', '#F9815C', '#EB4456', '#C82B3D'],
  49. intervalStack: ['#4E7CCC', '#36B3C3', '#4ECDA5', '#94E08A', '#E2F194', '#EDCC72', '#F8AB60', '#F9815C', '#EB4456', '#C82B3D']
  50. },
  51. shapes: {
  52. point: ['hollowCircle', 'hollowSquare', 'hollowDiamond', 'hollowBowtie', 'hollowTriangle',
  53. 'hollowHexagon', 'cross', 'tick', 'plus', 'hyphen', 'line'],
  54. line: ['line', 'dash', 'dot'],
  55. area: ['area']
  56. },
  57. hues: ['red', 'yellow', 'green', 'blue', 'orange', 'purple', 'pink', 'brown', 'white', 'gray', 'black'],
  58. axis: {
  59. top: {
  60. position: 'top',
  61. titleOffset: 30,
  62. title: {
  63. fontSize: 12,
  64. fill: '#999'
  65. },
  66. labels: {
  67. label: {
  68. fill: '#404040',
  69. fontSize: 12
  70. }
  71. },
  72. tickLine: {
  73. lineWidth: 1,
  74. stroke: '#ccc',
  75. value: 5
  76. }
  77. },
  78. bottom: {
  79. position: 'bottom',
  80. titleOffset: 45,
  81. labelOffset: 20,
  82. title: {
  83. fontSize: 12,
  84. textAlign: 'center',
  85. fill: '#999'
  86. },
  87. labels: {
  88. label: {
  89. fill: '#404040',
  90. fontSize: 12
  91. }
  92. },
  93. line: {
  94. lineWidth: 1,
  95. stroke: '#ccc'
  96. },
  97. tickLine: {
  98. lineWidth: 1,
  99. stroke: '#ccc',
  100. value: 5
  101. }
  102. },
  103. left: {
  104. position: 'left',
  105. titleOffset: 60,
  106. labelOffset: 13,
  107. title: {
  108. fontSize: 12,
  109. fill: '#999'
  110. },
  111. labels: {
  112. label: {
  113. fill: '#404040'
  114. }
  115. },
  116. line: {
  117. lineWidth: 1,
  118. stroke: '#ccc'
  119. },
  120. tickLine: {
  121. lineWidth: 1,
  122. stroke: '#ccc',
  123. value: 5
  124. },
  125. grid: {
  126. line: {
  127. stroke: '#d9d9d9',
  128. lineWidth: 1,
  129. lineDash: [2, 2]
  130. }
  131. }
  132. },
  133. right: {
  134. position: 'right',
  135. titleOffset: 60,
  136. labelOffset: 13,
  137. title: {
  138. fontSize: 12,
  139. fill: '#999'
  140. },
  141. labels: {
  142. label: {
  143. fill: '#404040'
  144. }
  145. },
  146. line: {
  147. lineWidth: 1,
  148. stroke: '#ccc'
  149. },
  150. tickLine: {
  151. lineWidth: 1,
  152. stroke: '#ccc',
  153. value: 5
  154. }
  155. },
  156. circle: {
  157. labelOffset: 5,
  158. line: {
  159. lineWidth: 1,
  160. stroke: '#ccc'
  161. },
  162. grid: {
  163. line: {
  164. stroke: '#d9d9d9',
  165. lineWidth: 1,
  166. lineDash: [1, 3]
  167. }
  168. },
  169. labels: {
  170. label: {
  171. fill: '#404040'
  172. }
  173. }
  174. },
  175. gauge: {
  176. grid: null,
  177. labelOffset: 5,
  178. tickLine: {
  179. lineWidth: 1,
  180. value: -20,
  181. stroke: '#ccc'
  182. },
  183. subTick: 5,
  184. labels: {
  185. label: {
  186. fill: '#404040'
  187. }
  188. }
  189. },
  190. clock: {
  191. grid: null,
  192. labelOffset: 5,
  193. tickLine: {
  194. lineWidth: 1,
  195. value: -20,
  196. stroke: '#C0D0E0'
  197. },
  198. subTick: 5,
  199. labels: {
  200. label: {
  201. fill: '#404040'
  202. }
  203. }
  204. },
  205. radius: {
  206. titleOffset: 45,
  207. labels: {
  208. label: {
  209. fill: '#404040'
  210. }
  211. },
  212. line: {
  213. lineWidth: 1,
  214. stroke: '#ccc'
  215. },
  216. grid: {
  217. line: {
  218. stroke: '#d9d9d9',
  219. lineWidth: 1,
  220. lineDash: [2, 2]
  221. },
  222. type: 'circle'
  223. }
  224. },
  225. helix: {
  226. grid: null,
  227. labels: {
  228. label: null
  229. },
  230. line: {
  231. lineWidth: 1,
  232. stroke: '#ccc'
  233. },
  234. tickLine: {
  235. lineWidth: 1,
  236. value: 5,
  237. stroke: '#ccc'
  238. }
  239. }
  240. },
  241. labels: {
  242. offset: 14,
  243. label: {
  244. fill: '#666',
  245. fontSize: 12
  246. }
  247. },
  248. treemapLabels: {
  249. offset: 10,
  250. label: {
  251. fill: '#fff',
  252. fontSize: 14,
  253. textBaseline: 'top',
  254. fontStyle: 'bold'
  255. }
  256. },
  257. innerLabels: {
  258. label: {
  259. fill: '#fff',
  260. fontSize: 12
  261. }
  262. }, // 在theta坐标系下的饼图文本内部的样式
  263. thetaLabels: {
  264. labelLine: {
  265. lineWidth: 1
  266. },
  267. labelHeight: 14,
  268. offset: 30
  269. }, // 在theta坐标系下的饼图文本的样式
  270. legend: {
  271. right: {
  272. position: 'right',
  273. back: null,
  274. spacingX: 10,
  275. spacingY: 12,
  276. markerAlign: 'center',
  277. wordSpaceing: 12,
  278. width: 20,
  279. height: 156
  280. },
  281. left: {
  282. position: 'left',
  283. back: null,
  284. spacingX: 10,
  285. spacingY: 12,
  286. markerAlign: 'center',
  287. wordSpaceing: 12,
  288. width: 20,
  289. height: 156
  290. },
  291. top: {
  292. position: 'top',
  293. title: null,
  294. back: null,
  295. spacingX: 16,
  296. spacingY: 10,
  297. markerAlign: 'center',
  298. wordSpaceing: 12,
  299. width: 156,
  300. height: 20
  301. },
  302. bottom: {
  303. position: 'bottom',
  304. title: null,
  305. back: null,
  306. spacingX: 16,
  307. spacingY: 10,
  308. markerAlign: 'center',
  309. wordSpaceing: 12,
  310. width: 156,
  311. height: 20
  312. }
  313. },
  314. tooltip: {
  315. crosshairs: false,
  316. offset: 15,
  317. crossLine: {
  318. stroke: '#666'
  319. },
  320. wordSpaceing: 6,
  321. markerCfg: {
  322. symbol: 'circle',
  323. radius: 3
  324. }
  325. },
  326. activeShape: {
  327. point: {
  328. radius: 5,
  329. fillOpacity: 0.7
  330. },
  331. hollowPoint: {
  332. lineWidth: 2,
  333. radius: 4
  334. },
  335. interval: {
  336. fillOpacity: 0.7
  337. },
  338. hollowInterval: {
  339. lineWidth: 2
  340. },
  341. area: {
  342. fillOpacity: 0.85
  343. },
  344. hollowArea: {
  345. lineWidth: 2
  346. },
  347. line: {
  348. lineWidth: 2
  349. },
  350. polygon: {
  351. fillOpacity: 0.75
  352. }
  353. }, // 图形激活时,鼠标移动到上面
  354. shape: {
  355. point: {
  356. lineWidth: 1,
  357. fill: DEFAULT_COLOR,
  358. radius: 4
  359. },
  360. hollowPoint: {
  361. fill: '#fff',
  362. lineWidth: 1,
  363. stroke: DEFAULT_COLOR,
  364. radius: 3
  365. },
  366. interval: {
  367. lineWidth: 0,
  368. fill: DEFAULT_COLOR,
  369. fillOpacity: 0.85
  370. },
  371. pie: {
  372. lineWidth: 1,
  373. stroke: '#fff'
  374. },
  375. hollowInterval: {
  376. fill: '#fff',
  377. stroke: DEFAULT_COLOR,
  378. fillOpacity: 0,
  379. lineWidth: 1
  380. },
  381. area: {
  382. lineWidth: 0,
  383. fill: DEFAULT_COLOR,
  384. fillOpacity: 0.6
  385. },
  386. polygon: {
  387. lineWidth: 0,
  388. fill: DEFAULT_COLOR,
  389. fillOpacity: 1
  390. },
  391. hollowPolygon: {
  392. fill: '#fff',
  393. stroke: DEFAULT_COLOR,
  394. fillOpacity: 0,
  395. lineWidth: 1
  396. },
  397. hollowArea: {
  398. fill: '#fff',
  399. stroke: DEFAULT_COLOR,
  400. fillOpacity: 0,
  401. lineWidth: 1
  402. },
  403. line: {
  404. stroke: DEFAULT_COLOR,
  405. lineWidth: 1,
  406. fill: null
  407. }
  408. },
  409. guide: {
  410. text: {
  411. fill: '#666',
  412. fontSize: 12
  413. },
  414. line: {
  415. stroke: DEFAULT_COLOR,
  416. lineDash: [0, 2, 2]
  417. },
  418. rect: {
  419. lineWidth: 0,
  420. fill: DEFAULT_COLOR,
  421. fillOpacity: 0.1
  422. },
  423. tag: {
  424. line: {
  425. stroke: DEFAULT_COLOR,
  426. lineDash: [0, 2, 2]
  427. },
  428. text: {
  429. fill: '#666',
  430. fontSize: 12,
  431. textAlign: 'center'
  432. },
  433. rect: {
  434. lineWidth: 0,
  435. fill: DEFAULT_COLOR,
  436. fillOpacity: 0.1
  437. }
  438. },
  439. html: {
  440. align: 'cc'
  441. }
  442. },
  443. tooltipMarker: {
  444. fill: '#fff',
  445. symbol: 'circle',
  446. lineWidth: 2,
  447. stroke: DEFAULT_COLOR,
  448. radius: 4
  449. } // 提示信息在折线图、区域图上形成点的样式
  450. };

方法

G2.Global 只提供了一种方法:setTheme(string | object) 设置主题。

Global.setTheme

方法名: Global.setTheme(theme)

参数

  • theme: String | Object

当参数为字符串时,取值必须为 ['default', 'dark', 'cheery'] 中的一种,每一个值代表一个主题,否则还是使用默认的主题。详见皮肤

  1. G2.Global.setTheme('dark'); // 将主题设置为 dark

当参数为 object 对象时,可以是 Global.Theme.darkGlobal.Theme.defaultGlobal.Theme.cheery 这三种,分别对应 'dark' 'default' 'cheery' 这三个主题名。

也可是使用户自定义的主题配置。

  1. G2.Global.setTheme(G2.Theme.dark); // 将主题设置为 dark
  2. var customTheme = {
  3. titleOffset: 30,
  4. plotCfg: {
  5. background: {
  6. fill: '#18242E'
  7. }
  8. },
  9. facetCfg: {
  10. type: 'rect',
  11. margin: 40
  12. }
  13. };
  14. Global.setTheme(customTheme); // 将主题设置为用户自定义的主题