0
다음 코드를 사용하여 D3 그림을 만듭니다. 여기에서 날씨 관련 데이터를 플롯하려고합니다. 코드에서 축 변경을 시도하고 있습니다. 가치 있지만 그것을 할 수 없습니다. 처음에 제가 제공 한 기본값으로 고정되어 가고 있습니다. 누군가 그것을 도와주세요. 내가 사용하는 데이터 세트는 json 데이터 (링크 : https://drive.google.com/file/d/13-unvzxvjmFWVgfSKRJObZJc81VriyIf/view?usp=sharing)입니다.D3에서 X/Y 축 값을 변경할 수 없습니다.
다음 코드는 내가 사용하고있는 코드입니다.
<!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>
Ahh. 감사. 도움이되었습니다. 두 실수를했습니다 .1) 잘못된 클래스를 추가하고 축 이드를 추가하는 것을 잊어 버렸습니다. 지금 그것은 작동한다. 나는 또 다른 문제가있다. 그림에서 x 축 레이블이 올바른 위치에없는 것처럼 보일 수 있습니다. 그것을 고치는 방법을 알고 있습니까? 미리 감사드립니다 – MindInDoubt
그림 링크 : https://drive.google.com/file/d/13-unvzxvjmFWVgfSKRJObZJc81VriyIf/view?usp=sharing – MindInDoubt
json 파일로 연결되는 링크입니다. – pmkro