Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript D3:缩放_Javascript_D3.js_Visualization_Data Visualization - Fatal编程技术网

JavaScript D3:缩放

JavaScript D3:缩放,javascript,d3.js,visualization,data-visualization,Javascript,D3.js,Visualization,Data Visualization,我有这个HTML页面,但我刚刚实现了一个新功能:当我将鼠标悬停在图形上时,创建一条垂直线来打印y轴的当前值。但是,添加此功能后,我无法再使用鼠标滚轮缩放图表。我该怎么做?谢谢 这是HTML页面: <!DOCTYPE html> <meta charset="utf-8"> <style> .area { fill: #ffe368; opacity: 0.6; clip-path: url(#clip); } .areax

我有这个HTML页面,但我刚刚实现了一个新功能:当我将鼠标悬停在图形上时,创建一条垂直线来打印
y
轴的当前值。但是,添加此功能后,我无法再使用鼠标滚轮缩放图表。我该怎么做?谢谢

这是HTML页面:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .area {
    fill: #ffe368;
    opacity: 0.6;
    clip-path: url(#clip);
  }

  .areax {
    fill: #8cffa4;
    opacity: 0.6;
    clip-path: url(#clip);
  }

  .zoom {
    cursor: move;
    fill: none;
    pointer-events: all;
  }

  body {
    background-color: #F1F3F3
  }

  .axis {
    font: 10px sans-serif;
  }

  .axis path,
  .axis line {
    fill: none;
    stroke: #D4D8DA;
    stroke-width: 2px;
    shape-rendering: crispEdges;
  }

  .line {
    fill: none;
    stroke: #6F257F;
    stroke-width: 5px;
  }

  .overlay {
    fill: none;
    pointer-events: all;
  }

  .focus circle {
    fill: #F1F3F3;
    stroke: #6F257F;
    stroke-width: 5px;
  }

  .hover-line {
    stroke: #6F257F;
    stroke-width: 2px;
    stroke-dasharray: 3, 3;
  }
</style>
<svg width="1200" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
  var svg = d3.select("svg"),
    margin = {
      top: 20,
      right: 20,
      bottom: 110,
      left: 40
    },
    margin2 = {
      top: 430,
      right: 20,
      bottom: 30,
      left: 40
    },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

  var parseDate = d3.timeParse("%b %Y")
  bisectDate = d3.bisector(function(d) {
    return d.date;
  }).left;;

  var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

  var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

  var brush = d3.brushX()
    .extent([
      [0, 0],
      [width, height2]
    ])
    .on("brush end", brushed);

  var zoom = d3.zoom()
    .scaleExtent([1, 10])
    .translateExtent([
      [0, 0],
      [width, height]
    ])
    .extent([
      [0, 0],
      [width, height]
    ])
    .on("zoom", zoomed);

  var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x(d.date);
    })
    .y0(height)
    .y1(function(d) {
      return y(d.price);
    });

  var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x2(d.date);
    })
    .y0(height2)
    .y1(function(d) {
      return y2(d.price);
    });

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

  var g = svg.append("g") // riferito al primo piano
    // .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  var context = svg.append("g") // riferito al secondo piano
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

  var data = [
    {date: 'Jan 2000', price: 9}, 
    {date: 'Feb 2000', price: 20}, 
    {date: 'Mar 2000', price: 2}, 
    {date: 'Apr 2000', price: 9}, 
    {date: 'May 2000', price: 11}, 
    {date: 'Jun 2000', price: 12}, 
    {date: 'Jul 2000', price: 21}, 
    {date: 'Aug 2000', price: 9}, 
    {date: 'Sep 2000', price: 15}, 
    {date: 'Oct 2000', price: 6}, 
    {date: 'Nov 2000', price: 49}, 
    {date: 'Dec 2000', price: 48}, 
    {date: 'Jan 2001', price: 55}, 
    {date: 'Feb 2001', price: 20}, 
    {date: 'Mar 2001', price: 2}, 
    {date: 'Apr 2001', price: 11}, 
    {date: 'May 2001', price: 49}, 
    {date: 'Jun 2001', price: 9}, 
    {date: 'Jul 2001', price: 32}, 
    {date: 'Aug 2001', price: 31}, 
    {date: 'Sep 2001', price: 12}, 
    {date: 'Oct 2001', price: 34}, 
    {date: 'Nov 2001', price: 11}, 
    {date: 'Dec 2001', price: 22}
  ];

  var datax = [
    {date: 'Jan 2000', price: 55}, 
    {date: 'Feb 2000', price: 3}, 
    {date: 'Mar 2000', price: 22}, 
    {date: 'Apr 2000', price: 2}, 
    {date: 'May 2000', price: 11}, 
    {date: 'Jun 2000', price: 23}, 
    {date: 'Jul 2000', price: 21}, 
    {date: 'Aug 2000', price: 19}, 
    {date: 'Sep 2000', price: 15}, 
    {date: 'Oct 2000', price: 16}, 
    {date: 'Nov 2000', price: 9}, 
    {date: 'Dec 2000', price: 18}, 
    {date: 'Jan 2001', price: 55}, 
    {date: 'Feb 2001', price: 20}, 
    {date: 'Mar 2001', price: 2}, 
    {date: 'Apr 2001', price: 33}, 
    {date: 'May 2001', price: 31}, 
    {date: 'Jun 2001', price: 9}, 
    {date: 'Jul 2001', price: 32}, 
    {date: 'Aug 2001', price: 7}, 
    {date: 'Sep 2001', price: 12}, 
    {date: 'Oct 2001', price: 2}, 
    {date: 'Nov 2001', price: 2}, 
    {date: 'Dec 2001', price: 3}
  ];

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  datax.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

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

  x2.domain(x.domain());
  y2.domain(y.domain());

  g.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area);

  g.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area);

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

  /*focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis); */

  g.append("g")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y).ticks(6).tickFormat(function(d) {
      return parseInt(d / 1000) + "k";
    }))
    .append("text")
    .attr("class", "axis-title")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .attr("fill", "#5D6971")
    .text("Visitatori)");

  var focus = g.append("g")
    .attr("class", "focus")
    .style("display", "none");

  focus.append("line")
    .attr("class", "x-hover-line hover-line")
    .attr("y1", 0)
    .attr("y2", height);

  focus.append("line")
    .attr("class", "y-hover-line hover-line")
    .attr("x1", width)
    .attr("x2", width);

  focus.append("circle")
    .attr("r", 7.5);

  focus.append("text")
    .attr("x", 15)
    .attr("dy", ".31em");

  context.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area2);

  context.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area2);

  context.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height2 + ")")
    .call(xAxis2);

  context.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.move, x.range());

  svg.append("rect")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .attr("class", "overlay")
    .attr("class", "zoom")
    .attr("width", width)
    .attr("height", height)
    .call(zoom)
    // .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .on("mouseover", function() {
      focus.style("display", null);
    })
    .on("mouseout", function() {
      focus.style("display", "none");
    })
    .on("mousemove", mousemove);

  function brushed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
    var s = d3.event.selection || x2.range();
    x.domain(s.map(x2.invert, x2));
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
  }

  function zoomed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
    var t = d3.event.transform;
    x.domain(t.rescaleX(x2).domain());
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
  }

  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.attr("transform", "translate(" + x(d.date) + "," + y(d.price) + ")");
    focus.select("text").text(function() {
      return d.price;
    });
    focus.select(".x-hover-line").attr("y2", height - y(d.price));
    focus.select(".y-hover-line").attr("x2", width + width);
  }
