【发布时间】:2018-10-20 20:00:38
【问题描述】:
我是 javascript 新手,我正在尝试用一条需要 1 秒才能出现的线来为我的图表制作动画(使用 .transition().duration(1000))。
很遗憾,我无法得到这个结果。 .duration(1000) 似乎不适用于画线。
我的摘要
svg.append("path")
.data([data])
.transition().duration(1000)
.attr("class", "line")
.attr("d", valueline(xy));
如果我输入.transition().duration(1000).style("color", "red),字体会在 1 秒内变为红色。
我的问题:为什么持续时间适用于颜色而不是线条绘制?
如果你能帮助我,我将非常感激!
我你需要我的全部代码,但它在那里:
//VOLCAN FUNCTION
function draw_volcan(url){
d3.select("svg").remove() // remove the old graph
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
d3.json(url, function(error, data) { //chargement des data volcans
if(error) throw ('There was an error while getting geoData: '+error);
//get in an array the occurance of eruptions
var volcans_incidence_annee_2018 = new Array(59).fill(0) // on prépare une array avec pour le nombre d'éruptions de volcan par année
data.forEach(function(d) {
var single_date_index = d.Date-1960 //année-1960 = index où il faudra ajouter 1 pour l'occurence de l'année
volcans_incidence_annee_2018[single_date_index] += 1 //ajoute 1 d'occurance à l'année souhaitée
});
var volcans_incidence_annee = new Array(56).fill(0)
for(i in volcans_incidence_annee_2018){ //créé un array volcans_incidence_annee qui contient les données de 1960 à 2015 uniquement
if(i<56){
volcans_incidence_annee[i] = volcans_incidence_annee_2018[i]
};
};
var year_array = []
for(var i = 1960; i<=2015; i++) {year_array.push(i);} //créé un array avec chaque année
//create array with all the points
var xy=[];
for(var i = 0; i < year_array.length; i++ ) {
xy.push({x: year_array[i], y: volcans_incidence_annee[i]});
};
// set the ranges
var x = d3.scaleLinear().domain([1960, 2015]).range([0, width]);
var y = d3.scaleLinear().domain([Math.min(...volcans_incidence_annee), Math.max(...volcans_incidence_annee)]).range([height, 0]);
// create a line function that can convert data[] into x and y points
var valueline = d3.line()
.x(function(d) { return x(d.x);})
.y(function(d) { return y(d.y);});
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#graph_draw").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 valueline path.
svg.append("path")
.data([data])
.transition().duration(1000)
.attr("class", "line")
.attr("d", valueline(xy));
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
//svg.append("g").attr("d", line(+volcans_incidence_annee));
});
【问题讨论】:
标签: javascript animation d3.js transition