Skip to content

Commit 17af058

Browse files
committed
feat: save canvas to temp file path
#73
1 parent aaee801 commit 17af058

File tree

8 files changed

+187
-5
lines changed

8 files changed

+187
-5
lines changed

app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"pages/themeRiver/index",
2323
"pages/lazyLoad/index",
2424
"pages/multiCharts/index",
25-
"pages/move/index"
25+
"pages/move/index",
26+
"pages/saveCanvas/index"
2627
],
2728
"window":{
2829
"backgroundTextStyle":"light",

ec-canvas/ec-canvas.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import WxCanvas from './wx-canvas';
22
import * as echarts from './echarts';
33

4+
let ctx;
5+
46
Component({
57
properties: {
68
canvasId: {
@@ -41,9 +43,9 @@ Component({
4143
return;
4244
}
4345

44-
const ctx = wx.createCanvasContext(this.data.canvasId, this);
46+
ctx = wx.createCanvasContext(this.data.canvasId, this);
4547

46-
const canvas = new WxCanvas(ctx);
48+
const canvas = new WxCanvas(ctx, this.data.canvasId);
4749

4850
echarts.setCanvasCreator(() => {
4951
return canvas;
@@ -60,6 +62,16 @@ Component({
6062
}).exec();
6163
},
6264

65+
canvasToTempFilePath(opt) {
66+
if (!opt.canvasId) {
67+
opt.canvasId = this.data.canvasId;
68+
}
69+
70+
ctx.draw(true, () => {
71+
wx.canvasToTempFilePath(opt, this);
72+
});
73+
},
74+
6375
touchStart(e) {
6476
if (!this.data.ec.disableTouch && this.chart && e.touches.length > 0) {
6577
var touch = e.touches[0];

ec-canvas/wx-canvas.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default class WxCanvas {
2-
constructor(ctx, domId, opts) {
2+
constructor(ctx, canvasId) {
33
this.ctx = ctx;
4-
this.opts = opts || {};
4+
this.canvasId = canvasId;
55
this.chart = null;
66

77
// this._initCanvas(zrender, ctx);
@@ -15,6 +15,14 @@ export default class WxCanvas {
1515
}
1616
}
1717

18+
// canvasToTempFilePath(opt) {
19+
// if (!opt.canvasId) {
20+
// opt.canvasId = this.canvasId;
21+
// }
22+
23+
// return wx.canvasToTempFilePath(opt, this);
24+
// }
25+
1826
setChart(chart) {
1927
this.chart = chart;
2028
}

pages/index/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ Page({
7474
}, {
7575
id: 'move',
7676
name: '页面不阻塞滚动'
77+
}, {
78+
id: 'saveCanvas',
79+
name: '保存 Canvas 到本地文件'
7780
}]
7881
},
7982

pages/saveCanvas/index.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import * as echarts from '../../ec-canvas/echarts';
2+
3+
let chart = null;
4+
5+
function initChart(canvas, width, height) {
6+
chart = echarts.init(canvas, null, {
7+
width: width,
8+
height: height
9+
});
10+
canvas.setChart(chart);
11+
12+
var option = {
13+
color: ['#37a2da', '#32c5e9', '#67e0e3'],
14+
tooltip: {
15+
trigger: 'axis',
16+
axisPointer: { // 坐标轴指示器,坐标轴触发有效
17+
type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
18+
}
19+
},
20+
legend: {
21+
data: ['热度', '正面', '负面']
22+
},
23+
grid: {
24+
left: 20,
25+
right: 20,
26+
bottom: 15,
27+
top: 40,
28+
containLabel: true
29+
},
30+
xAxis: [
31+
{
32+
type: 'value',
33+
axisLine: {
34+
lineStyle: {
35+
color: '#999'
36+
}
37+
},
38+
axisLabel: {
39+
color: '#666'
40+
}
41+
}
42+
],
43+
yAxis: [
44+
{
45+
type: 'category',
46+
axisTick: { show: false },
47+
data: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信', '微博', '知乎'],
48+
axisLine: {
49+
lineStyle: {
50+
color: '#999'
51+
}
52+
},
53+
axisLabel: {
54+
color: '#666'
55+
}
56+
}
57+
],
58+
series: [
59+
{
60+
name: '热度',
61+
type: 'bar',
62+
label: {
63+
normal: {
64+
show: true,
65+
position: 'inside'
66+
}
67+
},
68+
data: [300, 270, 340, 344, 300, 320, 310],
69+
itemStyle: {
70+
// emphasis: {
71+
// color: '#37a2da'
72+
// }
73+
}
74+
},
75+
{
76+
name: '正面',
77+
type: 'bar',
78+
stack: '总量',
79+
label: {
80+
normal: {
81+
show: true
82+
}
83+
},
84+
data: [120, 102, 141, 174, 190, 250, 220],
85+
itemStyle: {
86+
// emphasis: {
87+
// color: '#32c5e9'
88+
// }
89+
}
90+
},
91+
{
92+
name: '负面',
93+
type: 'bar',
94+
stack: '总量',
95+
label: {
96+
normal: {
97+
show: true,
98+
position: 'left'
99+
}
100+
},
101+
data: [-20, -32, -21, -34, -90, -130, -110],
102+
itemStyle: {
103+
// emphasis: {
104+
// color: '#67e0e3'
105+
// }
106+
}
107+
}
108+
]
109+
};
110+
111+
chart.setOption(option);
112+
return chart;
113+
}
114+
115+
Page({
116+
onShareAppMessage: function (res) {
117+
return {
118+
title: 'ECharts 可以在微信小程序中使用啦!',
119+
path: '/pages/index/index',
120+
success: function () { },
121+
fail: function () { }
122+
}
123+
},
124+
data: {
125+
ec: {
126+
onInit: initChart
127+
}
128+
},
129+
130+
onReady() {
131+
setTimeout(() => {
132+
this.save();
133+
});
134+
},
135+
136+
save() {
137+
// 保存图片到临时的本地文件
138+
const ecComponent = this.selectComponent('#mychart-dom-save');
139+
ecComponent.canvasToTempFilePath({
140+
success: res => console.log(res.tempFilePath),
141+
fail: res => console.log(res)
142+
});
143+
}
144+
});

pages/saveCanvas/index.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"usingComponents": {
3+
"ec-canvas": "../../ec-canvas/ec-canvas"
4+
}
5+
}

pages/saveCanvas/index.wxml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!--index.wxml-->
2+
<view class="container">
3+
<ec-canvas id="mychart-dom-save" canvas-id="mychart-save" ec="{{ ec }}"></ec-canvas>
4+
</view>

pages/saveCanvas/index.wxss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**index.wxss**/
2+
ec-canvas {
3+
width: 100%;
4+
height: 100%;
5+
}

0 commit comments

Comments
 (0)