echarts如何调用wx.request返回数据中的某字段数据?
发布于 3 年前 作者 pjin 2661 次浏览 来自 官方Issues

js文件(需要把series中的三个data字段替换成我数据库中的三个字段)

import * as echarts from '../../ec-canvas/echarts';

const app = getApp();

function initChart(canvas, width, height, dpr{
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    tooltip: {
      trigger'axis',
      axisPointer: {            // 坐标轴指示器,坐标轴触发有效
        type'shadow'        // 默认为直线,可选为:'line' | 'shadow'
      },
      confinetrue
    },
    legend: {
      data: ['热度''正面''负面']
    },
    grid: {
      left20,
      right20,
      bottom15,
      top40,
      containLabeltrue
    },
    xAxis: [
      {
        type'value',
        axisLine: {
          lineStyle: {
            color'#999'
          }
        },
        axisLabel: {
          color'#666'
        }
      }
    ],
    yAxis: [
      {
        type'category',
        axisTick: { showfalse },
        data: ['汽车之家''今日头条''百度贴吧''一点资讯''微信''微博''知乎'],
        axisLine: {
          lineStyle: {
            color'#999'
          }
        },
        axisLabel: {
          color'#666'
        }
      }
    ],
    series: [
      {
        name'热度',
        type'bar',
        label: {
          normal: {
            showtrue,
            position'inside'
          }
        },
        data: [300270340344300320310],
        itemStyle: {
          // emphasis: {
          //   color: '#37a2da'
          // }
        }
      },
      {
        name'正面',
        type'bar',
        stack'总量',
        label: {
          normal: {
            showtrue
          }
        },
        data: [120102141174190250220],
        itemStyle: {
          // emphasis: {
          //   color: '#32c5e9'
          // }
        }
      },
      {
        name'负面',
        type'bar',
        stack'总量',
        label: {
          normal: {
            showtrue,
            position'left'
          }
        },
        data: [-20-32-21-34-90-130-110],
        itemStyle: {
          // emphasis: {
          //   color: '#67e0e3'
          // }
        }
      }
    ]
  };

  chart.setOption(option);
  return chart;
}

Page({
  onShareAppMessagefunction (res{
    return {
      title'ECharts 可以在微信小程序中使用啦!',
      path'/pages/index/index',
      successfunction () { },
      failfunction () { }
    }
  },
  data: {
    ec: {
      onInit: initChart
    }
  },
  onLoadfunction (options{
    var that = this;//把this对象复制到临时变量that
    const wxreq = wx.request({
      url'http://123.23.5.12/data/xxx.php',
      data:{
        //id:"1",
        //name:'Leanne Graham'
      },
      header: {
        'content-type''application/json' // 默认值
      },
      successfunction (res){
        console.log(res.data);
        that.userData = res.data; //无效不能实时的渲染到页面
        that.setData({ userData: res.data });//和页面进行绑定可以动态的渲染到页面
    
      },
      failfunction (res){
        console.log(res.data);
        this.userData = "数据获取失败";
      }
    })
  
  },
  onReady() {
  }
});

数据库中的字段名为,date2,yuegangluo,yuegangpan,lpjc(需要后面3个)

调试输出结果为:

但是我不会调用到前面的data中,查了很多资料,新手,越看越迷糊,求点拨迷津,感谢~

2 回复

initChart

这个函数是可以写到pages里的,和onLoad同级,请求完成之后直接调用,把数据传进去就ok了

大佬,我换了个方式,直接在initChart给option里的series赋值,定义了X轴和series值,目前都正常可以显示了,tooltip也正常了,就是Y轴变成了2条折现上限的累加了。

js文件

import * as echarts from '../../ec-canvas/echarts';

const app = getApp();

function initChart(canvas, width, height, dpr{
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  //var that = this;//把this对象复制到临时变量that
  const wxreq = wx.request({
    url'http://121.60.40.43/data/bar2.php',
    data:{
      //id:"1",
      //name:'Leanne Graham'
    },
    header: {
      'content-type''application/json' // 默认值
    },
    successfunction (res){
      console.log(res.data) 
      const date2=[];
     // const lpjc=[];
      const yuegangpan=[];
      const yuegangluo=[];
      for(var i=0;i
回到顶部