|
| 1 | +import { Heatmap } from '@antv/g2plot'; |
| 2 | + |
| 3 | +// 数据截取于: 2021-02-21 13:04:19, 前往参与游戏: https://antv-studio.antfin.com/bless-heatmap |
| 4 | +fetch('https://gw.alipayobjects.com/os/antfincdn/wOj8DF0KF0/desire-heatmap.json') |
| 5 | + .then((data) => data.json()) |
| 6 | + .then((data) => { |
| 7 | + const COLS = 15; |
| 8 | + const ROWS = 10; |
| 9 | + function getSizeFactor() { |
| 10 | + const box = document.getElementById('container').getBoundingClientRect(); |
| 11 | + const size = Math.min(box.width / COLS, box.height / ROWS); |
| 12 | + return { |
| 13 | + width: size * COLS, |
| 14 | + height: size * ROWS, |
| 15 | + size, |
| 16 | + }; |
| 17 | + } |
| 18 | + const { width, height, size } = getSizeFactor(); |
| 19 | + const plot = new Heatmap('container', { |
| 20 | + data, |
| 21 | + autoFit: false, |
| 22 | + width, |
| 23 | + height, |
| 24 | + xField: 'row', |
| 25 | + yField: 'col', |
| 26 | + colorField: 'count', |
| 27 | + shape: 'square', |
| 28 | + sizeRatio: 1, |
| 29 | + color: [ |
| 30 | + '#dcdcdc', |
| 31 | + '#dad0bf', |
| 32 | + '#d9c3a1', |
| 33 | + '#d7b784', |
| 34 | + '#d6aa67', |
| 35 | + '#da9a54', |
| 36 | + '#e3864c', |
| 37 | + '#ec7344', |
| 38 | + '#f65f3c', |
| 39 | + '#ff4b34', |
| 40 | + ], |
| 41 | + tooltip: false, |
| 42 | + xAxis: false, |
| 43 | + yAxis: false, |
| 44 | + meta: { |
| 45 | + count: { |
| 46 | + max: 200, |
| 47 | + }, |
| 48 | + }, |
| 49 | + label: { |
| 50 | + formatter: (datum) => datum.item, |
| 51 | + layout: [{ type: 'adjust-color' }], |
| 52 | + style: { fontWeight: 700, fontSize: (size / 36) * 14 * (size < 32 ? 1.2 : 1) }, |
| 53 | + }, |
| 54 | + theme: { |
| 55 | + geometries: { |
| 56 | + polygon: { |
| 57 | + square: { |
| 58 | + active: { |
| 59 | + style: { |
| 60 | + stroke: '#fff', |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + interactions: [ |
| 68 | + { |
| 69 | + type: 'element-active', |
| 70 | + cfg: { |
| 71 | + showEnable: [ |
| 72 | + { trigger: 'element:mouseenter', action: 'cursor:pointer' }, |
| 73 | + { trigger: 'element:mouseleave', action: 'cursor:default' }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + plot.render(); |
| 80 | + |
| 81 | + window.addEventListener('resize', () => { |
| 82 | + const { width, height, size } = getSizeFactor(); |
| 83 | + plot.changeSize(width, height); |
| 84 | + if (plot.options.width / COLS !== size) { |
| 85 | + plot.update({ |
| 86 | + label: { |
| 87 | + formatter: (datum) => datum.item, |
| 88 | + layout: [{ type: 'adjust-color' }], |
| 89 | + style: { fontWeight: 700, fontSize: size < 32 ? size * 0.46 : size < 40 ? 0.48 : 0.3 }, |
| 90 | + }, |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
0 commit comments