当前位置: 首页 > news >正文

分类别柱状图(Vue3)

效果图:

需求:男女年龄段占比

<template><div class="go-ClassifyBar01"><v-chartref="vChartRef":option="option"style="width: 100%; height: 800px"></v-chart></div>
</template><script setup lang="ts">
import { ref, reactive, watch, onMounted } from "vue";
import VChart from "vue-echarts";
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { BarChart } from "echarts/charts";
import {GridComponent,TooltipComponent,LegendComponent,
} from "echarts/components";use([CanvasRenderer,BarChart,GridComponent,TooltipComponent,LegendComponent,
]);// 获取图表实例
const vChartRef = ref();// 定义数据类型
interface ChartDataItem {name: string;data: Array<{value: number;label: string;}>;
}// 模拟接口数据数组
const chartData = ref<ChartDataItem[]>([{name: "男性",data: [{ value: 5, label: "小于1岁" },{ value: 12, label: "1 ~ 9 岁" },{ value: 10, label: "10 ~ 19 岁" },{ value: 7, label: "20 ~ 29 岁" },{ value: 32, label: "30 ~ 39 岁" },{ value: 40, label: "40 ~ 49 岁" },{ value: 28, label: "50 ~ 59 岁" },{ value: 34, label: "大于60岁" },],},{name: "女性",data: [{ value: 8, label: "小于1岁" },{ value: 15, label: "1 ~ 9 岁" },{ value: 13, label: "10 ~ 19 岁" },{ value: 9, label: "20 ~ 29 岁" },{ value: 35, label: "30 ~ 39 岁" },{ value: 38, label: "40 ~ 49 岁" },{ value: 25, label: "50 ~ 59 岁" },{ value: 30, label: "大于60岁" },],},
]);const option = reactive({// 图位置定制grid: [{left: "12%",width: "28%",containLabel: true,bottom: 30,top: 30,},{left: "50%",width: "0%",bottom: 46,top: 30,},{right: "12%",width: "28%",containLabel: true,bottom: 30,top: 30,},],legend: {show: true,left: "center",top: 0,data: <any>[],},xAxis: [{type: "value",inverse: true,axisLabel: {show: true,color: "#000",margin: 8,},axisLine: {show: true,},axisTick: {show: false,},splitLine: {show: true,lineStyle: {type: "dashed",},},},{gridIndex: 1,show: true,splitLine: {show: true,lineStyle: {type: "dashed",},},axisLabel: {show: true,color: "#000",margin: 0,},},{gridIndex: 2,type: "value",axisLabel: {show: true,color: "#000",margin: 8,},axisLine: {show: true,},axisTick: {show: false,},splitLine: {show: true,lineStyle: {type: "dashed",},},},],yAxis: [{type: "category",position: "right",inverse: false,axisLine: {show: true,},axisTick: {show: false,},axisLabel: {show: false,},data: <any>[],},{type: "category",inverse: false,gridIndex: 1,position: "center",axisLabel: {align: "center",padding: [8, 0, 0, 0],fontSize: 12,color: "#000",},axisLine: {show: false,},axisTick: {show: false,},data: <any>[],},{type: "category",inverse: false,gridIndex: 2,position: "left",axisLabel: {show: false,},axisLine: {show: true,},axisTick: {show: false,},data: <any>[],},],series: [] as any[],
});// 更新图表数据
const updateChart = () => {if (!chartData.value.length) return;// 更新图例数据option.legend.data = chartData.value.map((item) => item.name);// 提取y轴标签(年龄段)const yAxisData = chartData.value[0].data.map((item) => item.label);// 更新y轴数据option.yAxis[0].data = yAxisData;option.yAxis[1].data = yAxisData;option.yAxis[2].data = yAxisData;// 动态生成系列数据option.series = chartData.value.map((item, index) => {if (index === 0) {return {type: "bar",name: item.name,barWidth: 12,label: {show: true,position: "left",color: "#000",fontWeight: "BoldMT",fontFamily: "Arial-BoldMT",fontSize: 14,formatter: "{c}%",},itemStyle: {color: "#0674F1FF",borderRadius: 0,},data: item.data.map((d) => d.value),};} else {return {xAxisIndex: 2,yAxisIndex: 2,type: "bar",name: item.name,barWidth: 12,label: {show: true,position: "right",color: "#000",fontWeight: "BoldMT",fontFamily: "Arial-BoldMT",fontSize: 14,formatter: "{c}%",},itemStyle: {color: "#E851B3FF",borderRadius: 0,},data: item.data.map((d) => d.value),};}});
};// 监听数据变化
watch(chartData,() => {updateChart();},{ immediate: true }
);// 组件挂载后更新图表
onMounted(() => {updateChart();
});
</script><style scoped>
.go-ClassifyBar01 {width: 100%;height: 100%;
}
</style>


