VUE echarts 柱状图、折线图 双Y轴 显示

在这里插入图片描述
weekData: [“1周”,“2周”,“3周”,“4周”,“5周”,“6周”,“7周”,“8周”,“9周”,“10周”], //柱状图横轴
jdslData: [150, 220, 430, 360, 450, 680, 100, 450, 680, 200], // 折线图的数据
cyslData: [100, 200, 400, 300, 500, 500, 500, 450, 480, 400], // 柱状图1的数据
plgtData: [200, 200, 430, 360, 500, 500, 500, 450, 580, 500], // 柱状图2的数据
jdgtData: [300, 200, 100, 400, 100, 200, 100, 350, 380, 300], // 柱状图3的数据
cygtData: [150, 220, 430, 360, 450, 680, 100, 450, 680, 200], // 折线图的数据

  //千分位格式
  stateFormat(num,n=2,symbol) {
       // 保证为number类型
       num = Number(num)
       if(typeof num!=='number')throw new TypeError('num参数应该是一个number类型');
       if(n<0)throw new Error('参数n不应该小于0');
       var hasDot=parseInt(num)!=num;//这里检测num是否为小数,true表示小数
       var m=(n!=undefined&&n!=null)?n:1;
       num=m==0?num.toFixed(m)+'.':hasDot?(n?num.toFixed(n):num):num.toFixed(m);
       symbol=symbol||',';
       num=num.toString().replace(/(\d)(?=(\d{3})+\.)/g,function(match, p1,p2) {
           return p1 + symbol;
       });
       if(n==0||(!hasDot&&!n)){//如果n为0或者传入的num是整数并且没有指定整数的保留位数,则去掉前面操作中的小数位
           num=num.substring(0,num.indexOf('.'));
       }
       return num;
   },
            
  // xAxisData:x轴的值, lineData1:已接单数量, lineData2:差异数量, barData1:工厂产能工天, barData2:已接单工天 ,lineData3 差异工天
  drawLine(xAxisData, lineData1, lineData2, barData1, barData2 , barData3)
  {
      var that = this;
      let eChart = echarts.init(document.getElementById("echart")); // 基于准备好的dom,初始化echarts实例
      that.eChart = eChart;
      eChart.setOption({
          // 绘制图表
          title: {text: ""},
          //悬浮标
          tooltip: {
              trigger: 'axis',
              axisPointer: {
                  type: 'shadow'
              },
              // formatter: '{a}: {c}', //官方
              formatter(params) {  //自定义
                  var relVal = params[0].name;
                  for (var i = 0, l = params.length; i < l; i++) {
                      //relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + Number(params[i].value).toFixed(0)
                      relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + that.stateFormat(params[i].value,0)
                  }
                  return relVal;
              },

          },
          grid: {
              left: '3%',
              right: '3%',
              bottom: '3%',
              containLabel: true,
          },
          legend: {//图例名
              show: true,
              data: ['已接单工天','差异工天', '已接单数量', '差异数量'],
              x: 'center',        //图例在中间center 左边left 右边right
              textStyle: {        //图例字体的颜色
                  color: "#000" ,  //图例文字
              },
              selected:{
                '已接单数量':false,
                '差异数量' :false
            }
          },

          xAxis: [
              // x轴 10周
              {
                  type: "category",
                  axisTick: {
                      show: false, // 坐标轴刻度
                  },
                  axisLine: {
                      show: true, // 坐标轴轴线
                      lineStyle: {
                          //color: "#eeeeee",
                          color: '#cccccc'
                      }
                  },
                  axisLabel: {
                      // 坐标轴刻度标签的相关设置
                      inside: false,
                      textStyle: {
                          color: "#999",
                          fontWeight: "normal",
                          fontSize: "12",
                      },
                  },
                  splitLine: {show: false}, // 去除网格线
                  axisPointer: {
                      type: 'shadow',
                  },
                  data: xAxisData,
              },
              {
                  type: "category",
                  axisLine: {show: false}, // 是否显示坐标轴轴线
                  axisTick: {show: false}, // 是否显示坐标轴刻度
                  axisLabel: {show: false}, // 是否显示刻度标签 柱状图上的标签
                  splitArea: {show: false}, // 是否显示分隔区域 背景遮罩
                  splitLine: {show: false}, // 是否显示分隔线
              },
          ],
          yAxis: [
              // y轴
              {
                  type: "value",
                  max: value => {
                      const { max, min } = value;
                      const absMax = Math.max(Math.abs(max), Math.abs(min));
                      return Math.floor(absMax * 1.2);
                  } ,
                  min: value => {
                      const { max, min } = value;
                      const absMax = Math.max(Math.abs(max), Math.abs(min));
                      return Math.floor(-absMax * 1.2);
                  },
                  axisTick: {
                      show: false,
                  },
                  axisLine: {
                      show: true,
                      lineStyle: {
                          //color: "#eeeeee",
                          color: '#cccccc'
                      },
                  },
                  axisLabel: {
                      textStyle: {
                          color: "#bac0c0",
                          fontWeight: "normal",
                          fontSize: "12",
                      },
                      formatter: "{value}",
                  },
                  splitLine: {
                      show: true, // 去除网格线
                      lineStyle: {
                          color: '#f8f8f8',
                      }
                  },
              },{
                  type: "value",
                  max: value => {
                      const { max, min } = value;
                      const absMax = Math.max(Math.abs(max), Math.abs(min));
                      return Math.floor(absMax * 1.2);
                  } ,
                  min: value => {
                      const { max, min } = value;
                      const absMax = Math.max(Math.abs(max), Math.abs(min));
                      return Math.floor(-absMax * 1.2);
                  },
                  axisLabel: {
                      textStyle: {
                          color: "#bac0c0",
                          fontWeight: "normal",
                          fontSize: "12",
                      },
                      formatter: "{value}",
                  },
                  splitLine: {
                      show: true, // 去除网格线
                      lineStyle: {
                          color: '#f8f8f8',
                      }
                  },
              }

          ],
          series: [
              // { // 柱状图1 工厂产能工天
              //     type: "bar",
              //     name: '工厂产能工天',
              //     itemStyle: {
              //         show: true,
              //         //color: "#7ca6f8",  // 柱状图的颜色
              //         color: "#5470C6",  // 柱状图的颜色
              //         borderWidth: 0,
              //         borderType: "solid",
              //         emphasis: {
              //             shadowBlur: 15,
              //             shadowColor: "rgba(105,123, 214, 0.7)",
              //         },
              //     },
              //     zlevel: 1,
              //     barWidth: 40,
              //     data: barData1,
              // },
              { // 柱状图2  已接单工天
                  type: "bar",
                  name: '已接单工天',
                  yAxisIndex: 1 ,
                  itemStyle: {
                      show: true,
                      // color: "#6ebbb8",
                      color: "#91CC75",
                      borderWidth: 0,
                      borderType: "solid",
                      emphasis: {
                          shadowBlur: 15,
                          shadowColor: "rgba(105,123, 214, 0.7)",
                      },
                  },
                  label: {
                      show: true,
                      position: 'insideRight',
                      // formatter: '{c}'
                      // formatter(params) {  //自定义
                      //     relVal = that.stateFormat(params.value,0);
                      //     return relVal;
                      // },
                  },
                  zlevel: 2,
                  barWidth: 40,
                  data: barData2,
              },
              { // 柱状图1 工厂差异工天
                  type: "bar",
                  name: '差异工天',
                  yAxisIndex: 1 ,
                  itemStyle: {
                      show: true,
                      //color: "#7ca6f8",  // 柱状图的颜色
                      color: "#5470C6",  // 柱状图的颜色
                      borderWidth: 0,
                      borderType: "solid",
                      emphasis: {
                          shadowBlur: 15,
                          shadowColor: "rgba(105,123, 214, 0.7)",
                      },
                  },
                  label: {
                      show: true,
                      position: 'insideRight',
                      // formatter: '{c}'
                      // formatter(params) {  //自定义
                      //     relVal = that.stateFormat(params.value,0);
                      //     return relVal;
                      // },
                  },
                  zlevel: 1,
                  barWidth: 40,
                  data: barData3,
              },
              { // 折线1  已接单数量
                  zlevel: 3,
                  type: "line",
                  name: '已接单数量',
                  color: ["#8d83f7"],  // 拐点颜色
                  symbolSize: 8,   // 拐点的大小
                  symbol: "circle",  // 拐点样式
                  label: {
                      show: true,
                      position: 'top',
                      // formatter: '{c}'
                      // formatter(params) {  //自定义
                      //     relVal = that.stateFormat(params.value,0);
                      //     return relVal;
                      // },
                  },
                  data: lineData1,
                  itemStyle: {
                      normal: {
                          lineStyle: {
                              color: "#8d83f7", // 折线的颜色
                              type: "solid",    // 折线的类型
                          }
                      }
                  },
                  // tooltip: {
                  //     formatter: '{b}<br/>已接单数量:{c}<br/>',
                  // }
              },
              { // 折线2  差异数量
                  zlevel: 4,
                  type: "line",
                  name: '差异数量',
                  color: ["#ef836f"],  // 拐点颜色
                  symbolSize: 8,       // 拐点的大小
                  symbol: "circle",    // 拐点样式
                  label: {
                      show: true,
                      position: 'top',
                      // formatter: '{c}'
                      // formatter(params) {  //自定义
                      //     relVal = that.stateFormat(params.value,0);
                      //     return relVal;
                      // },
                  },
                  data: lineData2,
                  itemStyle: {
                      normal: {
                          lineStyle: {
                              color: "#ef836f", // 折线的颜色
                              type: "solid" ,   // 折线的类型
                          }
                      }
                  },
                  // tooltip: {
                  //     formatter: '{b}<br/>差异数量:{c}<br/>',
                  // }
              }
          ],
      });
  },
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts 是一个基于 JavaScript 的数据可视化库,可以帮助开发人员快速构建各种图表。在 Echarts 中,要实现柱状图加折线的效果,可以使用组合图(Mixed Chart)功能。 在 Vue 开发中使用 Echarts,可以按照以下步骤进行操作: 1. 首先,你需要在项目中引入 Echarts 库。可以通过 npm 安装 echarts,然后在项目中引入 echarts。 2. 在 Vue 组件中,按照 Echarts 的文档,设置好需要展示的数据和配置项。 3. 创建一个 div 元素作为图表的容器,通过 ref 属性获取该元素的引用。 4. 在 Vue 的 mounted 生命周期函数中,使用引入的 Echarts 库初始化图表,并将容器和配置项传递给 Echarts 实例。 5. 最后,将图表实例挂载到 Vue 组件中,即可在页面上显示柱状图加折线的效果。 下面是一个示例代码,展示了如何在 Vue 中实现柱状图加折线的效果: ```javascript <template> <div ref="chart" style="width: 100%; height: 400px;"></div> </template> <script> import echarts from 'echarts'; export default { mounted() { const chart = echarts.init(this.$refs.chart); const option = { xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], }, yAxis: [ { type: 'value', name: '柱状图', }, { type: 'value', name: '折线图', }, ], series: [ { name: '柱状图', type: 'bar', data: [120, 200, 150, 80, 70, 110, 130], }, { name: '折线图', type: 'line', yAxisIndex: 1, data: [20, 40, 50, 30, 15, 25, 35], }, ], }; chart.setOption(option); }, }; </script> ``` 在上述代码中,通过设置 xAxis、yAxis 和 series 来配置柱状图折线图的相关参数。其中,xAxis 配置 x 轴数据,yAxis 配置 y 轴数据,series 配置图表的系列数据。将配置项传递给 Echarts 实例后,使用 setOption 方法来渲染图表。最后,将图表实例挂载到 Vue 组件中的 div 容器中,即可在页面上显示柱状图加折线的效果。 希望以上信息能够对你有所帮助,如果有任何疑问,请随时提问。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [Echarts 柱状图+折线图合并---实现效果详解(vue+Echarts实现)](https://blog.csdn.net/wuzhiyue2/article/details/118530433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值