BarSingle.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. normal: {
  52. borderColor: '#ffffff',
  53. },
  54. },
  55. show: true,
  56. },
  57. },
  58. },
  59. dataZoom: null,
  60. legend: null,
  61. timeline: null,
  62. xAxis: null,
  63. yAxis: null,
  64. series: null,
  65. });
  66. watchEffect(() => {
  67. props.dataSource && option.series && initCharts();
  68. });
  69. function initChartsOption() {
  70. const optionUtil = new EchartsUtil(option);
  71. optionUtil.initChartOption('listMonitor', chartsColumns);
  72. console.log('option----------->', option);
  73. }
  74. initChartsOption();
  75. function initCharts() {
  76. //轴数据
  77. if (option.series) {
  78. const xAxisData: string[] = [];
  79. const seriesData = [];
  80. props.xAxisData.forEach((item: any) => {
  81. xAxisData.push(item['key']);
  82. seriesData.push(props.dataSource[item.valueKey] || '');
  83. });
  84. option.series[0].data = seriesData;
  85. option.xAxis[0].data = xAxisData;
  86. console.log('option------------->', option);
  87. setOptions(option, false);
  88. }
  89. }
  90. return { chartRef };
  91. },
  92. });
  93. </script>