Newer
Older
smartKitchenFront / src / components / echart / barChart / verticalBarChart.vue
<template>
  <Echart ref="chart" :options="option" height="100%" width="100%" />
</template>
  <script>

import Echart from "./../index";
export default {
  name: "verticalBarChart",
  components: { Echart },
  props: {
    unit: {
      // 单位
      type: String,
      default: "",
    },
    colors: {
      // 颜色
      type: Array,
      default: () => {
        return ["#2D8CF0", "#0337ff"];
      },
    },
    axisLineColor: {
      // 轴线颜色
      type: String,
      default: "#101d5d",
    },
    fontColor: {
      // 轴线上文字颜色
      type: String,
      default: "#000fff",
    },
    labelColor: {
      // 图形上文本标签颜色
      type: String,
      default: "#47c8b2",
    },
    seriesData: {
      // 数据
      type: Array,
      default: () => {
        return [];
      },
    },
    yAxisData: {
      // X轴数据
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      curWidth: this.width,
      curHeight: this.height,
      option: {
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 10,
          containLabel: true,
        },
        tooltip: {
          trigger: "", //悬浮提示框不显示
        },
        xAxis: [
          {
            type: "value",
            show: false,
            boundaryGap: false,
            data: [],
            boundaryGap: [0, 0],
            position: "top",
            axisLine: {
              lineStyle: {
                color: '#000', // 轴线的颜色
              },
            },
            axisLabel: {
              color: '#000', // X轴名称颜色
              fontSize: 14,
            },
          },
        ],
        yAxis: [
          {
            name: this.unit,
            type: "category",
            inverse: true, // 开启方向坐标轴
            data: [], //y轴显示内容
            show: true,
            color: "#000",
            nameTextStyle: {
              color: "#000",
              fontSize: 14,
              verticalAlign: "middle",
            },
            max: function (value) {
              return value.max;
            },
            minIntermal: 1,
            axisLine: {
              show: false,
            },
            axisLabel: {
              color: "#000",
              fontSize: 12,
              formatter: function (value, index) {
                // 用于修改y轴显示内容
                return "{a|NO.}" + `{a|${index + 1}}` + value.split('-')[0];
              },
              // aligin:'right',
              inside:'true',
              verticalAlign :'bottom',
              margin:0,
              lineHeight :20,
              rich: {
                a: {
                  color: "blue",
                },
              },
            },
          },
        ],
        series: [],
      },
    };
  },
  watch: {
    yAxisData(newVal) {
      this.option.yAxis[0].data = newVal;
      //   this.refreshEchart();
    },
    seriesData: {
      handler(newVal) {
        const newSeries = [];
        for (const item of newVal) {
          const series = {
            name: item.name,
            type: "bar",
            barWidth: item.width,
            // showBackground: true,
            barCategoryGap: "100%",
            label: {
              show: item.width < 2, //显示文本
              textStyle: {
                color: "#000",
                fontSize: "14",
              },
              offset:[-20,-14],
              formatter :(params)=> params.name.split('-')[1],
              position:'right',
            },
            itemStyle:{
                color:this.colors[0]
              },
            data: item.data,
            
          };
          newSeries.push(series);
        }
        this.$set(this.option, "series", newSeries);
        // this.refreshChart();
      },
      deep: true,
    },
  },
};
</script>
  
  <style scoped>
</style>