RTMMBarchart.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from "react";
  2. import EChart, {type EChartProps} from "../echarts";
  3. // 柱状图特定属性接口
  4. export type BarChartProps = {
  5. // 柱状图数据
  6. data: { value: number; name: string }[];
  7. // 柱状图标题
  8. title?: string;
  9. // X轴名称
  10. xAxisName?: string;
  11. // Y轴名称
  12. yAxisName?: string;
  13. // 柱状图颜色配置
  14. color?: string;
  15. // 其他EChart通用属性
  16. } & Omit<EChartProps, "option">;
  17. const BarChart = ({
  18. data,
  19. title = "统计数据",
  20. xAxisName = "",
  21. yAxisName = "",
  22. color = "#1890ff",
  23. ...props
  24. }: BarChartProps) => {
  25. // 提取x轴数据和系列数据
  26. const xAxisData = data.map(item => item.name);
  27. const seriesData = data.map(item => item.value);
  28. // 默认柱状图配置
  29. const defaultOption = {
  30. tooltip: {
  31. trigger: 'axis',
  32. axisPointer: {
  33. type: 'shadow'
  34. },
  35. },
  36. grid: {
  37. left: '0%',
  38. right: '0%',
  39. bottom: '0%',
  40. top: '10%',
  41. containLabel: true
  42. },
  43. xAxis: [
  44. {
  45. type: 'category',
  46. data: xAxisData,
  47. axisLine: {
  48. lineStyle: {
  49. color: '#fff'
  50. }
  51. },
  52. axisLabel: {
  53. color: 'black'
  54. },
  55. name: xAxisName,
  56. nameTextStyle: {
  57. color: 'black',
  58. padding: [0, 0, 10, 0]
  59. }
  60. }
  61. ],
  62. yAxis: [
  63. {
  64. type: 'value',
  65. axisLine: {
  66. lineStyle: {
  67. color: 'black'
  68. }
  69. },
  70. axisLabel: {
  71. color: 'black',
  72. formatter: '{value}'
  73. },
  74. splitLine: {
  75. lineStyle: {
  76. color: 'rgba(255, 255, 255, 0.1)'
  77. }
  78. },
  79. name: yAxisName,
  80. nameTextStyle: {
  81. color: 'black',
  82. padding: [0, 10, 0, 0]
  83. }
  84. }
  85. ],
  86. series: [
  87. {
  88. name: title,
  89. type: 'bar',
  90. barWidth: '60%',
  91. data: seriesData,
  92. itemStyle: {
  93. color: color,
  94. borderRadius: [4, 4, 0, 0]
  95. }
  96. }
  97. ]
  98. };
  99. return <EChart option={defaultOption} {...props} />;
  100. };
  101. export default BarChart;