echarts实现3D柱状图(视觉层面)

一、第一种效果

效果图

使用步骤 

完整实例,copy就可直接使用

 
  1. <template>

  2. <div :class="className" :style="{height:height,width:width}" />

  3. </template>

  4. <script>

  5. import echarts from 'echarts'

  6. require('echarts/theme/macarons') // echarts theme

  7. import resize from '@/views/dashboard/mixins/resize'

  8. export default {

  9. mixins: [resize],

  10. props: {

  11. className: {

  12. type: String,

  13. default: 'chart'

  14. },

  15. width: {

  16. type: String,

  17. default: '100%'

  18. },

  19. height: {

  20. type: String,

  21. default: '400px'

  22. },

  23. dataList: {

  24. type: Array,

  25. default: []

  26. }

  27. },

  28. data() {

  29. return {

  30. chart: null,

  31. xAxisData: ['数学', '语文', '英语', '物理', '化学'],

  32. data1: [200, 100, 200, 50, 100],

  33. data2: [300, 200, 300, 200, 300]

  34. }

  35. },

  36. mounted() {

  37. this.$nextTick(() => {

  38. this.initChart()

  39. })

  40. },

  41. beforeDestroy() {

  42. if (!this.chart) {

  43. return

  44. }

  45. this.chart.dispose()

  46. this.chart = null

  47. },

  48. watch: {

  49. dataList(val, oldVal) {//普通的watch监听

  50. this.initChart()

  51. }

  52. },

  53. methods: {

  54. initChart() {

  55. this.chart = echarts.init(this.$el, 'macarons')

  56. this.chart.setOption(

  57. {

  58. tooltip: {

  59. trigger: 'item'

  60. },

  61. grid: {

  62. top: '10%',

  63. bottom: '20%',

  64. right: '2%',

  65. left: '10%'

  66. },

  67. legend: {

  68. data: ['2021', '2022'],

  69. left: '20px',

  70. textStyle: {

  71. color: '#FFFFFF'

  72. }

  73. },

  74. graphic: [

  75. {

  76. type: 'image', // 图形元素类型

  77. id: 'logo', // 更新或删除图形元素时指定更新哪个图形元素,如果不需要用可以忽略。

  78. right: 'center', // 根据父元素进行定位 (居中)

  79. bottom: '0%', // 根据父元素进行定位 (0%), 如果bottom的值是 0,也可以删除该bottom属性值。

  80. z: 0, // 层叠

  81. bounding: 'all', // 决定此图形元素在定位时,对自身的包围盒计算方式

  82. style: {

  83. image:

  84. 'https://img0.baidu.com/it/u=3846011338,1538056540&fm=253&fmt=auto&app=138&f=PNG?w=889&h=500', // 这里一定要注意、注意,必须是https开头的图片路径地址

  85. width: 800,

  86. height: 400

  87. }

  88. }

  89. ],

  90. xAxis: {

  91. data: this.xAxisData,

  92. axisTick: {

  93. show: false

  94. },

  95. axisLine: {

  96. show: false

  97. },

  98. axisLabel: {

  99. interval: 0,

  100. textStyle: {

  101. color: '#fff',

  102. fontSize: 20

  103. },

  104. margin: 40

  105. }

  106. },

  107. yAxis: {

  108. splitLine: {

  109. show: false

  110. },

  111. axisTick: {

  112. show: true,

  113. alignWithLabel: true,

  114. inside: true

  115. },

  116. axisLine: {

  117. show: true

  118. },

  119. axisLabel: {

  120. textStyle: {

  121. color: '#fff',

  122. fontSize: 20

  123. }

  124. }

  125. },

  126. series: [

  127. // 底部的光晕

  128. {

  129. name: '',

  130. type: 'pictorialBar',

  131. tooltip: {

  132. show: false

  133. },

  134. symbolSize: [90, 40],

  135. symbolOffset: [0, 20],

  136. z: 1,

  137. itemStyle: {

  138. normal: {

  139. color: 'transparent',

  140. borderColor: '#26B2E8',

  141. borderType: 'solid',

  142. borderWidth: 1

  143. }

  144. },

  145. data: [1, 1, 1, 1, 1]

  146. },

  147. {

  148. name: '2021',

  149. type: 'bar',

  150. barWidth: 45,

  151. barGap: '-100%',

  152. z: 0,

  153. itemStyle: {

  154. color: '#E8CCFF',

  155. opacity: 0.7

  156. },

  157. data: this.data2

  158. },

  159. {

  160. name: '2022',

  161. type: 'bar',

  162. barWidth: 45,

  163. barGap: '-100%',

  164. itemStyle: {

  165. color: '#FF9A22'

  166. },

  167. data: this.data1

  168. },

  169. //头部、中部、尾部圆片

  170. {

  171. name: '2021', // 头部

  172. type: 'pictorialBar',

  173. symbolSize: [45, 25],

  174. symbolOffset: [0, -10],

  175. z: 12,

  176. symbolPosition: 'end',

  177. itemStyle: {

  178. color: '#D28EFF',

  179. opacity: 1

  180. },

  181. data: this.data2

  182. },

  183. {

  184. name: '2022',//中部

  185. type: 'pictorialBar',

  186. symbolSize: [45, 25],

  187. symbolOffset: [0, -10],

  188. z: 12,

  189. symbolPosition: 'end',

  190. itemStyle: {

  191. opacity: 1,

  192. color: '#FF3EFF'

  193. },

  194. data: this.data1

  195. },

  196. {

  197. //三个最低下的圆片

  198. name: '',

  199. type: 'pictorialBar',

  200. symbolSize: [45, 25],

  201. symbolOffset: [0, 10],

  202. z: 12,

  203. itemStyle: {

  204. opacity: 1,

  205. color: 'red'

  206. },

  207. data: this.data1

  208. }

  209. ]

  210. }

  211. )

  212. }

  213. }

  214. }

  215. </script>

 
  1. itemStyle: {

  2. normal: {

  3. //这里是颜色

  4. color: function (params) {

  5. //注意,如果颜色太少的话,后面颜色不会自动循环,最好多定义几个颜色

  6. var colorList = ['#00A3E0', '#FFA100', '#ffc0ca', '#CCCCCC', '#749f83', '#04A035', '#8729D9', '#c207c9', '#c90762', '#c90707'];

  7. return colorList[params.dataIndex]

  8. }

  9. }

  10. }

