echarts按需引入

3 篇文章 0 订阅

这篇文章只对echarts v4.x 有效
官方更新到v5.x 后 可以查看官方实例 查看完整代码->按需引入

echarts 的完整包太大了,但是在项目中,其实很多都是用不上的,所以按需引入是很有必要的

import echarts from "echarts/lib/echarts";

import "echarts/lib/chart/bar";
import "echarts/lib/chart/line";
import "echarts/lib/chart/pie";
import "echarts/lib/component/legend";
import "echarts/lib/component/title";
import "echarts/lib/component/tooltip";
import "echarts/lib/component/grid";
// import 'echarts/theme/macarons' // echarts theme
import installTheme from "./echarts-theme";
installTheme(echarts);

export default echarts;
  • 这里的主题使用了自定义的主题(基本上是删除了没用上图形的颜色样式),因为我发现使用 echarts 自带的主题会按需引入失败(因为定义了所有的图形样式形状)
  • 主题参考链接
export default function installTheme(echarts) {
  var colorPalette = [
    "#2ec7c9",
    "#b6a2de",
    "#5ab1ef",
    "#ffb980",
    "#d87a80",
    "#8d98b3",
    "#e5cf0d",
    "#97b552",
    "#95706d",
    "#dc69aa",
    "#07a2a4",
    "#9a7fd1",
    "#588dd5",
    "#f5994e",
    "#c05050",
    "#59678c",
    "#c9ab00",
    "#7eb00a",
    "#6f5553",
    "#c14089",
  ];

  const theme = {
    color: colorPalette,

    title: {
      textStyle: {
        fontWeight: "normal",
        color: "#008acd",
      },
    },

    tooltip: {
      backgroundColor: "rgba(50,50,50,0.5)",
      axisPointer: {
        type: "line",
        lineStyle: {
          color: "#008acd",
        },
        crossStyle: {
          color: "#008acd",
        },
        shadowStyle: {
          color: "rgba(200,200,200,0.2)",
        },
      },
    },

    grid: {
      borderColor: "#eee",
    },

    line: {
      smooth: true,
      symbol: "emptyCircle",
      symbolSize: 3,
    },
  };

  echarts.registerTheme("macarons", theme);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值