gpt4 book ai didi

javascript - 为什么我的 D3 线图显示每个实体的黑色区域?

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:39 26 4
gpt4 key购买 nike

我制作了一个网络爬虫,它获取有关不同银行的货币转账率的数据,并在 D3 线图中显示随时间变化的数据(每家银行都有一条线,汇率是每天)。 cronjob 服务器端确保每天进行抓取。

在前端,它使用 D3.json 从这个 url 获取数据:

http://rateswebscraper.herokuapp.com/rates

我制作了折线图,但它显示的不是每个银行的线,而是黑色区域,请参见下面的屏幕截图:

enter image description here

这是我的代码:

/*global d3*/

var margin = {

top: 20,
right: 50,
bottom: 20,
left: 50

};

var w = 1000 - margin.left - margin.right,
h = 500 - margin.top - margin.bottom;

var ratesData = "http://rateswebscraper.herokuapp.com/rates";

//load buy or sell data
var loadData = function(dataSet, title) {

//set chart title
document.getElementById("title").innerHTML = title;

//reset SVG container element
d3.select("#chart").select("svg").remove();

var svg = d3.select("#chart")
.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("id", "area");

//Create the scales
var x = d3.scaleTime().range([0, w]),
y = d3.scaleLinear().range([h, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);

//Line generator for path
var line = d3.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.rate);
});

d3.json(ratesData, function(data) {

//convert date strings bank to date objects for d3.scaleTime
var rates = data.map(function(item) {

item.date = new Date(item.date);

return item;

});

var banks = rates[0].data.map(function(item) {
//loop through the .data array and get the bank names for the series

var bank = {};

bank.id = item.name;

bank.values = [];

return bank;

});


var createRates = function(type) {

rates.forEach(function(item) {

item.data.forEach(function(rate) {

for (var i = 0; i < banks.length; i++) {

if (banks[i].id === rate.name) {

var rateObject = {};

rateObject.date = new Date(item.date);

if (type === "buy") {

rateObject.rate = Number(rate.buy);

} else {

rateObject.rate = Number(rate.sell);

}

banks[i].values.push(rateObject);

}

}

});

});

};

//Create a rates array per bank of buy values
if (dataSet === "buy") {

createRates("buy");

} else {

createRates("sell");

}

//Get the Min/Max values for date and rates and setup color scale based on bank ID
x.domain(d3.extent(rates, function(d) {
return d.date;
}));
y.domain([
d3.min(banks, function(c) {
return d3.min(c.values, function(d) {
if (d.rate !== 0) {
return d.rate;
} else {
return
}
});
}),
d3.max(banks, function(c) {
return d3.max(c.values, function(d) {
return d.rate;
});
})
]);
//color scale needs an array of bank id's
z.domain(banks.map(function(c) {
return c.id;
}));

//append x Axis
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + h + ")")
.call(d3.axisBottom(x));

//append y Axis
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Rates in HKD");

//for every bank element we append a g group element
var bank = svg.selectAll(".bank")
.data(banks)
.enter().append("g")
.attr("class", "bank");

//for every bank element we append a path element and use the line generator
bank.append("path")
.attr("class", "line")
.attr("d", function(d) {
return line(d.values);
})
.style("stroke", function(d) {
return z(d.id);
});

//append a text next to the line for every bank
bank.append("text")
.datum(function(d) {
return {
id: d.id,
value: d.values[d.values.length - 1]
};
})
.attr("transform", function(d) {
return "translate(" + x(d.value.date) + "," + y(d.value.rate) + ")";
})
.attr("x", 3)
.attr("dy", "0.35em")
.style("font", "10px work sans")
.text(function(d) {
return d.id;
});

}); =

};

//Call loadData function on page load with buy rates
loadData("buy", "Buy Rates");


//Add click events to buttons to load buy or sell data
document.getElementById("buyData").addEventListener("click", function() {

loadData("buy", "Buy Rates");

});

document.getElementById("sellData").addEventListener("click", function() {

loadData("sell", "Sell Rates");

})

任何人都可以帮助我在我的代码中做错了什么?

最佳答案

给路径添加样式:

.style("fill","none");

关于javascript - 为什么我的 D3 线图显示每个实体的黑色区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com