Javascript 在线鼠标悬停不';t在d3中给出圆

Javascript 在线鼠标悬停不';t在d3中给出圆,javascript,d3.js,Javascript,D3.js,当鼠标在网上移动时,我试着画圆圈。我用的是D3JS而不是nvd3。我的代码是 <!DOCTYPE html> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } div.circle{ border-radius: 50%; width:

当鼠标在网上移动时,我试着画圆圈。我用的是D3JS而不是nvd3。我的代码是

<!DOCTYPE html>
<style>

body {
  font: 10px sans-serif;
}

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

div.circle{
  border-radius: 50%;        
  width: 30px;                  
  height: 30px;                 
}
</style>
<html>
<head>
  <script type="text/javascript" src="d3.v3.min.js"></script>
</head>
<body>
<script type="text/javascript">

data = [

  {date: 1,temp:10},{date: 2,temp:40},{date: 3,temp:90},
  {date: 4,temp:30},{date: 5,temp:20},{date: 6,temp:10}
];

  var margin = {top: 20,left: 30, bottom: 30,right: 40},
      height = 500 - margin.top - margin.bottom,
      width = 900 - margin.left - margin.right;

  var x = d3.time.scale()
            .range([0,width]);

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

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

  var yAxis = d3.svg.axis()
              .scale(y)
              .orient("left");

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

  var line = d3.svg.line()
                .x(function(d){return x(d.date);})
                .y(function(d){return y(d.temp);})
                .interpolate("linear");

    var mycir = d3.select("body").append("div")
                                  .attr("class","circle");

    x.domain(d3.extent(data,function(d){return d.date}));
    y.domain([d3.min(data,function(d){return d.temp}),d3.max(data,function(d){return d.temp})]);

    chart.append("g")
          .attr("class","x axis")
          .attr("transform","translate(0,"+height+")")
          .call(xAxis);
    chart.append("g")
          .attr("class","y axis")
          .call(yAxis);
    chart.append("path")
          .attr("class", "line")
          .attr("d",line(data))
          .attr("stroke","red")
          .attr("stroke-width",2)
          .attr("fill","none")
    chart.select(".line")
          .data(data)
          .on("mouseover",function(d){
                mycir.transition()
                      .duration(200)
                      .style("fill-opacity",.9);
                mycir .attr("cx",(d3.event.pageX))
                      .attr("cy",(d3.event.pageY-28))
                      .attr("r",5)
                      .style("fill","green")

          })
          .on("mouseout",function(d){
                mycir.transition()
                      .duration(200)
                      .style("fill-opacity",0)
          })

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

身体{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
分区圆{
边界半径:50%;
宽度:30px;
高度:30px;
}
数据=[
{date:1,temp:10},{date:2,temp:40},{date:3,temp:90},
{date:4,temp:30},{date:5,temp:20},{date:6,temp:10}
];
var margin={顶部:20,左侧:30,底部:30,右侧:40},
高度=500-页边距.顶部-页边距.底部,
宽度=900-边距。左侧-边距。右侧;
var x=d3.time.scale()
.范围([0,宽度]);
变量y=d3.scale.linear()
.范围([高度,0]);
var xAxis=d3.svg.axis()
.比例(x)
.东方(“底部”);
var yAxis=d3.svg.axis()
.比例(y)
.东方(“左”);
var chart=d3.选择(“主体”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
var line=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.temp);})
.插入(“线性”);
var mycir=d3.选择(“主体”).追加(“div”)
.attr(“类”、“圈”);
x、 域(d3.extent(数据,函数(d){返回d.date}));
y、 域([d3.min(数据,函数(d){return d.temp}),d3.max(数据,函数(d){return d.temp}]);
图表.附加(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
图表.附加(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis);
图表.附加(“路径”)
.attr(“类”、“行”)
.attr(“d”,行(数据))
.attr(“笔划”、“红色”)
.attr(“笔划宽度”,2)
.attr(“填充”、“无”)
图表。选择(“.线”)
.数据(数据)
.on(“鼠标悬停”,功能(d){
mycir.transition()
.持续时间(200)
.style(“填充不透明度”,.9);
mycir.attr(“cx”,(d3.event.pageX))
.attr(“cy”,(d3.事件,第28页))
.attr(“r”,5)
.style(“填充”、“绿色”)
})
.开启(“鼠标出”,功能(d){
mycir.transition()
.持续时间(200)
.style(“填充不透明度”,0)
})

当我尝试在线上鼠标移动时,我看不到圆圈,但我可以看到圆圈的
cx
cy
在元素检查中发生了变化。

您应该向svg添加一个
svg:circle
,如下所示:

var mycir = chart.append("svg:circle").attr("class","circle").attr('fill', 'red').attr('r', 5);

是的,它正在工作,但不像nvd3那样。有没有其他方法可以通过d3获得与nvd3完全相同的结果。@user3002180为什么不直接使用nvd3?据我所知,nvd3不如d3灵活,并且没有适当的文档。