附带网图背景

二、第二种效果

效果图

使用步骤 

完整实例,copy就可直接使用

 
  1. <template>

  2. <div :class="className" :style="{height:height,width:width}" />

  3. </template>

  4. <script>

  5. import echarts from 'echarts'

  6. require('echarts/theme/macarons') // echarts theme

  7. import resize from '@/views/dashboard/mixins/resize'

  8. export default {

  9. mixins: [resize],

  10. props: {

  11. className: {

  12. type: String,

  13. default: 'chart'

  14. },

  15. width: {

  16. type: String,

  17. default: '100%'

  18. },

  19. height: {

  20. type: String,

  21. default: '400px'

  22. },

  23. dataList: {

  24. type: Array,

  25. default: []

  26. }

  27. },

  28. data() {

  29. return {

  30. chart: null,

  31. xAxisData: ['区域1', '区域2', '区域3', '区域4', '区域5', '区域6', '区域7', '区域8', '区域9'],

  32. colorArr: ['#0C628C', '#3887D5', '#2570BB'],

  33. barWidth: 30,

  34. data1: [6, 7, 3, 11, 33, 38, 22, 55, 66],

  35. bottomData: [1, 1, 1, 1, 1, 1, 1, 1, 1],

  36. topData: [100, 100, 100, 100, 100, 100, 100, 100, 100],

  37. names: ["区域"]

  38. }

  39. },

  40. mounted() {

  41. this.$nextTick(() => {

  42. this.initChart()

  43. })

  44. },

  45. beforeDestroy() {

  46. if (!this.chart) {

  47. return

  48. }

  49. this.chart.dispose()

  50. this.chart = null

  51. },

  52. watch: {

  53. dataList(val, oldVal) {//普通的watch监听

  54. this.initChart()

  55. }

  56. },

  57. methods: {

  58. initChart() {

  59. var color = {

  60. type: 'linear',

  61. x: 0,

  62. x2: 1,

  63. y: 0,

  64. y2: 0,

  65. colorStops: [

  66. {

  67. offset: 0,

  68. color: this.colorArr[0]

  69. },

  70. {

  71. offset: 0.5,

  72. color: this.colorArr[0]

  73. },

  74. {

  75. offset: 0.5,

  76. color: this.colorArr[1]

  77. },

  78. {

  79. offset: 1,

  80. color: this.colorArr[1]

  81. }

  82. ]

  83. }

  84. this.chart = echarts.init(this.$el, 'macarons')

  85. this.chart.setOption(

  86. {

  87. tooltip: {

  88. trigger: 'axis',

  89. formatter: function (params) {

  90. var str = params[0].name + ':'

  91. params.filter(function (item) {

  92. if (item.componentSubType == 'bar' && item.seriesName === '数据') {

  93. str += item.value

  94. }

  95. })

  96. return str

  97. }

  98. },

  99. grid: {

  100. left: '0%',

  101. right: '10%',

  102. bottom: '3%',

  103. containLabel: true

  104. },

  105. xAxis: [

  106. {

  107. type: 'category',

  108. name: this.names[0],

  109. data: this.xAxisData,

  110. // 更改坐标轴颜色

  111. axisLine: {

  112. lineStyle: {

  113. color: "#FFFFFF"

  114. },

  115. onZero: false

  116. },

  117. // x轴的字体样式

  118. axisLabel: {

  119. interval: 0,

  120. textStyle: {

  121. color: "#FFFFFF", // 更改坐标轴文字颜色

  122. fontSize: 14, // 更改坐标轴文字大小

  123. fontFamily: 'MicrosoftYaHei'

  124. },

  125. }

  126. }

  127. ],

  128. yAxis: [

  129. {

  130. type: 'value',

  131. name: '得分',

  132. axisLabel: {

  133. formatter: '{value}',

  134. textStyle: {

  135. color: this.fontColorY, // 更改坐标轴文字颜色

  136. fontSize: 12, // 更改坐标轴文字大小

  137. fontFamily: 'MicrosoftYaHei'

  138. }

  139. },

  140. // 更改坐标轴颜色

  141. axisLine: {

  142. lineStyle: {

  143. color: "#FFFFFF"

  144. }

  145. },

  146. // 网格线

  147. splitLine: {

  148. // 网格线

  149. lineStyle: {

  150. type: 'solid',

  151. with: 0.5,

  152. color: this.borderColor

  153. }

  154. }

  155. }

  156. ],

  157. series: [

  158. // 数据低下的圆片

  159. {

  160. name: '',

  161. type: 'pictorialBar',

  162. symbolOffset: ['0%', '50%'],

  163. symbolSize: [this.barWidth - 4, (10 * (this.barWidth - 4)) / this.barWidth],

  164. z: 12,

  165. symbol: 'diamond',

  166. itemStyle: {

  167. opacity: 1,

  168. color: color

  169. // color: 'transparent'

  170. },

  171. data: this.bottomData

  172. },

  173. // 数据的柱状图

  174. {

  175. name: '数据',

  176. type: 'bar',

  177. barWidth: this.barWidth,

  178. itemStyle: {

  179. // lenged文本

  180. opacity: 1, // 这个是 透明度

  181. color: color

  182. },

  183. data: this.data1

  184. },

  185. // 替代柱状图 默认不显示颜色,是最下方柱图的value值 - 20

  186. {

  187. type: 'bar',

  188. symbol: 'diamond',

  189. barWidth: this.barWidth + 2,

  190. itemStyle: {

  191. color: 'transparent'

  192. },

  193. data: this.data1

  194. },

  195. // 数据顶部的样式

  196. {

  197. name: '',

  198. type: 'pictorialBar',

  199. symbol: 'diamond',

  200. symbolOffset: ['0%', '-50%'],

  201. symbolSize: [this.barWidth, 10],

  202. z: 12,

  203. itemStyle: {

  204. normal: {

  205. opacity: 1,

  206. color: this.colorArr[2],

  207. label: {

  208. show: true, // 开启显示

  209. position: 'top', // 在上方显示

  210. textStyle: {

  211. // 数值样式

  212. color: '#FFFFFF',

  213. fontSize: 12,

  214. top: 50

  215. }

  216. }

  217. }

  218. },

  219. symbolPosition: 'end',

  220. data: this.data1

  221. },

  222. // 阴影的顶部

  223. {

  224. name: '', // 头部

  225. type: 'pictorialBar',

  226. symbol: 'diamond',

  227. symbolOffset: ['0%', '-50%'],

  228. symbolSize: [this.barWidth, 10],

  229. z: 12,

  230. symbolPosition: 'end',

  231. itemStyle: {

  232. color: 'blue',

  233. opacity: 0.3,

  234. borderWidth: 1,

  235. borderColor: 'rgba(18, 47, 133,1)'

  236. },

  237. data: this.topData

  238. },

  239. // 后面的背景

  240. {

  241. name: '',

  242. type: 'bar',

  243. barWidth: this.barWidth,

  244. barGap: '-100%',

  245. z: 0,

  246. itemStyle: {

  247. color: 'rgba(18, 47, 133,0.3)'

  248. },

  249. data: this.topData

  250. }

  251. ]

  252. }

  253. )

  254. }

  255. }

  256. }

  257. </script>

