Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Svg_D3.js_Zooming - Fatal编程技术网

Javascript D3变焦不工作

Javascript D3变焦不工作,javascript,svg,d3.js,zooming,Javascript,Svg,D3.js,Zooming,我知道这里有一个简单的解决方案,但我尝试运行了一些教程,但未能将它们应用到我的代码中。我通过为3-5个数据点的每一段绘制一个单独的区域,创建了一个立面图。所以,我在同一张图上有很多情节。正因为如此,我尝试了缩放svg元素的技术,但结果却是空的。这是我的最新尝试。希望你们能帮忙 当前,当我尝试缩放时,它会将x和y比例重置为[0,1],并使用填充颜色填充整个区域 这里有一个 /******************************// //设立// // *******************

我知道这里有一个简单的解决方案,但我尝试运行了一些教程,但未能将它们应用到我的代码中。我通过为3-5个数据点的每一段绘制一个单独的
区域
,创建了一个立面图。所以,我在同一张图上有很多情节。正因为如此,我尝试了缩放
svg
元素的技术,但结果却是空的。这是我的最新尝试。希望你们能帮忙

当前,当我尝试缩放时,它会将x和y比例重置为[0,1],并使用填充颜色填充整个区域

这里有一个

/******************************//
//设立//
// ***************************** //
//***映射变量***
L.mapbox.accessToken='pk.eyJ1Ijoid2lsbGlhbWx1Y2UiLCJhIjoiNE1zU0xMNCJ9.X9y-S0ubezlH-aefwwuzsla';
var map=L.mapbox.map('map','examples.map-i86nkdio')
//***图表变量***
var margin={顶部:10,右侧:20,底部:30,左侧:100},
宽度=1100-margin.left-margin.right,
高度=150-margin.top-margin.bottom;
//将颜色映射到限制
var color=d3.scale.ordinal()
.domain([-10,-5,0,5,10])
.范围(['a1d99b'、'c7e9c0'、'fdd0a2'、'fdae6b'、'fd8d3c'、'e6550d');
//设置图表相对于div的大小
var x=d3.scale.linear()范围([0,宽度]);
变量y=d3.scale.linear().range([height,0]);
//定义轴的外观
var xAxis=d3.svg.axis().scale(x)、orient(“bottom”)、ticks(5);
var yAxis=d3.svg.axis().scale(y)、orient(“left”)、ticks(5);
//定义一个区域。区域被颜色填充。
var area=d3.svg.area()
.x(函数(d){返回x(d.distance);})
.y0(高度)
.y1(函数(d){returny(d.elevation);});
//界定界线
var valueline=d3.svg.line()
.插入(“线性”)
.x(函数(d){返回x(d.distance);})
.y(函数(d){返回y(d.elevation);})
//设置SVG元素
var svg=d3。选择(“图表容器”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”)
.呼叫(缩放);
//定义缩放行为
var zoom=d3.behavior.zoom()
.x(x)
.y(y)
.scaleExtent([1,10])
。打开(“缩放”,缩放);
//计算数据组的平均梯度。
函数dataGroupGradient(数据组)
{
var sum=数据组[0]。梯度;
对于(var i=1;i
D3的电子秤文件附在电子秤上,规定:

#zoom.x([x])

…如果电子秤的域或范围按程序修改
// ***************************** //
//             Set Up            //
// ***************************** //

// *** MAP VARIABLES ***

L.mapbox.accessToken = 'pk.eyJ1Ijoid2lsbGlhbWx1Y2UiLCJhIjoiNE1zU0xMNCJ9.X9y-S0ubezlH-aefwUZslA';
var map = L.mapbox.map('map', 'examples.map-i86nkdio')



// *** CHART VARIABLES ***

var margin = {top: 10, right: 20, bottom: 30, left: 100},
    width = 1100 - margin.left - margin.right,
    height = 150 - margin.top - margin.bottom;

// Map colors to limits
var color = d3.scale.ordinal()
    .domain([-10,-5,0,5,10])
    .range(['#a1d99b','#c7e9c0','#fdd0a2','#fdae6b','#fd8d3c','#e6550d']);

// Set up the size of the chart relative to the div
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the look of the axis
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y).orient("left").ticks(5);

// Define an area. Areas are filled with color.
var area = d3.svg.area()
    .x(function(d) { return x(d.distance); })
    .y0(height)
    .y1(function(d) { return y(d.elevation); });

// Define the line
var valueline = d3.svg.line()
    .interpolate("linear")
    .x(function(d) { return x(d.distance); })
    .y(function(d) { return y(d.elevation); })

// Set up the SVG element
var svg = d3.select("#chart-container")
    .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 + ")")
    .call(zoomed);

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

// Caculate the average gradient of a dataGroup.
function dataGroupGradient(dataGroup)
{
  var sum = dataGroup[0].gradient;
  for (var i = 1; i < dataGroup.length; i++)
  {
    sum += parseFloat(dataGroup[i].gradient);  
  }
  return sum/dataGroup.length;
}

// Define polyline options
// http://leafletjs.com/reference.html#polyline
var polyline_options = {
    stroke: true,
    weight: 3,
    fill: true,
    color: '#03f'
};

// Declare an array for holding the points that make up the path on the map
var line_points = [];


// ***************************** //
//     WORKING WITH THE DATA     //
// ***************************** //

// Get the data
d3.csv("first5km_Strade_Bianche.csv", function(error, data) {
    data.forEach(function(d) {
        d.distance = +d.distance;
        d.elevation = +d.elevation;
        d.gradient = +d.gradient;
        d.latitude = +d.latitude;
        d.longitude = +d.longitude;
        line_points.push([d.latitude, d.longitude]);
    });

    // Scale the range of the entire chart
    x.domain(d3.extent(data, function(d) { return d.distance; }));
    y.domain([0, d3.max(data, function(d) { return d.elevation; })]);

    // Add the overall valueline path. This path uses all of the data.
    svg.append("path")
        .attr("d", valueline(data));

    // Split the data based on "group"
    var dataGroup = d3.nest()
        .key(function(d) {
            return d.group;
        })
        .entries(data);

    // To remove white space between dataGroups, append the first element of one
    // dataGroup to the last element of the previous dataGroup.
    dataGroup.forEach(function(group, i) {
      if(i < dataGroup.length - 1) {
        group.values.push(dataGroup[i+1].values[0])
      }
    })

    // Draw the array of line_points to the map and fit the bounds.
    var polyline = L.polyline(line_points, polyline_options).addTo(map);
    map.fitBounds(polyline.getBounds());

    // Add a line and an area for each dataGroup
    dataGroup.forEach(function(d, i){
        svg.append("path")
            .datum(d.values)
            .attr("class", "area")
            .attr("d", area);
        });

    // Fill the dataGroups with color
    svg.selectAll(".area")
        .style("fill", function(d) { return color(dataGroupGradient(d)); });

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

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);

    // Add the text label for the X axis
    svg.append("text")
        .attr("transform",
              "translate(" + (width/2) + " ," + 
                             (height+margin.bottom) + ")")
        .style("text-anchor", "middle")
        .text("Distance");

    // Add the text label for the Y axis
    svg.append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("x", margin.top - (height / 2))
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("");

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

});

// *********************** //
//  Zoom specific updates  //
// *********************** //
function zoomed() {
    svg.select(".x.axis").call(xAxis);
    svg.select(".y.axis").call(yAxis);   
    svg.selectAll('path.line').attr('d', line);   
}