定制小程序开发费用【ECharts】调用接口获取后端数据的四种方法

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

目录



方法一:在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中的

  1. <script>
  2. import * as echarts from 'echarts'
  3. import { getStatistics } from '@/api/home'
  4. export default {
  5.   data () {
  6.     return {
  7.       mainData: [],//折线图数据
  8.     }
  9.   },
  10.   mounted () {
  11.      this.chartSetting();
  12.   },
  13.   created () {
  14.     this.CeData()
  15.   },
  16.   methods: {
  17.     // 返回数据
  18.     async CeData () {
  19.       const { data } = await getStatistics()
  20.       this.mainData = data.mainData
  21.     },
  22.     // 折现图
  23.     chartSetting () {
  24.       // 基于准备好的dom,初始化echarts实例
  25.       this.mainChart = echarts.init(document.getElementById('main'))
  26.       const option = {
  27.         tooltip: {
  28.           trigger: 'axis',
  29.           axisPointer: {
  30.             type: 'cross',
  31.             label: {
  32.               backgroundColor: '#6a7985'
  33.             }
  34.           }
  35.         },
  36.         dataset: [ // 数据
  37.           {  source: this.mainData // 表数据 },
  38.           { transform: {
  39.               type: 'sort'
  40.             }
  41.           }
  42.         ],
  43.         xAxis: [
  44.           {
  45.             type: 'category',
  46.             boundaryGap: false,
  47.             axisLabel: { // 底部文字设置
  48.               interval: 0, // 控制是否全部显示
  49.               fontSize: 12
  50.             },
  51.             axisLine: { // 底部横线
  52.               show: false
  53.             },
  54.             axisTick: { // 刻度线
  55.               show: false
  56.             }
  57.           }
  58.         ],
  59.         yAxis: [
  60.           { type: 'value' }
  61.         ],
  62.         series: [
  63.           {
  64.             name: '项目',
  65.             type: 'line',
  66.             stack: 'Total',
  67.             smooth: true,
  68.             lineStyle: {  width: 1,   color: '#2e3192' },
  69.             showSymbol: false,
  70.             label: {  show: true,  position: 'top' },
  71.             areaStyle: {
  72.               opacity: 0.8,
  73.               color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  74.                 {  offset: 0,  color: '#62a0f8' },
  75.                 {  offset: 1, color: '#b5d5ff' }
  76.               ])
  77.             },
  78.             markPoint: { // 最大值和最小值标注
  79.               data: [
  80.                 { type: 'max', name: '最大值' }
  81.               ]
  82.             },
  83.             emphasis: {  focus: 'series' }
  84.           }
  85.         ]
  86.       }
  87.       // 绘制图表
  88.       this.mainChart.setOption(option)
  89.       const that = this
  90.       window.addEventListener('resize', function () {
  91.         that.mainChart.resize()
  92.       })
  93.     },
  94.   }
  95. }
  96. </script>



 

方法四:在对应图表中,用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.     },

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

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发