D3,将工具提示添加到水平条形图,工具提示未显示

时间:2015-09-01 21:12:53

标签: javascript d3.js tooltip

我是D3的新手,也是Javascript的新手。我的条形图按原样工作,但是当我添加任何注释掉的代码块试图添加工具提示时,而不是条形图顶部的工具提示渲染,我只得到条形渲染以及颜色填充过渡。 x轴,y轴和条形内的文本消失。我在这里做错了什么?

<script>

            var employees = []
            employees[0] = { name: "Bob", age: 31 }
            employees[1] = { name: "Doug", age: 22 }
            employees[2] = { name: "Christine", age: 58 }
            employees[3] = { name: "Sarah", age: 40 }

            var width = 700;
            var height = 800;

            //create scales
            var widthScale = d3.scale.linear()
                            .domain([0, 58])
                            .range([0, width]);

            var yScale = d3.scale.ordinal()
                        .domain(['200,000', '175,000', '150,000', '125,000', '100,000', '75,000', '50,000', '25,000', '0'])
                        .rangePoints([0, 300]);

            //create axis using scales
            var xAxis = d3.svg.axis()
                      .ticks(10)
                      .tickSize(2)
                      .scale(widthScale);

            var yAxis = d3.svg.axis()
                .orient('left')
                .tickSize(2)
                .scale(yScale)

            //colorscale for bar color transition
            var colorScale = d3.scale.linear()
                            .domain([0, 58])
                            .range(["green", "yellow"]);

            //tooltip------------------------------------------------
            var tooltip = d3.select("body")
                  .append("div")
                  .attr("class", "mytooltip")
                  .style("position", "absolute")
                  .style("background", "white")
                  .style("opacity", "0")
                  .style("display", "none");

            //create canvas
            var canvas = d3.select("body")
                        .append("svg")
                        .attr("width", width)
                        .attr("height", height)
                        .append("g") //create a group
                        .attr("transform", "translate(150, 15)") //right, down

            //create bars & add bars to canvas using data
            var bars = canvas.selectAll("rect")
                        .data(employees)
                        .enter()
                            .append("rect")
                            .attr("y", function (d, i) { return i * 25; })
                            .attr("width", 0)
                            .attr("height", 20)
                        .transition()
                            .delay(function (d, i) { return i * 100; })
                            .duration(600)
                            .attr("width", function (d) { return widthScale(d.age); })
                            .attr("height", 20)
                            .attr("fill", function (d) { return colorScale(d.age); })
                            .attr("y", function (d, i) { return i * 25; })

                            //add tooltip--------------------------------------------------
                            //.on('mouseover', function (d) {

                            //    tooltip.transition()
                            //        .style('opacity', .8)

                            //    tooltip.html(d)
                            //        .style('left', (d3.event.pageX - 35) + 'px')
                            //        .style('top', (d3.event.pageY - 30) + 'px')
                            //  });

            //add text to bars
            canvas.selectAll("text")
                .data(employees)
                .enter()
                    .append("text")
                    .attr("fill", "black")
                    .attr("y", function (d, i) { return i * 25 + 14; })
                    .text(function (d) { return d.name });

            //append a group to canvas, transform & place xAxis on canvas
            canvas.append("g")    
                .attr("transform", "translate(0, 300)")
                .call(xAxis);

            //append a group to canvas, transform & place yAxis on canvas
            canvas.append("g")
                .attr("transform", "translate(0, 0)")
                .call(yAxis);

            //add tooltip-------------------------------------------
            //canvas.append("g")
            //    .on("mouseover", function (d) {
            //        d3.select(this)
            //            .transition()
            //            .duration(500)
            //            .attr("x", function (d) { return x(d.age) - 30; })
            //            .style("cursor", "pointer")
            //  });

        </script>

1 个答案:

答案 0 :(得分:1)

将鼠标监听器应用于条形图并使用css opacity显示/隐藏工具提示div。

var employees = [{
    name: "Bob",
    age: 31
}, {
    name: "Doug",
    age: 22
}, {
    name: "Sarah",
    age: 40
}];

var width = 700;
var height = 800;

//create scales
var widthScale = d3.scale.linear()
    .domain([0, 58])
    .range([0, width]);

var yScale = d3.scale.ordinal()
    .domain(['200,000', '175,000', '150,000', '125,000', '100,000', '75,000', '50,000', '25,000', '0'])
    .rangePoints([0, 300]);

//create axis using scales
var xAxis = d3.svg.axis()
    .ticks(10)
    .tickSize(2)
    .scale(widthScale);

var yAxis = d3.svg.axis()
    .orient('left')
    .tickSize(2)
    .scale(yScale)

//colorscale for bar color transition
var colorScale = d3.scale.linear()
    .domain([0, 58])
    .range(["green", "yellow"]);

//add tooltip------------------------------------------------
var tooltip = d3.select("body")
    .append("div")
    .attr("class", "d3-tip")
    .style("position", "absolute")
    .style("opacity", 0);

//create canvas
var canvas = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height)
    .append("g") //create a group
    .attr("transform", "translate(150, 15)") //right, down


//create bars & add bars to canvas using data
var bars = canvas.selectAll(".bar")
    .data(employees)
    .enter()
    .append("g")
    .attr("class","bar");

bars.append("rect")
    .attr("y", function(d, i) {
        return i * 25;
    })
    .attr("width", 0)
    .attr("height", 20)
    .transition()
    .delay(function(d, i) {
        return i * 100;
    })
    .duration(600)
    .attr("width", function(d) {
        return widthScale(d.age);
    })
    .attr("height", 20)
    .attr("fill", function(d) {
        return colorScale(d.age);
    })
    .attr("y", function(d, i) {
        return i * 25;
    });

//add text to bars
bars.selectAll("text")
    .data(employees)
    .enter()
    .append("text")
    .attr("fill", "black")
    .attr("y", function(d, i) {
        return i * 25 + 14;
    })
    .text(function(d) {
        return d.name
    });

//append a group to canvas, transform & place xAxis on canvas
canvas.append("g")
    .attr("transform", "translate(0, 300)")
    .call(xAxis);

//append a group to canvas, transform & place yAxis on canvas
canvas.append("g")
    .attr("transform", "translate(0, 0)")
    .call(yAxis);

//show/hide tooltip-------------------------------------------
canvas.selectAll(".bar")
    .on("mouseover", function(d) {
        var pos = d3.mouse(this);
        console.log(pos);
        tooltip
            .transition()
            .duration(500)
            .style("opacity", 1)
            .style("left", d3.event.x + "px")
            .style("top", d3.event.y + "px")
            .text(d.name);
    })
    .on("mouseout", function() {
        tooltip.style("opacity", 0);
    });
.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>