三、第三种效果

效果图

使用步骤

完整实例,copy就可直接使用 

 
  1. <template>

  2. <div :class="className" :style="{height:height,width:width}" />

  3. </template>

  4. <script>

  5. import echarts from 'echarts'

  6. require('echarts/theme/macarons') // echarts theme

  7. import resize from '@/views/dashboard/mixins/resize'

  8. export default {

  9. mixins: [resize],

  10. props: {

  11. className: {

  12. type: String,

  13. default: 'chart'

  14. },

  15. width: {

  16. type: String,

  17. default: '100%'

  18. },

  19. height: {

  20. type: String,

  21. default: '400px'

  22. },

  23. dataList: {

  24. type: Array,

  25. default: []

  26. }

  27. },

  28. data() {

  29. return {

  30. chart: null,

  31. MAX: [800, 800, 800, 800, 800, 800, 800],

  32. NAME: [2015, 2016, 2017, 2018, 2019, 2020, 2021],

  33. VALUE: [200, 400, 300, 500, 700, 300, 100],

  34. VALUE2: [500, 200, 700, 400, 300, 600, 400],

  35. }

  36. },

  37. mounted() {

  38. this.$nextTick(() => {

  39. this.initChart()

  40. })

  41. },

  42. beforeDestroy() {

  43. if (!this.chart) {

  44. return

  45. }

  46. this.chart.dispose()

  47. this.chart = null

  48. },

  49. watch: {

  50. dataList(val, oldVal) {//普通的watch监听

  51. this.initChart()

  52. }

  53. },

  54. methods: {

  55. initChart() {

  56. const offsetX = 16;

  57. const offsetY = 8;

  58. [-18, 18].forEach((customOffset, index) => {

  59. const CubeLeft = echarts.graphic.extendShape({

  60. shape: {

  61. x: 0,

  62. y: 0,

  63. },

  64. buildPath: function (ctx, shape) {

  65. const xAxisPoint = shape.xAxisPoint;

  66. const c0 = [shape.x - customOffset, shape.y];

  67. const c1 = [shape.x - offsetX - customOffset, shape.y - offsetY];

  68. const c2 = [xAxisPoint[0] - offsetX - customOffset, xAxisPoint[1] - offsetY];

  69. const c3 = [xAxisPoint[0] - customOffset, xAxisPoint[1]];

  70. ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath();

  71. },

  72. });

  73. const CubeRight = echarts.graphic.extendShape({

  74. shape: {

  75. x: 0,

  76. y: 0,

  77. },

  78. buildPath: function (ctx, shape) {

  79. const xAxisPoint = shape.xAxisPoint;

  80. const c1 = [shape.x - customOffset, shape.y];

  81. const c2 = [xAxisPoint[0] - customOffset, xAxisPoint[1]];

  82. const c3 = [xAxisPoint[0] + offsetX - customOffset, xAxisPoint[1] - offsetY];

  83. const c4 = [shape.x + offsetX - customOffset, shape.y - offsetY];

  84. ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();

  85. },

  86. });

  87. const CubeTop = echarts.graphic.extendShape({

  88. shape: {

  89. x: 0,

  90. y: 0,

  91. },

  92. buildPath: function (ctx, shape) {

  93. const c1 = [shape.x - customOffset, shape.y];

  94. const c2 = [shape.x + offsetX - customOffset, shape.y - offsetY];

  95. const c3 = [shape.x - customOffset, shape.y - offsetX];

  96. const c4 = [shape.x - offsetX - customOffset, shape.y - offsetY];

  97. ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();

  98. },

  99. });

  100. echarts.graphic.registerShape('CubeLeft' + index, CubeLeft);

  101. echarts.graphic.registerShape('CubeRight' + index, CubeRight);

  102. echarts.graphic.registerShape('CubeTop' + index, CubeTop);

  103. });

  104. this.chart = echarts.init(this.$el, 'macarons')

  105. this.chart.setOption(

  106. {

  107. backgroundColor: '#012366',

  108. grid: {

  109. left: '1%',

  110. right: '8%',

  111. bottom: '5%',

  112. top: '8%',

  113. containLabel: true,

  114. },

  115. tooltip: {

  116. trigger: 'axis',

  117. axisPointer: { // 坐标轴指示器,坐标轴触发有效

  118. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'

  119. },

  120. formatter: function (e) {

  121. var str =

  122. e[2].axisValue +

  123. "<br>" +

  124. "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:" +

  125. "rgba(225,155,172, 1)" +

  126. ";'></span>" +

  127. "课程数量 : " +

  128. e[2].value +

  129. "<br>" +

  130. "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:" +

  131. "rgba(25,155,172, 1)" +

  132. ";'></span>" +

  133. "完成数量 : " +

  134. e[3].value;

  135. return str;

  136. },

  137. },

  138. legend: {

  139. orient: 'vertical',

  140. x: 'right',

  141. y: 'center',

  142. data: ['课程数量', '完成数量',]

  143. },

  144. xAxis: {

  145. type: 'category',

  146. data: this.NAME,

  147. name:"年份",

  148. axisLine: {

  149. show: true,

  150. lineStyle: {

  151. width: 2,

  152. color: '#7ca7d9',

  153. },

  154. },

  155. axisTick: {

  156. show: false,

  157. },

  158. axisLabel: {

  159. fontSize: 14,

  160. },

  161. },

  162. yAxis: {

  163. type: 'value',

  164. name:"数量",

  165. minInterval: 1,

  166. axisLine: {

  167. show: true,

  168. lineStyle: {

  169. width: 2,

  170. color: '#2B7BD6',

  171. },

  172. },

  173. splitLine: {

  174. show: true,

  175. lineStyle: {

  176. color: 'rgba(201,217,241,0.23)',

  177. },

  178. },

  179. axisTick: {

  180. show: false,

  181. },

  182. axisLabel: {

  183. fontSize: 14,

  184. },

  185. },

  186. dataZoom: [{

  187. show: true,

  188. height: 10,

  189. xAxisIndex: [

  190. 0

  191. ],

  192. bottom: 0,

  193. start: 10,

  194. end: 50,

  195. handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',

  196. handleSize: '110%',

  197. handleStyle: {

  198. color: "#d3dee5",

  199. },

  200. textStyle: {

  201. color: "#fff"

  202. },

  203. borderColor: "#90979c"

  204. }, {

  205. type: "inside",

  206. show: true,

  207. height: 15,

  208. start: 1,

  209. end: 10

  210. }],

  211. series: [

  212. {

  213. // 最大高度

  214. type: 'custom',

  215. renderItem: function (params, api) {

  216. const location = api.coord([api.value(0), api.value(1)])

  217. return {

  218. type: 'group',

  219. children: [{

  220. type: 'CubeLeft0',

  221. shape: {

  222. api,

  223. x: location[0],

  224. y: location[1],

  225. xAxisPoint: api.coord([api.value(0), 0])

  226. },

  227. style: {

  228. fill: `rgba(25,155,172, .1)`

  229. }

  230. }, {

  231. type: 'CubeRight0',

  232. shape: {

  233. api,

  234. x: location[0],

  235. y: location[1],

  236. xAxisPoint: api.coord([api.value(0), 0])

  237. },

  238. style: {

  239. fill: `rgba(25,155,172, .3)`

  240. }

  241. }, {

  242. type: 'CubeTop0',

  243. shape: {

  244. api,

  245. x: location[0],

  246. y: location[1],

  247. xAxisPoint: api.coord([api.value(0), 0])

  248. },

  249. style: {

  250. fill: `rgba(25,155,172, .4)`

  251. }

  252. }]

  253. }

  254. },

  255. data: this.MAX

  256. },

  257. {

  258. // 最大高度

  259. type: 'custom',

  260. renderItem: function (params, api) {

  261. const location = api.coord([api.value(0), api.value(1)])

  262. return {

  263. type: 'group',

  264. children: [{

  265. type: 'CubeLeft1',

  266. shape: {

  267. api,

  268. x: location[0],

  269. y: location[1],

  270. xAxisPoint: api.coord([api.value(0), 0])

  271. },

  272. style: {

  273. fill: `rgba(225,155,172, .1)`

  274. }

  275. }, {

  276. type: 'CubeRight1',

  277. shape: {

  278. api,

  279. x: location[0],

  280. y: location[1],

  281. xAxisPoint: api.coord([api.value(0), 0])

  282. },

  283. style: {

  284. fill: `rgba(225,155,172, .3)`

  285. }

  286. }, {

  287. type: 'CubeTop1',

  288. shape: {

  289. api,

  290. x: location[0],

  291. y: location[1],

  292. xAxisPoint: api.coord([api.value(0), 0])

  293. },

  294. style: {

  295. fill: `rgba(225,155,172, .4)`

  296. }

  297. }]

  298. }

  299. },

  300. data: this.MAX

  301. },

  302. {

  303. type: 'custom',

  304. renderItem: (params, api) => {

  305. const location = api.coord([api.value(0), api.value(1)]);

  306. return {

  307. type: 'group',

  308. children: [

  309. {

  310. type: 'CubeLeft0',

  311. shape: {

  312. api,

  313. xValue: api.value(0),

  314. yValue: api.value(1),

  315. x: location[0] - 0,

  316. y: location[1],

  317. xAxisPoint: api.coord([api.value(0), 0]),

  318. },

  319. style: {

  320. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  321. {

  322. offset: 0,

  323. color: 'rgba(25,155,172, .5)',

  324. },

  325. {

  326. offset: 1,

  327. color: 'rgba(25,155,172, .5)',

  328. },

  329. ]),

  330. },

  331. },

  332. {

  333. type: 'CubeRight0',

  334. shape: {

  335. api,

  336. xValue: api.value(0),

  337. yValue: api.value(1),

  338. x: location[0] - 0,

  339. y: location[1],

  340. xAxisPoint: api.coord([api.value(0), 0]),

  341. },

  342. style: {

  343. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  344. {

  345. offset: 0,

  346. color: 'rgba(25,155,172, 1)',

  347. },

  348. {

  349. offset: 1,

  350. color: 'rgba(25,155,172, .5)',

  351. },

  352. ]),

  353. },

  354. },

  355. {

  356. type: 'CubeTop0',

  357. shape: {

  358. api,

  359. xValue: api.value(0),

  360. yValue: api.value(1),

  361. x: location[0] - 0,

  362. y: location[1],

  363. xAxisPoint: api.coord([api.value(0), 0]),

  364. },

  365. style: {

  366. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  367. {

  368. offset: 0,

  369. color: 'rgba(25,155,172, .8)',

  370. },

  371. {

  372. offset: 1,

  373. color: 'rgba(25,155,172, .8)',

  374. },

  375. ]),

  376. },

  377. },

  378. ],

  379. };

  380. },

  381. data: this.VALUE,

  382. },

  383. {

  384. type: 'custom',

  385. renderItem: (params, api) => {

  386. const location = api.coord([api.value(0), api.value(1)]);

  387. return {

  388. type: 'group',

  389. children: [

  390. {

  391. type: 'CubeLeft1',

  392. shape: {

  393. api,

  394. xValue: api.value(0),

  395. yValue: api.value(1),

  396. x: location[0] - 0,

  397. y: location[1],

  398. xAxisPoint: api.coord([api.value(0), 0]),

  399. },

  400. style: {

  401. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  402. {

  403. offset: 0,

  404. color: 'rgba(225,155,172, .5)',

  405. },

  406. {

  407. offset: 1,

  408. color: 'rgba(225,155,172, .5)',

  409. },

  410. ]),

  411. },

  412. },

  413. {

  414. type: 'CubeRight1',

  415. shape: {

  416. api,

  417. xValue: api.value(0),

  418. yValue: api.value(1),

  419. x: location[0] - 0,

  420. y: location[1],

  421. xAxisPoint: api.coord([api.value(0), 0]),

  422. },

  423. style: {

  424. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  425. {

  426. offset: 0,

  427. color: 'rgba(225,155,172, 1)',

  428. },

  429. {

  430. offset: 1,

  431. color: 'rgba(225,155,172, .5)',

  432. },

  433. ]),

  434. },

  435. },

  436. {

  437. type: 'CubeTop1',

  438. shape: {

  439. api,

  440. xValue: api.value(0),

  441. yValue: api.value(1),

  442. x: location[0] - 0,

  443. y: location[1],

  444. xAxisPoint: api.coord([api.value(0), 0]),

  445. },

  446. style: {

  447. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  448. {

  449. offset: 0,

  450. color: 'rgba(225,155,172, .8)',

  451. },

  452. {

  453. offset: 1,

  454. color: 'rgba(225,155,172, .8)',

  455. },

  456. ]),

  457. },

  458. },

  459. ],

  460. };

  461. },

  462. data: this.VALUE2,

  463. },

  464. ],

  465. }

  466. )

  467. }

  468. }

  469. }

  470. </script>

