g2({
type: "view",
width: 800,
height: 1200,
insetTop: 15,
insetRight: 10,
marginRight: 150,
data,
title: {
title: "巴以冲突时间线",
subtitle: "数据来源:新华网|统计时间:2023.10.17",
titleFontWeight: "bolder",
titleFontSize: 20
},
coordinate: { transform: [{ type: "transpose" }] },
encode: { x: "date", y: (d) => d.death + d.injured },
axis: false,
style: { viewFill: "rgb(253, 247, 239)" },
children: [
{
type: "area",
tooltip: false,
style: { fillOpacity: 0.1, fill: "red" }
},
{
type: "interval",
scale: {
x: { key: "interval", paddingInner: 0.15 },
color: {
range: [
"rgba(0, 0, 0, 0.8)",
"rgba(0, 0, 0, 0.5)",
"rgba(236, 61, 11, 0.8)",
"rgba(236, 61, 11, 0.5)"
]
}
},
legend: {
color: {
position: "bottom",
labelFormatter: (d) => {
const labelByValue = {
israelDeath: "以色列死亡人数",
hamasDeath: "哈马斯死亡人数",
israeInjured: "以色列受伤人数",
hamasInjured: "哈马斯受伤人数"
};
return labelByValue[d];
}
}
},
data: {
transform: [
{
type: "fold",
fields: [
"israelDeath",
"hamasDeath",
"israeInjured",
"hamasInjured"
],
key: "type",
value: "value"
}
]
},
encode: { y: "value", color: "type", series: "type" }
},
{
type: "line",
style: {
stroke: "black",
shape: "smooth",
strokeWidth: 10,
lineCap: "round"
},
tooltip: false,
labels: [
{
text: (d) => {
const N = d.date.split("-");
return `${N[1]}.${N[2]}`;
},
fontFamily: "Alibaba Sans 102",
letterSpacing: -1,
fontSize: 32,
fontWeight: "bold",
dy: (d) => (+d.date.split("-").pop() === 17 ? -15 : 0),
dx: (d) => (+d.date.split("-").pop() < 13 ? 30 : -30),
textAnchor: (d) => (+d.date.split("-").pop() < 13 ? "start" : "end"),
fillOpacity: 1
},
{
text: (d) => d.event,
render: (text, d) => {
const date = +d.date.split("-").pop();
const right = date < 13;
const dx = right ? 65 : -380;
const dy = right ? 15 : date === 17 ? -100 : -90;
const span = (text) => {
return `<span style="color:red;font-size:18px;font-weight:bold">${text}</span>`;
};
const textHtml = text
.replace(d.injured, span(d.injured))
.replace(d.death, span(d.death));
const width = date === 12 ? 250 : 300;
return `<div style="
width: ${width}px;
font-size: 12px;
transform: translate(${dx}px, ${dy}px);
padding: 5px;
color: black;
font-family: 'Alibaba Sans 102'"
>${textHtml}</div>`;
}
}
]
},
{
type: "point",
encode: { size: 14 },
style: {
shape: "point",
fill: "white",
stroke: "red",
strokeWidth: 3,
fillOpacity: 1
},
tooltip: false
},
{
type: "text",
encode: { text: "date" },
scale: {
text: {
type: "ordinal",
range: ["🚀", "🔥", "⚡️", "🏠", "✈️", "👊", "👊", "🙅"]
}
},
style: {
textAlign: "center",
textBaseline: "middle",
dx: 2,
fontSize: 15
}
}
]
})