svg 中的原点(0,0)位于左上角。
绘制折线图的步骤:
1.先写一个svg标签,定好宽和高
<svg id="chart" width="400" height="300"></svg>
2.定义一个数据集合和一些变量
const data = [
{ year: 2010, value: 10 },
{ year: 2011, value: 20 },
{ year: 2012, value: 15 },
{ year: 2013, value: 25 },
{ year: 2014, value: 30 },
{ year: 2015, value: 18 },
];
const svg = d3.select("#chart"),
margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
3.添加一个群组,并加入transform属性
const g = svg
.append("g")
.attr(
"transform",
"translate(" + margin.left + "," + margin.top + ")"
);
4.设置x,y的比例尺
d3.scaleLinear()
是线性比例尺
d3.scaleTime()
是时间比例尺,刻度可以是年、月、日
.domain()
是刻度数值的范围
range()
是刻度位置的范围
const x = d3
.scaleTime()
.domain(
d3.extent(data, function (d) {
return new Date(d.year);
})
)
.range([0, width]);
const y = d3
.scaleLinear()
.domain([
0,
d3.max(data, function (d) {
return d.value;
}),
])
.range([height, 0]);
5.创建折线生成器
.line().x()
是x轴
.line().y()
是y轴
const line = d3
.line()
.x(function (d) {
return x(d.year);
})
.y(function (d) {
return y(d.value);
});
6.绘制x轴和y轴
.ticks(num)
是指定刻度的数量
.tickFormat(d2.format("d"))
是格式化刻度为整数
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(data.length).tickFormat(d3.format("d")))
.append("text")
.attr("fill", "black")
.attr("x", width + 10)
.attr("y", -10)
.attr("text-anchor", "end")
.text("年份"); // x轴的名称
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("值"); // y轴的名称
7.绘制折线——添加一些样式信息
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>D3.js 折线图示例</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
<svg id="chart" width="400" height="300"></svg>
<script>
// 数据集
const data = [
{ year: 2010, value: 10 },
{ year: 2011, value: 20 },
{ year: 2012, value: 15 },
{ year: 2013, value: 25 },
{ year: 2014, value: 30 },
{ year: 2015, value: 18 },
];
// 设置SVG容器和边距
const svg = d3.select("#chart"),
margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg
.append("g")
.attr(
"transform",
"translate(" + margin.left + "," + margin.top + ")"
);
// 设置x和y比例尺
const x = d3
.scaleTime()
.domain(
d3.extent(data, function (d) {
return new Date(d.year);
})
)
.range([0, width]);
const y = d3
.scaleLinear()
.domain([
0,
d3.max(data, function (d) {
return d.value;
}),
])
.range([height, 0]);
// 创建折线生成器
const line = d3
.line()
.x(function (d) {
return x(d.year);
})
.y(function (d) {
return y(d.value);
});
// 绘制x轴和y轴
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(data.length).tickFormat(d3.format("d")))
.append("text")
.attr("fill", "black")
.attr("x", width + 10)
.attr("y", -10)
.attr("text-anchor", "end")
.text("年份");
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("值");
// 绘制折线
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
</script>
</body>
</html>