index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div ref="chartsRef" class="echarts" />
  3. </template>
  4. <script setup lang="ts">
  5. import BarCharts from './components/bar.vue'
  6. import * as echarts from 'echarts'
  7. import { EChartsType } from 'echarts/core'
  8. import { onMounted, ref, reactive } from 'vue'
  9. const chartsRef = ref<HTMLElement | null>()
  10. const data = [154, 230, 224, 218, 135, 147, 260]
  11. const color = ['#fa796f', '#54c1fb', '#ca6cd4', '#59dcc1', '#09a4ea', '#e98f4d', '#ea8e49']
  12. const dataOptions = []
  13. data.forEach((item, index) => {
  14. let obj = {
  15. value: data[index],
  16. itemStyle: {
  17. color: color[index],
  18. },
  19. }
  20. dataOptions.push(obj)
  21. })
  22. const options = {
  23. color,
  24. grid: {
  25. top: '10%',
  26. left: '3%',
  27. right: '4%',
  28. bottom: '10%',
  29. containLabel: true,
  30. },
  31. tooltip: {
  32. trigger: 'axis',
  33. backgroundColor: 'rgba(0,0,0,0.7)',
  34. borderWidth: 0,
  35. borderColor: 'rgba(0,0,0,0.7)',
  36. formatter: (name, val) => {
  37. const tipHtml = `
  38. <div class="m-info" style=" opacity: 0.95;font-size: 12px; color: white;" >
  39. <div class="title" ></div>
  40. <div class="title" >完成占比${name[0].value}</div>
  41. </div>`
  42. return tipHtml
  43. },
  44. },
  45. yAxis: {
  46. type: 'value',
  47. // 设置坐标轴的 文字样式
  48. axisLabel: {
  49. color: '#bbdaff',
  50. margin: 20, // 刻度标签与轴线之间的距离。
  51. },
  52. axisTick: {
  53. // 取消坐标轴刻度线
  54. show: false,
  55. },
  56. // 坐标轴轴线相关设置。
  57. splitLine: {
  58. lineStyle: {
  59. color: '#2d5baf',
  60. },
  61. },
  62. },
  63. xAxis: {
  64. type: 'category',
  65. splitLine: {
  66. show: false,
  67. },
  68. // 坐标轴轴线相关设置。
  69. axisLine: {
  70. lineStyle: {
  71. color: '#2d5baf',
  72. },
  73. },
  74. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  75. axisLabel: {
  76. // 设置坐标轴的 文字样式
  77. color: '#bbdaff',
  78. margin: 20, // 刻度标签与轴线之间的距离。
  79. },
  80. axisTick: {
  81. // 取消坐标轴刻度线
  82. show: false,
  83. },
  84. },
  85. series: [
  86. {
  87. data: dataOptions,
  88. type: 'bar',
  89. barMaxWidth: 18,
  90. markLine: {
  91. silent: true,
  92. },
  93. },
  94. ],
  95. }
  96. let chart: EChartsType
  97. const initChart = () => {
  98. const chart = echarts.init(chartsRef.value)
  99. chart.setOption(options)
  100. return chart
  101. }
  102. onMounted(() => {
  103. chart = initChart()
  104. window.addEventListener('resize', function () {
  105. chart && chart.resize()
  106. })
  107. })
  108. </script>
  109. <style lang="scss" scoped>
  110. .echarts {
  111. height: 100%;
  112. width: 100%;
  113. }
  114. </style>