8000 微信小程序wx-charts自适应屏幕宽度的问题 · Issue #4 · xiaolin3303/wx-charts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
微信小程序wx-charts自适应屏幕宽度的问题 #4
Open
@xiaolin3303

Description

@xiaolin3303

由于无法获取到canvas的大小尺寸,故图表工具需要调用者传入widthheight来主动告知。

各类终端的屏幕大小有差异,如何来让wx-charts自适应屏幕的尺寸呢,下面给出一种方法。

比如我们的需求是图表横向铺满屏幕的宽度

wxss

.canvas {
    width: 100%;
    height: 200px;
}

wxml

<view>
    <canvas canvas-id="wxChartCanvas" class="canvas"></canvas>
</view>

js

// 调用API getSystemInfoSync 获取设备信息
// 这里使用同步获取,也可以使用异步方式 getSystemInfo

......

let windowWidth = 320;
try {
    let res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
} catch (e) {
    // do something when get system info failed
}
new Charts({
    ...
    width: windowWidth,
    height: 200
});

同样的,如果需求中canvas与屏幕有一定的间距,还是先获取屏幕的宽度然后减去间距等计算出画布的实际宽度、高度传递给wx-charts即可。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0