如何在d3js中添加工具提示并更改线条描边的颜色?



我有一组数据,我正在多行中绘制。当我将鼠标悬停在圆圈中唯一的一个上时,我希望它弹出数据(如 x、y 值,也许更多),而且我对如何更改线条颜色感到困惑。这是我尝试过的代码

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// 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;
// parse the date / time
var parseTime = d3.timeParse("%Y");
// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var valueline = d3.line()
.x(function(d) { return x(d.Date); })
.y(function(d) { return y(d.Imports); });
// define the line
var valueline2 = d3.line()
.x(function(d) { return x(d.Date); })
.y(function(d) { return y(d.Exports); });
var valueline3 = d3.line()
.x(function(d) { return x(d.Date); })
.y(function(d) { return y(d.added); });

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 + ")");
function draw(data, country) {
var data = data[country];
// format the data
data.forEach(function(d) {
d.Date = parseTime(d.Date);
d.Imports = +d.Imports;
d.Exports = +d.Exports;
d.added = +d.added;
});
// sort years ascending
data.sort(function(a, b){
return a["Date"]-b["Date"];
})
// 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.Imports, d.Exports,d.added); })]);
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline);
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline2);
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline3);
// 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));
}
// Get the data
d3.json("data1.json", function(error, data) {
if (error) throw error;
// trigger render
draw(data, "purchase");
});
</script>
</body>

这是我的数据1.json

{
"purchase": [
{
"Date": 1999,
"Imports": "15",
"Exports": "20",
"added": "27"
},
{
"Date": 2008,
"Imports": "42",
"Exports": "115",
"added": "32"
},
{
"Date": 2007,
"Imports": "29",
"Exports": "79",
"added": "47"
},
{
"Date": 2009,
"Imports": "346",
"Exports": "324",
"added": "305"
},
{
"Date": 2006,
"Imports": "44",
"Exports": "69",
"added": "77"
},
{
"Date": 2010,
"Imports": "424",
"Exports": "503",
"added": "441"
},
{
"Date": 2005,
"Imports": "28",
"Exports": "48",
"added": "50"
},
{
"Date": 2011,
"Imports": "413",
"Exports": "603",
"added": "620"
},
{
"Date": 2004,
"Imports": "34",
"Exports": "41",
"added": "20"
},
{
"Date": 2012,
"Imports": "313",
"Exports": "517",
"added": "420"
},
{
"Date": 2003,
"Imports": "21",
"Exports": "36",
"added": "20"
},
{
"Date": 2013,
"Imports": "513",
"Exports": "615",
"added": "415"
},
{
"Date": 2002,
"Imports": "18",
"Exports": "23",
"added": "20"
},
{
"Date": 2014,
"Imports": "471",
"Exports": "766",
"added": "620"
},
{
"Date": 2001,
"Imports": "17",
"Exports": "36",
"added": "29"
},
{
"Date": 2015,
"Imports": "119",
"Exports": "181",
"added": "177"
},
{
"Date": 2000,
"Imports": "25",
"Exports": "25",
"added": "17"
}
]
}

谁能建议我如何在线添加工具提示数据点并更改颜色

我希望你仔细看看Chris给出的例子,如果你想更深入地了解发生了什么,这个例子利用了d3.mouse和d3.bisector。

我使用您的代码和提到的方法制作了一个Plunker。 看看mousemove()函数和focus变量,我的例子并不完美,你可以使代码更有效率,但它应该让你大致了解它是如何完成的。

我为线条赋予颜色所做的只是为每行添加一个.style("stroke", "steelblue")

最新更新