Newer
Older
DH_Apicture / src / views / pictureOnMap / page / PublicOpinionAnalysis / components / ChartRy.vue
@zhangqy zhangqy on 29 Nov 4 KB first commit
<template>
  <div class="chartBox" :id="id"></div>
</template>
<script setup name="pieChartGszl">
import { guid } from '@/utils/ruoyi';
const { proxy } = getCurrentInstance();
import { nowSize } from './nowSize.js';
import moment from 'moment';
const emit = defineEmits(['newsType']);
const props = defineProps({
  //刷新标志
  refresh: {
    type: [String, Number],
    default: 1,
  },
  //数据
  echartData: {
    type: Object,
    default: {},
  },
});

const id = guid();
const myChart = shallowRef('');

watch(
  () => props.refresh,
  value => {
    //先销毁实例
    myChart.value && myChart.value.dispose();
    intChart();
  }
);
//自适应
function resizeTheChart() {
  if (myChart.value) {
    myChart.value.resize();
  }
}
//初始化
function intChart() {
  myChart.value = proxy.echarts.init(document.getElementById(id));
  myChart.value.clear();
  // 绘制图表
  myChart.value.setOption({
    tooltip: {
      trigger: 'axis',
      backgroundColor: '#004284',
      borderColor: '#0B9BFF',
      textStyle: {
        // 字体颜色
        color: '#FFFFFF',
        // 字体大小
        fontSize: 14,
      },
      axisPointer: {
        type: 'shadow',
        // label: {
        //   show: true,
        // }
      },

      formatter: function (params) {
        return params[0].name + ' : ' + params[0].value + '';
      },
    },
    grid: {
      top: nowSize(14, 1920),
      bottom: nowSize(30, 1920), //也可设置left和right设置距离来控制图表的大小
      left: nowSize(80, 1920),
      right: nowSize(20, 1920),
    },
    xAxis: {
      max: props.echartData.Maximum,
      type: 'value',
      axisLine: {
        show: false, //隐藏X轴轴线
        lineStyle: {
          color: 'rgba(44, 110, 189, 1)',
        },
      },
      axisTick: {
        show: false, //隐藏X轴刻度
        lineStyle: {
          color: 'rgba(44, 110, 189, 1)',
        },
      },
      axisLabel: {
        show: true,
        color: '#AAC1CFFF',
        fontSize: nowSize(14, 1920),
      },
      splitLine: {
        //网格线
        lineStyle: {
          type: 'dashed',
          color: 'rgba(255, 255, 255, 0.2)', //设置网格线类型 dotted:虚线   solid:实线
        },
        show: true, //隐藏或显示
      },
    },
    yAxis: {
      type: 'category',
      data: props.echartData.xData,
      name: '数量(条)',
      nameLocation: 'start',
      nameTextStyle: {
        color: '#AAC1CFFF',
        fontSize: nowSize(14, 1920),
        padding: [nowSize(-8, 1920), 0, 0, nowSize(-90, 1920)],
      },
      axisTick: {
        show: false, //隐藏X轴刻度
        lineStyle: {
          color: 'rgba(44, 110, 189, 1)',
        },
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        textStyle: {
          // align: 'left', //设置左对齐
          // baseline: 'middle',
        },
        margin: 25,
        show: true,
        color: 'rgba(255, 255, 255, 1)',
        fontSize: nowSize(14, 1920),
      },
      // splitArea: {
      //   show: true,
      //   areaStyle: {
      //     color:["rgba(77, 149, 217, 0.15)","rgba(77, 149, 217,0)"]
      //   }
      // }
    },
    series: [
      {
        type: 'bar',
        name: '',
        data: props.echartData.data,
        barWidth: nowSize(10, 1920),
        zlevel: 1,
        itemStyle: {
          borderRadius: nowSize(5, 1920),
          color: new proxy.echarts.graphic.LinearGradient(0, 0, 1, 1, [
            {
              offset: 0,
              color: 'rgba(0, 167, 245,1)',
            },
            {
              offset: 1,
              color: 'rgba(0, 238, 253,1)',
            },
          ]),
        },
      },
      {
        name: '背景',
        type: 'bar',
        barWidth: nowSize(10, 1920),
        barGap: '-100%',
        data: props.echartData.num,
        itemStyle: {
          borderRadius: nowSize(5, 1920),
          color: 'rgba(77, 149, 217, 0.15)',
        },
      },
    ],
  });
  myChart.value.on('click', params => {
    console.log(params.name, 'paramsparamsparams');
    emit('newsType', params.name);
    myChart.value.resize();
  });
}
onMounted(() => {
  intChart();
  window.addEventListener('resize', resizeTheChart);
});
onBeforeUnmount(() => {
  myChart.value && myChart.value.dispose();
});
</script>

<style lang="scss" scoped>
.chartBox {
  width: 100%;
  height: 100%;
}
</style>