需求背景
微搭官方组件库已经提供了折线图、饼状图、柱状图图表组件,当这些图表组件不能满足实际业务需求时,可以用“更多图表”组件,这是一个通用的图表组件,可配置出多种复杂图表,使用该组件,可实现快速引入所有的 Echarts 图表。
实践步骤
更多图表组件引用复杂折线图
1. 在编辑器中使用更多图表组件,并将图表默认配置项清除。
![](https://qcloudimg.tencent-cloud.cn/image/document/3cd11dd5792ff00966e9b542cda314aa.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/3cd11dd5792ff00966e9b542cda314aa.png)
2. 前往 Echarts 官网 查看应用示例,选择堆叠面积图。
![](https://qcloudimg.tencent-cloud.cn/image/document/959e1c559322809fe1646a525279d0c5.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/959e1c559322809fe1646a525279d0c5.png)
3. 复制堆叠面积图示例 option 配置。
![](https://qcloudimg.tencent-cloud.cn/image/document/3aa5c2c89289789f7ca72f9e38295220.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/3aa5c2c89289789f7ca72f9e38295220.png)
4. 返回微搭编辑器中,将上述步骤复制的 option 配置粘贴至图表配置项。
![](https://qcloudimg.tencent-cloud.cn/image/document/a1f51140c575159f722af34058ca3959.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/a1f51140c575159f722af34058ca3959.png)
5. 图表配置项粘贴完成。
![](https://qcloudimg.tencent-cloud.cn/image/document/85c0d9da842f3bf3db0d2dcb3549f1f3.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/85c0d9da842f3bf3db0d2dcb3549f1f3.png)
图表配置项示例:
({title: {text: 'Stacked Area Chart'},tooltip: {trigger: 'axis',axisPointer: {type: 'cross',label: {backgroundColor: '#6a7985'}}},legend: {data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']},toolbox: {feature: {saveAsImage: {}}},grid: {left: '3%',right: '4%',bottom: '3%',containLabel: true},xAxis: [{type: 'category',boundaryGap: false,data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']}],yAxis: [{type: 'value'}],series: [{name: 'Email',type: 'line',stack: 'Total',areaStyle: {},emphasis: {focus: 'series'},data: [120, 132, 101, 134, 90, 230, 210]},{name: 'Union Ads',type: 'line',stack: 'Total',areaStyle: {},emphasis: {focus: 'series'},data: [220, 182, 191, 234, 290, 330, 310]},{name: 'Video Ads',type: 'line',stack: 'Total',areaStyle: {},emphasis: {focus: 'series'},data: [150, 232, 201, 154, 190, 330, 410]},{name: 'Direct',type: 'line',stack: 'Total',areaStyle: {},emphasis: {focus: 'series'},data: [320, 332, 301, 334, 390, 330, 320]},{name: 'Search Engine',type: 'line',stack: 'Total',label: {show: true,position: 'top'},areaStyle: {},emphasis: {focus: 'series'},data: [820, 932, 901, 934, 1290, 1330, 1320]}]})
6. 图表配置完成,效果展示。
![](https://qcloudimg.tencent-cloud.cn/image/document/00ef65c2c48a4da7d07a3784de62651d.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/00ef65c2c48a4da7d07a3784de62651d.png)
更多图表组件引用柱状图
1. 在编辑器中使用更多图表组件,模板选择不使用模板,并将图表默认配置项清除。
![](https://qcloudimg.tencent-cloud.cn/image/document/2cbbf86d20d6de0ed59fea42db3a7c42.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/2cbbf86d20d6de0ed59fea42db3a7c42.png)
2. 前往 Echarts 官网 查看应用示例,选择堆叠柱状图。
![](https://qcloudimg.tencent-cloud.cn/image/document/2311932730c638ef2c837f4b65a16113.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/2311932730c638ef2c837f4b65a16113.png)
3. 复制堆叠柱状图示例完整代码下的配置内容。
![](https://qcloudimg.tencent-cloud.cn/image/document/288436bcb056a69375dcf305d137b984.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/288436bcb056a69375dcf305d137b984.png)
4. 返回微搭编辑器,新建自定义
javascript
方法 setOption
。![](https://qcloudimg.tencent-cloud.cn/image/document/594022b2fee69b84a9877c304d07b3e1.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/594022b2fee69b84a9877c304d07b3e1.png)
5. 将在步骤3复制的内容粘贴到
setOption
方法中。export default function ({ event, data }) {let myChart = $w.chart1.echartsInstance;// There should not be negative values in rawDataconst rawData = [[100, 302, 301, 334, 390, 330, 320],[320, 132, 101, 134, 90, 230, 210],[220, 182, 191, 234, 290, 330, 310],[150, 212, 201, 154, 190, 330, 410],[820, 832, 901, 934, 1290, 1330, 1320]];const totalData = [];for (let i = 0; i < rawData[0].length; ++i) {let sum = 0;for (let j = 0; j < rawData.length; ++j) {sum += rawData[j][i];}totalData.push(sum);}const grid = {left: 100,right: 100,top: 50,bottom: 50};const series = ['Direct','Mail Ad','Affiliate Ad','Video Ad','Search Engine'].map((name, sid) => {return {name,type: 'bar',stack: 'total',barWidth: '60%',label: {show: true,formatter: (params) => Math.round(params.value * 1000) / 10 + '%'},data: rawData[sid].map((d, did) =>totalData[did] <= 0 ? 0 : d / totalData[did])};});const option = {legend: {selectedMode: false},grid,yAxis: {type: 'value'},xAxis: {type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']},series}option && myChart.setOption(option)}
说明:
模仿 Echarts 的示例改造,替换对应组件属性,
echarts
对应 $w.chart1.echarts
, myChart
对应 $w.chart1.echartsInstance
。6. 更多图表组件图表 Ready 事件绑定
setOption
方法。![](https://qcloudimg.tencent-cloud.cn/image/document/810a51a4968b4ee130f920b77ed8a485.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/810a51a4968b4ee130f920b77ed8a485.png)
7. 图表配置完成,效果展示。
![](https://qcloudimg.tencent-cloud.cn/image/document/3608d333802f80b125292c3ca11dfd4e.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/3608d333802f80b125292c3ca11dfd4e.png)
更多图表组件引用地图
1. 在编辑器中使用更多图表组件,并将图表默认配置项清除,并新建自定义
javascript
方法 function1
。![](https://qcloudimg.tencent-cloud.cn/image/document/2ed681828f4a19abef31d8011138f96b.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/2ed681828f4a19abef31d8011138f96b.png)
2. 前往 Echarts 官网 查看应用示例,选择航班选座(SVG)。
![](https://qcloudimg.tencent-cloud.cn/image/document/f743167e6e65778525dcef74a63e9cf9.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/f743167e6e65778525dcef74a63e9cf9.png)
3. 复制航班选座(SVG)示例完整代码下的配置内容。
![](https://qcloudimg.tencent-cloud.cn/image/document/9dd59b97c9d12402a89e54902994bf2a.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/9dd59b97c9d12402a89e54902994bf2a.png)
4. 将在上述复制的内容粘贴到
function1
方法中。export default async function ({ event }) {let res = await fetch('https://lowcode-0ghsfkoo8e3f415d-1258057692.tcloudbaseapp.com/resources/2024-06/lowcode-1834792', {headers: {'Content-Type': 'image/svg+xml'},method: 'GET'})const svg = await res.text()$w.chart1.echarts.registerMap('flight-seats', { svg: svg });const takenSeatNames = ['26E', '26D', '26C', '25D', '23C', '21A', '20F'];const option = {tooltip: {},geo: {map: 'flight-seats',roam: true,selectedMode: 'multiple',layoutCenter: ['50%', '50%'],layoutSize: '95%',tooltip: {show: true},itemStyle: {color: '#fff'},emphasis: {itemStyle: {color: undefined,borderColor: 'green',borderWidth: 2},label: {show: false}},select: {itemStyle: {color: 'green'},label: {show: false,textBorderColor: '#fff',textBorderWidth: 2}},regions: makeTakenRegions(takenSeatNames)}};function makeTakenRegions(takenSeatNames) {var regions = [];for (var i = 0; i < takenSeatNames.length; i++) {regions.push({name: takenSeatNames[i],silent: true,itemStyle: {color: '#bf0e08'},emphasis: {itemStyle: {borderColor: '#aaa',borderWidth: 1}},select: {itemStyle: {color: '#bf0e08'}}});}return regions;}$w.chart1.echartsInstance.setOption(option);// Get selected seats.$w.chart1.echartsInstance.on('geoselectchanged', function (params) {const selectedNames = params.allSelected[0].name.slice();// Remove taken seats.for (var i = selectedNames.length - 1; i >= 0; i--) {if (takenSeatNames.indexOf(selectedNames[i]) >= 0) {selectedNames.splice(i, 1);}}console.log('selected', selectedNames);});}
说明:
模仿 Echarts 的示例改造,替换对应组件属性,
echarts
对应 $w.chart1.echarts
, myChart
对应 $w.chart1.echartsInstance
。Echarts 示例代码中的
$.get
请求需要进行相应的改造。SVG 文件需要预先获取完整访问地址。
![](https://qcloudimg.tencent-cloud.cn/image/document/16fcd7fb2d8e10eeb5c75845b030e5c3.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/16fcd7fb2d8e10eeb5c75845b030e5c3.png)
5. 更多图表组件图表 Ready 事件绑定
function1
方法。![](https://qcloudimg.tencent-cloud.cn/image/document/840ca62c1e158456b6668bb6b507cba1.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/840ca62c1e158456b6668bb6b507cba1.png)
6. 图表配置完成,效果展示。
![](https://qcloudimg.tencent-cloud.cn/image/document/8487a71707cb53a8e0ed81505e3005b3.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/8487a71707cb53a8e0ed81505e3005b3.png)