• 【ECharts】调用接口获取后端数据的四种方法


    使用eacharts做大屏,需要使用后端数据,下面的方法是自己试过有效的,有什么不对的,望各位大佬指点。

    目录

    方法一:在mounted中使用定时器调用eacharts方法(定时器可以获取到data中的数据)

    方法二:在调用数据的时候调用图表(一个页面的所有数据都在这一个接口中)

    方法三:使用chartes中的dataset数据集

    方法四:在对应图表中,用ajax请求


    方法一:在mounted中使用定时器调用eacharts方法(定时器可以获取到data中的数据)

    1. mounted () {
    2. setTimeout(() => {
    3. this.axisOption() // 树状
    4. this.pieOption()//饼图
    5. }, 2000)
    6. },
    7. //或者
    8. mounted () {
    9. setTimeout(async () => {
    10. const res = await Getwx()
    11. this.Monthlist = res.Data.Monthlist
    12. this.Visitpvlist = res.Data.Visitpvlist
    13. this.drawLine();//柱状图
    14. }, 0);
    15. },

    方法二:在调用数据的时候调用图表(一个页面的所有数据都在这一个接口中)

    1. created () {
    2. this.GetProjectAll ()
    3. },
    4. methods: {
    5. // 获取数据
    6. async GetProjectAll () {
    7. const res = await GetProjectAll({ projectid: this.formdata.type })
    8. this.tableData = res.data.jrgrsl.data
    9. this.pieData = res.data.clbp.data
    10. this.$nextTick(() => {
    11. this.axisOption() // 树状
    12. this.pieOption()//饼图
    13. })
    14. },
    15. }

    方法三:使用chartes中的dataset数据集

    方法四:在对应图表中,用ajax请求

    1. drawLine2 () {
    2. var chartDom = document.getElementById('main2');
    3. var myChart = echarts.init(chartDom);
    4. var option;
    5. option = {
    6. tooltip: {
    7. trigger: 'axis',
    8. axisPointer: {
    9. type: 'cross',
    10. crossStyle: {
    11. color: '#999'
    12. }
    13. }
    14. },
    15. grid: {
    16. left: "11%",
    17. width: "80%",
    18. height: "60%"
    19. },
    20. legend: [{
    21. data: ['单位: 秒'],
    22. top: "10",
    23. left: "10",
    24. itemWidth: 8,
    25. itemHeight: 8,
    26. icon: "rect",
    27. textStyle: {
    28. color: "#fff"
    29. }
    30. }, {
    31. data: ['增速%'],
    32. top: "10",
    33. right: "5%",
    34. itemWidth: 8,
    35. itemHeight: 8,
    36. icon: "rect",
    37. textStyle: {
    38. color: "#fff"
    39. }
    40. }],
    41. xAxis: [
    42. {
    43. type: 'category',
    44. data: [],
    45. axisPointer: {
    46. type: 'shadow'
    47. },
    48. axisTick: {
    49. show: false
    50. },
    51. axisLabel: {
    52. interval: 0,
    53. textStyle: {
    54. color: '#b8b8ba',
    55. },
    56. }
    57. }
    58. ],
    59. yAxis: [
    60. {
    61. type: 'value',
    62. min: 0,
    63. max: 10000,
    64. interval: 2000,
    65. axisLabel: {
    66. formatter: function (value) {
    67. return value + ""
    68. },
    69. textStyle: {
    70. color: '#b8b8ba',
    71. },
    72. },
    73. axisLine: {
    74. show: true
    75. },
    76. axisTick: {
    77. show: false
    78. },
    79. splitLine: {
    80. show: true,
    81. lineStyle: {
    82. width: 0.5
    83. }
    84. },
    85. symbol: "triangle"
    86. },
    87. {
    88. type: 'value',
    89. min: 0.4,
    90. max: 0.9,
    91. interval: 0.1,
    92. axisLabel: {
    93. formatter: '{value}',
    94. textStyle: {
    95. color: '#b8b8ba',
    96. },
    97. },
    98. splitLine: {
    99. show: true,
    100. lineStyle: {
    101. width: 0.5
    102. }
    103. },
    104. }
    105. ],
    106. series: [
    107. {
    108. name: '单位: 秒',
    109. type: 'bar',
    110. data: [],
    111. itemStyle: {
    112. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    113. { offset: 0, color: '#01c7f4' },
    114. { offset: 1, color: '#003fe2' }
    115. ]),
    116. borderRadius: 8
    117. },
    118. barWidth: 10
    119. },
    120. {
    121. name: '增速%',
    122. type: 'line',
    123. areaStyle: {},
    124. yAxisIndex: 1,
    125. data: [],
    126. itemStyle: {
    127. color: "#77ff3b",
    128. },
    129. lineStyle: {
    130. width: 1
    131. },
    132. symbolSize: 7,
    133. areaStyle: {
    134. opacity: 0.4,
    135. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    136. { offset: 1, color: '#040d0c' },
    137. { offset: 0, color: '#5cd62c' }
    138. ])
    139. },
    140. }
    141. ]
    142. };
    143. const zoomSize = 6;
    144. myChart.on('click', function (params) {
    145. console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
    146. myChart.dispatchAction({
    147. type: 'dataZoom',
    148. startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
    149. endValue:
    150. dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
    151. });
    152. });
    153. option && myChart.setOption(option);
    154. $.ajax({
    155. type: "get",
    156. // async: false, //同步执行
    157. url: "api/WxStatistics/GetStatisticsData",
    158. data: {},
    159. success: function (result) {
    160. myChart.setOption({
    161. xAxis: { data: result.Data.Monthlist },
    162. series: [
    163. {
    164. data: result.Data.Staytimeuvlist,
    165. },
    166. {
    167. data: [0.6, 0.65, 0.65, 0.68, 0.58, 0.61, 0.58, 0.6, 0.61, 0.65, 0.63, 0.55],
    168. }
    169. ]
    170. })
    171. },
    172. error: function (errorMsg) {
    173. alert("不好意思,图表请求数据失败啦!");
    174. myChart.hideLoading();
    175. }
    176. })
    177. window.addEventListener("resize", function () {
    178. myChart.resize();
    179. });
    180. },

    注意
    如果一个图表需要展示不同数据时,每获取一次数据,图表都会重新渲染一次(例如下拉框中选取数据后,图表切换对应数据)。
    可能会出现There is a chart instance already initialized on the dom.这样的警告,意思是dom上已经初始化了一个图表实例。
    解决办法:可以在每次渲染前先销毁这个实例,然后再重新渲染。

    1. var myChart //先注册全局变量
    2. axisOption () {
    3. //在方法内判断,然后销毁实例,然后再初始化
    4. if (myChart != null && myChart != "" && myChart != undefined) {
    5. myChart.dispose();//销毁
    6. }
    7. // 基于准备好的dom,初始化echarts实例
    8. myChart = echarts.init(document.getElementById('axisMain'))
    9. const option = { }
    10. // 绘制图表
    11. myChart.setOption(option)
    12. window.addEventListener('resize', function () {
    13. myChart.resize()
    14. })
    15. },

     

     

    到这里就结束啦,这篇文章看完如果您觉得有所收获,认为还行的话,就点个赞收藏一下

  • 相关阅读:
    C# HTML
    无锁队列Disruptor使用笔记
    上位机使用JS SerialPort进行串口通信, 包含开发环境搭建和完整示例代码
    Docker、Jenkins 结合 SonarQube 和 Sonar scanner 进行代码质量扫描
    Java程序员进阶全过程
    【第三章-1】面向对象——类与对象基本概念
    DenseNet的基本思想
    独立站卖家投放Facebook广告的六大常见误区
    递归算法(1163~1167)(未完结)
    SpringCloud 简单的了解
  • 原文地址:https://blog.csdn.net/weixin_57031986/article/details/136235245