barChart.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useEffect, useRef } from 'react';
  2. import * as echarts from 'echarts';
  3. interface propsData {
  4. seriesData: string[];
  5. yData: string[];
  6. color: string;
  7. width: string;
  8. height: string;
  9. }
  10. const LineChart: React.FC<propsData> = (props) => {
  11. const chartRef: any = useRef();
  12. const { yData, seriesData, color, width, height } = props;
  13. // 初始化
  14. useEffect(() => {
  15. const chart = echarts.init(chartRef.current);
  16. const options = {
  17. legend: {
  18. show: false,
  19. },
  20. grid: {
  21. left: '2%',
  22. right: '2%',
  23. bottom: '2%',
  24. top: '3%',
  25. containLabel: true,
  26. },
  27. xAxis: {
  28. type: 'value',
  29. splitLine: {
  30. lineStyle: {
  31. color: '#b0b0b0',
  32. type: 'dashed',
  33. },
  34. },
  35. axisTick: {
  36. show: false,
  37. },
  38. axisLabel: {
  39. // 改变x轴字体颜色和大小
  40. textStyle: {
  41. color: '#858585',
  42. fontSize: 14,
  43. },
  44. },
  45. },
  46. yAxis: {
  47. type: 'category',
  48. data: yData,
  49. splitLine: {
  50. show: false,
  51. },
  52. axisTick: {
  53. show: false,
  54. },
  55. axisLine: {
  56. // 改变y轴颜色
  57. lineStyle: {
  58. color: '#b0b0b0',
  59. },
  60. },
  61. axisLabel: {
  62. textStyle: {
  63. color: '#858585',
  64. fontSize: 14,
  65. },
  66. },
  67. },
  68. series: [
  69. {
  70. type: 'bar',
  71. name: '产出',
  72. barWidth: 22,
  73. itemStyle: {
  74. normal: {
  75. label: {
  76. show: true, //开启显示
  77. position: 'insideRight', //在上方显示
  78. textStyle: {
  79. //数值样式
  80. color: '#FFFFFF',
  81. fontSize: 12,
  82. fontWeight: 600,
  83. },
  84. },
  85. color: color,
  86. barBorderRadius: 15,
  87. },
  88. },
  89. data: seriesData,
  90. },
  91. ],
  92. };
  93. chart.setOption(options);
  94. }, []);
  95. return <div style={{ width: width, height: height }} ref={chartRef} />;
  96. };
  97. export default LineChart;