gpt4 book ai didi

javascript - D3.js——如何通过 Javascript 更新数据集?

转载 作者:行者123 更新时间:2023-11-28 18:32:58 27 4
gpt4 key购买 nike

我有一个包含 11 个不同变量的数据集(包含 12 列的 csv 文件)。我希望能够为我的散点图选择某一列,但我遇到了一些困难。请耐心等待,因为 JavaScript 不是我的强项(显然)。这是我尝试过的:

<div class="variables" id="fixedacidity" onclick="drawPlot('fixedacidity');">
<h1>fixed acidity</h1>
</div>
<div class="variables" id="volatileacidity" onclick="drawPlot('volatileacidity');">
<h1>volatile acidity</h1>
</div>
<div class="variables" id="citricacid" onclick="drawPlot('citricacid');">
<h1>citric acid</h1>
</div>
<div class="variables" id="residualsugar" onclick="drawPlot('residualsugar');">
<h1>residual sugar</h1>
</div>
etc ...

我制作了一个调用drawPlot函数的简单菜单,但我在尝试正确传递变量时遇到了麻烦。

相关 d3/javascript:

function drawPlot(selectedVar){

$(".visarea").html("");

var wineVar = selectedVar;

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 860 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;

var x = d3.scale.linear()
.range([0, width]);

var y = d3.scale.linear()
.range([height, 0])
.domain([0,10]);

var color = d3.scale.category10();

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left").ticks(10);


var chart1 = d3.select(".visarea").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("red.csv", function(error, data) {
if (error) throw error;

data.forEach(function(d) {
d.wineVar = +d.wineVar;
d.quality = +d.quality;
});

x.domain(d3.extent(data, function(d) { return d.wineVar; })).nice();
y.domain([0,10]).nice();

chart1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(wineVar);

chart1.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Rated Quality")

chart1.selectAll(".red.dot")
.data(data)
.enter().append("circle")
.attr("class", "red dot")
.attr("r", 3)
.attr("cx", function(d) { return x(d.wineVar); })
.attr("cy", function(d) { return y(d.quality); })
.style("fill", "red");

});
}

虽然变量被传递给函数,但 d.wineVar 正如预期的那样,不会返回所需的值,因此图表不会绘制正确的值。谁能推荐一个解决方法?这看起来很简单,但我花了几个小时试图弄清楚这一点。

red.csv 示例:

fixedacidity,volatileacidity,citricacid,residualsugar,chlorides,freesulfurdioxide,totalsulfurdioxide,density,pH,sulphates,alcohol,quality
7.4,0.7,0,1.9,0.076,11,34,0.9978,3.51,0.56,9.4,5
7.8,0.88,0,2.6,0.098,25,67,0.9968,3.2,0.68,9.8,5
7.8,0.76,0.04,2.3,0.092,15,54,0.997,3.26,0.65,9.8,5

Image of what I'm trying to accomplish. The first dataset, fixedacidity, gets drawn up fine. I'm having difficulties trying to get the menu to correctly show its respective dataset. "Rated Quality" will always be the data for the Y-axis.我正在努力实现的目标的图像。第一个数据集,fixedacidity,绘制得很好。我在尝试让菜单正确显示其各自的数据集时遇到困难。 “额定质量”始终是 Y 轴的数据。

最佳答案

您的变量引用错误,此处:

  data.forEach(function(d) {
d.wineVar = +d.wineVar; // <---------Here
d.quality = +d.quality;
});

更改方式:

  data.forEach(function(d) {
d.wineVar = +d[wineVar]; // <----------Here
d.quality = +d.quality;
});

关于javascript - D3.js——如何通过 Javascript 更新数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630640/

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