|
@@ -0,0 +1,121 @@
|
|
|
|
|
+<template>
|
|
|
|
|
+ <div :id="props.id" class="barChart"></div>
|
|
|
|
|
+</template>
|
|
|
|
|
+<script setup lang="ts">
|
|
|
|
|
+ import * as echarts from 'echarts/core';
|
|
|
|
|
+ import { TooltipComponent, GridComponent, LegendComponent } from 'echarts/components';
|
|
|
|
|
+ import { BarChart } from 'echarts/charts';
|
|
|
|
|
+ import { CanvasRenderer } from 'echarts/renderers';
|
|
|
|
|
+ import { onMounted, ref, watch } from 'vue';
|
|
|
|
|
+ import type { BarChartData } from './types';
|
|
|
|
|
+
|
|
|
|
|
+ const props = defineProps<{
|
|
|
|
|
+ data: BarChartData;
|
|
|
|
|
+ id: string;
|
|
|
|
|
+ }>();
|
|
|
|
|
+
|
|
|
|
|
+ echarts.use([TooltipComponent, GridComponent, LegendComponent, BarChart, CanvasRenderer]);
|
|
|
|
|
+
|
|
|
|
|
+ let doughnut;
|
|
|
|
|
+
|
|
|
|
|
+ onMounted(() => {
|
|
|
|
|
+ doughnut = echarts.init(document.getElementById(props.id as string)!);
|
|
|
|
|
+ if (doughnut) {
|
|
|
|
|
+ initDoughnut(doughnut);
|
|
|
|
|
+ drawDoughnut(doughnut);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const drawDoughnut = (doughnut) => {
|
|
|
|
|
+ const doughnutOption = {
|
|
|
|
|
+ series: [
|
|
|
|
|
+ {
|
|
|
|
|
+ data: props.data,
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ };
|
|
|
|
|
+ doughnut.setOption(doughnutOption);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const initDoughnut = (doughnut) => {
|
|
|
|
|
+ const doughnutOption = {
|
|
|
|
|
+ tooltip: {
|
|
|
|
|
+ trigger: 'axis',
|
|
|
|
|
+ axisPointer: {
|
|
|
|
|
+ type: 'shadow',
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ grid: {
|
|
|
|
|
+ left: '5%',
|
|
|
|
|
+ right: '5%',
|
|
|
|
|
+ top: '20px',
|
|
|
|
|
+ bottom: '5%',
|
|
|
|
|
+ containLabel: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ xAxis: [
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'category',
|
|
|
|
|
+ data: props.data.category,
|
|
|
|
|
+ axisTick: {
|
|
|
|
|
+ alignWithLabel: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ axisLabel: {
|
|
|
|
|
+ color: 'rgba(0,0,0,0.5)',
|
|
|
|
|
+ fontFamily: 'HelveticaNeue',
|
|
|
|
|
+ fontSize: 12,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ yAxis: [
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'value',
|
|
|
|
|
+ splitLine: {
|
|
|
|
|
+ lineStyle: {
|
|
|
|
|
+ color: '#909399',
|
|
|
|
|
+ type: 'dashed',
|
|
|
|
|
+ width: 1,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ axisLabel: {
|
|
|
|
|
+ color: 'rgba(0,0,0,0.5)',
|
|
|
|
|
+ fontFamily: 'HelveticaNeue',
|
|
|
|
|
+ fontSize: 12,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ series: [
|
|
|
|
|
+ {
|
|
|
|
|
+ name: 'vehicle',
|
|
|
|
|
+ type: 'bar',
|
|
|
|
|
+ barWidth: '30%',
|
|
|
|
|
+ data: props.data.value,
|
|
|
|
|
+ itemStyle: {
|
|
|
|
|
+ color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
|
|
|
|
+ { offset: 0, color: '#38C3FE' },
|
|
|
|
|
+ { offset: 1, color: '#1777FF' },
|
|
|
|
|
+ ]),
|
|
|
|
|
+ },
|
|
|
|
|
+ label: {
|
|
|
|
|
+ show: true,
|
|
|
|
|
+ position: 'top',
|
|
|
|
|
+ color: '#38C3FE',
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ };
|
|
|
|
|
+ doughnut.setOption(doughnutOption);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ watch(
|
|
|
|
|
+ () => props.data,
|
|
|
|
|
+ () => {
|
|
|
|
|
+ drawDoughnut(doughnut);
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+</script>
|
|
|
|
|
+
|
|
|
|
|
+<style scoped lang="scss">
|
|
|
|
|
+ .barChart {
|
|
|
|
|
+ height: 100%;
|
|
|
|
|
+ }
|
|
|
|
|
+</style>
|