vue3+vite 使用echarts

2 阅读1分钟

安装Echarts

npm install echats --save

在需要的页面引入echarts

import * as echarts from "echarts";

Script

const echartRef = ref()
onMounted(()=>{
  let myChart = echarts.init(echartRef.value);
  let option = {
    title: {
      text: 'ECharts 示例'
    },
    tooltip: {},
    legend: {
      data: ['销量']
    },
    xAxis: {
      data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    },
    yAxis: {},
    series: [{
      name: '销量',
      type: 'bar',
      data: [5, 20, 36, 10, 10, 20],
	  label: {//在柱子上显示数字,设置颜色和其他
        show: true,   // 显示数字
        position: 'top',  // 设置数字显示在柱子顶部
        formatter: '{c}',  // 使用默认格式,直接显示数值
        color: '#000'  // 设置数字颜色
      }
    }]
  };
  myChart.setOption(option);
})

HTML

<div ref="echartRef" style="width: 200px; height: 200px;"></div>