关于javascript:将d3-tip和CSS悬停效果与d3堆叠条形图一起使用

Using d3-tip and CSS hover effect with d3 stacked bar chart

我正在尝试为一个简单的数据集制作一个堆积的条形图(下面可以找到代码和数据)。

对于鼠标悬停的栏,我尝试使用CSS悬停效果来更改栏的颜色(类为.bar的rect svg元素),并使用d3-tip显示工具提示,以显示该栏的区域名称属于。

我遇到的问题是:
1-CSS悬停效果根本不起作用。 (请在下面找到样式表)
2-显示工具提示,但仅当我从栏下方移动鼠标光标以输入它时才显示。如果我从栏的左/右/顶部移动鼠标光标,似乎无法检测到"鼠标悬停"。如果未检测到它,则单击该条将被检测到并显示工具提示。
3-工具提示应该显示" d.State"的数据(这是该区域的缩写文本),但它给我的定义是不确定的。不过," d.State"与条形图本身配合良好-轴刻度使用这些数据创建。

堆叠的条基于此条制成:https://bl.ocks.org/mbostock/3886208

工具提示和悬停效果基于以下内容:http://bl.ocks.org/Caged/6476579

index.html:

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="//d3js.org/d3.v3.min.js">
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js">
</head>
<body>
<script type="text/javascript" src="viz.js">
</body>
</html>

viz.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// begin of the js file
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 320 - margin.left - margin.right,
    height = 320 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
    .rangeRound([height, 0]);

var color = d3.scale.ordinal()
    .range(["#98abc5","#7b6888","#a05d56","#ff8c00"]);

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".2s"));

//define tooltip
var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([0, 0])
  .html(function(d) {
    return"Region: <span style='color:red'>" + d.State +"</span>";
  });

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 +")");

//call tooltip
svg.call(tip);

d3.csv("data.csv", function(error, data) {
  if (error) throw error;

  color.domain(d3.keys(data[0]).filter(function(key) { return key !=="State"; }));

  data.forEach(function(d) {
    var y0 = 0;
    d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
    d.total = d.ages[d.ages.length - 1].y1;
  });

  data.sort(function(a, b) { return b.total - a.total; });

  x.domain(data.map(function(d) { return d.State; }));
  y.domain([0, d3.max(data, function(d) { return d.total; })]);

  svg.append("g")
      .attr("class","x axis")
      .attr("transform","translate(0," + height +")")
      .call(xAxis)
        .selectAll("text")  
          .attr("font-size", 7);

  svg.append("g")
      .attr("class","y axis")
      .call(yAxis)
      .append("text")
      .attr("transform","rotate(-90)")
      .attr("y", -40)
      .attr("dy",".71em")
      .style("text-anchor","end")
      .text("Number of Organizations");

  var state = svg.selectAll(".state")
      .data(data)
      .enter().append("g")
      .attr("class","g")
      .attr("transform", function(d) { return"translate(" + x(d.State) +",1)"; });

  state.selectAll(".bar")
      .data(function(d) { return d.ages; })
      .enter().append("rect")
      .attr("class","bar")
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.y1); })
      .attr("height", function(d) { return y(d.y0) - y(d.y1); })
      .style("fill", function(d) { return color(d.name); })
      .on("mouseover", tip.show)
      .on("mouseout", tip.hide);

  var legend = svg.selectAll(".legend")
      .data(color.domain().slice().reverse())
      .enter().append("g")
      .attr("class","legend")
      .attr("transform", function(d, i) { return"translate(0," + i * 20 +")"; });

  legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

  legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy",".35em")
      .style("text-anchor","end")
      .text(function(d) { return d; });
});
// end of the js file

style.css:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.bar:hover {
  fill: steelblue;
  pointer-events: all;
}

.x.axis path {
  display: none;
}

.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 2px;
}

/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
  box-sizing: border-box;
  display: inline;
  font-size: 10px;
  width: 100%;
  line-height: 1;
  color: rgba(0, 0, 0, 0.8);
  content:"\\25BC";
  position: absolute;
  text-align: center;
}

/* Style northward tooltips differently */
.d3-tip.n:after {
  margin: -1px 0 0 0;
  top: 100%;
  left: 0;
}

data.csv:

1
2
3
4
5
6
7
8
State,Non Profit,For Profit,Developer Group,Other
EP,28,142,15,16
EC,81,292,39,22
LC,73,91,23,9
MN,3,5,2,1
NA,102,561,26,19
SA,11,49,9,4
SS,28,10,10,3

