|
| 1 | +import { Scatter } from '@antv/g2plot'; |
| 2 | + |
| 3 | +fetch('https://gw.alipayobjects.com/os/bmw-prod/f950b2f1-038b-47c2-afcc-63001bc8d07c.json') |
| 4 | + .then((res) => res.json()) |
| 5 | + .then((data) => { |
| 6 | + const processData = data.map((item) => { |
| 7 | + item['Average annual wage'] = item['Average annual wage'] * 1; |
| 8 | + item['probability'] = item['probability'] * 1; |
| 9 | + item['numbEmployed'] = item['numbEmployed'] * 1; |
| 10 | + return item; |
| 11 | + }); |
| 12 | + const labels = ['Airline Pilots, Copilots and Flight Engineers', 'Benefits Managers']; |
| 13 | + const scatterPlot = new Scatter('container', { |
| 14 | + appendPadding: 30, |
| 15 | + data: processData, |
| 16 | + xField: 'probability', |
| 17 | + yField: 'Average annual wage', |
| 18 | + colorField: 'education', |
| 19 | + size: [2, 16], |
| 20 | + sizeField: 'numbEmployed', |
| 21 | + shape: 'circle', |
| 22 | + yAxis: { |
| 23 | + nice: false, |
| 24 | + min: -20000, |
| 25 | + tickCount: 5, |
| 26 | + position: 'right', |
| 27 | + label: { |
| 28 | + formatter: (value) => { |
| 29 | + return Math.floor(value / 1000) + 'K'; |
| 30 | + }, |
| 31 | + }, |
| 32 | + grid: { |
| 33 | + line: { |
| 34 | + style: { |
| 35 | + stroke: '#eee', |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + line: { |
| 40 | + style: { |
| 41 | + stroke: '#aaa', |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + tooltip: { |
| 46 | + fields: ['probability', 'Average annual wage', 'numbEmployed'], |
| 47 | + }, |
| 48 | + legend: { |
| 49 | + position: 'top', |
| 50 | + }, |
| 51 | + xAxis: { |
| 52 | + min: -0.04, |
| 53 | + max: 1.04, |
| 54 | + nice: false, |
| 55 | + grid: { |
| 56 | + line: { |
| 57 | + style: { |
| 58 | + stroke: '#eee', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + line: false, |
| 63 | + label: false, |
| 64 | + }, |
| 65 | + label: { |
| 66 | + formatter: (item) => { |
| 67 | + return labels.includes(item['short occupation']) ? item['short occupation'] : ''; |
| 68 | + }, |
| 69 | + offsetY: -10, |
| 70 | + }, |
| 71 | + annotations: [ |
| 72 | + { |
| 73 | + type: 'line', |
| 74 | + start: [-0.04, 100000], |
| 75 | + end: [1.04, 30000], |
| 76 | + style: { |
| 77 | + stroke: '#aaa', |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + type: 'text', |
| 82 | + position: ['1.03', 'max'], |
| 83 | + content: 'Average annual wage', |
| 84 | + style: { |
| 85 | + textAlign: 'right', |
| 86 | + fontWeight: '500', |
| 87 | + fill: 'rgb(92, 92, 92)', |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + type: 'text', |
| 92 | + position: ['1.03', 'min'], |
| 93 | + content: 'Most likely to \nbe automated ', |
| 94 | + style: { |
| 95 | + textAlign: 'right', |
| 96 | + fontWeight: '500', |
| 97 | + fill: 'rgb(92, 92, 92)', |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + type: 'text', |
| 102 | + position: ['-0.03', 'min'], |
| 103 | + content: 'Least likely to \nbe automated ', |
| 104 | + style: { |
| 105 | + textAlign: 'left', |
| 106 | + fontWeight: '500', |
| 107 | + fill: 'rgb(92, 92, 92)', |
| 108 | + }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }); |
| 112 | + scatterPlot.render(); |
| 113 | + }); |
0 commit comments