wanghong
2023-12-21 ec3add985f600fe56a7232484bbe89a11b73c23b
Source/UBCS-WEB/src/components/StatisticsComponent/ColumnarChart.vue
@@ -1,121 +1,169 @@
<template>
  <basic-container>
    <v-chart id="chart" :auto-resize="true" :options="chartOptions"></v-chart>
    <div style="display: inline-block;float: right;margin-bottom: 10px;margin-right: 60px">
      <el-button size="small" type="primary" plain @click="delHandler">删除</el-button>
    </div>
  </basic-container>
</template>
<script>
import 'echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/chart/pie'
import 'echarts/lib/chart/bar'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/title'
import 'echarts/lib/component/legend'
var data = [
  [120, 132, 101, 134, 90, 230, 210, 130, 0, 122, 100, 80],
  [220, 232, 301, 334, 290, 330, 410, 330, 212, 322, 200, 234],
];
// 找到每一项数据中的最大值并保存到数组
var maxValues = data[0].map(function (_, i) {
  return Math.max.apply(null, data.map(function (item) {
    return item[i];
  }));
});
var colors = ['#91CC75', '#5470C6'];
// 生成柱状图的 series 数据
var seriesData = [];
for (var i = 0; i < data.length; i++) {
  var curSeriesData = [];
  // 计算数据中的每组数据的最大值
  for (var j = 0; j < data[i].length; j++) {
    var borderRadius = [0, 0, 0, 0];  // 默认不设置圆角
    if (data[i][j] === maxValues[j]) {  // 如果当前柱子的值等于对应列的最大值,设置圆角
      borderRadius = [50, 50, 0, 0];  // 设置四个角都为圆角
    }
    curSeriesData.push({
      value: data[i][j],
      itemStyle: {
        normal: {
          barBorderRadius: borderRadius,  // 设置圆角
          color: colors[i],
          show: function (params) {
            // 根据数据值的大小来判断是否显示 label
            return params.value > 30;
          }
        }
      }
    });
  }
  var seriesName = '';
  if (i === 0) {
    seriesName = '总量';
  } else if (i === 1) {
    seriesName = '新增';
  }
  seriesData.push({
    name: seriesName,
    type: 'bar',
    stack: '总量',
    data: curSeriesData,
    label: {
      show: true,
      position: 'top'
    },
    itemStyle: {
      color: colors[i]  // 设置柱子的颜色
    },
    emphasis: {
      focus: 'series'
    }
  });
}
import {deleteChartId} from "@/api/statistic/chart"
export default {
  name: "ColumnarChart",
  props:{
    columnarData:{
      type:Array,
      default:[]
    },
    chartName:{
      type:String,
      default: ""
    },
    btmname :{
      type: String
    }
  },
  watch:{
    columnarData:{
      immediate:true,
      handler(newval,oldval){
        if(newval && newval.length > 0){
          const minValues = newval[0].map(function (_, i) {
            return Math.min.apply(
              null,
              newval.map(function (item) {
                return item[i];
              })
            );
          });
          let colors = ['#84C9E5', '#F68686'];
          const seriesData = [];
          for (let i = 0; i < newval.length; i++) {
            let curSeriesData = [];
            for (let j = 0; j < newval[i].length; j++) {
              let borderRadius = [0, 0, 0, 0];
              if (newval[i][j] === minValues[j]) {
                borderRadius = [50, 50, 0, 0];
              }
              curSeriesData.push({
                value: newval[i][j],
                itemStyle: {
                  normal: {
                    // barBorderRadius: borderRadius,  // 设置圆角
                    color: colors[i],
                  }
                },
              });
            }
            let seriesName = (i === 0 ? "总量" : "新增")
            seriesData.push({
              name: seriesName,
              type: "bar",
              stack: "总量",
              barWidth: 60,
              data: curSeriesData,
              label: {
                show: true,
                position: 'top',
                formatter: function (params) {
                  return params.value === 0 ? '' : params.value; // 值为 0 不显示,其他情况显示
                }
              },
              emphasis: {
                focus: "series",
              },
            });
            this.chartOptions.series=seriesData
          }
        }
      }
    },
    chartName:{
      handler(newval,oldval){
        if(newval){
          this.chartOptions.title.text = newval + "数据统计"
        }
      },
      immediate:true,
      deep:true
    }
  },
  data() {
    return {
      chartOptions: {
        color:['#84C9E5', '#F68686'],
        title: {
          text: '人员主数据统计'
          text: "",
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: 'axis',
          trigger: "axis",
          axisPointer: {
            type: 'shadow'
          }
            type: "shadow",
          },
        },
        legend: {
          data: ['总量', '新增'],
          left: 'center'
          data: ["总量", "新增"],
          left: "center",
          textStyle: {
            fontSize: 14 // 调整字体大小
          }
        },
        xAxis: {
          type: 'category',
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
        },
        yAxis: {
          type: 'value'
          type: "value",
        },
        series: seriesData,
      }
        series: [],
      },
    };
  },
  created() {
  },
  methods:{
    delHandler(){
      deleteChartId({btmname:this.btmname,chartId:'2'}).then(res=>{
        this.$message.success('删除成功')
        this.$emit('refresh');
      }).catch(error=>{
        this.$message.error(error)
      })
    }
  }
}
};
</script>
<style scoped lang="scss">
#chart {
  width: 1280px;
  height: 800px;
  width: 100%;
  height: 560px;
}
</style>