d3 line chart drag chart to specified x axis number externally
<p><a href="http://jsfiddle.net/rvxb5t08/7/" rel="nofollow noreferrer">JSFiddle of chart</a></p>

<p>I need chart to be dragged to a specific axis say 6.0 on click of some external button.
Is it possible?</p>

<p>Please note that chart is draggable and zoomable, but what I need is that chart to go-to a specific dot and highlight it when it is out of sight.</p>

<p>Like in linked js fiddle chart, when you load it, point 6.0 x-axis is out of sight and needs to be dragged, but what I want is on click of a button, just drag chart automatically to point 6.0.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>//************************************************************
// Data notice the structure
//************************************************************
var data = [
  [{
    'x': 1,
    'y': 7
  }, {
    'x': 2,
    'y': 7
  }, {
    'x': 3,
    'y': 7
  }, {
    'x': 4,
    'y': 7
  }, {
    'x': 5,
    'y': 7
  }, {
    'x': 6,
    'y': 8
  }, {
    'x': 7,
    'y': 7
  }, {
    'x': 8,
    'y': 6
  }, {
    'x': 9,
    'y': 7
  }, {
    'x': 10,
    'y': 7
  }],
  [{
    'x': 1,
    'y': 9
  }, {
    'x': 2,
    'y': 8
  }, {
    'x': 3,
    'y': 11
  }, {
    'x': 4,
    'y': 10
  }, {
    'x': 5,
    'y': 10
  }, {
    'x': 6,
    'y': 9
  }, {
    'x': 7,
    'y': 10
  }, {
    'x': 8,
    'y': 10
  }, {
    'x': 9,
    'y': 11
  }, {
    'x': 10,
    'y': 10
  }],
  [{
    'x': 1,
    'y': 9.5
  }, {
    'x': 2,
    'y': 10
  }, {
    'x': 3,
    'y': 12
  }, {
    'x': 4,
    'y': 9
  }, {
    'x': 5,
    'y': 10
  }, {
    'x': 6,
    'y': 9
  }, {
    'x': 7,
    'y': 11
  }, {
    'x': 8,
    'y': 11
  }, {
    'x': 9,
    'y': 11
  }, {
    'x': 10,
    'y': 11
  }],
  [{
    'x': 1,
    'y': 8
  }, {
    'x': 2,
    'y': 8
  }, {
    'x': 3,
    'y': 8
  }, {
    'x': 4,
    'y': 8
  }, {
    'x': 5,
    'y': 10
  }, {
    'x': 6,
    'y': 10
  }, {
    'x': 7,
    'y': 8
  }, {
    'x': 8,
    'y': 8
  }, {
    'x': 9,
    'y': 9
  }, {
    'x': 10,
    'y': 9
  }],
  [{
    'x': 1,
    'y': 10
  }, {
    'x': 2,
    'y': 10
  }, {
    'x': 3,
    'y': 11
  }, {
    'x': 4,
    'y': 11
  }, {
    'x': 5,
    'y': 11
  }, {
    'x': 6,
    'y': 11
  }, {
    'x': 7,
    'y': 12
  }, {
    'x': 8,
    'y': 12
  }, {
    'x': 9,
    'y': 12
  }, {
    'x': 10,
    'y': 12
  }]
];

var dataAboveLine = [{
  'x': 1,
  'y': 10
}, {
  'x': 2,
  'y': 10
}, {
  'x': 3,
  'y': 11
}, {
  'x': 4,
  'y': 11
}, {
  'x': 5,
  'y': 11
}, {
  'x': 6,
  'y': 11
}, {
  'x': 7,
  'y': 12
}, {
  'x': 8,
  'y': 12
}, {
  'x': 9,
  'y': 12
}, {
  'x': 10,
  'y': 12
}];
var dataBelowLine = [{
  'x': 1,
  'y': 7
}, {
  'x': 2,
  'y': 7
}, {
  'x': 3,
  'y': 7
}, {
  'x': 4,
  'y': 7
}, {
  'x': 5,
  'y': 7
}, {
  'x': 6,
  'y': 8
}, {
  'x': 7,
  'y': 7
}, {
  'x': 8,
  'y': 6
}, {
  'x': 9,
  'y': 7
}, {
  'x': 10,
  'y': 7
}];

var colors = [
  'steelblue',
  'green',
  'red',
  'lightgreen'
]

var lineType = [
  '5 0',
  '5 0',
  '5 0',
  '8 5'
]


