echarts移动端的resize适配

function fontSize(res) {
  const clientWidth =
    window.innerWidth ||
    document.documentElement.clientWidth ||
    document.body.clientWidth;
  if (!clientWidth) return;
  let fontSize = clientWidth / 750;
  return res * fontSize;
}

var chartDom = document.getElementById("lineCharts");
var myChart = echarts.init(chartDom);
var option;
// 初始化options
function initOptions() {
  option = {
    grid: {
      top: fontSize(20),
      bottom: fontSize(40),
      right: fontSize(5),
    },
    xAxis: {
      type: "category",
      axisLabel: {
        color: "#666666",
        fontSize: fontSize(22),
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        lineStyle: {
          color: "#B2B2B2",
        },
      },
      data: ["三", "四", "五", "六", "日", "一", "二"],
    },
    yAxis: {
      type: "value",
      axisLabel: {
        color: "#B2B2B2",
        fontSize: fontSize(24),
      },
      splitLine: {
        lineStyle: {
          color: "#E5E5E5",
        },
      },
    },
    series: [
      {
        data: [110, 135, 105, 156, 148, 200, 120],
        type: "line",
        showSymbol: false,
        lineStyle: {
          width: fontSize(3),
          color: "#00A2FF",
        },
        areaStyle: {
          normal: {
            color: {
              type: "linear",
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: "#00A2FF33",
                },
                {
                  offset: 1,
                  color: "#00A2FF00",
                },
              ],
            },
          },
        },
      },
    ],
  };
}
initOptions();

option && myChart.setOption(option);

window.addEventListener("resize", function () {
  initOptions();
  option && myChart.setOption(option);
  myChart.resize();
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值