Giter Club home page Giter Club logo

Comments (14)

simaQ avatar simaQ commented on May 12, 2024

使用什么版本的 F2?

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

@simaQ 您好,我是直接在https://github.com/antvis/wx-f2.git clone下去的最新

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

那请你贴下源码吧

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

好的 稍等

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

创建 chart 的时候需要声明 width 和 height,看这个报错应该是没有声明或者宽高没有读取到

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

我看了下报错位置 是在这个位置报错的,我就怕是 生命周期的问题 document没有

 getStyle: function getStyle(el, property) {
    return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  },

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

恩,我知道,我在图表组件里面是查找到 canvas 组件之后,才读取 width 和 height 的,https://github.com/antvis/wx-f2/blob/master/f2-canvas/f2-canvas.js#L89,

所以你看下是不是你写代码的时候漏了声明 width 和 height, https://github.com/antvis/wx-f2/blob/master/pages/charts/column/index.js#L16。

如果你担心生命周期的问题,你自己处理下就行了。或者声明 lazyLoad: true, 参考这个例子,延迟加载:

https://github.com/antvis/wx-f2/blob/master/pages/charts/k/index.js#L14
https://github.com/antvis/wx-f2/blob/master/pages/charts/k/index.js#L26
https://github.com/antvis/wx-f2/blob/master/pages/charts/k/index.js#L27

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

组件代码(路径我做了一点调整):

import F2 from '../../../f2-canvas/lib/f2';

let chart = null;

function initChart(canvas, width, height) { // 使用 F2 绘制图表
  // const data = [
  //   { year: '1956', sales: [34,42] },
  //   { year: '1957', sales: 43 },
  //   { year: '1958', sales: 48 },
  //   { year: '1959', sales: 54 },
  //   { year: '1960', sales: 75 },
  //   { year: '1962', sales: 170 },
  // ];
  const data = [{
    name: 'London',
    月份: 'Jan.',
    月均降雨量: 18.9
  }, {
    name: 'London',
    月份: 'Feb.',
    月均降雨量: 28.8
  }, {
    name: 'London',
    月份: 'Mar.',
    月均降雨量: 39.3
  }, {
    name: 'Berlin',
    月份: 'Jan.',
    月均降雨量: 99.7
  }, {
    name: 'Berlin',
    月份: 'Feb.',
    月均降雨量: 52.6
  }, {
    name: 'Berlin',
    月份: 'Mar.',
    月均降雨量: 35.5
  }];
  chart = new F2.Chart({
    el: canvas,
    width,
    height
  });
  chart.source(data);
  // chart.source(data, {
  //   value: {
  //     tickCount: 5
  //   }
  // });
  chart.tooltip({
    showItemMarker: false,
    onShow(ev) {
      const { items } = ev;
      items[0].name = null;
      items[0].name = items[0].title;
      items[0].value = '¥ ' + items[0].value;
    }
  });
  //chart.interval().position('year*sales');
  chart.interval().position('月份*月均降雨量').color('name').adjust({
    type: 'dodge',
    marginRatio: 0.05 // 设置分组间柱子的间距
  });
  chart.render();
  return chart;
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    items: [1, 2, 3, 4, 5],
    opts: {
      onInit: initChart
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
   
  }
})

组件json

{
  "component": true,
  "usingComponents": {
    "ff-canvas": "/../../../f2-canvas/f2-canvas"
  }
}

组件wxml

<view class="container">
    <block wx:key='id' wx:for='0'>
      <ff-canvas class='aa' height='220px' width='123px' canvas-id="column" opts="{{ opts }}"></ff-canvas>
      <view class='split'>测试一下</view>
    </block>
</view>

引用json (位置做了调整)

{
  "usingComponents": {
    "x-column": "/components/charts/x-column/index"
  }
}

**引用wxml **

<view class="item">
      <x-column></x-column>
</view>

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

你是自己又封装了一个组件了吧?那这样吧,你调试下是不是组件 init 的时候 width 和 height 没有拿到,关键代码在这里:

https://github.com/antvis/wx-f2/blob/master/f2-canvas/f2-canvas.js#L93

这个报错原因就是因为 width 和 height 为空导致的。微信小程序的组件生命周期我也不是特别熟...

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

是的 我是在上面又封装了一层 ;我跟踪了下 在以下代码处的输出宽高都是有的...

query.select('.f2-canvas').boundingClientRect(res => {
        console.log(canvas);
        console.log(res);
        if (typeof callback === 'function') {
          this.chart = callback(canvas, res.width, res.height);
        } else if (this.data.opts && this.data.opts.onInit) {
          this.chart = this.data.opts.onInit(canvas, res.width, res.height);
        }
      }).exec();
    },
bottom
:
493
dataset
:
{}
height
:
0
id
:
""
left
:
31
right
:
383
top
:
493
width
:
352

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

height 是 0 哦。。。

那你试着给 canvas 组件加下样式看下,参考:https://github.com/antvis/wx-f2/blob/master/app.wxss#L16

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

哈哈 老眼昏花 。。 不过给了还是不行...
谢谢你帮我回答这么多,我自己再看一下,万分感谢!

ps:如果你有时间你在上面也封装一个 看看能不能重现吧 。。哈哈

from wx-f2.

simaQ avatar simaQ commented on May 12, 2024

我试了一下,是可以的呢,基于你贴出代码的基础上,添加了 x-column.wxss 文件

from wx-f2.

KarainYang avatar KarainYang commented on May 12, 2024

@simaQ 感谢感谢!只要在container样式中加一个position: absolute; 就好了 ,你去掉的话就报错...

from wx-f2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.