Public
Edited
Nov 27, 2022
Fork of Emojis
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// modified by #SkyFi to use a function instead of in-lined code - the function allows the code to be shared
viewof choice = inputEmojiPicker()
Insert cell
Insert cell
Insert cell
sortedEmojis = sorted.map(d => d.Emoji)
Insert cell
sizeScale = d3
.scaleLog()
.domain(d3.extent(emojis, d => d.Occurrences))
.range([2, 64])
Insert cell
opacityScale = d3
.scaleLinear()
.domain(d3.extent(emojis, d => d.Occurrences))
.range([0.1, 1])
Insert cell
emojis = FileAttachment("Emoji_Sentiment_Data_v1.0.csv").csv({ typed: false })
Insert cell
emojisWithMoreData = emojis.map(e => {
e.descLength = e['Unicode name'].length;
e.descWords = e['Unicode name'].split(' ').length;
// e.topColors = topColors(e.Emoji);
return e;
})
Insert cell
//sorted = emojisWithMoreData.sort((a, b) => b.Occurrences - a.Occurrences)
sorted = emojis.sort((a, b) => a.descWords - b.descWords)
Insert cell
Insert cell
// chosen = ({
// // Emoji: "🙈"
// // Emoji: "😱"
// Emoji: "🍁"
// })
// //sorted[6]
Insert cell
// viewof chosen = Scrubber(emojisWithMoreData, {
// delay: 2500,
// autoplay: false,
// format: e => e.Emoji
// })
Insert cell
// chosen = ({ Emoji: "🔥" })
Insert cell
fontSize = 40
Insert cell
canvasSize = fontSize + 10
Insert cell
emojipixels = emojicanvas
.getContext("2d")
.getImageData(0, 0, canvasSize, canvasSize)
Insert cell
seedSize = 100
Insert cell
Insert cell
Insert cell
result = dominantColors(chosen)
Insert cell
topcolors = result.centroids
.filter(d => {
return d.variance && (d[0] < 253 && d[1] < 253 && d[2] < 253);
})
.map(d => {
return {
color: d,
value: d.points.length
};
})
.sort((a, b) => b.value - a.value)
Insert cell
// preprocessed = sorted.slice(0, 1000).map(d => {
// console.log(d.Emoji);
// return {
// ...d,
// topColors: dominantColors(d.Emoji)
// .centroids.filter(d => {
// return d.variance && (d[0] < 252 && d[1] < 252 && d[2] < 252);
// })
// .map(d => {
// return {
// color: d,
// value: d.points.length
// };
// })
// .sort((a, b) => b.value - a.value)
// };
// })
Insert cell
Insert cell
promises = files.map(file => file.json())
Insert cell
datasets = Promise.all(promises)
Insert cell
Insert cell
treemapWidth = 100
Insert cell
treemapHeight = 100
Insert cell
Insert cell
data = ({
name: "emojis",
children: topcolors
})
Insert cell
root = d3
.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value)
Insert cell
Insert cell
pixelSize = 10
Insert cell
reconstructed = {
let canvas = d3
.create("canvas")
.attr("width", canvasSize * pixelSize)
.attr("height", canvasSize * pixelSize)
.node();
// canvas.width = `${canvasSize}px`;
// canvas.height = `${canvasSize}px`;

let ctx = canvas.getContext("2d");

ctx.fillStyle = "black";
// ctx.fillRect(0, 0, canvasSize, canvasSize);

let a = emojipixels.data;
for (let i = 0; i < emojipixels.data.length; i += 4) {
let x = ((i / 4) % canvasSize) * pixelSize;
let y = Math.floor(i / 4 / canvasSize) * pixelSize;

ctx.fillStyle = `rgba(${[a[i], a[i + 1], a[i + 2], a[i + 3]]})`;
ctx.fillRect(x, y, pixelSize - 2, pixelSize - 2);
}

return canvas;
}
Insert cell
function renderImageData(imageData) {
let canvas = d3
.create("canvas")
.attr("width", canvasSize * pixelSize)
.attr("height", canvasSize * pixelSize)
.node();
// canvas.width = `${canvasSize}px`;
// canvas.height = `${canvasSize}px`;

let ctx = canvas.getContext("2d");

ctx.fillStyle = "black";
// ctx.fillRect(0, 0, canvasSize, canvasSize);

let a = imageData.data;
for (let i = 0; i < a.length; i += 4) {
let x = ((i / 4) % canvasSize) * pixelSize;
let y = Math.floor(i / 4 / canvasSize) * pixelSize;

ctx.fillStyle = `rgba(${[a[i], a[i + 1], a[i + 2], a[i + 3]]})`;
ctx.fillRect(x, y, pixelSize - 2, pixelSize - 2);
}

return canvas;
}
Insert cell
d3.extent(emojipixels.data)
Insert cell
d3.extent(emojis, d => d.Position)
Insert cell
Insert cell
Q5 = require("https://cdn.jsdelivr.net/gh/LingDong-/q5xjs/q5.min.js").catch(
() => window.Q5
)
Insert cell
import { Scrubber } from "@mbostock/scrubber"
Insert cell
Insert cell
Insert cell
inputEmojiPicker = (control) => {
let div = html`<div></div>`;
div.value = sorted[0];
d3.select(div)
.selectAll("div.emoji")
.data(sorted)
.join("div")
.classed("emoji", true)
// .style("font-size", d => sizeScale(d.Occurrences) + "px")
.style("font-size", "25px")
.style("display", "inline-block")
.style("cursor", "pointer")
.style("opacity", d => {
return opacityScale(d.Occurences);
})
.attr("title", d => d["Unicode name"])
.text(d => d.Emoji)
.on("click", (event, d) => {
div.value = d;
div.dispatchEvent(new CustomEvent("input", { bubbles: true }));
// THIS IS THE CODE THE UPDATES THE MAIN INPUT:
if (control) {
control.value += d["Emoji"]
control.dispatchEvent(new CustomEvent("input", { bubbles: true }));
}
});

return div;
}
Insert cell
Insert cell
viewof someInputText = Inputs.text({value: "👍"})
Insert cell
Insert cell
inputEmojiPicker(viewof someInputText)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more