为啥我的 d3.tip 不工作?

Posted

技术标签:

【中文标题】为啥我的 d3.tip 不工作?【英文标题】:Why is my d3.tip not working?为什么我的 d3.tip 不工作? 【发布时间】:2014-09-19 06:42:41 【问题描述】:

在将 d3.tip 方法添加到代码中之前,会出现条形图。但是,在添加 d3.tip 方法后,什么都没有显示,并且这个错误显示在控制台中。如何解决这个问题并将 d3.tip 添加到我的条形图中? Uncaught TypeError: Object # has no method 'tip'

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body 
  font: 10px sans-serif;


.y.axisRight text 
    fill: orange;


.y.axisLeft text 
    fill: steelblue;


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


.bar1 
  fill: steelblue;


.bar2 
  fill: orange;


.x.axis path 
  display: black;


.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;

</style>
<body>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = top: 80, right: 80, bottom: 80, left: 80,
    width = 400 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

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

var y0 = d3.scale.linear().domain([100,500]).range([height, 0]),
y1 = d3.scale.linear().domain([500,1000]).range([height, 0]);

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


// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y0).ticks(5).orient("left");
// create right yAxis
var yAxisRight = d3.svg.axis().scale(y1).orient("right");

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) 
    return "<strong>Health Workers:</strong> <span style='color:red'>" + d.totalhealthworkers + "</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("class", "graph")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")

svg.call(tip);

d3.json("workforce.json", function(error, data) 
  x.domain(data.map(function(d)  return d.countryName; ));
  y0.domain([0, d3.max(data, function (d)  return d.totalhealthworkers; )]);
  y1.domain([0, d3.max(data, function (d)  return d.totalhealthfacilities; )]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis axisLeft")
      .attr("transform", "translate(0,0)")
      .call(yAxisLeft)
    .append("text")
      .attr("y", 35)
      .attr("dy", "-2em")
       .attr("transform", "rotate(-90)")
      .style("text-anchor", "end")
      .text("Total health workers per 100,000 people");

  svg.append("g")
      .attr("class", "y axis axisRight")
      .attr("transform", "translate(" + (width) + ",0)")
      .call(yAxisRight)
    .append("text")
      .attr("y", 15)
       .attr("x", -20)
       .attr("transform", "rotate(-90)")
      .attr("dy", "-2em")
      .attr("dx", "2em")
      .style("text-anchor", "end")
      .text("Total health facilities per 100,000 people");

  bars = svg.selectAll(".bar").data(data).enter();

  bars.append("rect")
      .attr("class", "bar1")
      .attr("x", function (d)  return x(d.countryName); )
      .attr("width", x.rangeBand() / 3)
      .attr("y", function (d)  return y0(d.totalhealthworkers); )
      .attr("height", function (d, i, j)  return height - y0(d.totalhealthworkers); )
          .on('mouseover', tip.show)
          .on('mouseout', tip.hide)

  bars.append("rect")
      .attr("class", "bar2")
      .attr("x", function(d)  return x(d.countryName) + x.rangeBand()/2; )
      .attr("width", x.rangeBand() / 3)
      .attr("y", function(d)  return y1(d.totalhealthfacilities); )
      .attr("height", function (d, i, j)  return height - y1(d.totalhealthfacilities); )
);

function type(d) 
    d.totalhealthworkers = +d.totalhealthworkers;
    d.totalhealthfacilities = +d.totalhealthfacilities;
  return d;


</script>
  </body></div>
</html>

这是我正在使用的 JSON 文件。

["countryName":"Afghanistan","totalhealthworkers":"113.9","totalhealthfacilities":"4.50239"]

【问题讨论】:

我认为您需要 d3-tip.js .. 只需检查您是否正在导入该 javascript 【参考方案1】:

您可能需要添加

<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>

【讨论】:

【参考方案2】:

这是根据d3版本如果你使用v3那么tip是

<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>

如果您使用 v4,请使用此提示

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>

【讨论】:

以上是关于为啥我的 d3.tip 不工作?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 android studio 模拟器不工作?

为啥我的 vigenere.c 不工作?

为啥我的 StackView 不工作?元素完全移位

为啥我的 Mac XAMPP 服务器不工作?

有人可以解释一下,我的 mysql 匹配为啥不工作

为啥我的 $.getJSON 工作但不执行 onsuccess 部分?