在d3中的多轴线图上添加x和y2轴的工具提示和聚焦指南

时间:2015-12-09 17:06:00

标签: javascript d3.js

我试图找出如何添加更多"华丽的"带有辅助Y轴的折线图的工具提示,只有我的导向指向x轴和y2轴(绿色数字)。

以下是图表目前的情况: enter image description here

这是我的目标: enter image description here

这是我的JSFIDDLE

1 个答案:

答案 0 :(得分:1)

这是一个快速重构器。

构建工具提示框(注意我正在对所有元素进行分组以使更新更容易):

  // place the significant features at the intersection
  var tooltip = focus.append("g")
    .attr("class","toolbox");

  tooltip.append("rect")
    .style("fill", "#eee")
    .style("opacity",0.8)
    .attr("width", 130)
    .attr("height", 15);

  tooltip.append("rect")
    .style("fill", "none")
    .style("stroke", "#eee")
    .style("stroke-width", 2)
    .style("opacity",0.8)
    .attr("width", 130)
    .attr("height", 35);

  tooltip.append("text")
    .attr("dx", 8)
    .attr("dy", 12)
    .text("Number Of Features:");

  tooltip.append("text")
    .attr("class","featText")
    .attr("dx", 20)
    .attr("dy", 30);

  tooltip.append("circle")
    .attr("class", "y")
    .style("fill", "#fff")
    .style("stroke", "#00B151")
    .attr("r", 6);

将鼠标改为:

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

    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)); //<-- fixes horizontal line to go only to right
  }

完整的工作代码:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
    <style>
    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;
    }
    
    p.tooltip-title {
      background-color: #E0E0E0;
    }
    
    pre {
      display: none;
    }
  </style>
  </head>

  <body>
    <pre id="data">date,sigFeatures,cumCPA,goal,cumConversions
13-Jan-15,0,56.56,12,96
14-Jan-15,16,46.97,12,142
15-Jan-15,32,41.50,12,190
16-Jan-15,32,34.86,12,261
17-Jan-15,34,25.95,12,395
18-Jan-15,68,21.50,12,532
19-Jan-15,68,18.80,12,674
20-Jan-15,68,17.39,12,798
21-Jan-15,119,16.65,12,908
22-Jan-15,119,15.74,12,1041
23-Jan-15,115,14.67,12,1200
24-Jan-15,124,14.18,12,1322
25-Jan-15,124,13.63,12,1462
26-Jan-15,125,13.05,12,1619
27-Jan-15,125,12.58,12,1784
28-Jan-15,125,11.82,12,2024
29-Jan-15,189,11.02,12,2301
30-Jan-15,189,10.85,12,2458
31-Jan-15,218,10.49,12,2671
1-Feb-15,246,10.08,12,2905
2-Feb-15,246,9.81,12,3114
3-Feb-15,246,9.56,12,3336
4-Feb-15,283,9.31,12,3570
5-Feb-15,323,8.95,12,3869
6-Feb-15,323,8.85,12,4067
7-Feb-15,325,8.76,12,4265
8-Feb-15,325,8.66,12,4477
9-Feb-15,325,8.55,12,4695
10-Feb-15,325,8.58,12,4840
11-Feb-15,334,8.52,12,5039
12-Feb-15,343,8.39,12,5274
13-Feb-15,343,8.42,12,5403
14-Feb-15,340,8.41,12,5557
15-Feb-15,357,8.37,12,5739
16-Feb-15,373,8.34,12,5917
17-Feb-15,373,8.29,12,6117
18-Feb-15,373,8.29,12,6260
19-Feb-15,364,8.28,12,6410
20-Feb-15,379,8.31,12,6559
21-Feb-15,373,8.25,12,6780
22-Feb-15,399,8.20,12,6996
23-Feb-15,399,8.15,12,7208
24-Feb-15,370,8.05,12,7471
25-Feb-15,370,8.00,12,7690
26-Feb-15,394,7.94,12,7932
27-Feb-15,408,7.89,12,8169
28-Feb-15,408,7.70,12,8556
1-Mar-15,410,7.60,12,8862
2-Mar-15,410,7.48,12,9199
3-Mar-15,449,7.30,12,9627
4-Mar-15,442,7.18,12,9983
5-Mar-15,442,7.02,12,10424
6-Mar-15,439,6.95,12,10725
7-Mar-15,418,6.86,12,11064
8-Mar-15,418,6.81,12,11337
9-Mar-15,474,6.74,12,11679
10-Mar-15,457,6.65,12,12049
11-Mar-15,468,6.57,12,12381
12-Mar-15,427,6.53,12,12683
13-Mar-15,463,6.49,12,12966
14-Mar-15,411,6.43,12,13310
15-Mar-15,411,6.38,12,13628
16-Mar-15,443,6.33,12,13947
17-Mar-15,437,6.24,12,14366
18-Mar-15,445,6.21,12,14676
19-Mar-15,460,6.21,12,14917
20-Mar-15,458,6.20,12,15146
</pre>
    <script>
      var margin = {
          top: 30,
          right: 40,
          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("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 + ")");

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

      var data = d3.csv.parse(d3.select("pre#data").text());
      // Get the data
      // d3.csv("campaign_data.csv", function(error, data) {
      data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.sigFeatures = +d.sigFeatures;
        d.cumCPA = +d.cumCPA;
      });

      // 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")
        .attr("class","toolbox");
        
      tooltip.append("rect")
        .style("fill", "#fff")
        .style("stroke", "#eee")
        .style("stroke-width", 2)
        .style("opacity", 1)
        .attr("width", 130)
        .attr("height", 35);
        
      tooltip.append("rect")
        .style("fill", "#eee")
        .style("opacity",0.8)
        .attr("width", 130)
        .attr("height", 15);    
    
      
      tooltip.append("text")
        .attr("dx", 8)
        .attr("dy", 12)
        .text("Number Of Features:");
      
      tooltip.append("text")
        .attr("class","featText")
        .attr("dx", 20)
        .attr("dy", 30);
      
      tooltip.append("circle")
        .attr("class", "y")
        .style("fill", "#fff")
        .style("stroke", "#00B151")
        .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.sigFeatures);

        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));
      }
    </script>
  </body>

</html>