Vue使用Echart圖標(biāo)插件之柱狀圖
Echart是一個很好的圖表繪制插件,里面有各種各樣的圖表供我們選擇,最近用echart做圖表比較多,所以現(xiàn)在記錄一下用到的柱狀圖用到的一些配置和用法:
主要注意的點:
1、創(chuàng)建的畫布必須給定大小,不然無法顯示
2、xAxis中的data默認(rèn)為空時,X軸的的描述不存在,xAxis中axisLabel的interval的值表示隔幾列顯示,默認(rèn)為1
3、Series就是圖表的數(shù)據(jù)中心,data是傳入的數(shù)據(jù),可以通過barMaxWidth設(shè)置柱子的寬度
4、重點是柱子的顏色Series中itemStyle的color就是用來設(shè)置柱子的顏色的,如果柱子要使用不同的顏色那么需要先自定義一個數(shù)組來存放顏色字符串,然后通過遍歷的方法進行顏色的渲染
好了,廢話不多說直接上代碼,代碼的注釋很詳細(xì),應(yīng)該能看得懂。
<template> ? <div class="count-chart-wrapper"> ? ? <div class="chart-title"> ? ? ? 工程發(fā)貨統(tǒng)計 ? ? ? <span>(近六天)</span> ? ? </div> ? ? <div class="count-chart" ref="chart"></div><!--必須設(shè)置寬高--> ? </div> </template> ? <script> import echarts from "echarts"; ? export default { ? data() { ? ? return { ? ? ? colorList: [ ? ? ? ? //設(shè)置發(fā)貨柱子的,普通的柱子顏色 ? ? ? ? "#69D3BE", ? ? ? ? "#9D9E9F", ? ? ? ? "#F88282" ? ? ? ], ? ? ? colorList2: [ ? ? ? ? //設(shè)置發(fā)貨柱子的,柱子實現(xiàn)漸變色 ? ? ? ? ["#0282DE", "#3F28D0"], ? ? ? ? ["#FED701", "#E66938"], ? ? ? ? ["#67E0E3", "#0181DE"] ? ? ? ] ? ? }; ? }, ? mounted() { ? ? this.initChart(); ? }, ? methods: { ? ? // 初始化圖表樣式 ? ? initChart() { ? ? ? this.chart = echarts.init(this.$refs.chart); ? ? ? let _this = this; ? ? ? this.chart.setOption({ ? ? ? ? grid: { ? ? ? ? ? left: "50" ? ? ? ? ? // right: "60" ? ? ? ? }, ? ? ? ? legend: { ? ? ? ? ? show: false, ? ? ? ? ? data: this.legends ? ? ? ? }, ? ? ? ? tooltip: { ? ? ? ? ? trigger: "axis", ? ? ? ? ? show: true, ? ? ? ? ? axisPointer: { ? ? ? ? ? ? // 坐標(biāo)軸指示器,坐標(biāo)軸觸發(fā)有效 ? ? ? ? ? ? type: "shadow" // 默認(rèn)為直線,可選為:'line' | 'shadow' ? ? ? ? ? } ? ? ? ? }, ? ? ? ? xAxis: { ? ? ? ? ? axisLine: { show: false }, // 軸線 ? ? ? ? ? axisTick: { show: false }, // 刻度 ? ? ? ? ? type: "category", ? ? ? ? ? data: ["一", "二", "三", "四", "五", "六"],//X軸顯示 ? ? ? ? ? axisLabel: { ? ? ? ? ? ? color: "#333", ? ? ? ? ? ? interval: 0 //0:不隔行顯示,1:隔一行顯示 ? ? ? ? ? } ? ? ? ? }, ? ? ? ? yAxis: { ? ? ? ? ? type: "value", ? ? ? ? ? nameTextStyle: { ? ? ? ? ? ? fontWeight: "bold", ? ? ? ? ? ? align: "left", ? ? ? ? ? ? padding: [0, 50, 10, 0], ? ? ? ? ? ? color: "#ffffff" ? ? ? ? ? }, ? ? ? ? ? axisLine: { show: false }, // 軸線 ? ? ? ? ? axisTick: { show: false }, // 刻度 ? ? ? ? ? axisLabel: { ? ? ? ? ? ? color: "#333", ? ? ? ? ? ? formatter: `{value}`//Y軸的顯示形式,value,percent ? ? ? ? ? } ? ? ? ? }, ? ? ? ? series: [ ? ? ? ? ? {//實現(xiàn)漸變色的柱子 ? ? ? ? ? ? data: ["1", "2", "3", "1", "2", "3"],//顯示的數(shù)據(jù) ? ? ? ? ? ? type: "bar", ? ? ? ? ? ? smooth: true, // 平滑 ? ? ? ? ? ? symbol: "none", ? ? ? ? ? ? lineStyle: { ? ? ? ? ? ? ? color: "#FF5858" ? ? ? ? ? ? }, ? ? ? ? ? ? barMaxWidth: 30,//設(shè)置柱子的寬度 ? ? ? ? ? ? itemStyle: { ? ? ? ? ? ? ? normal: { ? ? ? ? ? ? ? ? label: { ? ? ? ? ? ? ? ? ? show: true, //開啟顯示 ? ? ? ? ? ? ? ? ? position: "top", //在上方顯示 ? ? ? ? ? ? ? ? ? textStyle: { ? ? ? ? ? ? ? ? ? ? //數(shù)值樣式 ? ? ? ? ? ? ? ? ? ? color: "#222", ? ? ? ? ? ? ? ? ? ? fontSize: 14 ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? color: params => { ? ? ? ? ? ? ? ? ? let colorList = _this.colorList2;//實現(xiàn)柱子的漸變色數(shù)組 ? ? ? ? ? ? ? ? ? let index = params.dataIndex;//dataIndex ?data中數(shù)據(jù)的下標(biāo) ? ? ? ? ? ? ? ? ? if (params.dataIndex >= colorList.length) { ? ? ? ? ? ? ? ? ? ? index = params.dataIndex - colorList.length; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? return new echarts.graphic.LinearGradient(0, 0, 0, 1, [ //漸變色使用方法 ? ? ? ? ? ? ? ? ? ? { offset: 0, color: colorList[index][0] }, ? ? ? ? ? ? ? ? ? ? { offset: 1, color: colorList[index][1] } ? ? ? ? ? ? ? ? ? ]); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? }, ? ? ? ? ? {//實現(xiàn)普通色的柱子 ? ? ? ? ? ? data: ["2.5", "3.5", "1.5", "2.5", "1.5", "2.5"], ? ? ? ? ? ? type: "bar", ? ? ? ? ? ? smooth: true, // 平滑 ? ? ? ? ? ? symbol: "none", ? ? ? ? ? ? lineStyle: { ? ? ? ? ? ? ? color: "#FF5858" ? ? ? ? ? ? }, ? ? ? ? ? ? barMaxWidth: 30, ? ? ? ? ? ? itemStyle: { ? ? ? ? ? ? ? normal: { ? ? ? ? ? ? ? ? label: { ? ? ? ? ? ? ? ? ? show: true, //開啟顯示 ? ? ? ? ? ? ? ? ? position: "top", //在上方顯示 ? ? ? ? ? ? ? ? ? textStyle: { ? ? ? ? ? ? ? ? ? ? //數(shù)值樣式 ? ? ? ? ? ? ? ? ? ? color: "#222", ? ? ? ? ? ? ? ? ? ? fontSize: 14 ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? color: params => { ? ? ? ? ? ? ? ? ? let colorList = _this.colorList;//柱子的顏色是普通的顏色 ? ? ? ? ? ? ? ? ? let index = params.dataIndex; ? ? ? ? ? ? ? ? ? if (params.dataIndex >= colorList.length) { ? ? ? ? ? ? ? ? ? ? index = params.dataIndex - colorList.length; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? return colorList[index]; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ] ? ? ? }); ? ? } ? } }; </script> ? <style> .count-chart-wrapper { ? width: 800px; ? margin: 0 auto; ? background-color: antiquewhite; } .count-chart { ? position: relative; ? margin: 0 auto; ? width: 400px; ? height: 400px; } </style>
結(jié)果圖樣:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3二次封裝element-ui中的table組件的過程詳解
這篇文章主要給大家介紹了vue3二次封裝element-ui中的table組件的過程,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友跟著小編一起來學(xué)習(xí)吧2024-01-01vue中對監(jiān)聽esc事件和退出全屏問題的解決方案
這篇文章主要介紹了vue中對監(jiān)聽esc事件和退出全屏問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue中computed(計算屬性)和watch(監(jiān)聽屬性)的用法及區(qū)別說明
這篇文章主要介紹了Vue中computed(計算屬性)和watch(監(jiān)聽屬性)的用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Vue 微信端掃描二維碼蘋果端卻只能保存圖片問題(解決方法)
這幾天在做項目時遇到微信掃描二維碼的然后進入公眾號網(wǎng)頁巴拉巴拉的,然后就很順利的遇到了在安卓端掃碼的時候,順利的一塌糊涂,然后到了蘋果端的時候,就只能出現(xiàn)一個保存圖片,然后就寫一下記錄一下這問題的解決方法2020-01-01