微信小程序Echarts—折线图

Echarts官方文档:http://www.echartsjs.com/examples/

在这里插入图片描述
Echars文件:

 链接:https://pan.baidu.com/s/1NGKs4JKTPOiWjtrFpI6pDw 
 提取码:472z 

wxml:

 <ec-canvas id="businessTrend" class='mychart-businessTrend' canvas-id="mychart-businessTrend" ec="{{ line_ec }}"></ec-canvas>

json:

"usingComponents": {
    "ec-canvas": "../../lib/ec-canvas/ec-canvas"
 }

js:

import * as echarts from '../../lib/ec-canvas/echarts';
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    line_ec: {
      lazyLoad: true // 延迟加载
    }, //折线图
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    this.linechartsComponnet = this.selectComponent('#businessTrend'); //折线图
  },

  //初始化图表--折线图
  init_lineCharts: function() {
    this.linechartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const lineChart = echarts.init(canvas, null, {
        width: width,
        height: height
      });

      lineChart.setOption(this.getLineOption());

      //此处为折线图的点击事件,点击展示折点信息
      lineChart.on('click', function(handler, context) {
        var handlerValue = handler.name + ' :  ' + handler.value
        wx.showToast({
          title: handlerValue,
          icon: 'none',
          duration: 1200,
          mask: true
        })
      });
      return lineChart;
    });
  },

  /**
   * 折线图
   */
  getLineOption: function() {
    var option = {
      title: {
        text: '营业趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
        },
      },
      toolbox: {
        show: true,
        feature: {
          mark: {
            show: true
          },
          dataView: {
            show: true,
            readOnly: false
          },
          restore: {
            show: true
          },
          saveAsImage: {
            show: true
          }
        }
      },
      legend: {
        data: ['营业趋势']
      },
      grid: {
        left: '3%',
        bottom: '3%',
        containLabel: true
      },
      toolbox: {
        show: true,
        feature: {
          mark: {
            show: true
          },
          dataView: {
            show: true,
            readOnly: false
          },
          magicType: {
            show: true,
            type: ['line', 'bar', 'stack', 'tiled']
          },
          restore: {
            show: true
          },
          saveAsImage: {
            show: true
          }
        }
      },
      calculable: true,
      xAxis: [{
        type: 'category',
        boundaryGap: false,
        data: this.data.consumeDay,//此处为数组
        axisLabel: {
          interval: this.data.interval, //x轴间隔多少显示刻度
          fontSize: 8,
        }
      }],
      yAxis: [{
        type: 'value',
        axisLine: {
          show: true
        },
        axisTick: {
          show: true
        },
      }],
      series: [{
        name: '营业趋势',
        type: 'line',
        smooth: true,
        center: ['100%', '100%'],
        itemStyle: {
          normal: {
            color: '#56cbff',
            fontSize: '80',
            lineStyle: {
              color: '#56cbff'
            },
            areaStyle: {
              color: 'rgb(230,249,252)'
            },
          }
        },
        data: this.data.consumeAmount,//此处为数组
      }]
    };
    return option;
  },
});
  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
微信小程序中使用 ECharts 绘制折线图并实现点击折点显示数据,首先你需要在页面中引入 ECharts 库,并在 WXML 中创建图表容器。以下是一个简单的步骤: 1. 引入 ECharts 和所需模块: 在你的项目中,通常需要在 `index.js` 或相关模块中引入 ECharts 和其他依赖库: ```javascript import wx from 'wx'; import echarts from '@vant/weapp/echarts'; ``` 2. 初始化 ECharts 实例: 在 WXML 文件(如 `chart.wxml`)中创建一个 `view` 用于存放图表: ```html <view id="chartContainer" style="height: 400rpx;"></view> ``` 在对应的 JS 文件中初始化图表: ```javascript Page({ data: { chart: null, seriesData: [], }, onReady: function () { this.initChart(); }, initChart: function () { const chartContainer = wx.createSelectorQuery().select('#chartContainer'); chartContainer.nodes.style.cssText = 'width: 100%; height: 100%;'; this.chart = echarts.init(chartContainer); // ... 接下来设置图表配置和数据 }, }); ``` 3. 设置折线图配置和数据: ```javascript initChart: function () { // ... (假设你已经有了 seriesData 数据) const option = { xAxis: { data: this.seriesData.map(item => item.name) }, // x轴数据 yAxis: {}, // y轴配置 series: [{ type: 'line', data: this.seriesData.map(item => item.value) }], // 折线系列配置 onClick: this.handlePointClick, // 添加点击事件处理函数 }; this.chart.setOption(option); }, handlePointClick: function (params) { const { name, value } = params.data; // 获取点击的点名称和值 wx.showModal({ title: '点击详情', content: `点名:${name}\n值:${value}`, showCancel: false, }); }, ``` 4. 更新数据或重绘图表(如果需要动态数据): 当数据更新后,调用 `this.chart.setOption()` 方法重新设置图表选项。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值