image.png

dataset

DatasetComponent https://echarts.apache.org/examples/zh/editor.html?c=dataset-link&edit=1&reset=1

  1. import { useEffect, useState } from 'react';
  2. import {array, number, string, oneOfType} from 'prop-types';
  3. import {
  4. DatasetComponent,
  5. TooltipComponent,
  6. GridComponent,
  7. LegendComponent
  8. } from 'echarts/components';
  9. import { BarChart } from 'echarts/charts';
  10. import { UniversalTransition } from 'echarts/features'
  11. import ECharts from '../../ECharts';
  12. import { getOption } from './rowBarChartOption'
  13. Chart.propTypes = {
  14. data: array,
  15. height: oneOfType([number, string]),
  16. className: string,
  17. };
  18. Chart.defaultProps = {
  19. height: 320,
  20. }
  21. function Chart({data, height, className}) {
  22. const [options, setOptions] = useState({})
  23. useEffect(update, [data])
  24. function update() {
  25. setOptions(getOption({data}))
  26. }
  27. const attrs = {
  28. // renderType: 'svg',
  29. className,
  30. height,
  31. options,
  32. components: [
  33. DatasetComponent,
  34. TooltipComponent,
  35. GridComponent,
  36. LegendComponent,
  37. BarChart,
  38. UniversalTransition
  39. ]
  40. }
  41. return (
  42. <ECharts {...attrs} />
  43. );
  44. }
  45. export default Chart;

rowBarChartOption

  1. const color = ['rgb(116,166,49)', 'rgb(190,245,99)', 'rgb(202,252,137)', 'rgb(251,253,142)']
  2. export function getOption({data, length, theme}) {
  3. return {
  4. color,
  5. grid: {
  6. top: length === 1 ? 24 : 40,
  7. left: 24,
  8. right: 24,
  9. bottom: 24,
  10. containLabel: true,
  11. },
  12. dataset: {
  13. source: [
  14. ['指标', '0-20', '20-30', '30-50', '50+'],
  15. ['年龄分布', 300, 400, 500, 600]
  16. ]
  17. },
  18. xAxis: {
  19. type: 'value',
  20. max: 1800,
  21. axisLine: {
  22. lineStyle: {
  23. color: 'rgb(50,51,53)',
  24. width: 3
  25. }
  26. },
  27. axisTick: { show: false },
  28. axisLabel: {
  29. color: 'rgb(98,105,113)',
  30. fontSize: 18
  31. }
  32. },
  33. yAxis: {
  34. type: 'category',
  35. show: false,
  36. },
  37. series: [
  38. {
  39. type: 'bar',
  40. stack: 'total',
  41. barMaxWidth: 40
  42. },
  43. {
  44. type: 'bar',
  45. stack: 'total'
  46. },
  47. {
  48. type: 'bar',
  49. stack: 'total'
  50. },
  51. {
  52. type: 'bar',
  53. stack: 'total'
  54. }
  55. ],
  56. tooltip: {
  57. trigger: 'axis',
  58. backgroundColor: 'rgba(50,50,50, .8)',
  59. textStyle: {
  60. fontSize: 24, color: '#ccc',
  61. },
  62. enterable: true,
  63. axisPointer: {
  64. type: 'shadow'
  65. },
  66. extraCssText: 'max-height: 300px; overflow: auto;',
  67. },
  68. legend: {
  69. type: 'scroll',
  70. show: length !== 1,
  71. icon: 'circle',
  72. top: 0,
  73. right: 24,
  74. itemWidth: 24,
  75. textStyle: {
  76. color: theme === 'dark' ? '#fff' : '#333',
  77. },
  78. },
  79. };
  80. }