四、第四种效果

效果图

使用步骤

完整实例,copy就可直接使用

 
  1. <template>

  2. <div :class="className" :style="{height:height,width:width}" />

  3. </template>

  4. <script>

  5. import echarts from 'echarts'

  6. require('echarts/theme/macarons') // echarts theme

  7. import resize from '@/views/dashboard/mixins/resize'

  8. export default {

  9. mixins: [resize],

  10. props: {

  11. className: {

  12. type: String,

  13. default: 'chart'

  14. },

  15. width: {

  16. type: String,

  17. default: '100%'

  18. },

  19. height: {

  20. type: String,

  21. default: '400px'

  22. },

  23. dataList: {

  24. type: Array,

  25. default: []

  26. }

  27. },

  28. data() {

  29. return {

  30. chart: null,

  31. targetData: [50, 60, 40, 80, 120, 90, 70],

  32. manualData: [22, 35, 30, 25, 12, 41, 51],

  33. mechanismData: [40, 45, 40, 55, 22, 46, 61],

  34. xData:['1月', '2月', '3月', '4月', '5月', '6月', '7月'],

  35. names:['手工', '机制', '目标值']

  36. }

  37. },

  38. mounted() {

  39. this.$nextTick(() => {

  40. this.initChart()

  41. })

  42. },

  43. beforeDestroy() {

  44. if (!this.chart) {

  45. return

  46. }

  47. this.chart.dispose()

  48. this.chart = null

  49. },

  50. watch: {

  51. dataList(val, oldVal) {//普通的watch监听

  52. this.initChart()

  53. }

  54. },

  55. methods: {

  56. initChart() {

  57. const color1 = {

  58. type: 'linear',

  59. x: 1,

  60. y: 0,

  61. x2: 1,

  62. y2: 1,

  63. colorStops: [

  64. {

  65. offset: 0,

  66. color: '#9DCAF4'

  67. },

  68. {

  69. offset: 0.8,

  70. color: '#0B87FB'

  71. }

  72. ]

  73. };

  74. const color2 = {

  75. type: 'linear',

  76. x: 1,

  77. y: 0,

  78. x2: 1,

  79. y2: 1,

  80. colorStops: [

  81. {

  82. offset: 0,

  83. color: '#31D5C7'

  84. },

  85. {

  86. offset: 0.8,

  87. color: 'rgba(29, 39, 115,0.2)'

  88. }

  89. ]

  90. };

  91. this.chart = echarts.init(this.$el, 'macarons')

  92. this.chart.setOption(

  93. {

  94. legend: {

  95. data: this.names,

  96. x: 'right',

  97. textStyle: {

  98. // 图例文字大小颜色

  99. fontSize: 12,

  100. color: '#ffffff'

  101. }

  102. },

  103. tooltip: {

  104. trigger: 'axis',

  105. axisPointer: {

  106. type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'

  107. }

  108. },

  109. grid: {

  110. left: '1%',

  111. right: '1%',

  112. bottom: '2%',

  113. top: '12%',

  114. containLabel: true

  115. },

  116. xAxis: {

  117. type: 'category',

  118. data: this.xData,

  119. axisLabel: {

  120. color: '#fff'

  121. },

  122. axisLine: {

  123. show: true,

  124. lineStyle: {

  125. color: 'rgba(255, 255, 2555, 0.1)'

  126. }

  127. }

  128. },

  129. yAxis: [

  130. {

  131. type: 'value',

  132. name: '制作数量',

  133. axisLabel: {

  134. color: '#fff',

  135. axisLabel: {

  136. color: '#fff',

  137. //y轴文字的配置

  138. textStyle: {

  139. color: "#fff",

  140. },

  141. },

  142. },

  143. splitLine: {

  144. show: true,

  145. lineStyle: {

  146. color: 'rgba(255,255,255,0.2)'

  147. }

  148. },

  149. nameTextStyle: {

  150. //y轴上方单位的颜色

  151. color: "#fff",

  152. },

  153. },

  154. {

  155. type: 'value',

  156. name: '目标值',

  157. min: 0,

  158. max: 250,

  159. interval: 50,

  160. axisLabel: {

  161. color: '#fff',

  162. //y轴文字的配置

  163. textStyle: {

  164. color: "#fff",

  165. },

  166. },

  167. splitLine: {

  168. show: false,

  169. lineStyle: {

  170. color: 'rgba(255,255,255,0.2)'

  171. }

  172. },

  173. nameTextStyle: {

  174. //y轴上方单位的颜色

  175. color: "#fff",

  176. },

  177. }

  178. ],

  179. series: [

  180. {

  181. itemStyle: {

  182. normal: {

  183. // 这里是用一个柱子,从左到右的渐变。也可以用两个柱子做从上往下的渐变,和上面的透明渐变一样用法

  184. color: color1

  185. }

  186. },

  187. data: this.manualData,

  188. type: 'bar',

  189. barWidth: 19,

  190. z: 2,

  191. name: this.names[0]

  192. },

  193. {

  194. z: 3,

  195. name: this.names[0],

  196. type: 'pictorialBar',

  197. // 柱子顶部

  198. symbolPosition: 'end',

  199. data: this.manualData,

  200. symbol: 'diamond',

  201. symbolOffset: ['-12', -11],

  202. symbolRotate: 90,

  203. symbolSize: [10, 21],

  204. itemStyle: {

  205. normal: {

  206. borderWidth: 1,

  207. color: 'blue'

  208. }

  209. },

  210. tooltip: {

  211. show: false

  212. }

  213. },

  214. {

  215. itemStyle: {

  216. normal: {

  217. // 这里是用一个柱子,从左到右的渐变。也可以用两个柱子做从上往下的渐变,和上面的透明渐变一样用法

  218. color: color2

  219. }

  220. },

  221. data: this.mechanismData,

  222. type: 'bar',

  223. barWidth: 19,

  224. z: 2,

  225. name: this.names[1]

  226. },

  227. {

  228. z: 3,

  229. name: this.names[1],

  230. type: 'pictorialBar',

  231. // 柱子顶部

  232. symbolPosition: 'end',

  233. data: this.mechanismData,

  234. symbol: 'diamond',

  235. symbolOffset: [12, -11],

  236. symbolRotate: 90,

  237. symbolSize: [8, 19],

  238. itemStyle: {

  239. normal: {

  240. borderWidth: 1,

  241. color: 'green'

  242. }

  243. },

  244. tooltip: {

  245. show: false

  246. }

  247. },

  248. {

  249. name: this.names[2],

  250. type: 'line',

  251. yAxisIndex: 1,

  252. data: this.targetData,

  253. itemStyle: {

  254. normal: {

  255. color: '#FFDE55', //圈圈的颜色

  256. lineStyle: {

  257. color: '#FFDE55' //线的颜色

  258. }

  259. }

  260. }

  261. }

  262. ]

  263. }

  264. )

  265. }

  266. }

  267. }

  268. </script>

