vue使用echarts實(shí)現(xiàn)立體柱形圖
本文實(shí)例為大家分享了vue使用echarts實(shí)現(xiàn)立體柱形圖的具體代碼,供大家參考,具體內(nèi)容如下
立體柱形圖是由前面、右面、上面三部分組成,繪制時(shí)需要先繪制前面為一個(gè)圖形,右面和上面繪制為一個(gè)圖形,然后在echats中注冊(cè),在option的series中renderItem中渲染
代碼如下:
(1)注冊(cè)繪制圖形
registry () {
? ? ? let MyCubeRect = this.$echarts.graphic.extendShape({
? ? ? ? shape: {
? ? ? ? ? x: 0,
? ? ? ? ? y: 0,
? ? ? ? ? width: 20,
? ? ? ? ? zWidth: 8,
? ? ? ? ? zHeight: 4
? ? ? ? },
? ? ? ? buildPath: function (ctx, shape) {
? ? ? ? ? const api = shape.api
? ? ? ? ? const xAxisPoint = api.coord([shape.xValue, 0])
? ? ? ? ? const p0 = [shape.x, shape.y]
? ? ? ? ? const p1 = [shape.x - shape.width / 2, shape.y]
? ? ? ? ? const p4 = [shape.x + shape.width / 2, shape.y]
? ? ? ? ? const p2 = [shape.x - shape.width / 2, xAxisPoint[1]]
? ? ? ? ? const p3 = [shape.x + shape.width / 2, xAxisPoint[1]]
? ? ? ? ? ctx.moveTo(p0[0], p0[1])
? ? ? ? ? ctx.lineTo(p1[0], p1[1])
? ? ? ? ? ctx.lineTo(p2[0], p2[1])
? ? ? ? ? ctx.lineTo(p3[0], p3[1])
? ? ? ? ? ctx.lineTo(p4[0], p4[1])
? ? ? ? ? ctx.lineTo(p0[0], p0[1])
? ? ? ? ? ctx.closePath()
? ? ? ? }
? ? ? })
? ? ? let MyCubeShadow = this.$echarts.graphic.extendShape({
? ? ? ? shape: {
? ? ? ? ? x: 0,
? ? ? ? ? y: 0,
? ? ? ? ? width: 20,
? ? ? ? ? zWidth: 8,
? ? ? ? ? zHeight: 4
? ? ? ? },
? ? ? ? buildPath: function (ctx, shape) {
? ? ? ? ? const api = shape.api
? ? ? ? ? const xAxisPoint = api.coord([shape.xValue, 0])
? ? ? ? ? const p1 = [shape.x - shape.width / 2, shape.y]
? ? ? ? ? const p4 = [shape.x + shape.width / 2, shape.y]
? ? ? ? ? const p6 = [shape.x + shape.width / 2 + shape.zWidth, shape.y - shape.zHeight]
? ? ? ? ? const p7 = [shape.x - shape.width / 2 + shape.zWidth, shape.y - shape.zHeight]
? ? ? ? ? const p3 = [shape.x + shape.width / 2, xAxisPoint[1]]
? ? ? ? ? const p5 = [shape.x + shape.width / 2 + shape.zWidth, xAxisPoint[1] - shape.zHeight]
? ? ? ? ? ctx.moveTo(p4[0], p4[1])
? ? ? ? ? ctx.lineTo(p3[0], p3[1])
? ? ? ? ? ctx.lineTo(p5[0], p5[1])
? ? ? ? ? ctx.lineTo(p6[0], p6[1])
? ? ? ? ? ctx.lineTo(p4[0], p4[1])
? ? ? ? ? ctx.moveTo(p4[0], p4[1])
? ? ? ? ? ctx.lineTo(p6[0], p6[1])
? ? ? ? ? ctx.lineTo(p7[0], p7[1])
? ? ? ? ? ctx.lineTo(p1[0], p1[1])
? ? ? ? ? ctx.lineTo(p4[0], p4[1])
? ? ? ? ? ctx.closePath()
? ? ? ? }
? ? ? })
? ? ? this.$echarts.graphic.registerShape('MyCubeRect', MyCubeRect)
? ? ? this.$echarts.graphic.registerShape('MyCubeShadow', MyCubeShadow)
? ? }(2)渲染圖形
barChartOption: {
? ? ? ? tooltip: {
? ? ? ? ? trigger: 'axis',
? ? ? ? ? axisPointer: {
? ? ? ? ? ? type: 'cross',
? ? ? ? ? ? label: {
? ? ? ? ? ? ? backgroundColor: '#6a7985'
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? },
? ? ? ? grid: {
? ? ? ? ? containLabel: true,
? ? ? ? ? top: '30px',
? ? ? ? ? bottom: '0px',
? ? ? ? ? left: '0px'
? ? ? ? },
? ? ? ? xAxis: {
? ? ? ? ? type: 'category',
? ? ? ? ? axisLabel: {
? ? ? ? ? ? interval: 0,
? ? ? ? ? ? fontSize: fontSize(12)
? ? ? ? ? },
? ? ? ? ? axisLine: {
? ? ? ? ? ? show: false,
? ? ? ? ? ? lineStyle: {
? ? ? ? ? ? ? color: '#98b9c5'
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? data: [] ?//通過后端傳入數(shù)據(jù)js傳入
? ? ? ? },
? ? ? ? yAxis: {
? ? ? ? ? type: 'value',
? ? ? ? ? axisLabel: {
? ? ? ? ? ? fontSize: fontSize(12)
? ? ? ? ? },
? ? ? ? ? axisLine: {
? ? ? ? ? ? show: false,
? ? ? ? ? ? lineStyle: {
? ? ? ? ? ? ? color: '#98b9c5'
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? splitLine: {
? ? ? ? ? ? lineStyle: {
? ? ? ? ? ? ? color: '#3a586a',
? ? ? ? ? ? ? type: 'dashed'
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? },
? ? ? ? series: [{
? ? ? ? ? name: '單位面積能耗',
? ? ? ? ? type: 'custom',
? ? ? ? ? renderItem: (params, api) => {
? ? ? ? ? ? let location = api.coord([api.value(0), api.value(1)])
? ? ? ? ? ? return {
? ? ? ? ? ? ? type: 'group',
? ? ? ? ? ? ? children: [{
? ? ? ? ? ? ? ? type: 'MyCubeRect',
? ? ? ? ? ? ? ? shape: {
? ? ? ? ? ? ? ? ? api,
? ? ? ? ? ? ? ? ? xValue: api.value(0) - 0.5,
? ? ? ? ? ? ? ? ? yValue: api.value(1),
? ? ? ? ? ? ? ? ? x: location[0],
? ? ? ? ? ? ? ? ? y: location[1]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? style: api.style()
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? type: 'MyCubeShadow',
? ? ? ? ? ? ? ? shape: {
? ? ? ? ? ? ? ? ? api,
? ? ? ? ? ? ? ? ? xValue: api.value(0) - 0.5,
? ? ? ? ? ? ? ? ? yValue: api.value(1),
? ? ? ? ? ? ? ? ? x: location[0],
? ? ? ? ? ? ? ? ? y: location[1]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? style: {
? ? ? ? ? ? ? ? ? fill: api.style(),
? ? ? ? ? ? ? ? ? text: ''
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }]
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? stack: '單位面積能耗',
? ? ? ? ? label: {
? ? ? ? ? ? show: true,
? ? ? ? ? ? position: 'top',
? ? ? ? ? ? formatter: '{c}',
? ? ? ? ? ? textStyle: {
? ? ? ? ? ? ? fontSize: fontSize(12),
? ? ? ? ? ? ? color: '#fff',
? ? ? ? ? ? ? align: 'center'
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? itemStyle: {
? ? ? ? ? ? color: new this.$echarts.graphic.LinearGradient(
? ? ? ? ? ? ? 0, 0, 0, 1,
? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? { offset: 0, color: '#b1950d' },
? ? ? ? ? ? ? ? { offset: 0.5, color: '#aea20d' },
? ? ? ? ? ? ? ? { offset: 1, color: '#a5aa12' }
? ? ? ? ? ? ? ]
? ? ? ? ? ? )
? ? ? ? ? },
? ? ? ? ? data: [] //后端傳入數(shù)據(jù)
? ? ? ? }]
? ? ? }注意事項(xiàng):
1)、在注冊(cè)圖形時(shí)style:只能使用 style: api.style();
text: ''后面才能使用label在圖形頂部放置value
2)、this.$echarts是經(jīng)過統(tǒng)一封裝之后的,具體情況還需具體考慮
(3)生成圖形
generateBarChart () {
? ? ? let vm = this
? ? ? if (this.$refs['uintEnergyConsume']) { //this.$refs是生成圖形區(qū)域的ref值
? ? ? ? this.$echarts.graphic.registerShape('MyCubeRect', this.MyCubeRect)
? ? ? ? this.$echarts.graphic.registerShape('MyCubeShadow', this.MyCubeShadow)
? ? ? ? this.barChart = this.$echarts.init(this.$refs['uintEnergyConsume'])
? ? ? ? this.barChart.setOption(this.barChartOption, false, true)
? ? ? ? $(window).resize(function () { //屏幕自適應(yīng)
? ? ? ? ? vm.handleResize()
? ? ? ? })
? ? ? }
? ? }(4)template中代碼
<div ?ref="uintEnergyConsume" ?id="uintEnergyConsume" ?class="chart-container"? ?style="width: 100%;" ?element-loading-text="加載中..."></div> </div>
(5)效果如下:

參考圖形網(wǎng)址:Vue使用Echarts實(shí)現(xiàn)立體柱狀圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue?Echarts實(shí)現(xiàn)帶滾動(dòng)效果的柱形圖
- vue+echarts實(shí)現(xiàn)3D柱形圖
- vue基于echarts實(shí)現(xiàn)立體柱形圖
- vue使用echarts實(shí)現(xiàn)水平柱形圖實(shí)例
- 使用D3.js+Vue實(shí)現(xiàn)一個(gè)簡單的柱形圖
- Vue使用Echarts實(shí)現(xiàn)立體柱狀圖
- vue echarts實(shí)現(xiàn)橫向柱狀圖
- vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示
- vue+echarts實(shí)現(xiàn)堆疊柱狀圖
- 如何在vue 中使用柱狀圖 并自修改配置
相關(guān)文章
在 vue-cli v3.0 中使用 SCSS/SASS的方法
關(guān)于如何在 vue-cli v3.0 中使用 SCSS/SASS,這里提供三種方案。感興趣的朋友通過本文一起學(xué)習(xí)吧2018-06-06
vue中beforeRouteLeave實(shí)現(xiàn)頁面回退不刷新的示例代碼
這篇文章主要介紹了vue中beforeRouteLeave實(shí)現(xiàn)頁面回退不刷新的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue3?中的toRef函數(shù)和toRefs函數(shù)的基本使用
這篇文章主要介紹了vue3?toRef函數(shù)和toRefs函數(shù),文中介紹了ref和toRef的區(qū)別,ref本質(zhì)是拷貝,修改響應(yīng)式數(shù)據(jù)不會(huì)影響原始數(shù)據(jù),toRef的本質(zhì)是引用關(guān)系,修改響應(yīng)式數(shù)據(jù)會(huì)影響原始數(shù)據(jù),需要的朋友可以參考下2022-11-11
VueCli4項(xiàng)目配置反向代理proxy的方法步驟
這篇文章主要介紹了VueCli4項(xiàng)目配置反向代理proxy的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

