EnquirePieChart.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from "react";
  2. import * as echarts from "echarts";
  3. import styles from "../styles/enquire_pie_chart.less";
  4. class Pie3DChart extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.chartRef = React.createRef();
  8. this.chartInstance = null;
  9. this.endColor = [126, 206, 244]; // RGB 起点
  10. this.startColor = [15, 40, 99]; // RGB 终点
  11. }
  12. componentDidMount() {
  13. this.initChart();
  14. window.addEventListener("resize", this.chartResize);
  15. }
  16. componentDidUpdate(prevProps) {
  17. this.initChart();
  18. }
  19. componentWillUnmount() {
  20. if (this.chartInstance) {
  21. window.removeEventListener("resize", this.chartResize);
  22. this.chartInstance.dispose();
  23. }
  24. }
  25. colorScale(index, total) {
  26. const { startColor, endColor } = this;
  27. return `rgb(${Math.round(
  28. startColor[0] + ((endColor[0] - startColor[0]) * index) / (total - 1)
  29. )},
  30. ${Math.round(
  31. startColor[1] +
  32. ((endColor[1] - startColor[1]) * index) / (total - 1)
  33. )},
  34. ${Math.round(
  35. startColor[2] +
  36. ((endColor[2] - startColor[2]) * index) / (total - 1)
  37. )})`;
  38. }
  39. initChart() {
  40. if (!this.chartInstance) {
  41. this.chartInstance = echarts.init(this.chartRef.current);
  42. }
  43. const option = {
  44. color: this.props.data.map((item, index) =>
  45. this.colorScale(index, this.props.data.length)
  46. ), // 自动生成渐变颜色
  47. tooltip: {
  48. trigger: "item",
  49. textStyle: {
  50. fontSize: 25, // 设置字体大小
  51. fontWeight: "bold", // 可选:加粗
  52. // color: "#fff", // 可选:字体颜色
  53. },
  54. },
  55. legend: {
  56. // type: "scroll", // 关键
  57. // itemGap: 15,
  58. // width: "90%", // 控制整体宽度
  59. // height: 60, // ⚡ 关键:高度大约容纳 3 行 (每行大约 18-20px)
  60. bottom: 0,
  61. // width: "80%", // 限制宽度
  62. textStyle: { color: "#fff", fontSize: "12px" },
  63. formatter: function (name) {
  64. return name.length > 6 ? name.slice(0, 6) + "…" : name;
  65. }
  66. },
  67. grid: {
  68. left: "4%",
  69. right: "4%",
  70. bottom: "8%",
  71. top: "10%",
  72. containLabel: true,
  73. },
  74. series: [
  75. {
  76. // name: 'Access From',
  77. type: "pie",
  78. // radius: ["50%", "80%"],
  79. center: ["50%", "35%"], // 默认是 ["50%", "50%"],这里把 y 改成 35% 就能往上移
  80. avoidLabelOverlap: false,
  81. itemStyle: {
  82. // borderRadius: 10,
  83. // borderColor: "#fff",
  84. borderWidth: 2,
  85. },
  86. label: {
  87. show: false,
  88. position: "center",
  89. },
  90. emphasis: {
  91. label: {
  92. show: true,
  93. fontSize: 25,
  94. fontWeight: "bold",
  95. color: "#fff",
  96. },
  97. },
  98. labelLine: {
  99. show: false,
  100. },
  101. data: this.props.data,
  102. },
  103. ],
  104. };
  105. this.chartInstance.setOption(option);
  106. }
  107. render() {
  108. return (
  109. <div ref={this.chartRef} style={{ width: "100%", height: "100%" }} />
  110. );
  111. }
  112. }
  113. export default Pie3DChart;