D3 面积图无法正确呈现
D3 area chart not rendering right
我正在创建一个 D3 面积图,该面积显示在线上方而不是下方。
Area chart with area appearing above line
这是一个 fiddle:https://jsfiddle.net/8xsrmgzw/
//Read the data
d3.csv("https://raw.githubusercontent.com/kalidogg/UCB/main/DebtPenny6.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
},
// Now I can use this dataset:
function(data) {
// Add X axis --> it is a date format
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.value; })])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// Add the area
svg.append("path")
.datum(data)
.attr("fill", "#cce5df")
.attr("stroke", "#69b3a2")
.attr("stroke-width", 1.5)
.attr("d", d3.area()
.x(function(d) { return x(d.date) })
.y0(y(0))
.y1(function(d) { return y(d.value) })
)
})
</script>
谢谢。
有几个问题。首先,您需要在绘制区域之前按日期对数据进行排序。其次,您的数据集包含空值。在下面的例子中,我已经过滤掉了那些,但你可以考虑做一些事情 like these examples 来指示数据丢失的地方。
此外,在该区域设置描边颜色将创建该区域的轮廓,包括侧面和底部。如果你只想要一条线勾勒出区域的顶部,那么你可以添加一个单独的路径并使用 area.lineY1()
为区域的顶部线获取线生成器。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://d3js.org/d3.v7.js"></script>
</head>
<body>
<div id="chart"></div>
<script>
d3.csv("https://raw.githubusercontent.com/kalidogg/UCB/main/DebtPenny6.csv", function(d) {
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : +d.value }
}).then(chart);
function chart(data) {
// set up
const margin = { top: 10, bottom: 30, left: 50, right: 10};
const width = 600 - margin.left - margin.right;
const height = 200 - margin.top - margin.bottom;
const svg = d3.select('#chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom);
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// filter and sort data
data = data
.filter(d => !isNaN(d.value))
.sort((a, b) => d3.ascending(a.date, b.date));
// scales
const x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([0, width]);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height, 0]);
// area generator
const area = d3.area()
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d.value));
// Add the area and top line
g.append("path")
.datum(data)
.attr("fill", "#cce5df")
.attr("d", area);
g.append("path")
.datum(data)
.attr("stroke", "#69b3a2")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.attr("d", area.lineY1());
// Axes
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
g.append("g")
.call(d3.axisLeft(y)
.tickFormat(d3.format('~s'))
.tickSizeOuter(0));
}
</script>
</body>
</html>
我正在创建一个 D3 面积图,该面积显示在线上方而不是下方。
Area chart with area appearing above line
这是一个 fiddle:https://jsfiddle.net/8xsrmgzw/
//Read the data
d3.csv("https://raw.githubusercontent.com/kalidogg/UCB/main/DebtPenny6.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
},
// Now I can use this dataset:
function(data) {
// Add X axis --> it is a date format
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.value; })])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// Add the area
svg.append("path")
.datum(data)
.attr("fill", "#cce5df")
.attr("stroke", "#69b3a2")
.attr("stroke-width", 1.5)
.attr("d", d3.area()
.x(function(d) { return x(d.date) })
.y0(y(0))
.y1(function(d) { return y(d.value) })
)
})
</script>
谢谢。
有几个问题。首先,您需要在绘制区域之前按日期对数据进行排序。其次,您的数据集包含空值。在下面的例子中,我已经过滤掉了那些,但你可以考虑做一些事情 like these examples 来指示数据丢失的地方。
此外,在该区域设置描边颜色将创建该区域的轮廓,包括侧面和底部。如果你只想要一条线勾勒出区域的顶部,那么你可以添加一个单独的路径并使用 area.lineY1()
为区域的顶部线获取线生成器。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://d3js.org/d3.v7.js"></script>
</head>
<body>
<div id="chart"></div>
<script>
d3.csv("https://raw.githubusercontent.com/kalidogg/UCB/main/DebtPenny6.csv", function(d) {
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : +d.value }
}).then(chart);
function chart(data) {
// set up
const margin = { top: 10, bottom: 30, left: 50, right: 10};
const width = 600 - margin.left - margin.right;
const height = 200 - margin.top - margin.bottom;
const svg = d3.select('#chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom);
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// filter and sort data
data = data
.filter(d => !isNaN(d.value))
.sort((a, b) => d3.ascending(a.date, b.date));
// scales
const x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([0, width]);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height, 0]);
// area generator
const area = d3.area()
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d.value));
// Add the area and top line
g.append("path")
.datum(data)
.attr("fill", "#cce5df")
.attr("d", area);
g.append("path")
.datum(data)
.attr("stroke", "#69b3a2")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.attr("d", area.lineY1());
// Axes
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
g.append("g")
.call(d3.axisLeft(y)
.tickFormat(d3.format('~s'))
.tickSizeOuter(0));
}
</script>
</body>
</html>