五、第五种效果

效果图

使用步骤

完整实例,copy就可直接使用

 
  1. <template>

  2. <div :class="className" :style="{height:height,width:width}" />

  3. </template>

  4. <script>

  5. import echarts from 'echarts'

  6. require('echarts/theme/macarons') // echarts theme

  7. import resize from '@/views/dashboard/mixins/resize'

  8. export default {

  9. mixins: [resize],

  10. props: {

  11. className: {

  12. type: String,

  13. default: 'chart'

  14. },

  15. width: {

  16. type: String,

  17. default: '100%'

  18. },

  19. height: {

  20. type: String,

  21. default: '400px'

  22. },

  23. dataList: {

  24. type: Array,

  25. default: []

  26. },

  27. titleName: {

  28. type: String,

  29. default: 'echarts'

  30. },

  31. names: {

  32. type: Array,

  33. default: ['成本', '目标']

  34. }

  35. },

  36. data() {

  37. return {

  38. chart: null,

  39. sourceList: [],

  40. listData1: [],

  41. dataBottom: [],

  42. listData2: [],

  43. max: 0,

  44. maxDate1: 0,

  45. maxDate2: 0,

  46. }

  47. },

  48. mounted() {

  49. this.$nextTick(() => {

  50. this.initChart()

  51. })

  52. },

  53. beforeDestroy() {

  54. if (!this.chart) {

  55. return

  56. }

  57. this.chart.dispose()

  58. this.chart = null

  59. },

  60. watch: {

  61. dataList(val, oldVal) {//普通的watch监听

  62. this.initChart()

  63. }

  64. },

  65. methods: {

  66. initChart() {

  67. this.chart = echarts.init(this.$el, 'macarons')

  68. this.sourceList = []

  69. this.listData1 = []

  70. this.listData2 = []

  71. for (let i = 0; i < this.dataList.length; i++) {

  72. this.sourceList.push(this.dataList[i].techName)

  73. this.listData1.push((this.dataList[i].oneNum))

  74. this.dataBottom.push(1)

  75. this.listData2.push(this.dataList[i].twoNum)

  76. }

  77. this.sourceList = ["2024","2023","2022","2021","2020","2019","2018"]

  78. this.listData1 = [12,10,0,9,0,5,8]

  79. this.dataBottom = [1,1,1,1,1,1,1]

  80. this.listData2 = [11,12,15,6,22,15,10]

  81. //获取纵坐标最大值--start

  82. this.maxDate1 = this.listData1[0];

  83. for (let i = 0; i < this.listData1.length; i++) {

  84. let item = this.listData1[i];

  85. item > this.maxDate1 ? this.maxDate1 = item : this.maxDate1 = this.maxDate1;

  86. }

  87. this.maxDate2 = this.listData2[0];

  88. for (let i = 0; i < this.listData2.length; i++) {

  89. let item = this.listData2[i];

  90. item > this.maxDate2 ? this.maxDate2 = item : this.maxDate2 = this.maxDate2;

  91. }

  92. this.maxDate1 = this.maxDate1 + 1

  93. this.maxDate2 = this.maxDate2 + 1

  94. //获取纵坐标最大值--end

  95. this.chart.setOption({

  96. tooltip: {

  97. trigger: 'axis',

  98. axisPointer: {

  99. type: 'cross',

  100. label: {

  101. backgroundColor: '#6a7985'

  102. }

  103. },

  104. formatter: function (params) {

  105. var str = '<div style=" font-size: 16px;color:#FFFFFF">'

  106. str = str + params[0].name + '<br>'

  107. params.filter(function (item) {

  108. if (item.componentSubType === 'bar' || item.componentSubType === 'line') {

  109. str = str + item.seriesName + ':' + item.data + '<br>'

  110. }

  111. })

  112. str = str + '</div>'

  113. return str;

  114. }

  115. },

  116. toolbox: {

  117. feature: {

  118. dataView: { show: true, readOnly: true },

  119. magicType: { show: true, type: ['line', 'bar'] },

  120. restore: { show: true },

  121. saveAsImage: { show: true, backgroundColor: '#79B9F5', name: this.titleName }

  122. },

  123. right: '20px',

  124. },

  125. grid: {

  126. left: '3%',

  127. right: '3%',

  128. bottom: '3%',

  129. containLabel: true

  130. },

  131. legend: {

  132. data: this.names,

  133. left: '20px',

  134. textStyle: {

  135. color: '#FFFFFF'

  136. }

  137. },

  138. xAxis: [

  139. {

  140. type: 'category',

  141. data: this.sourceList,

  142. axisLine: {

  143. //x轴线的颜色以及宽度

  144. show: true,

  145. lineStyle: {

  146. color: "#FFFFFF",

  147. type: "solid",

  148. },

  149. },

  150. axisPointer: {

  151. type: 'shadow'

  152. },

  153. axisLabel: {

  154. show: true,

  155. rotate: 45, // 设置x轴标签旋转角度

  156. margin: 15

  157. }

  158. }

  159. ],

  160. yAxis: [

  161. {

  162. type: 'value',

  163. name: this.names[0],

  164. axisLabel: {

  165. //y轴文字的配置

  166. textStyle: {

  167. color: "#fff",

  168. },

  169. },

  170. axisLine: {

  171. //y轴线的颜色以及宽度

  172. show: true,

  173. lineStyle: {

  174. color: "#fff",

  175. width: 1,

  176. type: "solid",

  177. },

  178. },

  179. splitLine: {

  180. //分割线配置

  181. show: false,

  182. lineStyle: {

  183. color: "#fff",

  184. },

  185. },

  186. nameTextStyle: {

  187. //y轴上方单位的颜色

  188. color: "#fff",

  189. },

  190. max: this.maxDate1

  191. },

  192. {

  193. type: 'value',

  194. name: this.names[1],

  195. axisLabel: {

  196. //y轴文字的配置

  197. textStyle: {

  198. color: "#fff",

  199. },

  200. },

  201. axisLine: {

  202. //y轴线的颜色以及宽度

  203. show: true,

  204. lineStyle: {

  205. color: "#fff",

  206. width: 1,

  207. type: "solid",

  208. },

  209. },

  210. splitLine: {

  211. //分割线配置

  212. show: false,

  213. lineStyle: {

  214. color: "#fff",

  215. },

  216. },

  217. nameTextStyle: {

  218. //y轴上方单位的颜色

  219. color: "#fff",

  220. },

  221. //设置纵坐标最大值

  222. max: this.maxDate2

  223. },

  224. ],

  225. series: [

  226. {

  227. itemStyle: {

  228. normal: {

  229. // 这里是用一个柱子,从左到右的渐变。也可以用两个柱子做从上往下的渐变,和上面的透明渐变一样用法

  230. color: {

  231. type: 'linear',

  232. x: 1,

  233. y: 0,

  234. x2: 1,

  235. y2: 1,

  236. colorStops: [

  237. { offset: 0, color: '#83bff6' },

  238. { offset: 0.5, color: '#188df0' },

  239. { offset: 1, color: 'blue' }

  240. ]

  241. }

  242. }

  243. },

  244. data: this.listData1,

  245. type: 'bar',

  246. barWidth: 40,

  247. tooltip: {

  248. valueFormatter: function (value) {

  249. return value;

  250. }

  251. },

  252. z: 2,

  253. name: this.names[0]

  254. },

  255. // 底部的光晕

  256. {

  257. name: this.names[0],

  258. type: 'pictorialBar',

  259. tooltip: {

  260. show: false

  261. },

  262. symbolSize: [60, 20],

  263. symbolOffset: [0, 10],

  264. z: 1,

  265. itemStyle: {

  266. normal: {

  267. color: 'transparent',

  268. borderColor: '#26B2E8',

  269. borderType: 'solid',

  270. borderWidth: 2

  271. }

  272. },

  273. data: this.dataBottom

  274. },

  275. {

  276. //底部圆片

  277. name: this.names[0],

  278. type: 'pictorialBar',

  279. symbolSize: [40, 15],

  280. symbolOffset: [0, 5],

  281. z: 12,

  282. itemStyle: {

  283. opacity: 1,

  284. color: 'blue',

  285. borderColor: '#03529A'

  286. },

  287. data: this.dataBottom

  288. },

  289. //头部圆片

  290. {

  291. name: this.names[0], // 头部

  292. type: 'pictorialBar',

  293. symbolSize: [40, 15],

  294. symbolOffset: [0, -7],

  295. z: 3,

  296. symbolPosition: 'end',

  297. itemStyle: {

  298. color: '#188df0',

  299. opacity: 1

  300. },

  301. data: this.listData1

  302. },

  303. {

  304. name: this.names[1],

  305. type: 'line',

  306. z: 12,

  307. yAxisIndex: 1,

  308. tooltip: {

  309. valueFormatter: function (value) {

  310. return value;

  311. }

  312. },

  313. lineStyle: {

  314. normal: {

  315. width: 3 //折线宽度

  316. },

  317. },

  318. itemStyle: {

  319. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

  320. { offset: 0, color: '#93CE07' },

  321. { offset: 0.2, color: '#FBDB0F' },

  322. { offset: 0.4, color: '#FC7D02' },

  323. { offset: 0.6, color: '#FD0100' },

  324. { offset: 0.8, color: '#AA069F' },

  325. { offset: 1, color: '#AC3B2A' }

  326. ])

  327. },

  328. data: this.listData2

  329. }

  330. ]

  331. })

  332. }

  333. }

  334. }

  335. </script>

