|
| 1 | +import F2 from '@antv/my-f2'; |
| 2 | +import methods from '../mixins/methods'; |
| 3 | +import util from '../util'; |
| 4 | + |
| 5 | +Component({ |
| 6 | + mixins: [methods], |
| 7 | + props: { |
| 8 | + categories: [], |
| 9 | + series: [], |
| 10 | + xAxis: { |
| 11 | + tickCount: 3, |
| 12 | + }, |
| 13 | + yAxis: { |
| 14 | + tickCount: 3, |
| 15 | + }, |
| 16 | + tooltip: false, |
| 17 | + legend: false, |
| 18 | + }, |
| 19 | + didMount() { |
| 20 | + my.call('getStartupParams', {}, (result) => { |
| 21 | + util.tracert('area', result.appId, result.url); |
| 22 | + }); |
| 23 | + |
| 24 | + const id = `am-mc-area-${this.$id}`; |
| 25 | + const ctx = my.createCanvasContext(id); |
| 26 | + const canvas = this.canvas = new F2.Renderer(ctx); |
| 27 | + |
| 28 | + const pixelRatio = my.getSystemInfoSync().pixelRatio; |
| 29 | + ctx.scale(pixelRatio, pixelRatio); |
| 30 | + |
| 31 | + my.createSelectorQuery() |
| 32 | + .select(`#${id}`) |
| 33 | + .boundingClientRect() |
| 34 | + .exec(res => { |
| 35 | + if(!res || !res.length) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const {width, height} = res[0]; |
| 39 | + this.setData({ |
| 40 | + width: width * pixelRatio, |
| 41 | + height: height * pixelRatio, |
| 42 | + }); |
| 43 | + const { padding, appendPadding } = this.props; |
| 44 | + |
| 45 | + this.chart = new F2.Chart({ |
| 46 | + el: canvas, |
| 47 | + width, |
| 48 | + height, |
| 49 | + padding, |
| 50 | + appendPadding, |
| 51 | + }); |
| 52 | + }); |
| 53 | + }, |
| 54 | + didUpdate() { |
| 55 | + const { categories, series, xAxis, yAxis, tooltip, legend, adjust } = this.props; |
| 56 | + |
| 57 | + const chart = this.chart; |
| 58 | + chart.clear(); |
| 59 | + |
| 60 | + let data = []; |
| 61 | + if(series.length === 1) { |
| 62 | + data = series[0].data.map((item, i) => { |
| 63 | + return { |
| 64 | + key: categories[i], |
| 65 | + value: item, |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + else if(series.length > 1) { |
| 70 | + series.forEach(kind => { |
| 71 | + data = data.concat(kind.data.map((item, i) => { |
| 72 | + return { |
| 73 | + key: categories[i], |
| 74 | + value: item, |
| 75 | + type: kind.type, |
| 76 | + }; |
| 77 | + })); |
| 78 | + }); |
| 79 | + } |
| 80 | + chart.source(data); |
| 81 | + |
| 82 | + if(xAxis) { |
| 83 | + xAxis.label = util.label; |
| 84 | + chart.scale('key', util.scale(xAxis)); |
| 85 | + chart.axis('key', util.axis(xAxis)); |
| 86 | + } |
| 87 | + if(yAxis) { |
| 88 | + chart.scale('value', util.scale(yAxis)); |
| 89 | + chart.axis('value', util.axis(yAxis)); |
| 90 | + } |
| 91 | + chart.tooltip(tooltip); |
| 92 | + chart.legend(legend); |
| 93 | + |
| 94 | + const style = {}; |
| 95 | + series.forEach(kind => { |
| 96 | + style[kind.type] = kind.style; |
| 97 | + }); |
| 98 | + const color = {}; |
| 99 | + series.forEach(kind => { |
| 100 | + color[kind.type] = kind.color; |
| 101 | + }); |
| 102 | + |
| 103 | + if(series.length === 1) { |
| 104 | + chart.area().position('key*value').color('type', type => { |
| 105 | + return color[type]; |
| 106 | + }).adjust(adjust); |
| 107 | + chart.line().position('key*value').color('type', type => { |
| 108 | + return color[type]; |
| 109 | + }).shape('type', type => { |
| 110 | + return style[type] || 'line'; |
| 111 | + }).adjust(adjust); |
| 112 | + if(series[0].point) { |
| 113 | + chart.point().position('key*value').style(series[0].point); |
| 114 | + } |
| 115 | + } |
| 116 | + else if(series.length > 1) { |
| 117 | + chart.area().position('key*value').color('type', type => { |
| 118 | + return color[type]; |
| 119 | + }).adjust(adjust); |
| 120 | + chart.line().position('key*value').color('type', type => { |
| 121 | + return color[type]; |
| 122 | + }).shape('type', type => { |
| 123 | + return style[type] || 'line'; |
| 124 | + }).adjust(adjust); |
| 125 | + let pointType = []; |
| 126 | + let pointStyle; |
| 127 | + series.forEach(kind => { |
| 128 | + if(kind.point) { |
| 129 | + pointType.push(kind.type); |
| 130 | + pointStyle = kind.point; |
| 131 | + } |
| 132 | + }); |
| 133 | + if(pointType.length) { |
| 134 | + chart.point().position('key*value').color('type').size('type', v => { |
| 135 | + if(pointType.indexOf(v) === -1) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + }).style(pointStyle); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + chart.render(); |
| 143 | + }, |
| 144 | +}); |
0 commit comments