javascript - 鼠标悬停时在 d3 图表旁边的框中显示变量的所有数据

标签 javascript d3.js

我有一个 d3 折线图,当我将鼠标悬停在显示下面 json 对象中的 topNewFeature 数据点的行时,我将其与工具提示放在一起。除了该工具提示之外,我还想迭代功能变量,并在鼠标悬停在图表旁边的框中列出所有功能描述。这是我的数据结构:

data = [{
        date : 2015-01-01,
        topNewFeature : XYZ,
        sigFeatures : 12,
        cumCPA : 43,
        features : [
            {coeff : 0.3345, featureDescription : ABC},
            {coeff : 0.7232, featureDescription : XYZ}, 
            ...
        ]
    },

        ...
]

我的 json 文件太大,无法上传到 stack Overflow 提供的 jsfiddle、myjson 或 html/css/javascript 编辑器,因此我制作了一个小 Flask 应用程序来提供它:JSON DATA

以下是相关的js、html和css文件:

      var margin = {
          top: 30,
          right: 200,
          bottom: 30,
          left: 50
        },
        width = 960 - margin.left - margin.right,
        height = 470 - margin.top - margin.bottom;

      var parseDate = d3.time.format("%d-%b-%y").parse;
      var formatDate = d3.time.format("%e %B"); // ********
      var bisectDate = d3.bisector(function(d) {
        return d.date;
      }).left;

      // Set chart ranges
      var x = d3.time.scale().range([0, width]);
      var y0 = d3.scale.linear().range([height, 0]);
      var y1 = d3.scale.linear().range([height, 0]);

      // Define X-Axis
      var xAxis = d3.svg.axis().scale(x)
        .orient("bottom").ticks(5);

      // Define Left Y-Axis
      var yAxisLeft = d3.svg.axis().scale(y0)
        .orient("left").ticks(5)
        .tickFormat(d3.format("$"));

      // Define Right Y-Axis
      var yAxisRight = d3.svg.axis().scale(y1)
        .orient("right").ticks(5);

      // Define cumulative CPA line
      var valueline = d3.svg.line()
        .x(function(d) {
          return x(d.date);
        })
        .y(function(d) {
          return y0(d.cumCPA);
        });

      // Define significant features line
      var valueline2 = d3.svg.line()
        .x(function(d) {
          return x(d.date);
        })
        .y(function(d) {
          return y1(d.sigFeatures);
        });

      // Define the div for the tooltip
      var tooldiv = d3.select("body").append("div")
        .attr("class", "tooltip")
        .style("opacity", "0")
        .style("display", "none");

      // Add svg canvas
      var svg = d3.select("#chart")
        .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 + ")");

      // Add tooltip line & focus element
      var lineSvg = svg.append("g");

      // Get the data
      d3.json("princess_cruise.json", function(error, data) {
      data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.sigFeatures = +d.sigFeatures;
        d.cumCPA = +d.cumCPA;
        d.features = d.features;
        d.topNewFeature = d.topNewFeature;
      });

      // Scale the range of the data
      x.domain(d3.extent(data, function(d) {
        return d.date;
      }));
      y0.domain([0, d3.max(data, function(d) {
        return Math.max(d.cumCPA);
      })]);
      y1.domain([0, d3.max(data, function(d) {
        return Math.max(d.sigFeatures);
      })]);

      // Add cumulative CPA path
      svg.append("path")
        .style("stroke", "#158BD5")
        .attr("d", valueline(data));

      // Add significant features path
      svg.append("path")
        .style("stroke", "#00B151")
        .attr("d", valueline2(data));

      // Add the X Axis (date)
      svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

      // Add Y axis (cumulative CPA)
      svg.append("g")
        .attr("class", "y axis")
        .style("fill", "#158BD5")
        .call(yAxisLeft);

      // Add Y axis (significant features)
      svg.append("g")
        .attr("class", "y axis")
        .attr("transform", "translate(" + width + " ,0)")
        .style("fill", "#00B151")
        .call(yAxisRight);


      // START TOOLTIP INTEGRATION
      
      var focus = svg.append("g")
        .style("display", "none");

      // append the x line
      focus.append("line")
        .attr("class", "x")
        .style("stroke", "#555555")
        .style("stroke-dasharray", "3,3")
        .style("opacity", 0.5)
        .attr("y1", 0)
        .attr("y2", height);

      // append the y line
      focus.append("line")
        .attr("class", "y")
        .style("stroke", "#555555")
        .style("stroke-dasharray", "3,3")
        .style("opacity", 0.5)
        .attr("x1", width)
        .attr("x2", width / 2);

      // append the circle at the intersection

      // place the significant features at the intersection
      var tooltip = focus.append("g")
        .data(data)
        .attr("class","toolbox");
        
      tooltip.append("rect")
        .style("fill", "#fff")
        .style("stroke", "#eee")
        .style("stroke-width", 2)
        .style("opacity", 1)
        .attr("width", 200)
        .attr("height", 35);
        
      tooltip.append("rect")
        .style("fill", "#eee")
        .style("opacity",0.8)
        .attr("width", 200)
        .attr("height", 15);    
    
      
      tooltip.append("text")
        .attr("dx", 8)
        .attr("dy", 12)
        .text("Top New Feature:");
      
      tooltip.append("text")
        .attr("class","featText")
        .attr("dx", 8)
        .attr("dy", 30);
      
      tooltip.append("circle")
        .attr("class", "y")
        .style("fill", "#fff")
        .style("stroke", "#00B151")
        .style("stroke-width", 2)
        .attr("r", 6);
      
      // append the rectangle to capture mouse
      svg.append("rect")
        .attr("width", width)
        .attr("height", height)
        .style("fill", "none")
        .style("pointer-events", "all")
        .on("mouseover", function(d) {
          focus.style("display", null);
        })
        .on("mouseout", function(d) {
          focus.style("display", "none");
        })
        .on("mousemove", mousemove);

      function mousemove() {
        var x0 = x.invert(d3.mouse(this)[0]),
          i = bisectDate(data, x0, 1),
          d0 = data[i - 1],
          d1 = data[i],
          d = x0 - d0.date > d1.date - x0 ? d1 : d0;

        focus.select(".toolbox")
          .attr("transform",
          "translate(" + x(d.date) + "," +
            y1(d.sigFeatures) + ")");
            
        focus.select(".featText")
          .text(d.topNewFeature);

        focus.select(".x")
          .attr("transform",
            "translate(" + x(d.date) + "," +
            y1(d.sigFeatures) + ")")
          .attr("y2", height - y1(d.sigFeatures));

        focus.select(".y")
          .attr("transform",
            "translate(" + width * -1 + "," +
            y1(d.sigFeatures) + ")")
          .attr("x2", width + width)
          .attr("x1", x(d.date) - (width * -1));
      }

});
		body { font: 12px Arial;}

		path { 
		    stroke: steelblue;
		    stroke-width: 2;
		    fill: none;
		}

		.axis path,
		.axis line {
		    fill: none;
		    stroke: grey;
		    stroke-width: 1;
		    shape-rendering: crispEdges;
		}
		div.tooltip {	
		    position: absolute;			
		    text-align: left;				
		    font: 12px sans-serif;
		    padding-top: 2px;
		    padding-bottom: 2px;
		    padding-right: 6px;
		    padding-left: 6px;		
		    background: white;	
		    border: 1px;
		    border-style: solid;			
		    pointer-events: none;			
		}
		
