gpt4 book ai didi

javascript - 无法更改 D3 中的 X/Y 轴值

转载 作者:行者123 更新时间:2023-12-01 02:39:00 25 4
gpt4 key购买 nike

我正在使用以下代码创建一个 D3 图形。在此,我尝试绘制一些与天气相关的数据。在代码中,我尝试更改轴值但无法执行此操作。它被固定为我一开始提供的默认值。有人可以帮我吗?我使用的数据集是json数据(链接:https://drive.google.com/file/d/13-unvzxvjmFWVgfSKRJObZJc81VriyIf/view?usp=sharing)。

以下是我正在使用的代码。

<!DOCTYPE html>
<html>
<meta charset="utf-8">



<style>
body {
font: 11px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.dot {
stroke: #000;
}

.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right, //900
height = 500 - margin.top - margin.bottom; //450

// setup fill color
var colorScale = d3.scale.linear()
.domain([-15, 30])
.range(["blue","red"]);

// add the graph canvas to the body of the webpage
var svg = d3.select("body").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 + ")");

// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var selectData = [ { "text" : "meantempm" },
{ "text" : "meanpressurem" },
{ "text" : "meanwindspdm" },
{ "text" : "humidity" },
]
var body = d3.select('body');
// load data
d3.json("weatherdata.json", function(error, data) {

// change string (from CSV) into number format
data.forEach(function(d) {//to check the utility
d.meanwindspdm = +d.history.dailysummary[0].meanwindspdm;
d.meantempm = +d.history.dailysummary[0].meantempm;
d.meanpressurem = +d.history.dailysummary[0].meanpressurem;
d.humidity = +d.history.dailysummary[0].humidity;

// console.log(d);
});


// setup x
var xExtent = d3.extent(data, function(d) { return (d.meanpressurem);}), //given
xScale = d3.scale.linear().domain(xExtent).range([0, width]), // value -> display //given
xValue = function(d) { return ((d.meanpressurem));}, // data -> value
xMap = function(d) { return xScale(d.meanpressurem);}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom"); //given

// setup y
var yExtent = d3.extent(data, function(d) {return (d.meantempm);}),
yValue = function(d) { return d.meantempm;}, // data -> value
yScale = d3.scale.linear().domain(yExtent).range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");

//new addition
// Select X-axis Variable
var defs = body.append('defs')
.text('X-Axis:')
var yInput = body.append('select')
.attr('id','xSelect')
.on('change',xChange)
.selectAll('option')
.data(selectData)
.enter()
.append('option')
.attr('value', function (d) { return d.text }) ///check
.text(function (d) { return d.text ;})
//body.append('br')

// Select Y-axis Variable
var span = body.append('defs')
.text('Y-Axis: ')
var yInput = body.append('select')
.attr('id','ySelect')
.on('change',yChange)
.selectAll('option')
.data(selectData)
.enter()
.append('option')
.attr('value', function (d) { return d.text })//check
.text(function (d) { return d.text ;})
body.append('br')

//new addition upto

// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr('id','xAxisLabel')
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("meanwindspdm");

// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr('id','yAxisLabel')
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("meantempm");

// draw dots
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d){return colorScale(d.meantempm)})
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.history.date.pretty + "<br/> (" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});

// draw legend
var legend = svg.selectAll(".legend")
.data(colorScale.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

// draw legend colored rectangles
legend.append("rect")
.attr("x", width - 10)
.attr("width", 10)
.attr("height", 10)
.style("fill", colorScale);

// draw legend text
legend.append("text")
.attr("x", width - 16)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d;})
// new addiiton/////////

function yChange() {
var value = this.value // get the new y value
yScale // change the yScale
.domain([
d3.min([0,d3.min(data,function (d) { return +d.history.dailysummary[0][value] })]),
d3.max([0,d3.max(data,function (d) { return +d.history.dailysummary[0][value] })])
])
yAxis.scale(yScale) // change the yScale
d3.select('#yAxis') // redraw the yAxis
.transition().duration(50)
.call(yAxis)
d3.select('#yAxisLabel') // change the yAxisLabel
.text(value)
d3.selectAll('circle') // move the circles
.transition().duration(50)
.delay(50)
//.delay(function (d,i) { return i*100})
.attr('cy',function (d) { return yScale(+d.history.dailysummary[0][value]) })
}

function xChange() {
var value = this.value // get the new x value
console.log(value);
console.log(data);
xScale // change the xScale
.domain([
d3.min([0,d3.min(data,function (d) {console.log( +d.history.dailysummary[0][value] ) ; return +d.history.dailysummary[0][value] })]),
d3.max([0,d3.max(data,function (d) { return +d.history.dailysummary[0][value] })])
])

xAxis.scale(xScale) // change the xScale //issue here I guess`enter code here`
d3.select('#xAxis') // redraw the xAxis
.transition().duration(50)
.call(xAxis)
d3.select('#xAxisLabel') // change the xAxisLabel
.transition().duration(50)
.text(value)
d3.selectAll('circle') // move the circles
.transition().duration(50)
.delay(50)
//.delay(function (d,i) { return i*100})
.attr('cx',function (d) { return xScale(+d.history.dailysummary[0][value]); })

}
// new addiiton upto/////////
});

</script>
</body>
</html>

最佳答案

如果我正确地阅读了您的代码,您正在尝试选择具有 id xAxisyAxis 的元素,您尚未在给定的代码中指定该元素,您可以给了

.attr("class", "x axis")

尝试使用 d3.select(".x")d3.select(".y") 而不是 d3.select("#xAxis ")d3.select("#yAxis")

关于javascript - 无法更改 D3 中的 X/Y 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47700549/

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