1. <template>
    2. <div v-chartResize style="width: 100%; height:600px;" ref="main"></div>
    3. </template>
    4. <script setup>
    5. import { ref, shallowRef, onMounted, onUnmounted, nextTick } from 'vue' // vue 内置方法
    6. import * as echarts from 'echarts/core' // echarts 图表
    7. // 定义响应式数据
    8. const main = ref(null)
    9. const state = shallowRef({
    10. myChart: null, // echarts 图表
    11. })
    12. // 组件挂载后,此方法执行后,页面显示
    13. onMounted(() => {
    14. nextTick(() => {
    15. initChart()
    16. })
    17. })
    18. // 初始化 echarts
    19. const initChart = () => {
    20. const colors = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']
    21. state.myChart = echarts.init(main.value)
    22. state.myChart.setOption({
    23. title: { // 标题组件,包含主标题和副标题
    24. text: '柱状图', // 主标题文本
    25. textStyle: { // 主标题文字参数设置
    26. width: 500, // 文本显示宽度
    27. color: '#0095e1', // 主标题文字的颜色
    28. fontSize: 18, // 主标题文字的字体大小
    29. },
    30. subtext: '生活费用', // 副标题文本
    31. subtextStyle: { // 副标题文字参数设置
    32. color: '#000', // 副标题文字的颜色
    33. },
    34. itemGap: 10, // 主副标题之间的间距
    35. x: 'left', // 主副标题x轴对齐方式
    36. y: 'top', // 主副标题y轴对齐方式
    37. },
    38. legend: {}, // 图例组件展现了不同系列的标记
    39. tooltip: { // 鼠标滑入提示框
    40. trigger: 'axis', // 触发类型
    41. axisPointer: { // 坐标轴指示器配置项
    42. type: 'shadow' // 指示器类型
    43. }
    44. },
    45. grid: { // 直角坐标系内绘图网格,类似padding
    46. top: '15%',
    47. left: '3%',
    48. right: '4%',
    49. bottom: '3%',
    50. containLabel: true, // grid 区域是否包含坐标轴的刻度标签
    51. },
    52. xAxis: { // 直角坐标系 grid 中的 x 轴
    53. type: 'category',
    54. data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    55. },
    56. yAxis: [ // 直角坐标系 grid 中的 y 轴
    57. {
    58. type: 'value', // 坐标轴类型
    59. name: '人民币', // 坐标轴名称
    60. position: 'left', // y 轴的位置
    61. alignTicks: true, // 在多个 y 轴为数值轴的时候,可以开启该配置项自动对齐刻度
    62. axisLine: { // 坐标轴轴线相关设置
    63. show: true,
    64. lineStyle: {
    65. color: colors[0]
    66. }
    67. },
    68. axisLabel: { // 坐标轴刻度标签的相关设置
    69. formatter: '{value} ¥'
    70. }
    71. },
    72. {
    73. type: 'value',
    74. name: '美元',
    75. position: 'right',
    76. alignTicks: true,
    77. axisLine: {
    78. show: true,
    79. lineStyle: {
    80. color: colors[0]
    81. }
    82. },
    83. axisLabel: {
    84. formatter: '{value} $'
    85. }
    86. },
    87. ],
    88. series: [
    89. {
    90. type: 'bar', // echarts 图表类型
    91. name: '美杜莎', // 系列名称,用于tooltip的显示
    92. color: '#0095e1', // 调色盘颜色列表
    93. data: [120, { // 定义单个柱子颜色
    94. value: 200,
    95. itemStyle: {
    96. color: '#F56C6C'
    97. }
    98. }, 150, 80, 70, 110, 130], // 系列中的数据内容数组。数组项通常为具体的数据项
    99. showBackground: true, // 是否显示柱条的背景色
    100. backgroundStyle: { // 柱条的背景颜色
    101. color: 'rgba(180, 180, 180, 0.2)'
    102. },
    103. },
    104. {
    105. type: 'bar', // echarts 图表类型
    106. name: '焰灵姬', // 系列名称,用于tooltip的显示
    107. data: [20, { // 定义单个柱子颜色
    108. value: 300,
    109. itemStyle: {
    110. color: '#F56C6C'
    111. }
    112. }, 40, 60, 170, 210, 230], // 系列中的数据内容数组。数组项通常为具体的数据项
    113. showBackground: true, // 是否显示柱条的背景色
    114. backgroundStyle: { // 柱条的背景颜色
    115. color: 'rgba(180, 180, 180, 0.2)'
    116. }
    117. },
    118. {
    119. type: 'line', // echarts 图表类型
    120. name: '云韵', // 系列名称,用于tooltip的显示
    121. data: [20, 100, 10, 180, 120, 200, 300], // 系列中的数据内容数组。数组项通常为具体的数据项
    122. showBackground: true, // 是否显示柱条的背景色
    123. backgroundStyle: { // 柱条的背景颜色
    124. color: 'rgba(180, 180, 180, 0.2)'
    125. },
    126. yAxisIndex: 1, // 对应 yAxis 下标为 1 的轴
    127. }
    128. ]
    129. })
    130. }
    131. // 组件销毁完成后执行的函数
    132. onUnmounted(() => {
    133. if (!state.myChart) {
    134. return
    135. }
    136. state.myChart.dispose()
    137. state.myChart = null
    138. })
    139. </script>
    140. <style lang='scss' scoped>
    141. </style>