<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

		<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	</head>

	<body>
		<div class="container-fluid">
			<div class="row">
				<div class="col-md-8">
					<div id="chart"></div>
				</div>
				<div class="col-md-4">
					<h5>Features for day being hovered over</h5>
					<div id="showdata"></div>
				</div>
			</div>
		</div>
		
		<script src="/js/chart.js" type="text/javascript"></script>
	</body>
</html>

最佳答案

您可以在鼠标悬停功能中执行类似的操作:

//remove all the old selection this can me moved to mouse out as well
d3.select("#showdata").selectAll("*").remove();
//here we will append feature description to the div showdata 
d.features.forEach(function(m){
  d3.select("#showdata")
    .append("div")
    .text(m.featureDescription)
})

工作代码here

希望这有帮助!

关于javascript - 鼠标悬停时在 d3 图表旁边的框中显示变量的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34296826/

相关文章:

JavaScript || (或)条件具有更好的性能或包括?

javascript - 如何在 Angularjs 中使用不存在的值设置选择框的默认值

javascript - 这个 for-in 循环符合标准吗?

javascript - 试图让我的传单 map 在 Internet Explorer 上工作

javascript - NVD3饼图自定义颜色的方法

javascript - react 。无法在 getDerivedStateFromProps 中启动函数

javascript - D3 从所有圆圈到不同形状的散点图

javascript - 工具提示随 d3 地理 map 缩放(缩放)

javascript - 创建条形图时缺少第一个值

svg - D3 如何放大 SVG 矩形内的 SVG 文本?