六、3D饼图实现

vue中如何使用echarts和echarts-gl实现3D饼图环形饼图_echarts 3d饼图-CSDN博客

七、Echarts:象形柱图实现水塔水位的动画、水球图和液位柱子图 

Echarts:象形柱图实现水塔水位的动画、水球图和液位柱子图_echarts html 液位柱子图-CSDN博客

八、3D折线图实现

vue中如何使用echarts和echarts-gl实现三维折线图-CSDN博客

resize.js 

 
  1. import { debounce } from '@/utils'

  2. export default {

  3. data() {

  4. return {

  5. $_sidebarElm: null,

  6. $_resizeHandler: null

  7. }

  8. },

  9. mounted() {

  10. this.initListener()

  11. },

  12. activated() {

  13. if (!this.$_resizeHandler) {

  14. // avoid duplication init

  15. this.initListener()

  16. }

  17. // when keep-alive chart activated, auto resize

  18. this.resize()

  19. },

  20. beforeDestroy() {

  21. this.destroyListener()

  22. },

  23. deactivated() {

  24. this.destroyListener()

  25. },

  26. methods: {

  27. // use $_ for mixins properties

  28. // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential

  29. $_sidebarResizeHandler(e) {

  30. if (e.propertyName === 'width') {

  31. this.$_resizeHandler()

  32. }

  33. },

  34. initListener() {

  35. this.$_resizeHandler = debounce(() => {

  36. this.resize()

  37. }, 100)

  38. window.addEventListener('resize', this.$_resizeHandler)

  39. this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]

  40. this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)

  41. },

  42. destroyListener() {

  43. window.removeEventListener('resize', this.$_resizeHandler)

  44. this.$_resizeHandler = null

  45. this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)

  46. },

  47. resize() {

  48. const { chart } = this

  49. chart && chart.resize()

  50. }

  51. }

  52. }

