• echarts设置竖向不同区间范围颜色,并且x轴自定义轴线刻度范围


    需求:设置竖向范围区间,不同范围区间颜色不同并且提示信息不同,之后修改x轴的固定间距范围,让0-200-400-600改为0-340-476-754这种,在这我是markLine的方式实现的,这边我还用到x轴的翻转所以显示的是镜像的。

    1.效果

    2.主要代码

    inverse:翻转轴线

    type必须为value,不然图片显示不出来,其他就是把x轴线给隐藏了,之后再用markLine去手动画一条线

    1. xAxis: {
    2. inverse: true,
    3. type: 'value',
    4. axisTick: { show: false }, // 隐藏刻度线
    5. splitLine: { show: false }, // 隐藏分割线
    6. axisLabel: { show: false } // 隐藏刻度值
    7. },

    使用markLine画线,data就是线的间隔数据, 

    lineStyle: { color: 'black', type: 'dotted' },线的颜色和类型,可选solid(实线),dashed(虚线),dotted(斑点虚线)

    1. markLine: {
    2. symbol: false, // 取消箭头
    3. silent: true, // 取消鼠标hover事件
    4. label: {
    5. position: 'start', // 改变label位置
    6. formatter: (obj) => obj.value // 格式化标签文本
    7. },
    8. lineStyle: { color: 'black', type: 'dotted' },
    9. data: [0, 530, 580, 630].map((val) => {
    10. return {
    11. xAxis: val
    12. };
    13. })
    14. },

    注意这个data,data的值是530-580之间的差值,差50value就写50,写多了图就错了。。。

    1. {
    2. name: '',
    3. tooltip: {
    4. trigger: 'item',
    5. formatter: '\n围岩级别:IV
      \n。'
    6. },
    7. type: 'bar',
    8. stack: 'total',
    9. itemStyle: {
    10. color: 'green'
    11. },
    12. label: {
    13. show: true,
    14. formatter: 'IV',
    15. color: 'black'
    16. },
    17. emphasis: {
    18. focus: 'series'
    19. },
    20. data: [
    21. {
    22. value: 50,
    23. xAxis: 1
    24. }
    25. ]
    26. },

    3.完整代码

    1. <script>
    2. export default {
    3. name: 'LineECharts', // 折线图
    4. data() {
    5. return {};
    6. },
    7. methods: {
    8. drawLine() {
    9. // 获取当前日期
    10. // let nowTime = new Date().toLocaleDateString().split('/').join('-');
    11. // 基于准备好的dom,初始化echarts实例
    12. let myChartOne = this.$echarts.init(document.getElementById('LineECharts'));
    13. // 绘制图表
    14. myChartOne.setOption({
    15. tooltip: {
    16. trigger: 'item',
    17. axisPointer: {
    18. type: 'shadow'
    19. }
    20. },
    21. grid: {
    22. left: '3%',
    23. right: '4%',
    24. bottom: '3%',
    25. containLabel: true
    26. },
    27. xAxis: {
    28. inverse: true,
    29. type: 'value',
    30. axisTick: { show: false }, // 隐藏刻度线
    31. splitLine: { show: false }, // 隐藏分割线
    32. axisLabel: { show: false } // 隐藏刻度值
    33. },
    34. yAxis: {
    35. type: 'category',
    36. data: ['级别']
    37. },
    38. series: [
    39. {
    40. name: '',
    41. tooltip: {
    42. trigger: 'item',
    43. formatter: '\n 围岩级别:III
      \n'
    44. },
    45. type: 'bar',
    46. stack: 'total',
    47. itemStyle: {
    48. color: '#ddd'
    49. },
    50. label: {
    51. show: true,
    52. formatter: 'III',
    53. color: 'black'
    54. },
    55. emphasis: {
    56. focus: 'series'
    57. },
    58. data: [
    59. {
    60. value: 530,
    61. xAxis: 0
    62. }
    63. ],
    64. markLine: {
    65. symbol: false, // 取消箭头
    66. silent: true, // 取消鼠标hover事件
    67. label: {
    68. position: 'start', // 改变label位置
    69. formatter: (obj) => obj.value // 格式化标签文本
    70. },
    71. lineStyle: { color: 'black', type: 'dotted' },
    72. data: [0, 530, 580, 630].map((val) => {
    73. return {
    74. xAxis: val
    75. };
    76. })
    77. }
    78. },
    79. {
    80. name: '',
    81. tooltip: {
    82. trigger: 'item',
    83. formatter: '\n围岩级别:IV
      \n。'
    84. },
    85. type: 'bar',
    86. stack: 'total',
    87. itemStyle: {
    88. color: 'green'
    89. },
    90. label: {
    91. show: true,
    92. formatter: 'IV',
    93. color: 'black'
    94. },
    95. emphasis: {
    96. focus: 'series'
    97. },
    98. data: [
    99. {
    100. value: 50,
    101. xAxis: 1
    102. }
    103. ]
    104. },
    105. {
    106. name: '',
    107. tooltip: {
    108. trigger: 'item',
    109. formatter: '\n 围岩级别:V
      \n 。'
    110. },
    111. type: 'bar',
    112. stack: 'total',
    113. itemStyle: {
    114. color: '#333'
    115. },
    116. label: {
    117. show: true,
    118. formatter: 'V',
    119. color: 'black'
    120. },
    121. emphasis: {
    122. focus: 'series'
    123. },
    124. data: [
    125. {
    126. value: 50,
    127. xAxis: 2
    128. }
    129. ]
    130. }
    131. ]
    132. });
    133. window.onresize = () => {
    134. // 根据窗口大小变化图表自适应
    135. myChartOne.resize();
    136. };
    137. }
    138. },
    139. mounted() {
    140. this.drawLine();
    141. }
    142. };
    143. script>

    4.其他补充

    这个是鼠标移动后显示的内容的,可以自定义内容,也可以设置弹窗距离鼠标的位置等

    1. // 鼠标划入的弹出框
    2. tooltip: {
    3. trigger: 'axis',
    4. axisPointer: {
    5. // 坐标轴指示器,坐标轴触发有效
    6. type: this.colunm.type ? this.colunm.type : 'shadow' // 默认为阴影,可选为:'line' | 'shadow'
    7. },
    8. textStyle: {
    9. color: this.colunm.tooltipcolor,
    10. fontFamily: '等线'
    11. // fontSize:'0.1rem'
    12. },
    13. borderColor: this.colunm.tooltipborderColor,
    14. backgroundColor: this.colunm.tooltipbackgroundColor,
    15. position: function (p) {
    16. //其中p为当前鼠标的位置
    17. return [p[0] + 10, p[1] - 60]
    18. },
    19. // textStyle: {
    20. // fontSize: 12 // 字体大小
    21. // }
    22. // confine: true,
    23. // extraCssText: 'white-space: normal; word-break: break-all;',
    24. confine: true, //限制tooltip在图表范围内展示
    25. // extraCssText: 'max-height:100%;overflow:scroll', //最大高度以及超出处理
    26. // enterable: true, //鼠标可以进入tooltip区域,使用滚动条
    27. // position: [100, 10]// position: ['10px', '10px']
    28. formatter: function (params) {
    29. // 提示内容太多隔行显示内容
    30. let astr = ''
    31. params.forEach((ele, index) => {
    32. astr += `
    33. ${
    34. index % 2 === 0
    35. ? 'width: 45%;'
    36. : 'width: 38%;'
    37. }float:left;">
    38. ${
    39. ele.color
    40. };border-radius: 10px;">
    41. ${ele.seriesName}: ${ele.data}
  • `
  • })
  • const b = '
    ' + astr + '
    '
  • return b
  • }
  • },
  • 文章到此结束,希望对你有所帮助~~

  • 相关阅读:
    [机缘参悟-72]:深度思考-人生自省的四重境界:不觉、自觉、觉他、圆满
    Mysql 读写分离
    第三章:使用Vue脚手架
    《机械原理》上 学后感
    kotlin类与对象
    奇迹MU架设常见问题解决
    我操作MySQL的惊险一幕
    配置Android Studio问题总结
    Qt-OpenCV学习笔记--膨胀--dilate()
    基于ssm设备租赁报修借用管理系统java项目源码
  • 原文地址:https://blog.csdn.net/qq_44278289/article/details/133928180