如果有任何不清楚的部分,请告诉我。我是d3和stackoverflow的新手。谢谢!


  • CSS悬停效果根本不起作用。(我想不是没有遗漏吗?)

    问题是它已经使用d3填充了。 要覆盖它,只需在悬停填充上添加!important

    填充:steelblue!重要;

  • 显示工具提示,但仅当我从栏下方移动鼠标光标以输入它时才显示。 如果将鼠标光标从栏的左/右/顶部移动(我的代码没有遇到任何问题?)

    我不确定到底是什么问题,但是我猜想,仅当您将鼠标悬停在onmouseover上时,它才会起作用。 因此,如果鼠标指针在生成之前已经在图形上,则不会显示工具提示。

  • 工具提示应该显示" d.State"的数据。

    这里的问题是State数据未附加到元素,即d.ages不包含状态值。 绑定数据时只需附加状态值。

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    var margin = {top: 20, right: 20, bottom: 30, left: 40},
        width = 320 - margin.left - margin.right,
        height = 320 - margin.top - margin.bottom;

    var x = d3.scale.ordinal()
        .rangeRoundBands([0, width], .1);

    var y = d3.scale.linear()
        .rangeRound([height, 0]);

    var color = d3.scale.ordinal()
        .range(["#98abc5","#7b6888","#a05d56","#ff8c00"]);

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

    var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left")
        .tickFormat(d3.format(".2s"));

    //define tooltip
    var tip = d3.tip()
      .attr('class', 'd3-tip')
      .offset([0, 0])
      .html(function(d) {
        return"Region: <span style='color:red'>" + d.State +"</span>";
      });

    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 +")");

    //call tooltip
    svg.call(tip);

    d3.csv("data.csv", function(error, data) {
      if (error) throw error;

      color.domain(d3.keys(data[0]).filter(function(key) { return key !=="State"; }));

      data.forEach(function(d) {
        var y0 = 0;
        d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
        d.total = d.ages[d.ages.length - 1].y1;
      });

      data.sort(function(a, b) { return b.total - a.total; });

      x.domain(data.map(function(d) { return d.State; }));
      y.domain([0, d3.max(data, function(d) { return d.total; })]);

      svg.append("g")
          .attr("class","x axis")
          .attr("transform","translate(0," + height +")")
          .call(xAxis)
            .selectAll("text")  
              .attr("font-size", 7);

      svg.append("g")
          .attr("class","y axis")
          .call(yAxis)
          .append("text")
          .attr("transform","rotate(-90)")
          .attr("y", -40)
          .attr("dy",".71em")
          .style("text-anchor","end")
          .text("Number of Organizations");

      var state = svg.selectAll(".state")
          .data(data)
          .enter().append("g")
          .attr("class","g")
          .attr("transform", function(d) { return"translate(" + x(d.State) +",1)"; });

      state.selectAll(".bar")
          .data(function(d) {
    \t  \tfor(var l = 0 ; l < d.ages.length ; l++) {
    \t\t\td.ages[l].State = d.State;
    \t\t}
    \t  \treturn d.ages; })
          .enter().append("rect")
          .attr("class","bar")
          .attr("width", x.rangeBand())
          .attr("y", function(d) {
    \t  \treturn y(d.y1); })
          .attr("height", function(d) { return y(d.y0) - y(d.y1); })
          .style("fill", function(d) { return color(d.name); })
          .on("mouseover", tip.show)
          .on("mouseout", tip.hide);

      var legend = svg.selectAll(".legend")
          .data(color.domain().slice().reverse())
          .enter().append("g")
          .attr("class","legend")
          .attr("transform", function(d, i) { return"translate(0," + i * 20 +")"; });

      legend.append("rect")
          .attr("x", width - 18)
          .attr("width", 18)
          .attr("height", 18)
          .style("fill", color);

      legend.append("text")
          .attr("x", width - 24)
          .attr("y", 9)
          .attr("dy",".35em")
          .style("text-anchor","end")
          .text(function(d) { return d; });
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    body {
      font: 10px sans-serif;
    }

    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }

    .bar:hover {
      fill: steelblue !important;
      pointer-events: all;
    }

    .x.axis path {
      display: none;
    }

    .d3-tip {
      line-height: 1;
      font-weight: bold;
      padding: 12px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      border-radius: 2px;
    }

    /* Creates a small triangle extender for the tooltip */
    .d3-tip:after {
      box-sizing: border-box;
      display: inline;
      font-size: 10px;
      width: 100%;
      line-height: 1;
      color: rgba(0, 0, 0, 0.8);
      content:"\\25BC";
      position: absolute;
      text-align: center;
    }

    /* Style northward tooltips differently */
    .d3-tip.n:after {
      margin: -1px 0 0 0;
      top: 100%;
      left: 0;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="//d3js.org/d3.v3.min.js">
    <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js">
    </head>
    <body>
    <script type="text/javascript" src="viz.js">
    </body>
    </html>