简体   繁体   中英

Displaying SVG elements with D3 and d3.slider: Uncaught ReferenceError: svg is not defined

Hey Im' currently experimenting with this D3 Example and I want to implement my own slider which is showing data of a json File (I included everything in a github repo because I don't know how to show you my working files and not spend to much space for it - especially the json file. Any tips for the future?). So basically I have my bubble.html:

<!DOCTYPE html>
<head>
    <title>D3 Mapping Timeline</title>
<meta charset="utf-8">
<link rel="stylesheet" href="d3.slider.css" />
<style>
path {
  fill: none;
  stroke: #333;
  stroke-width: .5px;
}
.land-boundary {
  stroke-width: 1px;
}
.county-boundary {
  stroke: #ddd;
}
.site {
    stroke-width: .5px;
  stroke: #333;
  fill: #9cf;
}
#slider3 {
  margin: 20px 0 10px 20px;
  width: 900px;
}
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="d3.slider.js"></script>
</head>


<body>

<div id="slider3"></div>



<script>
var width = 1240,
    height = 720;
var projection = d3.geo.mercator()
    .translate([width / 2, height / 2])
    .scale((width - 1) / 2 / Math.PI);
d3.json("vorfaelle.json", function(error, data){
    console.log(data.features[1].geometry.coordinates);
    window.site_data = data;
});
var displaySites = function(data) {
  var sites = svg.selectAll(".site")
            .data(data);
  sites.enter().append("circle")
      .attr("class", "site")
      .attr("cx", function(d) {
                for (var i = 0; i < d.features.length+1; i++) {
                    console.log(d.features[i].geometry.coordinates[0]);
                    return projection(d.features[i].geometry.coordinates[0])
                    //return projection([d.lng, d.lat])[0];
                }
      })
      .attr("cy", function(d) {
                for (var i = 0; i < d.features.length+1; i++) {
                    console.log(d.features[i].geometry.coordinates[1]);
                    return projection([d.features[i].geometry.coordinates[1]])
                    //return projection([d.lng, d.lat])[0];
                }
      })
      .attr("r", 1)
      .transition().duration(400)
        .attr("r", 5);
  sites.exit()
    .transition().duration(200)
      .attr("r",1)
      .remove();
};
// var minDateUnix = moment('2014-07-01', "YYYY MM DD").unix();
// var maxDateUnix = moment('2015-07-21', "YYYY MM DD").unix();
var dateParser = d3.time.format("%d.%m.%Y").parse;
var minDate = dateParser("01.01.2015");
var maxDate = dateParser("31.12.2015");
console.log(minDate);
var secondsInDay = 60 * 60 * 24;
d3.select('#slider3').call(d3.slider()
  .axis(true).min(minDate).max(maxDate).step(1)
  .on("slide", function(evt, value) {
    var newData = _(site_data).filter( function(site) {
            console.log(site)
            // for (var i = 0; i < site.features.length; i++) {
            //  return site.features[i].properties.date < value;
            // }
    })
    console.log("New set size ", newData.length);
    displaySites(newData);
  })
);
</script>
</body>

which is getting the data from the json (on my repo vorfaelle.json). Now when I move the slider handle it "crashes" and gives me this error: 在此处输入图片说明 What's exactly wrong? Is it because i did not read the data properly?

Add the svg element to the dom and assign it to a variable so that you can use it your code.

var svg = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM