这篇文章主要讲解了“vue中如何封装echarts公共组件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue中如何封装echarts公共组件”吧!
定义图表公共样式是为了统一同一网站各页面图表的基础样式baseOption.js(轴线、区域、色系、字体),统一封装后页面需要直接引入,传入所需参即可覆盖基础样式。
npm install echarts --save npm install lodash --save // 若已安装请忽略
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
baseOption.js文件:公共样式定义,为了统一同网站各种图表的基础样式,比如轴线、图各板块颜色,值仅供参考):
// baseOption.js export default { color: [ "#0067E1", "#0CC1FF", "#00D1B3", "#85E814", "#FFA082", ], tooltip: {}, legend: { orient:'horizontal', type:'scroll', pageIconSize:12, pageIconColor:'#aaa', pageIconInactiveColor :'#2f4554', pageTextStyle:{ color:'#999999' }, itemWidth:20, itemHeight:12, top:0, textStyle:{ color:'#999999' }, }, grid: { top: "13%", left: "3%", right: "10%", bottom: "2%", containLabel: true, }, xAxis: [ { axisLine: { lineStyle: { color: "rgba(65, 97, 128, 0.15)", // type: "dashed", }, }, axisTick: { show: false, }, axisLabel: { interval:0, color: "#rgba(0, 0, 0, 0.25)", textStyle: { fontSize: 10, } }, nameTextStyle:{ color:'#999999', fontSize: 10, }, }, ], yAxis: [ { axisLabel: { color: "#rgba(0, 0, 0, 0.25)", textStyle: { fontSize: 11, }, }, axisLine: { lineStyle: { color: "rgba(65, 97, 128, 0.15)", }, }, splitLine: { lineStyle: { color: "rgba(65, 97, 128, 0.15)", }, }, axisTick: { show: false, }, nameTextStyle:{ color:'#999999', fontSize: 10, padding:[0,20,0,0] }, minInterval: 1 }, ], };
Echart.vue文件:图本身组件
// Echart.vue <template> <div :id="elId" /> </template>
<script> import echarts from "echarts"; import { merge, debounce } from "lodash"; // 引入公共样式 import baseOption from "./baseOption" export default { data() { return { elId: "", vOption: { series: [], }, }; }, props: { optionData: Object, }, computed: { // 合并配置对象 option() { return merge({}, baseOption, this.vOption, this.optionData); }, }, created() { this.elId = this.uuid(); }, mounted() { // 实例化echarts对象 this.$nextTick(() => { this.initChart(); }); }, beforeDestroy() { if (!this.chart) { return; } this.chart.dispose(); this.chart = null; }, watch: { optionData: { deep: true, handler: function() { this.$nextTick(() => { this.initChart(); }); }, }, }, methods: { // 生成唯一uuid做为唯一标识符 uuid() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { var r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8; return v.toString(16); }); }, // 绘图 initChart() { if(!document.getElementById(this.elId)) return this.chart = echarts.init(document.getElementById(this.elId)); this.chart.setOption(this.option); const that = this; window.addEventListener( "resize", debounce(() => { // 引入debounce,防止频繁更改浏览页尺寸出现页面抖动 if (that.chart) { that.chart.resize(); } }, 100) ); }, }, }; </script>
传入目标数据就可以了,以下示例数据为饼图:
// index.vue <template> <div class="chartBox"> <Chart :optionData="chartData"></Chart> </div> </template>
<script> // 引入Echart.vue组件 import Chart from "./Echart.vue"; export default { components: { Chart, }, data() { return { // 图目标数据 chartData: { tooltip: { show:true, trigger:'item', formatter: "{b} : {c} ({d}%)", }, xAxis: [{ show: false }], yAxis: [{ show: false }], series: [ { name: "访问来源", type: "pie", // 饼图 radius: ["30%", "45%"], // 饼图大小 data: [ { value: 1048, name: "搜索引擎" }, { value: 735, name: "直接访问" }, { value: 580, name: "邮件营销" }, { value: 484, name: "联盟广告" }, { value: 300, name: "视频广告" }, ], }, ], }, }; }, }; </script>
此时好看的饼图就出现啦~~
感谢各位的阅读,以上就是“vue中如何封装echarts公共组件”的内容了,经过本文的学习后,相信大家对vue中如何封装echarts公共组件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。