//************************************************************
// Create Margins and Axis and hook our zoom function
//************************************************************
var margin = {
    top: 20,
    right: 30,
    bottom: 30,
    left: 50
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
  .domain([0, 5])
  .range([0, width]);

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

var xAxis = d3.svg.axis()
  .scale(x)
  .tickSize(-height)
  .tickPadding(10)
  .tickSubdivide(true)
  .orient("bottom");

var yAxis = d3.svg.axis()
  .scale(y)
  .tickPadding(10)
  .tickSize(-width)
  .tickSubdivide(true)
  .orient("left");

var area = d3.svg.area()
  .x(function(d) {
    return x(d.x);
  })
  .y0(height)
  .y1(function(d) {
    return y(d.y);
  });
// size of the diagram
       var viewerWidth = 960;
       var viewerHeight = 500;

var zoom = d3.behavior.zoom()
  .x(x)
  .y(y)
  .scaleExtent([1, 10])
  .on("zoom", zoomed);

// define the zoomListener which calls the zoom function on the "zoom" event constrained within the scaleExtents
       var zoomListener = d3.behavior.zoom()
       .scaleExtent([1, 20])
       .on("zoom", zoomed);

function centerNode(source) {
        var scale = zoomListener.scale();
        x = -source.point.y;
        y = -source.point.x;
        x = x * scale + viewerWidth / 2;
        y = y * scale + viewerHeight / 2;
        d3.select('g').transition()
            .duration(750)
            .attr("transform", "translate(" + x + "," + y + ")scale(" + scale + ")");
        zoomListener.scale(scale);
        zoomListener.translate([x, y]);
        
    }

//************************************************************
// Generate our SVG object
//************************************************************ 
var svg = d3.select("body").append("svg")
  .call(zoom)
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis);

svg.append("g")
  .attr("class", "y axis")
  .append("text")
  .attr("class", "axis-label")
  .attr("transform", "rotate(-90)")
  .attr("y", (-margin.left) + 10)
  .attr("x", -height / 2)
  .text('Axis Label');

svg.append("clipPath")
  .attr("id", "clip")
  .append("rect")
  .attr("width", width)
  .attr("height", height);


//   svg.append("path")
//         .datum(dataBelowLine)
//         .attr("class", "areaBelow")
//         .attr("d", area);


svg.append("path")
  .datum(dataBelowLine)
  .attr("class", "areaAbove")
  .attr("d", area)
  .attr("clip-path", "url(#clip)");



//************************************************************
// Create D3 line object and draw data on our SVG object
//************************************************************
var line = d3.svg.line()
  .interpolate("linear")
  .x(function(d) {
    return x(d.x);
  })
  .y(function(d) {
    return y(d.y);
  });

svg.selectAll('.line')
  .data(data)
  .enter()
  .append("path")
  .attr("class", "line")
  .attr("clip-path", "url(#clip)")
  .attr('stroke', function(d, i) {
    return colors[i % colors.length];
  })
  .style("stroke-dasharray", function(d, i) {
    return lineType[i % lineType.length];
  })
  .attr("d", line);


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

//************************************************************
// Draw points on SVG object based on the data given
//************************************************************
var points = svg.selectAll('.dots')
  .data(data)
  .enter()
  .append("g")
  .attr("class", "dots")
  .attr("clip-path", "url(#clip)");

points.selectAll('.dot')
  .data(function(d, index) {
    var a = [];
    d.forEach(function(point, i) {
      a.push({
        'index': index,
        'point': point
      });
    });
    return a;
  })
  .enter()
  .append('circle')
  .on("click", function(d) {
    click(d)
  })
  .attr('class', 'dot')
  .attr("r", 2.5)
  .attr('fill', function(d, i) {
    return colors[d.index % colors.length];
  })
  .attr("transform", function(d) {
    return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")";
  })
  .on("mouseover", function(d) {
    div.transition()
      // .duration(200)  
      .style("opacity", .9);
    div.html("X:" + d.point.x + "&lt;br/&gt;" + "Y:" + d.point.y)
      .style("left", (d3.event.pageX) + "px")
      .style("top", (d3.event.pageY - 28) + "px");
  })
  .on("mouseout", function(d) {
    div.transition()
      // .duration(500)  
      .style("opacity", 0);
  });


function click(d) {
  alert(JSON.stringify(d.point));
  centerNode(d);
}



