当前位置:   article > 正文

vue中使用echarts记录属性3立体柱形图_vue + echarts 立体柱状图

vue + echarts 立体柱状图

环境:vue2 + element-ui

echarts版本:5.4.0

安装:

npm install echarts --save

需要的页面引用:

import * as echarts from 'echarts';

 先上一个效果图(有借鉴copy网上的内容,忘了哪个了)

页面准备容器

<div id="fftjChart"></div>

js部分,我是在父组件中拿到的数据,传递给子组件

  1. <script>
  2. import * as echarts from "echarts";
  3. export default {
  4. // 组件传参,拿到父组件传递的数据
  5. props: {
  6. greenTraffic: {
  7. type: Array,
  8. default() {
  9. return [];
  10. },
  11. },
  12. },
  13. data() {
  14. return {
  15. arrX: [],
  16. arrY: [],
  17. };
  18. },
  19. watch: {
  20. // 侦听父组件的数据,发生变化后处理数据后重新渲染dom
  21. greenTraffic: {
  22. handler(val) {
  23. this.arrX = [];
  24. this.arrY = [];
  25. if (Array.isArray(val) && val.length) {
  26. val.forEach((item) => {
  27. this.arrX.push(item.trafficKey);
  28. this.arrY.push((Number(item.trafficValue) / 10000).toFixed(2));
  29. });
  30. }
  31. this.drawFftjChart();
  32. },
  33. deep: true,
  34. },
  35. },
  36. mounted() {
  37. this.drawFftjChart();
  38. },
  39. methods: {
  40. drawFftjChart() {
  41. let yData = this.arrY;
  42. var fftjChart = echarts.init(document.getElementById("fftjChart"));
  43. fftjChart.setOption({
  44. grid: { // 容器距离
  45. left: "5%",
  46. right: "5%",
  47. // top: "5%",
  48. bottom: "5%",
  49. containLabel: true,
  50. },
  51. tooltip: {
  52. trigger: "item",
  53. formatter: function (parms) {
  54. return (
  55. parms.marker + " " + parms.name + ":" + parms.value + "万辆"
  56. );
  57. },
  58. },
  59. xAxis: {
  60. type: "category", // category(坐标轴类型)
  61. data: this.arrX,
  62. axisTick: {
  63. // 坐标轴刻度相关配置
  64. show: true, // 是否显示坐标轴刻度
  65. },
  66. axisLine: {
  67. // 坐标轴轴线相关配置
  68. lineStyle: {
  69. // 坐标轴轴线样式
  70. color: "#666666", // 坐标轴轴线颜色
  71. },
  72. },
  73. axisLabel: {
  74. // 坐标轴刻度标签相关配置
  75. color: "#fff",
  76. fontSize: 14,
  77. margin: 20,
  78. },
  79. },
  80. yAxis: {
  81. type: "value", // value(数值轴,适用于连续数据)
  82. name: "单位:万辆",
  83. nameTextStyle: {
  84. color: "#fff",
  85. fontSize: 12,
  86. },
  87. axisTick: {
  88. // 坐标轴刻度相关配置
  89. show: true, // 是否显示坐标轴刻度
  90. },
  91. axisLine: {
  92. // 坐标轴轴线相关配置
  93. show: true, // 是否显示坐标轴轴线
  94. },
  95. axisLabel: {
  96. // 坐标轴刻度标签相关配置
  97. color: "#fff",
  98. fontSize: 14,
  99. },
  100. splitLine: {
  101. // 坐标轴在 grid 区域中的分隔线
  102. lineStyle: {
  103. // 分割线配置
  104. color: "rgba(255,255,255,0.15)", // 分割线颜色
  105. },
  106. },
  107. },
  108. series: [
  109. // 底部的椭圆形(象形柱图):pictorialBar
  110. {
  111. type: "pictorialBar", // pictorialBar(象形柱图)
  112. label: {
  113. // 图形上的文本标签,可用于说明图像的一些数据信息,比如值,名称等
  114. // show: true, //是否显示标签
  115. position: ["17", "-30"], // 标签的位置(可以是绝对的像素值或者百分比['50%','50%',也可以是top,left等])
  116. color: "#01E4FF",
  117. fontSize: 14,
  118. },
  119. symbolSize: [30, 20], // 图形的大小用数组分别比表示宽和高,也乐意设置成10相当于[10,10]
  120. symbolOffset: [0, 10], // 图形相对于原本位置的偏移
  121. z: 12, // 象形柱状图组件的所有图形的 z 值.控制图形的前后顺序.z 值小的图形会被 z 值大的图形覆盖.
  122. itemStyle: {
  123. // 图形样式
  124. // echarts.graphic.LinearGradient(echarts内置的渐变色生成器)
  125. // 4个参数用于配置渐变色的起止位置,这4个参数依次对应右 下 左 上
  126. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  127. // 这里 offset: 0 1 ,表示从下往上的渐变色
  128. {
  129. offset: 0, // 0%处的颜色
  130. color: "rgba(31,155,255,1)",
  131. },
  132. {
  133. offset: 1, // 100%处的颜色
  134. color: "#1F61EA",
  135. },
  136. ]),
  137. },
  138. data: yData,
  139. },
  140. // 中间的长方形柱状图(柱状图):bar
  141. {
  142. type: "bar", // 柱状图
  143. barWidth: 30, // 柱条的宽度,不设时自适应
  144. barGap: "0%", // 柱子与柱子之间的距离
  145. itemStyle: {
  146. // 图形样式
  147. // color支持(rgb(255,255,255)、rgba(255,255,255,1)、#fff,也支持渐变色和纹理填充)
  148. // 下面就是使用线性渐变
  149. color: {
  150. x: 0,
  151. y: 0,
  152. x2: 0,
  153. y2: 1,
  154. type: "linear",
  155. global: false,
  156. colorStops: [
  157. {
  158. offset: 0, // 0%处的颜色
  159. color: "rgba(34,68,172,0.9)",
  160. },
  161. {
  162. offset: 1, // 100%处的颜色
  163. color: "rgba(0,183,255,0.8)",
  164. },
  165. ],
  166. },
  167. },
  168. data: yData,
  169. },
  170. // 顶部的椭圆形(象形柱图):pictorialBar
  171. {
  172. type: "pictorialBar",
  173. symbolSize: [30, 20],
  174. symbolOffset: [0, -10],
  175. z: 12,
  176. symbolPosition: "end",
  177. itemStyle: {
  178. color: new echarts.graphic.LinearGradient(
  179. 0,
  180. 0,
  181. 0,
  182. 1,
  183. [
  184. {
  185. offset: 0,
  186. color: "rgba(31,155,255,0.8)",
  187. },
  188. {
  189. offset: 1,
  190. color: "rgba(0,229,255,0.5)",
  191. },
  192. ],
  193. false
  194. ),
  195. },
  196. data: yData,
  197. },
  198. ],
  199. });
  200. window.addEventListener("resize", () => {
  201. fftjChart.resize();
  202. });
  203. },
  204. },
  205. };
  206. </script>

下一篇整理,3D饼状图

工作记录,有不足之处还请见谅,还望指出,感谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/834710
推荐阅读
相关标签
  

闽ICP备14008679号