BarSingle1.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect, onMounted } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { toEchartsData } from '/@/utils/ventutil';
  8. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  9. import EchartsUtil from '/@/utils/echartsUtil';
  10. export default defineComponent({
  11. name: 'BarAndLine',
  12. props: {
  13. chartsColumns: {
  14. type: Array,
  15. default: () => [],
  16. },
  17. chartsColumnsType: {
  18. type: String,
  19. },
  20. dataSource: {
  21. type: Object,
  22. default: () => {},
  23. },
  24. xAxisData: {
  25. type: Array,
  26. default: () => [],
  27. },
  28. width: {
  29. type: String as PropType<string>,
  30. default: '100%',
  31. },
  32. height: {
  33. type: String as PropType<string>,
  34. default: 'calc(100vh - 78px)',
  35. },
  36. },
  37. setup(props) {
  38. const chartRef = ref<HTMLDivElement | null>(null);
  39. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  40. const chartData = getTableHeaderColumns(props.chartsColumnsType) || [];
  41. const chartsColumns = props.chartsColumns.length > 0 ? props.chartsColumns : chartData;
  42. const option = reactive({
  43. name: '',
  44. color: ['#7B68EE', '#0000CD', '#6495ED', '#00BFFF', '#AFEEEE', '#008080', '#00FA9A', '#2E8B57', '#FAFAD2', '#DAA520'],
  45. tooltip: null,
  46. grid: null,
  47. toolbox: {
  48. feature: {
  49. saveAsImage: {
  50. iconStyle: {
  51. borderColor: '#ffffff',
  52. },
  53. show: true,
  54. },
  55. },
  56. },
  57. dataZoom: null,
  58. legend: null,
  59. timeline: null,
  60. xAxis: null,
  61. yAxis: null,
  62. series: null,
  63. });
  64. watchEffect(() => {
  65. props.dataSource && option.series && initCharts();
  66. });
  67. function initChartsOption() {
  68. const optionUtil = new EchartsUtil(option);
  69. optionUtil.initChartOption('listMonitor', chartsColumns);
  70. console.log('option----------->', option);
  71. }
  72. initChartsOption();
  73. function initCharts() {
  74. //轴数据
  75. if (option.series) {
  76. const xAxisData: string[] = [];
  77. const seriesData = [];
  78. props.xAxisData.forEach((item: any) => {
  79. xAxisData.push(item['key']);
  80. seriesData.push(props.dataSource[item.valueKey] || '');
  81. });
  82. option.series[0].data = seriesData;
  83. option.xAxis[0].data = xAxisData;
  84. console.log('option------------->', option);
  85. setOptions(option, false);
  86. }
  87. }
  88. return { chartRef };
  89. },
  90. });
  91. </script>