//************************************************************
// Zoom specific updates
//************************************************************
function zoomed() {
  svg.select(".areaAbove").call(area);

  svg.select(".x.axis").call(xAxis);
  svg.select(".y.axis").call(yAxis);

  svg.selectAll('path.line').attr('d', line);
  
  svg.select('.areaAbove').attr('d', area);

  points.selectAll('circle').attr("transform", function(d) {
    return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")";
  });
}

var slider = d3.select("body").append("p").append("input")
  .datum({})
  .attr("type", "range")
  .attr("value", zoom.scaleExtent()[0])
  .attr("min", zoom.scaleExtent()[0])
  .attr("max", zoom.scaleExtent()[1])
  .attr("step", (zoom.scaleExtent()[1] - zoom.scaleExtent()[0]) / 100)
  .on("input", slided);

function slided(d) {
  zoom.scale(d3.select(this).property("value"))
    .event(svg);
}




// add cross hairs and floating value on axis
/*
  var chart = d3.select("svg");
  
  var focusText = chart.append("g")
  .attr("class","focus")
  .style("display", "none");

     focusText.append("text").attr({"x": 9, "dy": "0.35em"});
     
     
  var focus = chart.append("g")
   .attr("class","focus")
   .style("display", "none");

      //focus.append("text").attr({"x": 9, "dy": "0.35em"});
     
      // horizontal crosshair 
      focus.append("line")
            .attr({
              "x1": -width,
              "y1": 0,
              "x2": width,
              "y2": 0
            });


      // vertical crosshair
      
      focus.append("line")
              .attr({
                "x1": 0,
                "y1": -height,
                "x2": 0,
                "y2": height
              });
      
      
      chart.append("rect")
          .attr({"class": "overlay" , "width": width , "height": height})
          .on({
            "mouseover": function() { focus.style("display", null);   focusText.style("display", null); },
            "mouseout":  function() { focus.style("display", "none"); focusText.style("display", "none"); }, 
            "mousemove": mousemove
          });
    


  function mousemove() {
   var x0 = xScale.invert(d3.mouse(this)[0]),
          i = bisectDate(sample2, x0, 1),
          d0 = sample2[i-1],
          d1 = sample2[i],
          d = x0 - d0.date &gt; d1.date - x0 ? d1 : d0;
    
   focus.attr("transform", "translate(" + (d3.mouse(this)[0]) + "," + yScale(d.close) + ")");
      focusText.attr("transform", "translate(" + (width-margin.right) + "," + yScale(d.close) + ")");
      focusText.select("text").text("$" + d.close);

  } */</code></pre>
<pre class="snippet-code-css lang-css prettyprint-override"><code>body {
  font: 10px sans-serif;
  margin: 50px;
}

.grid .tick {
  stroke: lightgrey;
  opacity: 0.7;
  shape-rendering: crispEdges;
}

.grid path {
  stroke-width: 0;
}

.axis path {
  fill: none;
  stroke: #bbb;
  shape-rendering: crispEdges;
}

.axis text {
  fill: #555;
}

.axis line {
  stroke: #e7e7e7;
  shape-rendering: crispEdges;
}

.axis .axis-label {
  font-size: 14px;
}

.line {
  fill: none;
  stroke-width: 1.5px;
}

.dot {
  /* consider the stroke-with the mouse detect radius? */
  stroke: transparent;
  stroke-width: 10px;
  cursor: pointer;
}

div.tooltip {
  position: absolute;
  text-align: center;
  width: 60px;
  height: 28px;
  padding: 2px;
  font: 12px sans-serif;
  background: lightsteelblue;
  border: 0px;
  border-radius: 8px;
  pointer-events: none;
}

.areaBelow {
  fill: green !important;
  stroke-width: 0;
}

.areaAbove {
  fill: lightsteelblue;
  stroke-width: 0;
}

.dot:hover {
  stroke: rgba(68, 127, 255, 0.3);
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;


&lt;/script&gt;

&lt;svg width="960" height="1"&gt;&lt;/svg&gt;
&lt;script src="http://d3js.org/d3.v3.js"&gt;&lt;/script&gt;

&lt;body class="sapUiBody" role="application"&gt;
  &lt;div id="container" style="min-width: 310px; margin: 0 auto"&gt;&lt;/div&gt;

  &lt;div id="content"&gt;&lt;/div&gt;
&lt;/body&gt;</code></pre>
</div>
</div>
</p>