基于 G2与G2Plot封装的React图表库, React-Antd技术栈
bizchart网站 https://bizcharts.net
bizchart鱼雀文档 https://www.yuque.com/antv/g2-docs/api-geom#label

bizcharts4x和 3x的区别

https://bizcharts.net/product/BizCharts4/category/61/page/104
image.png

折线图缩放

  1. bizchart@3.5.8 暂时不支持 4.x版本
  2. 缩放用到g2-brush
  3. 折线图缩放案例:https://codepen.io/fengyue/pen/WNNwJEZ?editors=1010
  1. import React from 'react';
  2. import { Axis, Chart, Geom, Tooltip } from 'bizcharts';
  3. import Brush from '@antv/g2-brush';
  4. import dayJs from 'dayjs';
  5. export default class LineChart extends React.Component {
  6. static defaultProps = {
  7. data: [],
  8. padding: 'auto',
  9. className: '',
  10. };
  11. componentDidUpdate = () => {
  12. if (this.chart && !this.brush) {
  13. this.brush = new Brush({
  14. canvas: this.chart.get('canvas'),
  15. chart: this.chart,
  16. type: 'X',
  17. onBrushstart() {
  18. this.chart.hideTooltip();
  19. },
  20. onBrushmove() {
  21. this.chart.hideTooltip();
  22. },
  23. });
  24. }
  25. };
  26. processData = (data = []) => {
  27. return data.map(item => {
  28. item.time = `${dayJs(item.time).format('YYYY-MM-DD HH:mm')}`;
  29. return item;
  30. });
  31. };
  32. render() {
  33. const { height, data, padding, className } = this.props;
  34. const scale = {
  35. value: { min: 0 },
  36. time: { tickCount: 20 },
  37. };
  38. return (
  39. <div className={`g-flexbox g-justify-center ${className}`}>
  40. <Chart
  41. forceFit
  42. onGetG2Instance={g2Chart => {
  43. g2Chart.animate(false);
  44. this.chart = g2Chart;
  45. }}
  46. scale={scale}
  47. padding={padding}
  48. height={height}
  49. data={this.processData(data)}
  50. >
  51. <Axis name="time" />
  52. <Axis name="value" />
  53. <Geom type="line" position="time*value" size={2} shape="smooth" />
  54. <Tooltip shared />
  55. </Chart>
  56. </div>
  57. );
  58. }
  59. }

legend图例

  1. legend API文档 https://bizcharts.net/product/bizcharts/category/7/page/29#g2
  2. 自定义图例形状 https://bizcharts.net/product/bizcharts/category/7/page/29#custom

折柱混合图表

  1. const legendItem = [
  2. {
  3. value: "百分比", // percent
  4. marker: {
  5. symbol: "square",
  6. fill: "#3182bd", // 深蓝
  7. radius: 5
  8. }
  9. },
  10. {
  11. value: "总量", // total
  12. fill: '#99d8c9',
  13. marker: {
  14. symbol: "hyphen",
  15. stroke: "#ffae6b",
  16. radius: 5,
  17. lineWidth: 3
  18. }
  19. },
  20. {
  21. value: "已使用", // used
  22. fill: '#fdae6b',
  23. marker: {
  24. symbol: "hyphen",
  25. stroke: "#336699",
  26. radius: 5,
  27. lineWidth: 3
  28. }
  29. }
  30. ]
  31. let chartIns = null
  32. const padding = [32, 16, 64, 48]
  33. const data = [
  34. {
  35. time: "10:16",
  36. percent: 90,
  37. total: 600,
  38. used: 200
  39. }
  40. ]
  41. const scale = {
  42. util: {
  43. alias: "使用率",
  44. type: "linear",
  45. formatter: val => `${val}%`,
  46. tickCount: 5,
  47. ticks: [0, 25, 50, 75, 100]
  48. },
  49. total: {
  50. alias: "总量",
  51. type: "linear",
  52. },
  53. used: {
  54. alias: "已使用",
  55. type: "linear",
  56. }
  57. }
  58. class LineBar extends React.PureComponent {
  59. render() {
  60. return (
  61. <Chart
  62. autoFit
  63. padding={padding}
  64. scale={scale}
  65. data={data}
  66. height={360}
  67. onGetG2Instance={chart => {
  68. chartIns = chart
  69. }}
  70. >
  71. <Legend
  72. custom={true}
  73. allowAllCanceled={true}
  74. items={legendItem}
  75. onClick={ev => {
  76. const item = ev.item;
  77. const value = item.value;
  78. const checked = ev.checked;
  79. const geoms = chartIns.getAllGeoms();
  80. for (let i = 0; i < geoms.length; i++) {
  81. const geom = geoms[i];
  82. if (geom.getYScale().field === value) {
  83. if (checked) {
  84. geom.show();
  85. } else {
  86. geom.hide();
  87. }
  88. }
  89. }
  90. }}
  91. />
  92. <Axis
  93. name="used"
  94. grid={null}
  95. label={{
  96. textStyle: {
  97. fill: "#fdae6b"
  98. }
  99. }}
  100. />
  101. <Tooltip />
  102. <Geom type="interval" position="time*util" color="#3182bd" />
  103. <Geom
  104. type="line"
  105. position="time*total"
  106. color="#9900ff"
  107. size={3}
  108. shape="smooth"
  109. />
  110. <Geom
  111. type="line"
  112. position="time*free"
  113. color="#fdae6b"
  114. size={3}
  115. shape="smooth"
  116. />
  117. </Chart>
  118. )
  119. }
  120. }
  121. export default LineBar