gpt4 book ai didi

javascript - D3 JS 附加到 div 奇怪的结果

转载 作者:行者123 更新时间:2023-11-28 09:05:52 24 4
gpt4 key购买 nike

你好,我遇到了一个相当奇怪的问题。我有一个图表,当它附加到正文时可以工作。当我将其更改为附加到 div 时,会发生这种情况:普通的![正常状态] /image/SCiuG.jpg附加到 div![div 状态] /image/SDdsC.jpg

我注意到轴没有正确绘制,但不知道如何解决这个问题。

有一次它保持正常,更新后它会随着正确的轴而变小。

我的代码:

// Set the dimensions of the canvas / graph
var margin = {top: 15, right: 10, bottom: 30, left: 50},
width = 400 - margin.left - margin.right,
height = 220 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);

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

// Define the line
var valueline = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });

// Define the second line
var valueline2 = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.open); });

// Define the second line
var valueline3 = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.open2); });

// Define the second line
var valueline4 = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.open3); });

// Adds the svg canvas
var svg = d3.select("#biggraph")
.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 + ")");

// Get the initial data
d3.tsv("data/data2.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
d.open2 = +d.open2;
d.open3 = +d.open3;
});

// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open, d.open2, d.open3); })]);

// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));

// Add the valueline2 path.
svg.append("path")
.attr("class", "line2")
.style("stroke", "red")
.attr("d", valueline2(data));

// Add the valueline3 path.
svg.append("path")
.attr("class", "line3")
.style("stroke", "#FF6600")
.attr("d", valueline3(data));


// Add the valueline4 path.
svg.append("path")
.attr("class", "line4")
.style("stroke", "green")
.attr("d", valueline4(data));

// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);

});

// ** Update data section (Called from the onclick)
function updateData() {

// Get the data again
d3.tsv("data/data-alt2.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
d.open2 = +d.open2;
d.open3 = +d.open3;
});

// Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open, d.open2, d.open3); })]);

// Select the section we want to apply our changes to
var svg = d3.select("#biggraph").transition();

// Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".line2") // change the line
.duration(750)
.attr("d", valueline2(data));
svg.select(".line3") // change the line
.duration(750)
.attr("d", valueline3(data));
svg.select(".line4") // change the line
.duration(750)
.attr("d", valueline4(data));

svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);


});
}



// ** Update data section (Called from the onclick)
function revertData() {

// Get the data again
d3.tsv("data/data2.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
d.open2 = +d.open2;
d.open3 = +d.open3;
});

// Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open, d.open2, d.open3); })]);;

// Select the section we want to apply our changes to
var svg = d3.select("#biggraph").transition();

// Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".line2") // change the line
.duration(750)
.attr("d", valueline2(data));
svg.select(".line3") // change the line
.duration(750)
.attr("d", valueline3(data));
svg.select(".line4") // change the line
.duration(750)
.attr("d", valueline4(data));
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);

});
}


// ** Update data section (Called from the onclick)
function updateData48h() {

// Get the data again
d3.tsv("data/data48h.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
d.open2 = +d.open2;
d.open3 = +d.open3;
});

// Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open, d.open2, d.open3); })]);

// Select the section we want to apply our changes to
var svg = d3.select("#biggraph").transition();

// Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".line2") // change the line
.duration(750)
.attr("d", valueline2(data));
svg.select(".line3") // change the line
.duration(750)
.attr("d", valueline3(data));
svg.select(".line4") // change the line
.duration(750)
.attr("d", valueline4(data));
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);

});
}

// ** Update data section (Called from the onclick)
function updateData24h() {

// Get the data again
d3.tsv("data/data24h.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
d.open2 = +d.open2;
d.open3 = +d.open3;
});

// Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open, d.open2, d.open3); })]);

// Select the section we want to apply our changes to
var svg = d3.select("#biggraph").transition();

// Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".line2") // change the line
.duration(750)
.attr("d", valueline2(data));
svg.select(".line3") // change the line
.duration(750)
.attr("d", valueline3(data));
svg.select(".line4") // change the line
.duration(750)
.attr("d", valueline4(data));
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis);

});
}

任何线索将不胜感激

最佳答案

我已经找到问题的答案了。这是同一页面上另一个数据流的问题。重命名后问题就解决了。

关于javascript - D3 JS 附加到 div 奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17116650/

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