在vue項目使用數據可視化 echarts ,柱狀圖、折線圖、餅狀圖使用示例詳解及屬性詳解

官網地址:Apache ECharts

?一、下載插件并在頁面中引入

? npm install echarts --save

頁面導入:

? import * as echarts from 'echarts'

全局導入:?main.js 中,導入并注冊到全局

? import echarts from 'echarts'
? Vue.prototype.$echarts = echarts

二、實現效果展示

三、繪制柱狀圖使用步驟

?代碼示例

<template><div class="chart"><divid="chart_one"style="height: 82%;padding: 10px;display: flex;align-items: center;justify-content: center;"></div><h2>近一周使用記錄</h2></div>
</template><script>import * as echarts from 'echarts'export default {mounted() {// 解決echarts圖表放大溢出父容器// 頁面上的元素進行監測和調整大小操作,當被監測的元素的大小發生變化時調用setTimeout(() => {const resizeOb = new ResizeObserver((entries) => {for (const entry of entries) {echarts.getInstanceByDom(entry.target).resize()}})resizeOb.observe(document.getElementById('chart_one')) })// 通過id獲取echarts元素初始化圖表的容器:創建一個 echarts 實例,調用 methods 里的 initChart 進行圖表初始化// 獲取id為chart_one的dom元素給chartDom容器,init初始化一個echarts實例給myChart,需以dom元素為參數this.$nextTick(() => {this.chartDom = document.getElementById('chart_one')this.myChart = echarts.init(this.chartDom)this.initChart() })// 接口獲取數據后,再為echarts賦值this.initData()setTimeout(() => {this.initChart() }, 2000)},methods: {initChart() {// 在 ECharts 的 X 軸上顯示當前日期前一周的月日const today = new Date()const lastWeek = new Date(today.getFullYear(),today.getMonth(),today.getDate() - 6) // 最近一周的日期const xAxisData = [] // 存儲要顯示的日期字符串for (let i = lastWeek.getTime(); i <= today.getTime(); i += 86400000) {const date = new Date(i)xAxisData.push(date.toLocaleDateString('en-US', {month: 'numeric',day: 'numeric',}))}this.option = {// 設置圖表的邊距,containLabel表示圖表內容應包含在邊距之內grid: {left: '3%',right: '4%',bottom: '4%',top: '11%',containLabel: true,},// 設置一個圖表的 x 軸屬性xAxis: {type: 'category',data: xAxisData, // x 軸上的數據axisLabel: {formatter: '{value}', // 顯示格式},},yAxis: {type: 'value',axisLabel: {formatter: '{value} m3',},},// 提示框的內容和樣式,tooltip: {trigger: 'axis', // 觸發方式formatter: '用氣日期:{b}<br />日用氣量:{c}', // 自定義數據backgroundColor: 'rgba(255, 255, 255, 0.8)',axisPointer: {type: 'shadow',},},// 圖表的配置series: [{// data: this.chartonedate, // 顯示數據data: [120, 200, 150, 80, 70, 110, 130],type: 'bar', // 圖表類型,柱狀showBackground: true, // 是否顯示柱狀圖背景色backgroundStyle: {color: 'rgba(180, 180, 180, 0.2)',},label: {// 標簽的樣式normal: {show: true,position: 'top',},},},],}// 根據 this.option 的值來設置 this.myChart 的選項this.option && this.myChart.setOption(this.option)}, },}
</script>// 根據需要添加類名修改樣式
<style lang="scss" scoped>.chart {width: 95%;height: 92%;background: linear-gradient(to bottom, #ffffff, #ffffff);h2 {text-align: center;margin: 0;font-size: 18px;color: #000;}}
</style>

四、繪制折線圖、餅圖代碼整合

<template><el-row :gutter="6"> <el-col :span="9"><div class="ul-one ul-two"><div class="chart"><divid="chart_two"style="height: 82%;padding: 10px;display: flex;align-items: center;justify-content: center;"></div><h2>充值記錄</h2></div></div></el-col><el-col :span="6"><div class="ul-one ul-two"><div class="chart"><divid="chart_three"style="height: 82%;padding: 10px;display: flex;align-items: center;justify-content: center;"></div><h2>用戶數據分布</h2></div></div></el-col></el-row>
</template><script>import * as echarts from 'echarts'export default {mounted() { setTimeout(() => {const resizeOb = new ResizeObserver((entries) => {for (const entry of entries) {echarts.getInstanceByDom(entry.target).resize()}}) resizeOb.observe(document.getElementById('chart_two'))resizeOb.observe(document.getElementById('chart_three'))}) this.$nextTick(() => {this.chartDom = document.getElementById('chart_one')this.myChart = echarts.init(this.chartDom)this.initChart()this.chartDomtwo = document.getElementById('chart_two')this.myCharttwo = echarts.init(this.chartDomtwo)this.initCharttwo()this.chartDomthree = document.getElementById('chart_three')this.myChartthree = echarts.init(this.chartDomthree)this.initChartthree()}) this.initData()setTimeout(() => { this.initCharttwo()this.initChartthree()}, 2000)},methods: { initCharttwo() {this.optiontwo = {grid: {left: '3%',right: '4%',bottom: '4%',top: '11%',containLabel: true,},xAxis: {type: 'category',data: ['周期七','周期六','周期五','周期四','周期三','周期二','周期一',],},yAxis: {type: 'value',axisLabel: {formatter: '{value} m3',},},tooltip: {trigger: 'axis',formatter: '用氣周期:{b}<br />周期用量:{c}', // 自定義數據backgroundColor: 'rgba(255, 255, 255, 0.8)',axisPointer: {type: 'shadow',},},series: [{// data: this.charttwodate,data: [120, 200, 150, 80, 70, 110, 130],type: 'line',// 圖表上顯示數據,并放置在數據點的上方。label: {normal: {show: true,position: 'top',},},},],}this.optiontwo && this.myCharttwo.setOption(this.optiontwo)},initChartthree() {this.optionthree = {tooltip: {trigger: 'item',},legend: {orient: 'vertical',left: 'left',},// 控制頂部小圖標的位置legend: {top: '0',left: 'center',},series: [{name: '用戶數量',type: 'pie',radius: '50%',center: ['50%', '60%'],data: [{ value: 83, name: '燃氣表用戶' },{ value: 16, name: '流量計用戶' },{ value: 9, name: '未開戶用戶' },],emphasis: {itemStyle: {shadowBlur: 10,shadowOffsetX: 0,shadowColor: 'rgba(0, 0, 0, 0.5)',},},},],}this.optionthree && this.myChartthree.setOption(this.optionthree)},},}
</script><style lang="scss" scoped>.ul-two {display: flex;justify-content: space-around;margin: 5px 3px;border: 1px solid #ccc;li {margin: 10px;list-style: none;width: 15%;height: 200px;.ranqibiao {border-radius: 50px;width: 100%;height: 100%;background: url('~@/assets/bg/bg1.png') no-repeat center center;box-shadow: 0 4px 9px 2px rgba(247, 189, 35, 0.91);background-size: cover;display: flex;justify-content: center;}.liuliangji {border-radius: 50px;width: 100%;height: 100%;background: url('~@/assets/bg/bg2.png') no-repeat center center;box-shadow: 0 4px 9px 2px rgba(62, 170, 255, 0.91);background-size: cover;display: flex;justify-content: center;}.weikahu {border-radius: 50px;width: 100%;height: 100%;background: url('~@/assets/bg/bg3.png') no-repeat center center;box-shadow: 0 4px 9px 2px rgba(80, 224, 254, 0.91);background-size: cover;display: flex;justify-content: center;}.image {border-radius: 10px;background: #ffffff;width: 45px;height: 45px;display: flex;align-items: center;justify-content: center;margin-top: 40px;}.text {color: #ffffff;font-weight: bolder;margin-top: 20px;}.number {color: #ffffff;font-size: 20px;font-weight: 800;margin-top: 30px;}}}.val {text-align: center;color: #000;h2 {margin: 10px 0 0 0;}}.ul-one {height: 320px;display: flex;justify-content: center;align-items: center;}.chart {width: 95%;height: 92%;background: linear-gradient(to bottom, #ffffff, #ffffff);h2 {text-align: center;margin: 0;font-size: 18px;color: #000;}}
</style>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/36101.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/36101.shtml
英文地址,請注明出處:http://en.pswp.cn/news/36101.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Clone函數

概述 Clone函數是一種用于復制的計算機函數。在程序編寫中&#xff0c;除了自定義一個拷貝構造函數來實現對象復制外&#xff0c;還可以實現一個clone函數。這需要借助編譯器實現的一個隱藏拷貝構造函數&#xff0c;這樣的做法&#xff0c;更省心。 中文名clone函數外文名clon…

C# 使用FFmpeg.Autogen對byte[]進行編解碼

C# 使用FFmpeg.Autogen對byte[]進行編解碼&#xff0c;參考&#xff1a;https://github.com/vanjoge/CSharpVideoDemo 入口調用類&#xff1a; using System; using System.IO; using System.Drawing; using System.Runtime.InteropServices; using FFmpeg.AutoGen;namespace F…

C++11異步與通信之 packaged_task

概念簡介 packaged_task 用于包裝可調用目標(Callable)為一個對象,如lambda&#xff0c;普通函數&#xff0c;小括號重載等&#xff0c;用于異步調用。 其返回值或所拋異常被存儲于能通過 std::future 對象訪問的共享狀態中&#xff0c;和promise類似。 將函數的調用與函數返…

時序預測 | MATLAB實現EEMD-LSTM、LSTM集合經驗模態分解結合長短期記憶神經網絡時間序列預測對比

時序預測 | MATLAB實現EEMD-LSTM、LSTM集合經驗模態分解結合長短期記憶神經網絡時間序列預測對比 目錄 時序預測 | MATLAB實現EEMD-LSTM、LSTM集合經驗模態分解結合長短期記憶神經網絡時間序列預測對比效果一覽基本介紹模型搭建程序設計參考資料 效果一覽 基本介紹 時序預測 | …

小龜帶你妙寫排序之快速排序

快速排序 一. 快速排序原理二. 題目三. 快速排序的思路分析&#xff08;圖文結合&#xff09;四.代碼 一. 快速排序原理 先從數據序列中選一個元素&#xff0c;并將序列中所有比該元素小的元素都放到它的右邊或左邊&#xff0c;再對左右兩邊分別用同樣的方法處之直到每一個待處…

runtime error: member access within misaligned address(力扣最常見錯誤之一)

runtime error: member access within misaligned address&#xff08;力扣最常見錯誤之一&#xff09; 前言原因和解決辦法總結 前言 最近博主在刷力扣時&#xff0c;明明代碼邏輯都沒問題&#xff0c;但總是報下面這個錯誤&#xff1a; runtime error: member access within…

django實現登錄和登錄的鑒權

1、創建數據庫的管理員表 在models.py 中定義admin表&#xff0c;為了簡單&#xff0c;表里只有用戶名和密碼還有默認加的id 三個字段 from django.db import models# Create your models here.class Admin(models.Model):username models.CharField(verbose_name"用戶…

源碼框架-三勾軟件

javaspringboot微信小程序商城SAAS前后端源碼: 三勾商城是開發友好的微信小程序商城&#xff0c;框架支持SAAS&#xff0c;支持發布 iOS Android 公眾號 H5 各種小程序&#xff08;微信/支付寶/百度/頭條/QQ/釘釘/淘寶&#xff09;等多個平臺&#xff0c;不可多得的二開神器…

訓練用于序列分類任務的 RoBERTa 模型的適配器

介紹 NLP當前的趨勢包括下載和微調具有數百萬甚至數十億參數的預訓練模型。然而,存儲和共享如此大的訓練模型非常耗時、緩慢且昂貴。這些限制阻礙了 RoBERTa 模型開發更多用途和適應性更強的 NLP 技術,該模型可以從多個任務中學習并針對多個任務進行學習;在本文中,我們將重…

Kafka:安裝和配置

producer&#xff1a;發布消息的對象&#xff0c;稱為消息產生者 &#xff08;Kafka topic producer&#xff09; topic&#xff1a;Kafka將消息分門別類&#xff0c;每一個消息稱為一個主題&#xff08;topic&#xff09; consumer&#xff1a;訂閱消息并處理發布消息的對象…

模擬 枚舉

分享牛客算法基礎精選題單題目打卡!!! 目錄 字符串的展開 多項式輸出 機器翻譯 : 鋪地毯 : [NOIP2016]回文日期 字符串的展開 原題鏈接 : 字符串的展開 思路 : 模擬 代碼 : #include<iostream> #include<cstring> #include<algorithm> using na…

Java課題筆記~ ServletContext

單個Servlet的配置對象 web.xml <servlet><servlet-name>FirstServlet</servlet-name><servlet-class>com.ambow.test.FirstServlet</servlet-class><init-param><param-name>charset</param-name><param-value>utf-8&…

centos自動同步北京時間

1、安裝ntpdate服務 yum -y install ntpdate 2、加入自動任務計劃 查找ntpdate的路徑&#xff1a; which ntpdate 復制這個路徑。 編輯自動任務計劃并加入ntpdate&#xff1a; crontab -e # 每小時第30分鐘同步AD域控時間 30 * * * * /usr/sbin/ntpdate -u 192.168.2.8 > …

DP——動態規劃

DP——動態規劃 動態規劃算法動態規劃的一般步驟特殊DP——背包0-1背包問題完全背包問題 總結 動態規劃算法 當涉及到解決具有重疊子問題的優化問題時&#xff0c;動態規劃是一種常用的算法技術。它通過將問題分解為一系列重疊子問題&#xff0c;并使用遞歸或迭代的方式來解決…

Spring Cloud Gateway系例—GatewayFilter 工廠

目錄 6.1.AddRequestHeader 6.2.AddRequestHeadersIfNotPresent 6.3.AddRequestParameter 6.4.AddResponseHeader 6.5.CircuitBreaker 6.5.1. 熔斷指定的狀態碼 6.6.CacheRequestBody 6.7.DedupeResponseHeader 6.8.FallbackHeaders 6.9.JsonToGrpc 6.10.LocalRespo…

TypeScript 非空斷言

TypeScript 非空斷言 發布于 2020-04-08 15:20:15 17.5K0 舉報 一、非空斷言有啥用 介紹非空斷言前&#xff0c;先來看個示例&#xff1a; function sayHello(name: string | undefined) {let sname: string name; // Error } 對于以上代碼&#xff0c;TypeScript 編譯器…

用戶端Web自動化測試-L1

目錄&#xff1a; Web自動化測試價值與體系環境安裝與使用自動化用例錄制自動化測試用例結構分析web瀏覽器控制常見控件定位方法強制等待與隱式等待常見控件交互方法自動化測試定位策略搜索功能自動化測試用戶端Web自動化測試 1.Web自動化測試價值與體系 功能測試場景: UI 自…

IntelliJ Idea 編譯時控制臺上中文輸出亂碼

猜測原因是IDEA啟動時未指定編碼信息&#xff0c;故與系統編碼保持一致&#xff08;windows中文系統默認為GBK編碼&#xff09;,當以UTF-8編碼進行編譯在控制臺會以GBK編碼輸出,從而導致亂碼 解決方案 指定Idea啟動時JVM的默認編碼為UTF-8 Help -> Edit Custom Options P…

本地圖片的image加密解密-Python 3.10-win10

本地圖片的image加密解密- Python 3.10 pyt3int22 -根據1zip下圖片批量生成加密的-物體識別.py import ioimport os import base64 import json # 指定圖片文件夾 image_dir = "./1zip/" base64code_dir = "./base64code/" base64_to_dir = "./bas…

AUTOSAR規范與ECU軟件開發(基礎篇)2.5 AUTOSAR方法論

前言 AUTOSAR方法論(AUTOSAR Methodology) 中車用控制器軟件的開發涉及系統級、 ECU級和軟件組件級。 系統級主要考慮系統功能需求、 硬件資源、 系統約束, 然后建立系統架構; ECU級根據抽象后的信息對ECU進行配置; 系統級和ECU級設計的同時, 伴隨著軟件組件級的開發。 上…