echarts图例的显示与隐藏

﹏ヽ暗。殇╰゛Y 2023-03-04 15:29 106阅读 0赞

需求

在图表加载的时候,默认只显示部分图例,只要图例显示部分,对应的柱图或者折线图也只显示对应的。点击一个按钮显示剩余的图例,或者点击置灰的图例让恢复显示。

核心

在legend里面的字段加个selected,然后设置不需要显示的数据标题:
以官方案例为例:

  1. selected: {'邮件营销': false, '联盟广告': false}

使用按钮切换可以动态改变selected里面对象的键值即可:

  1. this.options.legend.selected = {'邮件营销': false, '联盟广告': false};

效果

  1. 默认不展示折线
    在这里插入图片描述
  2. 代码

    1. option = {
    2. backgroundColor: '#031A32',
    3. tooltip: {
    4. trigger: "axis",
    5. axisPointer: {
    6. type: "shadow",
    7. label: {
    8. show: true
    9. }
    10. }
    11. },
    12. grid: {
    13. left: "4%",
    14. top: "18%",
    15. right: "5%",
    16. bottom: "22%"
    17. },
    18. legend: {
    19. data: ["昨日总人数", "今日实时人数", "昨日使用率"],
    20. top: "4%",
    21. textStyle: {
    22. color: "#1FC3CE",
    23. fontSize: 14
    24. },
    25. selected: { '昨日使用率': false} // 不需要显示的设置为false
    26. },
    27. xAxis: {
    28. data: [
    29. "会议室1",
    30. "会议室2",
    31. "会议室3",
    32. "会议室4",
    33. "会议室5",
    34. "会议室6",
    35. "会议室7",
    36. "会议室8",
    37. "会议室9",
    38. "会议室10",
    39. "会议室11",
    40. "会议室12"
    41. ],
    42. axisLine: {
    43. show: true, //隐藏X轴轴线
    44. lineStyle: {
    45. color: "#3d5269",
    46. width: 1
    47. }
    48. },
    49. axisTick: {
    50. show: true, //隐藏X轴刻度
    51. alignWithLabel: true
    52. },
    53. axisLabel: {
    54. show: true,
    55. textStyle: {
    56. color: "#396A87", //X轴文字颜色
    57. fontSize: 14
    58. },
    59. interval: 0,
    60. rotate: 30
    61. }
    62. },
    63. yAxis: [{
    64. type: "value",
    65. name: "人数",
    66. nameTextStyle: {
    67. color: "#396A87",
    68. fontSize: 14
    69. },
    70. splitLine: {
    71. show: true,
    72. lineStyle: {
    73. width: 1,
    74. color: "#3d5269"
    75. }
    76. },
    77. axisTick: {
    78. show: false
    79. },
    80. axisLine: {
    81. show: false
    82. },
    83. axisLabel: {
    84. show: true,
    85. textStyle: {
    86. color: "#396A87",
    87. fontSize: 14
    88. }
    89. }
    90. },
    91. {
    92. type: "value",
    93. name: "使用率%",
    94. nameTextStyle: {
    95. color: "#396A87",
    96. fontSize: 14
    97. },
    98. position: "right",
    99. splitLine: {
    100. show: false
    101. },
    102. axisTick: {
    103. show: false
    104. },
    105. axisLine: {
    106. show: false,
    107. lineStyle: {
    108. color: "#396A87",
    109. width: 2
    110. }
    111. },
    112. axisLabel: {
    113. show: true,
    114. formatter: "{value} %", //右侧Y轴文字显示
    115. textStyle: {
    116. color: "#396A87",
    117. fontSize: 14
    118. }
    119. }
    120. }
    121. ],
    122. series: [{
    123. name: "昨日总人数",
    124. type: "bar",
    125. barWidth: 18,
    126. itemStyle: {
    127. normal: {
    128. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
    129. offset: 0,
    130. color: "#00FFFF"
    131. },
    132. {
    133. offset: 1,
    134. color: "#0080FF"
    135. }
    136. ])
    137. }
    138. },
    139. data: [24, 45, 43, 35, 76, 154, 86, 42, 68, 97, 24, 34]
    140. },
    141. {
    142. name: "今日实时人数",
    143. type: "bar",
    144. barWidth: 18,
    145. itemStyle: {
    146. normal: {
    147. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
    148. offset: 0,
    149. color: "#E29052"
    150. },
    151. {
    152. offset: 1,
    153. color: "#FA5A53"
    154. }
    155. ])
    156. }
    157. },
    158. data: [133, 23, 114, 67, 89, 35, 67, 96, 90, 46, 75, 85]
    159. },
    160. {
    161. name: "昨日使用率",
    162. type: "line",
    163. yAxisIndex: 1, //使用的 y 轴的 index,在单个图表实例中存在多个 y轴的时候有用
    164. showAllSymbol: true, //显示所有图形。
    165. symbol: "circle", //标记的图形为实心圆
    166. symbolSize: 6, //标记的大小
    167. itemStyle: {
    168. //折线拐点标志的样式
    169. color: "#26D9FF",
    170. borderColor: "#26D9FF",
    171. width: 2,
    172. shadowColor: "#26D9FF",
    173. shadowBlur: 2
    174. },
    175. lineStyle: {
    176. color: "#26D9FF",
    177. width: 2,
    178. shadowBlur: 2
    179. },
    180. data: [4.2, 3.5, 2.9, 7.8, 2, 3, 4.2, 3.5, 2.9, 7.8, 2, 3]
    181. }
    182. ]
    183. }
  3. 点击显示效果
    在这里插入图片描述
  4. 也可以动态改变selected的值

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读