将工具提示添加到d3区域图表

时间:2016-05-02 16:20:47

标签: d3.js

有没有人有例子或知道如何将d3小费添加到区域图表中?我正在使用一种堆积区域图表。

let blockedGen = d3.svg.area()
            .x(function(d) { return x(d.projectDate); })
            .y0(height)
            .interpolate("basis")
            .y1(function(d) { return y(d.done + d.review + d.inprogress + d.blocked ); });

        let todoGen = d3.svg.area()
            .x(function(d) { return x(d.projectDate); })
            .y0(height)
            .interpolate("basis")
            .y1(function(d) { return y( d.done + d.review + d.inprogress + d.blocked + d.todo); });
var svg = d3.select(".demandChart").append("svg")
            .attr("id","demandchart")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

let todoLayer =  svg.append('svg:path')
            .attr('d', todoGen(dataDemand))
            .attr("fill-opacity","1")
            .attr('fill', 'rgb(49, 130, 189)');

        let blockedLayer =  svg.append('svg:path')
            .attr('d', blockedGen(dataDemand))
            .attr("fill-opacity",".7")
            .attr('fill', 'rgb(158, 202, 225)');

由于

1 个答案:

答案 0 :(得分:0)

添加var div = d3.select("#toolTip");

将以下代码添加到svg

.on("mouseover", function(d) {      
      div.transition()      
       .style("opacity", .9);       
       div.html("Project Date: " +  d.projectDate)
       .style("left",  (d3.event.pageX) + "px")
       .style("top",  (d3.event.pageY) + "px"); 
    }).on("mouseout", function() {
            div.transition()        
            .duration(500)      
            .style("opacity", 0);});

css文件中添加:

div.tooltip {
    position: absolute;
    padding: 10px;
    background: #f4f4f4;
    border: 0px;
    border-radius: 3px;
    pointer-events: none;
    font-size: 11px;
    color: #000;
    line-height: 16px;
    border: 1px solid #d4d4d4;
}