Published
Edited
May 3, 2021
7 stars
Insert cell
md`# Emoji waffle

you can copy eomji by click it
`
Insert cell
html`<div id="container" style="height:500px;"></div>`
Insert cell
plot.on('element:click', (event) => {
const emoji = event.data?.data?.emoji;
copyToClipboard(emoji);
});
Insert cell
plot.render()
Insert cell
plot = new G2Plot.Scatter("container", {
appendPadding: [10, 24, 10, 24],
data: waffleData,
xField: "x",
yField: "y",
colorField: "name",
shapeField: "name",
size: ({ percentage }) => Math.max(percentage, 0.5) * 10,
tooltip: { showCrosshairs: false, fields: ["name", "value"] },
xAxis: false,
yAxis: false,
rawFields: ["percentage"],
shape: ({ name }) => {
return name;
}, // 根据具体的字段指定 shape
pointStyle: ({ name }) => {
return {
lineWidth: name ? 1 : 0
};
},
legend: {
marker: {
spacing: 0
},
itemName: {
formatter: (text, item, idx) => {
return data[idx].emojiCode
? String.fromCodePoint(parseInt(data[idx].emojiCode, 16))
: "";
}
},
itemValue: {
formatter: (text) => {
return text;
},
style: {
fill: "rgba(0,0,0,0.65)"
}
},
itemStates: {
unchecked: {
nameStyle: {
fillOpacity: 0.45
},
valueStyle: {
fillOpacity: 0.45
}
}
}
},
state: {
active: {
style: {
shadowColor: "rgba(100, 100,100, 0.25)",
shadowBlur: 15
}
}
},
interactions: [
{
type: "element-active",
cfg: {
start: [
{
trigger: "element:mouseenter",
action: ["cursor:pointer", "element-active:active"]
}
],
end: [
{
trigger: "element:mouseleave",
action: ["cursor:default", "element-active:reset"]
}
]
}
}
]
})
Insert cell
waffleData = generateWaffleData()
Insert cell
function generateWaffleData() {
const waffleData= [];
const step = data.reduce((a, b) => a + b.value, 0) / (rows * cols);
let idx = 0;
let weight = 0;
for (let col = 0; col < cols + 1; col++) {
for (let row = 0; row < rows; row++) {
if (idx > data.length) {
break;
}
waffleData.push({
...data[idx],
y: `${row}`,
x: `${col}`,
percentage: Math.min((data[idx]?.value - weight) / step, 1),
});
weight += step;
if (weight >= data[idx]?.value) {
idx += 1;
// 重置
weight = 0;
}
}
}
return waffleData;
}
Insert cell
cols = rows * ratio;
Insert cell
rows = 10;
Insert cell
ratio = box.width / box.height;
Insert cell
box = document.getElementById('container')?.getBoundingClientRect() || { width: 800, height: 500 };
Insert cell
function copyToClipboard(text) {
var dummy = document.createElement("input");
document.body.appendChild(dummy);
dummy.setAttribute("value", text);
dummy.select();
const result = document.execCommand("copy");
document.body.removeChild(dummy);
if (result) {
window.alert(`复制 ${text} 成功`);
}
}
Insert cell
data.forEach((d) => {
if (d.emojiCode) {
registeEmoji(d.name, d.emojiCode);
}
});
Insert cell
function registeEmoji(name, emojiCode) {
G2Plot.G2.registerShape('point', name, {
draw(cfg, container) {
const { x, y, size, color: fill } = cfg;
const fontSize = size * 2;
return container.addShape('text', {
attrs: {
...cfg?.style,
text: String.fromCodePoint(parseInt(emojiCode, 16)),
x: x - fontSize / 2,
y: y + fontSize / 2 + 1,
fill,
fontSize,
},
});
},
getMarker() {
return { symbol: 'circle', style: { lineWidth: 0, r: 0 } };
},
});
}
Insert cell
data = generateData(dv)
Insert cell
function generateData(dataView) {
const data = [];
const keys = Object.keys(dataView.rows);
keys.forEach((k) => {
const values = dataView.rows[k];
const v = values[0];
data.push({ ...v, value: values.length, name: v.d, emojiCode: v.d });
});
return data;
}
Insert cell
dv.transform({
type: 'partition',
groupBy: ['emoji'], // 以year字段进行分组
});
Insert cell
dv = new DataSet.DataView().source(result);
Insert cell
result = FileAttachment("emoji-data.json").json()
Insert cell
DataSet = require('@antv/data-set')
Insert cell
G2Plot = require('@antv/g2plot')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more