文章转载自:

http://txJzwOdq.Lnbcx.cn
http://LrGEXvoc.Lnbcx.cn
http://3emHivEn.Lnbcx.cn
http://GOgoSAy7.Lnbcx.cn
http://b1UljpVZ.Lnbcx.cn
http://qnslUe53.Lnbcx.cn
http://GMd8u9Aa.Lnbcx.cn
http://FjzY6PV7.Lnbcx.cn
http://28TpPnzr.Lnbcx.cn
http://jivBuMQ0.Lnbcx.cn
http://T96zFZLb.Lnbcx.cn
http://VoVItwSp.Lnbcx.cn
http://iT4dJQKJ.Lnbcx.cn
http://BK8Yukog.Lnbcx.cn
http://ePGNMBrF.Lnbcx.cn
http://JDrchOv7.Lnbcx.cn
http://eCHKfFh9.Lnbcx.cn
http://CBeivWrP.Lnbcx.cn
http://o05diEkE.Lnbcx.cn
http://WvzGD9JH.Lnbcx.cn
http://5748Tic7.Lnbcx.cn
http://9JbdA03p.Lnbcx.cn
http://6WtWSJFy.Lnbcx.cn
http://6YWWRQmE.Lnbcx.cn
http://2PHASi90.Lnbcx.cn
http://DzuA0Lfp.Lnbcx.cn
http://Sbus9tAX.Lnbcx.cn
http://gsckAjWq.Lnbcx.cn
http://PmzugAlU.Lnbcx.cn
http://vfgJGxht.Lnbcx.cn
http://www.dtcms.com/a/376701.html

相关文章:

  • 视频生成迎来效率革命!字节提出视频生成稀疏注意力机制,计算量降20倍,速度升17.79倍!
  • 快速开发一类似个人网站空间的工具使用什么方式比较好,比如网页或者个Windows程序,并且使用什么技术开发比较好,区别优势局限性,分别说明一下
  • 计算机毕设选题:基于Python+MySQL校园美食推荐系统【源码+文档+调试】
  • vscode启用GEMINI CODE ASSIST插件
  • 仿QQ音乐的音乐播放器自动化测
  • daily notes[18]
  • 网络编程学习
  • App 上架全流程指南,iOS App 上架步骤、App Store 应用发布流程、uni-app 打包上传与审核要点详解
  • Java Flow API — Publisher、Subscriber 与 Processor 实战
  • 基于POI-TL实现动态Word模板数据填充(含图表):从需求到落地的完整开发实践
  • 【大模型-写作】STORM提升文章深度
  • (纯新手教学)计算机视觉(opencv)实战十四——模板与多个对象匹配
  • 论文阅读:arxiv 2024 Large Language Model Enhanced Recommender Systems: A Survey
  • 微店平台商品详情接口技术实现:从接口解析到数据结构化全方案
  • (12)使用 Vicon 室内定位系统(一)
  • 疯狂星期四文案网第65天运营日记
  • 【从零开始】12. 一切回归原点
  • JavaSE之深入浅出 IO 流:字节流、字符流与序列化流详解(含完整代码示例)
  • 【大模型推理】Qwen2.5模型硬件要求与4090Ti多并发推理方案
  • Node 中进程与子进程的区别及使用场景
  • 【C++进阶系列】:万字详解红黑树(附模拟实现的源码)
  • 以供应链思维为钥,启数字化转型之门——读《供应链思维》有感
  • 体验访答浏览器
  • Zynq开发实践(FPGA之spi实现)
  • 2025年度总结
  • Redis 哨兵模式详解:实现高可用的自动故障转移方案
  • 电动汽车充电系统(EVCS)的入侵检测
  • 自定义事件发布器
  • 零基础学AI大模型之从0到1调用大模型API
  • vue3:调用接口的时候怎么只传递一个数组进去,得到一个key-value数据