Public
Edited
Nov 6, 2023
Insert cell
Insert cell
231106_youtube_もう恋なんてしない_2011-04-10T11:55:01-07:00_to_2023-11-06T04:40:46-08:00@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
// プロットを描画
Plot.plot({
// グラフ内側の余白を設定
inset: 20,
// グラフの幅を指定
width: 1200,
// グラフの高さを指定
height: 675,
marginBottom: 45,
r: { range: [0, 32] },
style: {
background: "rgb(250, 245, 240)",
fontSize: "17px",
color: "deepgray"
},
x: {
// tickFormat: d3.timeFormat("%-y/%-m/%-d"), // この行を追加
label: "投稿日"
},
marks: [
Plot.text(
// グラフ内の任意の位置にテキストをプロットするコード
[
`YouTubeデータより徒然研究室(仮称)作成。データ取得日は2023年11月6日。マーカーの大きさはデータ取得時の視聴回数に対応する。`
],
{
frameAnchor: "top-left",
fontSize: 10,
fontFamily: "Noto Sans JP, sans-serif", // Noto Sans JP Bold を指定
fontWeight: "300",
fill: "black",
dy: 5
}
),
Plot.text(
[`YouTubeでタイトルに\n「もう恋なんてしない」\nを含む動画の投稿数 推移`],
{
frameAnchor: "top-left",
fontSize: 50,
fontFamily: "Noto Sans JP, sans-serif", // Noto Sans JP Bold を指定
fontWeight: "700",
// fill: "deeppink",
fill: "#313131",
dy: 30,
lineHeight: 1.1
}
),
Plot.frame({ anchor: "bottom" }),
Plot.image(
presidents,
Plot.dodgeY({
anchor: "bottom",
x: "release_date",
r: (datum) => Math.sqrt(datum.views) * 2, // viewsの平方根に比例してrを大きくする
preserveAspectRatio: "xMidYMin slice", // try not to clip heads
src: "thumbnail",
// title: "title",
tip: true,
title: (d) => {
const date = new Date(d.release_date);
const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1)
.toString()
.padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
return `タイトル: ${
d.title
}\n視聴回数: ${d.views.toLocaleString()}\nリリース日: ${formattedDate}`;
}
})
)
]
})
Insert cell
// データを定義
chart = {
var data = presidents;

// SVG要素の幅と高さを定義
var width = 1200;
var height = 600;

// SVG要素を作成
var svg = d3
.select(DOM.svg(width, height))
.style("background", "rgb(250, 245, 240)");

// グラフタイトルを追加
svg
.append("text")
.attr("x", width / 2)
.attr("y", 30)
.attr("text-anchor", "middle")
.text("YouTubeにおけるYOASOBI「アイドル」 関連動画の視聴回数");

// データをバインドして画像要素を作成
var images = svg
.selectAll("image")
.data(data)
.enter()
.append("image")
.attr("xlink:href", function (d) {
return d.thumbnail;
})
.attr("width", function (d) {
return Math.sqrt(d.views) * 2;
})
.attr("height", function (d) {
return Math.sqrt(d.views) * 2;
})
.attr("preserveAspectRatio", "xMidYMin slice")
.attr("x", function (d) {
return d.release_date;
})
.attr("y", height - 30);

// ツールチップを追加
images
.on("mouseover", function (event, d) {
d3.select(this).style("cursor", "pointer");
var tooltip = d3
.select(svg.node().parentNode)
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "1px")
.style("border-radius", "5px")
.style("padding", "5px")
.text(d.title);
tooltip
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 28 + "px");
})
.on("mouseout", function () {
d3.select(this).style("cursor", "default");
d3.select(svg.node().parentNode).select("div").remove();
});

return svg.node();
}
Insert cell
Insert cell
// プロットを描画
Plot.plot({
// グラフ内側の余白を設定
inset: 20,
// グラフの幅を指定
width: 1200,
// グラフの高さを指定
height: 675,
r: { range: [0, 40] },
style: `background:rgb(250, 245, 240)`,
marks: [
Plot.frame({ anchor: "bottom" }),
Plot.image(
presidents,
Plot.dodgeY({
anchor: "bottom",
x: "release_date",
r: (datum) => Math.sqrt(datum.views) * 2, // viewsの平方根に比例してrを大きくする
preserveAspectRatio: "xMidYMin slice", // try not to clip heads
src: "thumbnail",
title: "title",
tip: true,
// 透明度を指定(0から1の値、0が完全に透明、1が不透明)
opacity: 0.7,
})
),
// "views"を表示するためのPlot.textを追加
Plot.text(
presidents,
Plot.dodgeY({
anchor: "bottom",
x: "release_date",
r: (datum) => Math.sqrt(datum.views) * 2 + 4, // マーカーのサイズに応じて適切なオフセットを調整,
text: "views"
}),
{ field: "views", baseline: "top", dy: -10 } // "views"のテキストを表示する位置を調整
),
],
});

Insert cell
presidents = _youtube_search_results_yoasobiIdol
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