• Echarts 实现两两柱图重叠(背景和实际值柱图)


    Echarts实现两两重叠柱状图_echarts 重叠柱状图_Web_阿凯的博客-CSDN博客

    引用启发的博客

    先来效果:

    1. option = {
    2. backgroundColor: '#03213D',
    3. animation: true, // 控制动画是否开启
    4. animationDuration: 1000, // 动画的时长, 它是以毫秒为单位
    5. animationDuration: function (arg) {
    6. return 1000 * arg;
    7. },
    8. animationEasing: 'bounceOut', //linear 缓动动画
    9. animationThreshold: 8, // 动画元素的阈值
    10. tooltip: {
    11. trigger: 'axis',
    12. backgroundColor: 'rgba(0,0,0,.6)',
    13. borderColor: 'rgba(147, 235, 248, 0)',
    14. textStyle: {
    15. color: '#FFF'
    16. }
    17. },
    18. grid: {
    19. top: '10%',
    20. bottom: '5%',
    21. left: '3%',
    22. right: '8%',
    23. containLabel: true
    24. },
    25. xAxis: [
    26. {
    27. data: ['焦二', '焦三', '焦四'],
    28. axisLine: {
    29. show: true, //隐藏X轴轴线
    30. lineStyle: {
    31. color: '#163a5f',
    32. width: 2
    33. }
    34. },
    35. axisTick: {
    36. show: false, //隐藏X轴刻度
    37. alignWithLabel: true
    38. },
    39. axisLabel: {
    40. show: true,
    41. textStyle: {
    42. color: 'rgb(255,255,255,.7)',
    43. fontSize: 16,
    44. fontWeight: 'bold'
    45. },
    46. interval: 0,
    47. formatter: function (value) {
    48. var ret = ''; //拼接加\n返回的类目项
    49. var maxLength = 4; //每项显示文字个数
    50. var valLength = value.length; //X轴类目项的文字个数
    51. var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
    52. if (rowN > 1) {
    53. //如果类目项的文字大于5,
    54. for (var i = 0; i < rowN; i++) {
    55. var temp = ''; //每次截取的字符串
    56. var start = i * maxLength; //开始截取的位置
    57. var end = start + maxLength; //结束截取的位置
    58. //这里也可以加一个是否是最后一行的判断,但是不加也没有影响,那就不加吧
    59. temp = value.substring(start, end) + '\n';
    60. ret += temp; //凭借最终的字符串
    61. }
    62. return ret;
    63. } else {
    64. return value;
    65. }
    66. }
    67. }
    68. },
    69. {
    70. data: ['焦二', '焦三', '焦四'],
    71. show:false,
    72. axisLine: {
    73. show: false, //隐藏X轴轴线
    74. lineStyle: {
    75. color: '#163a5f',
    76. width: 2
    77. }
    78. },
    79. axisTick: {
    80. show: false, //隐藏X轴刻度
    81. alignWithLabel: true
    82. },
    83. axisLabel: {
    84. show: true,
    85. textStyle: {
    86. color: 'rgb(255,255,255,.7)',
    87. fontSize: 16,
    88. fontWeight: 'bold'
    89. },
    90. interval: 0,
    91. formatter: function (value) {
    92. var ret = ''; //拼接加\n返回的类目项
    93. var maxLength = 4; //每项显示文字个数
    94. var valLength = value.length; //X轴类目项的文字个数
    95. var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
    96. if (rowN > 1) {
    97. //如果类目项的文字大于5,
    98. for (var i = 0; i < rowN; i++) {
    99. var temp = ''; //每次截取的字符串
    100. var start = i * maxLength; //开始截取的位置
    101. var end = start + maxLength; //结束截取的位置
    102. //这里也可以加一个是否是最后一行的判断,但是不加也没有影响,那就不加吧
    103. temp = value.substring(start, end) + '\n';
    104. ret += temp; //凭借最终的字符串
    105. }
    106. return ret;
    107. } else {
    108. return value;
    109. }
    110. }
    111. }
    112. }
    113. ],
    114. yAxis: {
    115. type: 'value',
    116. // name: "单位:ml",
    117. nameTextStyle: {
    118. color: '#BDD8FB',
    119. fontSize: 12
    120. },
    121. splitLine: {
    122. show: true,
    123. lineStyle: {
    124. color: 'rgba(255, 255, 255, 0.15)',
    125. type: 'dashed' // dotted 虚线
    126. }
    127. },
    128. axisTick: {
    129. show: false
    130. },
    131. axisLine: {
    132. show: true, //隐藏X轴轴线
    133. lineStyle: {
    134. color: '#163a5f',
    135. width: 1
    136. }
    137. },
    138. axisLabel: {
    139. show: true,
    140. textStyle: {
    141. color: 'rgba(207,227,252,0.6)',
    142. fontSize: 12
    143. }
    144. }
    145. },
    146. series: [
    147. {
    148. name: '值 - 全焦产量',
    149. type: 'bar',
    150. barWidth: 30,
    151. itemStyle: {
    152. color: function (params) {
    153. return {
    154. type: 'linear',
    155. x: 0, //右
    156. y: 0, //下
    157. x2: 0, //左
    158. y2: 1, //上
    159. colorStops: [
    160. {
    161. offset: 0.01,
    162. color: '#ffffff' // 0% 处的颜色
    163. },
    164. {
    165. offset: 0.1,
    166. color: '#13D5FC'
    167. },
    168. {
    169. offset: 1,
    170. color: 'transparent' // 100% 处的颜色
    171. }
    172. ]
    173. };
    174. // }
    175. },
    176. barBorderRadius: [8, 8, 0, 0]
    177. },
    178. label: {
    179. show: true,
    180. position: 'top',
    181. distance: 0,
    182. color: 'rgb(0,255,255)',
    183. formatter: '{c}'
    184. },
    185. data: [35, 33, 65]
    186. },
    187. {
    188. name: '背景 - 全焦产量',
    189. type: 'bar',
    190. barWidth: '30px',
    191. xAxisIndex: 1,
    192. data: [100, 100, 100], //背景阴影长度
    193. itemStyle: {
    194. normal: {
    195. color: 'rgba(255,255,255,0.06)',
    196. barBorderRadius: [0, 0, 0, 0],
    197. borderColor: 'rgb(33,156,251)'
    198. }
    199. },
    200. tooltip: {
    201. show: false
    202. },
    203. zlevel: 9
    204. },
    205. {
    206. name: '值 - 冶金焦产量',
    207. type: 'bar',
    208. barWidth: 30,
    209. barGap: '100%',
    210. itemStyle: {
    211. color: function (params) {
    212. return {
    213. type: 'linear',
    214. x: 0, //右
    215. y: 0, //下
    216. x2: 0, //左
    217. y2: 1, //上
    218. colorStops: [
    219. {
    220. offset: 0.01,
    221. color: '#ffffff' // 0% 处的颜色
    222. },
    223. {
    224. offset: 0.1,
    225. color: '#13D5FC'
    226. },
    227. {
    228. offset: 1,
    229. color: 'transparent' // 100% 处的颜色
    230. }
    231. ]
    232. };
    233. // }
    234. },
    235. barBorderRadius: [8, 8, 0, 0]
    236. },
    237. label: {
    238. show: true,
    239. position: 'top',
    240. distance: 0,
    241. color: 'rgb(0,255,255)',
    242. formatter: '{c}'
    243. },
    244. data: [25, 33, 65]
    245. },
    246. {
    247. name: '背景 - 冶金焦产量',
    248. type: 'bar',
    249. xAxisIndex: 1,
    250. barWidth: '30px',
    251. barGap: '100%',
    252. data: [100, 100, 100], //背景阴影长度
    253. itemStyle: {
    254. normal: {
    255. color: 'rgba(255,255,255,0.06)',
    256. barBorderRadius: [0, 0, 0, 0],
    257. borderColor: 'rgb(33,156,251)'
    258. }
    259. },
    260. tooltip: {
    261. show: false
    262. },
    263. zlevel: 9
    264. }
    265. ]
    266. };

    源码直接粘贴到Echarts官网即可运行

    重要的是多个X轴,一个Y轴,使背景的都使用同一个X轴,实际值柱图使用另外一个X轴,然后每个使用X轴的同一系列的最后一个series属性中,将barGap属性调整一致即可。

  • 相关阅读:
    Java.lang.Byte类之parseByte()方法的简介说明
    洛谷P2763 试题库问题
    YOLO家族再度升级——阿里达摩院DAMO-YOLO重磅来袭
    WebRTC视频采集模块
    B+树索引(11)之索引挑选(上)
    聊聊HttpClient的DnsResolver
    Zone 和 Zoneset 是什么关系
    万字长文:从计算机本源深入探寻volatile和Java内存模型
    Android入门第5天-LinerLayout
    el-table表格设置——动态修改表头
  • 原文地址:https://blog.csdn.net/qq_31671169/article/details/134454768