vue2+Echart 实现柱状图和折线图组合样式

示例图:

实现代码:

BarLineChart.vue

javascript 复制代码
<!-- 库存周转率 -->
<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script >
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {graphic} from "echarts/lib/export";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '98%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    Data: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      percentage:0,
    }
  },
  created() {
    this.getPercentage();
  },
  mounted() {

    this.$nextTick(() => {
      this.initChart();
    })
  },
  beforeDestroy() {
    if (!this.chart) {

      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getPercentage(){
      this.percentage=this.Data.outBound.map((outBound,index)=>{
          const inventory=this.Data.inventory[index];
          return(outBound/inventory*100).toFixed(2);
    })},
    initChart(){
      console.log("新添加数据", this.Data)

      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.Data)
    },
    setOptions(){
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,.6)",
          borderColor: "rgba(147, 235, 248, .8)",
          textStyle: {
            color: "#FFF",
          },
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],

        },
        legend: {
          data: ["出库量", "库存量","周转率"],
          textStyle: {
            color: "#B4B4B4",
          },
          // top: "0",
          bottom:"0",
        },
        grid: {
          left: "50px",
          right: "40px",
          bottom: "50px",
          top: "10px",
        },
        xAxis: {
          data: this.Data.dateData.map(n=>n.substr(-5)),
          axisLine: {
            lineStyle: {
              color: "#B4B4B4",
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: [
          {
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
                color: "#B4B4B4",
              },
            },

            axisLabel: {
              formatter: "{value}",
            },
          },
          {
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
                color: "#B4B4B4",
              },
            },
            axisLabel: {
              formatter: "{value}% ",
            },
          },
        ],
        series: [
          {
            name: "出库量",
            type: "bar",
            barWidth: 10,
            itemStyle: {
              borderRadius: 5,

              color: new graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "#956FD4" },
                { offset: 1, color: "#3EACE5" }
              ]),
            },
            data: this.Data.outBound,
          },
          {
            name: "库存量",
            type: "bar",
            barGap: "-100%",
            barWidth: 10,
            itemStyle: {
              borderRadius: 5,
              color: new graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(156,107,211,0.8)" },
                { offset: 0.2, color: "rgba(156,107,211,0.5)" },
                { offset: 1, color: "rgba(156,107,211,0.2)" },
              ]),
            },
            z: -12,
            data: this.Data.inventory,

          },
          {
            name: "周转率",
            type: "line",
            smooth: true,
            showAllSymbol: true,
            symbol: "emptyCircle",
            symbolSize: 8,
            yAxisIndex: 1,
            itemStyle: {
              color: "#F02FC2",
            },
            data: this.percentage,
          },
        ],
      })
    }

  },
}



</script>


<style scoped lang="scss"></style>

数据参考:

父页面:index.vue调用BarLineChart.vue

html 复制代码
<el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-line-chart :data="lineChartData" v-if="!isLoading1"/>
        </div>
      </el-col>

注意引用

复制代码
import BarLineChart from "./dashboard/BarLineChart"
相关推荐
程序员阿鹏15 分钟前
Git的安装和配置(idea中配置Git)
java·开发语言·ide·git·intellij-idea·idea
m0_5139625316 分钟前
vue-ganttastic甘特图label标签横向滚动固定方法
javascript·vue.js·甘特图
景天科技苑26 分钟前
【Rust trait特质】如何在Rust中使用trait特质,全面解析与应用实战
开发语言·后端·rust·trait·rust trait·rust特质
PacosonSWJTU30 分钟前
python使用matplotlib画图
开发语言·python·matplotlib
Inverse16236 分钟前
C语言_自定义类型:结构体
c语言·开发语言·算法
enyp801 小时前
Qt原型模式实现与应用
开发语言·qt·原型模式
码农黛兮_461 小时前
HTML、CSS 和 JavaScript 基础知识点
javascript·css·html
CryptoRzz1 小时前
印度尼西亚数据源对接技术指南
开发语言·python·websocket·金融·区块链
zyx没烦恼1 小时前
unordered_map和unordered的介绍和使用
开发语言·c++
呵呵哒( ̄▽ ̄)"2 小时前
React - 编写选择礼物组件
前端·javascript·react.js