防抖函数 

 
  1. /**

  2. * @param {Function} fn 防抖函数

  3. * @param {Number} delay 延迟时间

  4. */

  5. export function debounce(fn, delay) {

  6. var timer;

  7. return function () {

  8. var context = this;

  9. var args = arguments;

  10. clearTimeout(timer);

  11. timer = setTimeout(function () {

  12. fn.apply(context, args);

  13. }, delay);

  14. };

  15. }

文章知识点与官方知识档案匹配,可进一步学习相关知识

Vue入门技能树首页概览42665 人正在系统学习中

关注博主即

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现echarts3D柱状图,可以参考以下步骤: 1. 首先,你可以查看echarts官方网站,了解echarts自定义图形的详细用法。\[1\] 2. 参考你提供的案例,可以创建左右两个面,也就是两个柱子。为了形成一点对比,可以稍微调整左右两个面的颜色。同时,可以使用"type: 'pictorialBar'"来实现底部和顶部的效果。\[2\] 3. 在代码中,可以设置柱状图的柱宽(barWidth)为一个固定值,例如30。如果改动这个值导致柱子形状变形,可以调整series中每一个对象的symbolOffset的值。\[3\] 4. 对于双柱状图,可以在series数组中定义多个元素,前几个元素表示"本期"柱状图,后几个元素表示"同期"柱状图。每个柱子都由上中下三部分组成,因此需要定义多个series元素来表示柱子的主体部分、底部和顶部。为了达到整体性的效果,每个柱状图的三部分颜色需要统一。\[3\] 通过以上步骤,你可以实现echarts3D柱状图。希望对你有帮助! #### 引用[.reference_title] - *1* [echarts实现3d柱状图的两种方式](https://blog.csdn.net/weixin_69773376/article/details/128199545)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [echarts 3d柱状图](https://blog.csdn.net/xuyanl/article/details/128011860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [echarts实现3D效果柱状图](https://blog.csdn.net/dqfkkll/article/details/120287486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值