gpt4 book ai didi

javascript - 在线条动画之后向折线图数据点添加工具提示

转载 作者:行者123 更新时间:2023-11-29 22:17:46 25 4
gpt4 key购买 nike

我有一个包含两条线的基本折线图。当我单击一个按钮时,两条线之一将绘制在图形上。

我使用 stroke-dasharray 来绘制线条。

有谁知道如何将工具提示添加到我的行中?使用 stroke-dasharray 会使它更难吗?

这是我的代码。

var button=d3.select("#button");

//defines canvas (area in which graph is placed)
var margin = {top: 30, right: 20, bottom: 50, left: 60},
width = 800 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;

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

//OUTPUT RANGE
var x = d3.time.scale()
.range([0, width]);

//OUTPUT RANGE
var y = d3.scale.linear()
.range([height, 0]);

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

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

//assigns coordinates for each piece of data
var valueline = d3.svg.line()
.interpolate("interpolation")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });

//second line data
var valueline2 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.open); });

//create area for 'area' below line
var area = d3.svg.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.close); });

//creates area to draw graph
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
//groups content
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5)
}

function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(30)
}

// csv callback function
d3.csv("myData3.csv", function(data) {

data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});


//INPUT DOMAINS
//.extent() returns min and max values of argument
x.domain(d3.extent(data, function(d) { return d.date; }));
//returns max of whichever set of data is bigger
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open); })]);


d3.select("#button1").on("click", function(){
var path = svg.append("path") // Add the valueline path.
.attr("class", "line")
.attr("d", valueline(data))
.attr("stroke", "steelblue")
.attr("stroke-width", "5")
.attr("fill", "none");

var totalLength = path.node().getTotalLength();

path
.attr("stroke-dasharray", totalLength + "30" * 30)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);

})

d3.select("#button2").on("click", function(){
var path2 = svg.append("path") // Add the valueline path.
.attr("class", "line2")
.attr("d", valueline2(data))
.attr("stroke", "steelblue")
.attr("stroke-width", "5")
.attr("fill", "none");

var totalLength = path2.node().getTotalLength();


path2
.attr("stroke-dasharray", totalLength + "30" * 30)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0)
})


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

//text label for x-axis
svg.append("text") // text label for the x axis
.attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom - 5 ) + ")")
.style("text-anchor", "middle")
.text("Date");

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

//text label for y-axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
//adds extra left padding as original y pos = 0
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Value");


//adding a title to the graph
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Graph");


//draw x axis grid
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat("")
)

//draw y axis grid
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)

});<!--d3.csv close-->​

提前致谢!

最佳答案

添加工具提示的最简单方法是将 svg:title 元素附加到您想要为其添加工具提示的元素。当您将鼠标悬停在元素上时,浏览器会自动显示它。它也适用于各种元素。

所以你的代码需要看起来像

var path = svg.append("path")   // Add the valueline path. 
.attr("class", "line")
.attr("d", valueline(data))
.attr("stroke", "steelblue")
.attr("stroke-width", "5")
.attr("fill", "none")
.append("title").text("whatever");

如果您需要更复杂的功能,您可以尝试例如 tipsy ,其工作方式类似。

关于javascript - 在线条动画之后向折线图数据点添加工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14112966/

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