效果圖:
餅圖:
??
環(huán)形圖:
帶透明度的環(huán)形圖:
安裝echarts
- ?"echarts":?"^5.1.2"
- ?"echarts-gl":?"^2.0.8"
import Vue from 'vue'
import * as echarts from 'echarts'
import 'echarts-gl' // 3d圖表庫
Vue.prototype.$echarts = echarts
.vue文件 【bindListen方法可以提取到mixins里面,以供組件多次調(diào)用】
<template>
<div class="chart-container">
<div class="chart" ref="chart"></div>
<!-- 底座背景 -->
<div class="bg"></div>
</div>
</template>
<script>
import { getPie3D, getParametricEquation } from 'chart.js' //工具類js,頁面路徑自己修改
const color = ['#005aff', '#f8b551']
export default {
name: 'chart',
data () {
return {
optionData: [
{
name: '啟用電梯',
value: 176
},
{
name: '停用電梯',
value: 288
}
],
statusChart:null,
option:{}
}
},
created () {
this.setLabel()
},
mounted () {
this.initChart()
//根據(jù)窗口變化自動調(diào)節(jié)圖表大小
const that = this
window.onresize = function () {
that.changeSize()
}
},
methods: {
// 初始化label樣式
setLabel () {
this.optionData.forEach((item, index) => {
item.itemStyle = {
color: color[index]
}
item.label = {
normal: {
show: true,
color: color[index],
formatter: [
'{b|}',
'{c|{c}}{b|臺}',
'{d|n5n3t3z%}'
].join('\n'), // 用\n來換行
rich: {
b: {
color: '#fff',
lineHeight: 25,
align: 'left'
},
c: {
fontSize: 22,
color: '#fff',
textShadowColor: '#1c90a6',
textShadowOffsetX: 0,
textShadowOffsetY: 2,
textShadowBlur: 5
},
d: {
color: color[index],
align: 'left'
}
}
}
}
item.labelLine = {
normal: {
lineStyle: {
width: 1,
color: 'rgba(255,255,255,0.7)'
}
}
}
})
},
// 圖表初始化
initChart () {
this.statusChart = this.$echarts.init(this.$refs.chart)
// 傳入數(shù)據(jù)生成 option, 構(gòu)建3d餅狀圖, 參數(shù)工具文件已經(jīng)備注的很詳細
this.option = getPie3D(this.optionData, 0.8, 240, 28, 26, 0.5)
this.statusChart.setOption(this.option)
// 是否需要label指引線,如果要就添加一個透明的2d餅狀圖并調(diào)整角度使得labelLine和3d的餅狀圖對齊,并再次setOption
this.option.series.push({
name: '電梯狀態(tài)', //自己根據(jù)場景修改
backgroundColor: 'transparent',
type: 'pie',
label: {
opacity: 1,
fontSize: 13,
lineHeight: 20
},
startAngle: -40, // 起始角度,支持范圍[0, 360]。
clockwise: false, // 餅圖的扇區(qū)是否是順時針排布。上述這兩項配置主要是為了對齊3d的樣式
radius: ['20%', '50%'],
center: ['50%', '50%'],
data: this.optionData,
itemStyle: {
opacity: 0 //這里必須是0,不然2d的圖會覆蓋在表面
}
})
this.statusChart.setOption(this.option)
this.bindListen(this.statusChart)
},
// 監(jiān)聽鼠標(biāo)事件,實現(xiàn)餅圖選中效果(單選),近似實現(xiàn)高亮(放大)效果。
// optionName是防止有多個圖表進行定向option傳遞,單個圖表可以不傳,默認是opiton
bindListen (myChart, optionName = 'option') {
let selectedIndex = ''
let hoveredIndex = ''
// 監(jiān)聽點擊事件,實現(xiàn)選中效果(單選)
myChart.on('click', (params) => {
// 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否選中取反。
const isSelected = !this[optionName].series[params.seriesIndex].pieStatus
.selected
const isHovered =
this[optionName].series[params.seriesIndex].pieStatus.hovered
const k = this[optionName].series[params.seriesIndex].pieStatus.k
const startRatio =
this[optionName].series[params.seriesIndex].pieData.startRatio
const endRatio =
this[optionName].series[params.seriesIndex].pieData.endRatio
// 如果之前選中過其他扇形,將其取消選中(對 option 更新)
if (selectedIndex !== '' && selectedIndex !== params.seriesIndex) {
this[optionName].series[
selectedIndex
].parametricEquation = getParametricEquation(
this[optionName].series[selectedIndex].pieData.startRatio,
this[optionName].series[selectedIndex].pieData.endRatio,
false,
false,
k,
this[optionName].series[selectedIndex].pieData.value
)
this[optionName].series[selectedIndex].pieStatus.selected = false
}
// 對當(dāng)前點擊的扇形,執(zhí)行選中/取消選中操作(對 option 更新)
this[optionName].series[
params.seriesIndex
].parametricEquation = getParametricEquation(
startRatio,
endRatio,
isSelected,
isHovered,
k,
this[optionName].series[params.seriesIndex].pieData.value
)
this[optionName].series[params.seriesIndex].pieStatus.selected = isSelected
// 如果本次是選中操作,記錄上次選中的扇形對應(yīng)的系列號 seriesIndex
selectedIndex = isSelected ? params.seriesIndex : null
// 使用更新后的 option,渲染圖表
myChart.setOption(this[optionName])
})
// 監(jiān)聽 mouseover,近似實現(xiàn)高亮(放大)效果
myChart.on('mouseover', (params) => {
// 準(zhǔn)備重新渲染扇形所需的參數(shù)
let isSelected
let isHovered
let startRatio
let endRatio
let k
// 如果觸發(fā) mouseover 的扇形當(dāng)前已高亮,則不做操作
if (hoveredIndex === params.seriesIndex) {
// 否則進行高亮及必要的取消高亮操作
} else {
// 如果當(dāng)前有高亮的扇形,取消其高亮狀態(tài)(對 option 更新)
if (hoveredIndex !== '') {
// 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 false。
isSelected = this[optionName].series[hoveredIndex].pieStatus.selected
isHovered = false
startRatio = this[optionName].series[hoveredIndex].pieData.startRatio
endRatio = this[optionName].series[hoveredIndex].pieData.endRatio
k = this[optionName].series[hoveredIndex].pieStatus.k
// 對當(dāng)前點擊的扇形,執(zhí)行取消高亮操作(對 option 更新)
this[optionName].series[
hoveredIndex
].parametricEquation = getParametricEquation(
startRatio,
endRatio,
isSelected,
isHovered,
k,
this[optionName].series[hoveredIndex].pieData.value
)
this[optionName].series[hoveredIndex].pieStatus.hovered = isHovered
// 將此前記錄的上次選中的扇形對應(yīng)的系列號 seriesIndex 清空
hoveredIndex = ''
}
// 如果觸發(fā) mouseover 的扇形不是透明圓環(huán),將其高亮(對 option 更新)
if (
params.seriesName !== 'mouseoutSeries' &&
params.seriesName !== 'pie2d'
) {
// 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 true。
isSelected =
this[optionName].series[params.seriesIndex].pieStatus.selected
isHovered = true
startRatio =
this[optionName].series[params.seriesIndex].pieData.startRatio
endRatio = this[optionName].series[params.seriesIndex].pieData.endRatio
k = this[optionName].series[params.seriesIndex].pieStatus.k
// 對當(dāng)前點擊的扇形,執(zhí)行高亮操作(對 option 更新)
this[optionName].series[
params.seriesIndex
].parametricEquation = getParametricEquation(
startRatio,
endRatio,
isSelected,
isHovered,
k,
this[optionName].series[params.seriesIndex].pieData.value + 60
)
this[optionName].series[
params.seriesIndex
].pieStatus.hovered = isHovered
// 記錄上次高亮的扇形對應(yīng)的系列號 seriesIndex
hoveredIndex = params.seriesIndex
}
// 使用更新后的 option,渲染圖表
myChart.setOption(this[optionName])
}
})
// 修正取消高亮失敗的 bug
myChart.on('globalout', () => {
// 準(zhǔn)備重新渲染扇形所需的參數(shù)
let isSelected
let isHovered
let startRatio
let endRatio
let k
if (hoveredIndex !== '') {
// 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 true。
isSelected = this[optionName].series[hoveredIndex].pieStatus.selected
isHovered = false
k = this[optionName].series[hoveredIndex].pieStatus.k
startRatio = this[optionName].series[hoveredIndex].pieData.startRatio
endRatio = this[optionName].series[hoveredIndex].pieData.endRatio
// 對當(dāng)前點擊的扇形,執(zhí)行取消高亮操作(對 option 更新)
this[optionName].series[
hoveredIndex
].parametricEquation = getParametricEquation(
startRatio,
endRatio,
isSelected,
isHovered,
k,
this[optionName].series[hoveredIndex].pieData.value
)
this[optionName].series[hoveredIndex].pieStatus.hovered = isHovered
// 將此前記錄的上次選中的扇形對應(yīng)的系列號 seriesIndex 清空
hoveredIndex = ''
}
// 使用更新后的 option,渲染圖表
myChart.setOption(this[optionName])
})
},
// 自適應(yīng)寬高
changeSize () {
this.statusChart.resize()
}
}
}
</script>
<style lang='less' scoped>
.chart-container {
position: relative;
width: 100%;
height: 100%;
.chart,
.bg {
width: 100%;
height: 100%;
}
.bg {
position: absolute;
bottom: 50px;
left: 50%;
z-index: -1;
width: 180px;
height: 73px;
background: no-repeat center;
background-image: url('https://ks3-cn-beijing.ksyun.com/sxjg-elevator/datav-platform-2.0/images/chart_opacity_bg.png');
background-size: 100% 100%;
transform: translateX(-50%);
}
}
</style>
?chart.js文章來源:http://www.zghlxwxcb.cn/news/detail-512584.html
/**
* 繪制3d圖
* @param pieData 總數(shù)據(jù)
* @param internalDiameterRatio:透明的空心占比
* @param distance 視角到主體的距離
* @param alpha 旋轉(zhuǎn)角度
* @param pieHeight 立體的高度
* @param opacity 餅或者環(huán)的透明度
*/
const getPie3D = (pieData, internalDiameterRatio, distance, alpha, pieHeight, opacity = 1) => {
const series = []
let sumValue = 0
let startValue = 0
let endValue = 0
let legendData = []
let legendBfb = []
const k = 1 - internalDiameterRatio
pieData.sort((a, b) => {
return b.value - a.value
})
// 為每一個餅圖數(shù)據(jù),生成一個 series-surface 配置
for (let i = 0; i < pieData.length; i++) {
sumValue += pieData[i].value
const seriesItem = {
name:
typeof pieData[i].name === 'undefined'
? `series${i}`
: pieData[i].name,
type: 'surface',
parametric: true,
wireframe: {
show: false
},
pieData: pieData[i],
pieStatus: {
selected: false,
hovered: false,
k: k
},
center: ['10%', '50%']
}
if (typeof pieData[i].itemStyle !== 'undefined') {
const itemStyle = {}
itemStyle.color =
typeof pieData[i].itemStyle.color !== 'undefined'
? pieData[i].itemStyle.color
: opacity
itemStyle.opacity =
typeof pieData[i].itemStyle.opacity !== 'undefined'
? pieData[i].itemStyle.opacity
: opacity
seriesItem.itemStyle = itemStyle
}
series.push(seriesItem)
}
// 使用上一次遍歷時,計算出的數(shù)據(jù)和 sumValue,調(diào)用 getParametricEquation 函數(shù),
// 向每個 series-surface 傳入不同的參數(shù)方程 series-surface.parametricEquation,也就是實現(xiàn)每一個扇形。
legendData = []
legendBfb = []
for (let i = 0; i < series.length; i++) {
endValue = startValue + series[i].pieData.value
series[i].pieData.startRatio = startValue / sumValue
series[i].pieData.endRatio = endValue / sumValue
series[i].parametricEquation = getParametricEquation(
series[i].pieData.startRatio,
series[i].pieData.endRatio,
false,
false,
k,
series[i].pieData.value
)
startValue = endValue
const bfb = fomatFloat(series[i].pieData.value / sumValue, 4)
legendData.push({
name: series[i].name,
value: bfb
})
legendBfb.push({
name: series[i].name,
value: bfb
})
}
const boxHeight = getHeight3D(series, pieHeight) // 通過pieHeight設(shè)定3d餅/環(huán)的高度,單位是px
// 準(zhǔn)備待返回的配置項,把準(zhǔn)備好的 legendData、series 傳入。
const option = {
legend: {
show: false,
data: legendData,
orient: 'vertical',
left: 10,
top: 10,
itemGap: 10,
textStyle: {
color: '#A1E2FF'
},
icon: 'circle',
formatter: function (param) {
const item = legendBfb.filter(item => item.name === param)[0]
const bfs = fomatFloat(item.value * 100, 2) + '%'
return `${item.name} ${bfs}`
}
},
labelLine: {
show: true,
lineStyle: {
color: '#fff'
}
},
label: {
show: true,
position: 'outside',
formatter: ' \n{c} n5n3t3z%'
},
tooltip: {
backgroundColor: '#033b77',
borderColor: '#21f2c4',
textStyle: {
color: '#fff',
fontSize: 13
},
formatter: params => {
if (
params.seriesName !== 'mouseoutSeries' &&
params.seriesName !== 'pie2d'
) {
const bfb = (
(option.series[params.seriesIndex].pieData.endRatio -
option.series[params.seriesIndex].pieData.startRatio) *
100
).toFixed(2)
return (
`${params.seriesName}<br/>` +
`<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` +
`${bfb}%`
)
}
}
},
xAxis3D: {
min: -1,
max: 1
},
yAxis3D: {
min: -1,
max: 1
},
zAxis3D: {
min: -1,
max: 1
},
grid3D: {
show: false,
boxHeight: boxHeight, // 圓環(huán)的高度
viewControl: {
// 3d效果可以放大、旋轉(zhuǎn)等,請自己去查看官方配置
alpha, // 角度
distance, // 調(diào)整視角到主體的距離,類似調(diào)整zoom
rotateSensitivity: 0, // 設(shè)置為0無法旋轉(zhuǎn)
zoomSensitivity: 0, // 設(shè)置為0無法縮放
panSensitivity: 0, // 設(shè)置為0無法平移
autoRotate: false // 自動旋轉(zhuǎn)
}
},
series: series
}
return option
}
/**
* 生成扇形的曲面參數(shù)方程,用于 series-surface.parametricEquation
*/
const getParametricEquation = (startRatio, endRatio, isSelected, isHovered, k, h) => {
// 計算
const midRatio = (startRatio + endRatio) / 2
const startRadian = startRatio * Math.PI * 2
const endRadian = endRatio * Math.PI * 2
const midRadian = midRatio * Math.PI * 2
// 如果只有一個扇形,則不實現(xiàn)選中效果。
if (startRatio === 0 && endRatio === 1) {
isSelected = false
}
// 通過扇形內(nèi)徑/外徑的值,換算出輔助參數(shù) k(默認值 1/3)
k = typeof k !== 'undefined' ? k : 1 / 3
// 計算選中效果分別在 x 軸、y 軸方向上的位移(未選中,則位移均為 0)
const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0
const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0
// 計算高亮效果的放大比例(未高亮,則比例為 1)
const hoverRate = isHovered ? 1.05 : 1
// 返回曲面參數(shù)方程
return {
u: {
min: -Math.PI,
max: Math.PI * 3,
step: Math.PI / 32
},
v: {
min: 0,
max: Math.PI * 2,
step: Math.PI / 20
},
x: function (u, v) {
if (u < startRadian) {
return (
offsetX +
Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
)
}
if (u > endRadian) {
return (
offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
)
}
return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate
},
y: function (u, v) {
if (u < startRadian) {
return (
offsetY +
Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
)
}
if (u > endRadian) {
return (
offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
)
}
return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate
},
z: function (u, v) {
if (u < -Math.PI * 0.5) {
return Math.sin(u)
}
if (u > Math.PI * 2.5) {
return Math.sin(u) * h * 0.1
}
return Math.sin(v) > 0 ? 1 * h * 0.1 : -1
}
}
}
/**
* 獲取3d丙圖的最高扇區(qū)的高度
*/
const getHeight3D = (series, height) => {
series.sort((a, b) => {
return b.pieData.value - a.pieData.value
})
return (height * 25) / series[0].pieData.value
}
/**
* 格式化浮點數(shù)
*/
const fomatFloat = (num, n) => {
let f = parseFloat(num)
if (isNaN(f)) {
return false
}
f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n) // n 冪
let s = f.toString()
let rs = s.indexOf('.')
// 判定如果是整數(shù),增加小數(shù)點再補0
if (rs < 0) {
rs = s.length
s += '.'
}
while (s.length <= rs + n) {
s += '0'
}
return s
}
export { getPie3D, getParametricEquation }
參考文章:?https://www.cnblogs.com/KaypoGeng/p/14338434.html?(我就是在這個基礎(chǔ)上優(yōu)化的)文章來源地址http://www.zghlxwxcb.cn/news/detail-512584.html
到了這里,關(guān)于echarts3d餅圖,環(huán)形圖(包含透明效果)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!