动态菜单和动态路由的 antd pro

index.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React, { Component } from 'react';
  2. import { Chart, Tooltip, Geom, Coord, Axis } from 'bizcharts';
  3. import { Row, Col } from 'antd';
  4. import autoHeight from '../autoHeight';
  5. import styles from './index.less';
  6. /* eslint react/no-danger:0 */
  7. @autoHeight()
  8. export default class Radar extends Component {
  9. state = {
  10. legendData: [],
  11. };
  12. componentDidMount() {
  13. this.getLengendData();
  14. }
  15. componentWillReceiveProps(nextProps) {
  16. if (this.props.data !== nextProps.data) {
  17. this.getLengendData();
  18. }
  19. }
  20. getG2Instance = chart => {
  21. this.chart = chart;
  22. };
  23. // for custom lengend view
  24. getLengendData = () => {
  25. if (!this.chart) return;
  26. const geom = this.chart.getAllGeoms()[0]; // 获取所有的图形
  27. const items = geom.get('dataArray') || []; // 获取图形对应的
  28. const legendData = items.map(item => {
  29. // eslint-disable-next-line
  30. const origins = item.map(t => t._origin);
  31. const result = {
  32. name: origins[0].name,
  33. color: item[0].color,
  34. checked: true,
  35. value: origins.reduce((p, n) => p + n.value, 0),
  36. };
  37. return result;
  38. });
  39. this.setState({
  40. legendData,
  41. });
  42. };
  43. handleRef = n => {
  44. this.node = n;
  45. };
  46. handleLegendClick = (item, i) => {
  47. const newItem = item;
  48. newItem.checked = !newItem.checked;
  49. const { legendData } = this.state;
  50. legendData[i] = newItem;
  51. const filteredLegendData = legendData.filter(l => l.checked).map(l => l.name);
  52. if (this.chart) {
  53. this.chart.filter('name', val => filteredLegendData.indexOf(val) > -1);
  54. this.chart.repaint();
  55. }
  56. this.setState({
  57. legendData,
  58. });
  59. };
  60. render() {
  61. const defaultColors = [
  62. '#1890FF',
  63. '#FACC14',
  64. '#2FC25B',
  65. '#8543E0',
  66. '#F04864',
  67. '#13C2C2',
  68. '#fa8c16',
  69. '#a0d911',
  70. ];
  71. const {
  72. data = [],
  73. height = 0,
  74. title,
  75. hasLegend = false,
  76. forceFit = true,
  77. tickCount = 4,
  78. padding = [35, 30, 16, 30],
  79. animate = true,
  80. colors = defaultColors,
  81. } = this.props;
  82. const { legendData } = this.state;
  83. const scale = {
  84. value: {
  85. min: 0,
  86. tickCount,
  87. },
  88. };
  89. const chartHeight = height - (hasLegend ? 80 : 22);
  90. return (
  91. <div className={styles.radar} style={{ height }}>
  92. {title && <h4>{title}</h4>}
  93. <Chart
  94. scale={scale}
  95. height={chartHeight}
  96. forceFit={forceFit}
  97. data={data}
  98. padding={padding}
  99. animate={animate}
  100. onGetG2Instance={this.getG2Instance}
  101. >
  102. <Tooltip />
  103. <Coord type="polar" />
  104. <Axis
  105. name="label"
  106. line={null}
  107. tickLine={null}
  108. grid={{
  109. lineStyle: {
  110. lineDash: null,
  111. },
  112. hideFirstLine: false,
  113. }}
  114. />
  115. <Axis
  116. name="value"
  117. grid={{
  118. type: 'polygon',
  119. lineStyle: {
  120. lineDash: null,
  121. },
  122. }}
  123. />
  124. <Geom type="line" position="label*value" color={['name', colors]} size={1} />
  125. <Geom
  126. type="point"
  127. position="label*value"
  128. color={['name', colors]}
  129. shape="circle"
  130. size={3}
  131. />
  132. </Chart>
  133. {hasLegend && (
  134. <Row className={styles.legend}>
  135. {legendData.map((item, i) => (
  136. <Col
  137. span={24 / legendData.length}
  138. key={item.name}
  139. onClick={() => this.handleLegendClick(item, i)}
  140. >
  141. <div className={styles.legendItem}>
  142. <p>
  143. <span
  144. className={styles.dot}
  145. style={{
  146. backgroundColor: !item.checked ? '#aaa' : item.color,
  147. }}
  148. />
  149. <span>{item.name}</span>
  150. </p>
  151. <h6>{item.value}</h6>
  152. </div>
  153. </Col>
  154. ))}
  155. </Row>
  156. )}
  157. </div>
  158. );
  159. }
  160. }