Echarts官网

Vue3.2+TS 示例

  1. <div id="container2" style="height: 500px; width: 90%"></div>
const storageSize = ref<number>(0);
const storageRecords = ref<number>(0);

const qianzhi = reactive<any>({
  Type: Array,
  list: [],
})

const lineEcharts = () => {
  request.get("xxx/xxx/xxx").then(res => {
      if (res.data.code === 0) {
        res = res.data.data

        qianzhi.list = res
        storageSize.value = res[(res as any).length - 1]['storageCapacity']
        storageRecords.value = res[(res as any).length - 1]['counter']

        let container: HTMLElement = document.getElementById("container1") as HTMLElement;
        let myChart = echarts.init(container);
        let app = {};

        let option;
        option = {
          tooltip: {
            trigger: 'axis',
            formatter: '{b}<br />累计:{c}万条'
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: qianzhi.list.map(d => (dayjs(d.createTime).format('YYYY-MM-DD'))),
          },

          yAxis: {
            type: 'value'
          },
          dataZoom: [{}],
          series: [
            {
              data: qianzhi.list.map(d => (d.counter / 10000).toFixed(2)),
              type: 'line',

              smooth: true,
              lineStyle: {
                color: "rgb(91,170,158)"
              },
              areaStyle: {
                color: "rgba(110,198,213,0.81)"
              },
              showSymbol: false,
            },

          ]
        };

        if (option && typeof option === 'object') {
          myChart.setOption(option);
        }
      } else {
        reject(new Error(res.data.msg));
      }
    }).catch(e => {
      reject(e);
    }
  )
};

onMounted(() => {
  lineEcharts();
});

JavaScript 按需引入

// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core';
// 引入柱状图图表,图表后缀都为 Chart
import { BarChart } from 'echarts/charts';
// 引入提示框,标题,直角坐标系,数据集,内置数据转换器组件,组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent
} from 'echarts/components';
// 标签自动布局,全局过渡动画等特性
import { LabelLayout, UniversalTransition } from 'echarts/features';
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers';

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer
]);

// 接下来的使用就跟之前一样,初始化图表,设置配置项
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
  // ...
});

TypeScript 按需引入

import * as echarts from 'echarts/core';
import {
  BarChart,
  // 系列类型的定义后缀都为 SeriesOption
  BarSeriesOption,
  LineChart,
  LineSeriesOption
} from 'echarts/charts';
import {
  TitleComponent,
  // 组件类型的定义后缀都为 ComponentOption
  TitleComponentOption,
  TooltipComponent,
  TooltipComponentOption,
  GridComponent,
  GridComponentOption,
  // 数据集组件
  DatasetComponent,
  DatasetComponentOption,
  // 内置数据转换器组件 (filter, sort)
  TransformComponent
} from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

// 通过 ComposeOption 来组合出一个只有必须组件和图表的 Option 类型
type ECOption = echarts.ComposeOption<
  | BarSeriesOption
  | LineSeriesOption
  | TitleComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | DatasetComponentOption
>;

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer
]);

const option: ECOption = {
  // ...
};