</script>

.区域{
填充:#368;
不透明度:0.6;
剪辑路径:url(#剪辑);
}
.区域{
填充:#8cffa4;
不透明度:0.6;
剪辑路径:url(#剪辑);
}
.zoom{
光标:移动;
填充:无;
指针事件:全部;
}
身体{
背景色:#F1F3
}
.安讯士{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
冲程:#D4D8DA;
笔画宽度:2px;
形状渲染:边缘清晰;
}
.线路{
填充:无;
冲程:#6F257F;
笔画宽度:5px;
}
.覆盖{
填充:无;
指针事件:全部;
}
.焦点圈{
填充:#f3;
冲程:#6F257F;
笔画宽度:5px;
}
.悬停线{
冲程:#6F257F;
笔画宽度:2px;
笔划数组:3,3;
}
var svg=d3。选择(“svg”),
保证金={
前20名,
右:20,,
底部:110,
左:40
},
边缘2={
排名:430,
右:20,,
底数:30,
左:40
},
宽度=+svg.attr(“宽度”)-margin.left-margin.right,
高度=+svg.attr(“高度”)-margin.top-margin.bottom,
height2=+svg.attr(“height”)-margin2.top-margin2.bottom;
var parseDate=d3.timeParse(“%b%Y”)
平分日期=d3.平分线(函数(d){
返回日期;
}).左;;
var x=d3.scaleTime().range([0,宽度]),
x2=d3.scaleTime().range([0,宽度]),
y=d3.scaleLinear().range([height,0]),
y2=d3.scaleLinear().range([height2,0]);
var xAxis=d3.axisBottom(x),
xAxis2=d3.轴底(x2),
yAxis=d3。轴左(y);
var brush=d3.brushX()
.范围([
[0, 0],
[宽度、高度2]
])
.在(“刷端”,刷过的);
var zoom=d3.zoom()
.scaleExtent([1,10])
.翻译范围([
[0, 0],
[宽度、高度]
])
.范围([
[0, 0],
[宽度、高度]
])
。打开(“缩放”,缩放);
var area=d3.area()
.curve(d3.curveMonotoneX)
.x(功能(d){
返回x(d.日期);
})
.y0(高度)
.y1(功能(d){
返回y(d.价格);
});
var area 2=d3.面积()
.curve(d3.curveMonotoneX)
.x(功能(d){
返回x2(d.日期);
})
.y0(高度2)
.y1(功能(d){
返回y2(d.价格);
});
svg.append(“defs”).append(“clipPath”)
.attr(“id”、“剪辑”)
.append(“rect”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var g=svg.append(“g”)//riferito al-primo piano
//.attr(“类”、“焦点”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
var context=svg.append(“g”)//riferito al secondo piano
.attr(“类”、“上下文”)
.attr(“transform”、“translate”(+margin2.left+)、“+margin2.top+”);
风险值数据=[
{日期:2000年1月,价格:9},
{日期:2000年2月,价格:20},
{日期:2000年3月,价格:2},
{日期:2000年4月,价格:9},
{日期:2000年5月,价格:11},
{日期:2000年6月,价格:12},
{日期:2000年7月,价格:21},
{日期:2000年8月,价格:9},
{日期:2000年9月,价格:15},
{日期:2000年10月,价格:6},
{日期:2000年11月,价格:49},
{日期:2000年12月,价格:48},
{日期:2001年1月,价格:55},
{日期:2001年2月,价格:20},
{日期:2001年3月,价格:2},
{日期:2001年4月,价格:11},
{日期:2001年5月,价格:49},
{日期:2001年6月,价格:9},
{日期:2001年7月,价格:32},
{日期:2001年8月,价格:31},
{日期:2001年9月,价格:12},
{日期:2001年10月,价格:34},
{日期:2001年11月,价格:11},
{日期:2001年12月,价格:22}
];
var datax=[
{日期:2000年1月,价格:55},
{日期:2000年2月,价格:3},
{日期:2000年3月,价格:22},
{日期:2000年4月,价格:2},
{日期:2000年5月,价格:11},
{日期:2000年6月,价格:23},
{日期:2000年7月,价格:21},
{日期:2000年8月,价格:19},
{日期:2000年9月,价格:15},
{日期:2000年10月,价格:16},
{日期:2000年11月,价格:9},
{日期:2000年12月,价格:18},
{日期:2001年1月,价格:55},
{日期:2001年2月,价格:20},
{日期:2001年3月,价格:2},
{日期:2001年4月,价格:33},
{日期:2001年5月,价格:31},
{日期:2001年6月,价格:9},
{日期:2001年7月,价格:32},
{日期:2001年8月,价格:7},
{日期:2001年9月,价格:12},
{日期:2001年10月,价格:2},
{日期:2001年11月,价格:2},
{日期:2001年12月,价格:3}
];
data.forEach(函数(d){
d、 日期=解析日期(d.date);
d、 价格=+d.价格;
});
datax.forEach(函数(d){
d、 日期=解析日期(d.date);
d、 价格=+d.价格;
});
x、 域(d3)。范围(数据,函数(d){
返回日期;
}));
y、 域([d3.min(数据,函数(d)){
返回d.price;
}),d3.max(数据,函数(d){
返回d.price;
})]);
x2.域(x.域());
y2.域(y.域());
g、 附加(“路径”)
.基准(数据)
.attr(“类别”、“区域”)
.attr(“d”,区域);
g、 附加(“路径”)
.基准面(数据x)
.attr(“类别”、“区域”)
.attr(“d”,区域);
g、 附加(“g”)
.attr(“类”、“轴--x”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
/*focus.append(“g”)
.attr(“类”、“轴--y”)
.呼叫(yAxis)*/
g、 附加(“g”)
.attr(“类”、“轴--y”)
.call(d3.axisLeft(y).ticks(6).tickFormat(函数(d){
返回parseInt(d/1000)+“k”;
}))
.append(“文本”)
.attr(“类”、“轴标题”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,6)
.attr(“dy”,“.71em”)
.style(“文本锚定”、“结束”)
.attr(“填充”、“5D6971”)
.文本(“Visitatori”);
var focus=g.append(“g”)
.
g.select(".area").attr("d", area);
g.select(".areax").attr("d", area);
g.select(".axis--x").call(xAxis);