echat 上下两个折线图 共用一个y轴,滚动条

 const xAxis = (data) => {
    return [
      {
        type: 'category',
        interval: 0, // 必须显示x轴文字
        axisTick: {
          show: true,
        },
        axisLine: {
          show: true, // 坐标轴横线
          lineStyle: {
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        data,
      },
      {
        gridIndex: 1,
        type: 'category',
        interval: 0, // 必须显示x轴文字
        axisTick: {
          show: true,
        },
        axisLine: {
          show: true, // 坐标轴横线
          lineStyle: {
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        data,
      },
    ];
  };

  const grid = () => {
    return [
      {
        left: 40,
        bottom: '60%',
        top: 28,
        right: 40,
        containLabel: true,
      },
      {
        left: 40,
        top: '52%',
        right: 40,
        containLabel: true,
        bottom: 50,
      },
    ];
  };

  const yAxis = (name1, name2, name3, name4, tip) => {
    return [
      {
        type: 'value',
        name: name1,
        scale: false,
        alignTicks: true, // 双y轴变为一条线
        min: 0,
        max: (value) => {
          return value.max ? null : 1;
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        axisLabel: {
          formatter(value) {
            return `${value.toFixed(2)}`;
          },
        },
      },
      {
        type: 'value',
        name: name2,
        scale: false,
        alignTicks: true,
        min: 0,
        max: (value) => {
          return value.max ? null : 1;
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        axisLabel: {
          formatter(value) {
            return `${value.toFixed(2)}`;
          },
        },
      },
      {
        type: 'value',
        name: name3,
        gridIndex: 1,
        scale: false,
        alignTicks: true,
        min: 0,
        max: (value) => {
          return value.max ? null : 1;
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        axisLabel: {
          formatter(value) {
            return `${value.toFixed(2)}`;
          },
        },
      },
      {
        type: 'value',
        name: name4,
        gridIndex: 1,
        scale: false,
        alignTicks: true,
        min: 0,
        max: (value) => {
          return value.max ? null : 1;
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: lineColor,
          },
        },
        textStyle: {
          color: textColor,
        },
        axisLabel: {
          formatter(value) {
            return `${value.toFixed(2)}`;
          },
        },
      },
    ];
  };

  const series = (name1, name2, name3, name4, data1, data2, data3, data4) => {
    return [
      {
        name: name1,
        type: 'bar',
        xAxisIndex: 0,
        yAxisIndex: 0,
        emphasis: {
          disabled: true,
          focus: 'none',
        },
        data: data1,
      },
      {
        name: name2,
        type: 'line',
        xAxisIndex: 0,
        yAxisIndex: 1,
        smooth: true,
        symbol: 'none',
        emphasis: {
          disabled: true,
          focus: 'none',
        },
        data: data2,
      },
      {
        name: name3,
        type: 'bar',
        xAxisIndex: 1,
        yAxisIndex: 2,
        emphasis: {
          disabled: true,
          focus: 'none',
        },
        data: data3,
      },
      {
        name: name4,
        type: 'line',
        xAxisIndex: 1,
        yAxisIndex: 3,
        smooth: true,
        symbol: 'none',
        emphasis: {
          disabled: true,
          focus: 'none',
        },
        data: data4,
      },
    ];
  };

  const tooltip = () => {
    return {
      trigger: 'axis',
      textStyle: {
        color: '#fff',
      },
      borderWidth: 1,
      backgroundColor:
        theme === 'dark' ? 'rgba(0, 0, 0,0.9)' : 'rgba(0,0,0,0.65)',
      borderColor:
        theme === 'dark' ? 'rgba(255,255,255,0.3)' : 'rgba(0,0,0,0.16)',
      formatter(params) {
        let str = '';
        str += `<div>${params[0].name}</div>`;
        for (let i = 0; i < params.length; i += 1) {
          str +=
            `<span style="display:inline-block;margin-right:5px;width:10px;height:10px;background-color:${
              params[i].color
            };"></span>
            <span style="display: inline-block;width:80px">${
              params[i].seriesName
            }${params[i].seriesType === 'bar' ? '(亿元)' : '(%)'}</span>` +
            `<span style="text-align: right;display: inline-block;width:100px"">${
              params[i]?.data ? `${Number(params[i]?.data).toFixed(2)}` : '0.00'
            }${params[i].seriesType === 'line' ? '%' : ''}</span></br>`;
        }
        return str;
      },
    };
  };

  const option = (tip) => {
    return {
      animation: false,
      animationDuration: 0, // 这里两个动画设置可以让图表更顺滑
      animationEasing: 'cubicInOut', // 这里两个动画设置可以让图表更顺滑
      color: ['#6998f6', '#efe8c8', '#8695f0', '#8cb6f7'],
      legend: {
        x: 'center',
        y: 'buttom',
        icon: 'react',
        top: '94%',
        textStyle: {
          color: textColor,
        },
        itemWidth: 8,
        itemHeight: 8,
      },
      grid: grid(),
      tooltip: tooltip(),
      // 将上下两个tootip合成一个
      axisPointer: {
        link: { xAxisIndex: 'all' },
      },
      xAxis: xAxis(tip ? xAxisExchange : xAxisBank),
      yAxis: yAxis(
        '大白',
        '小黑',
        '小红花',
        '小绿',
        'flag'
      ),
      dataZoom: [
        {
          type: 'slider',
          xAxisIndex: [0, 1], // 显示 0 1 的数据,这个要加,不加的话,悬浮提示就会出问题
          show: true,
          height: 16,
          maxSpan,
            // 刚进入页面 滚动条的位置
         // startValue: moment(watchSelectRow?.firstBuyDay).format('YYYY-MM-DD'),
        // endValue: moment(watchSelectRow?.finalSellDay).format('YYYY-MM-DD'),
        },
      ],
      series: series(
        '大白',
        '小黑',
        '小红花',
        '小绿',
        data1,
        data2,
        data3,
        data4
      ),
    };
  };

使用 echarts 编写一个前端可视化大屏,你需要先引入 echarts 库,并创建一个包含图表数据和配置的 JavaScript 对象。然后,根据需求选择合适的图表类型和样式进行配置。最后,将图表渲染到指定的 HTML 元素中。 以下是一个简单的示例代码,演示如何使用 echarts 编写一个前端可视化大屏: ```html <!DOCTYPE html> <html> <head> <title>可视化大屏示例</title> <!-- 引入 echarts 库 --> <script src="https://cdn.staticfile.org/echarts/5.2.1/echarts.min.js"></script> </head> <body> <!-- 指定一个容器用于显示可视化大屏 --> <div id="dashboard" style="width: 100%; height: 600px;"></div> <script> // 创建一个包含图表数据和配置的 JavaScript 对象 var data = { title: { text: '可视化大屏示例', subtext: '这是一个柱状图和饼图的组合示例', x: 'center' }, tooltip: { trigger: 'axis' }, legend: { data: ['销售额', '销售占比'], x: 'left' }, xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, yAxis: { type: 'value' }, series: [ { name: '销售额', type: 'bar', data: [120, 200, 150, 80, 70] }, { name: '销售占比', type: 'pie', radius: '55%', center: ['50%', '60%'], data: [ {value: 335, name: 'A'}, {value: 310, name: 'B'}, {value: 234, name: 'C'}, {value: 135, name: 'D'}, {value: 1548, name: 'E'} ] } ] }; // 使用 echarts 配置图表样式和属性 var dashboard = echarts.init(document.getElementById('dashboard')); dashboard.setOption(data); </script> </body> </html> ``` 你可以根据自己的需求修改数据和样式,然后将上述代码保存为 HTML 文件并在浏览器中打开,就可以看到生成的前端可视化大屏了。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值