简体   繁体   中英

Why is nothing being displayed on my webpage?

The following is my code. Yet when I run it I get a blank page. Why is this the case ? Also how can I use data from hundreds of columns to make a simple interactive visual using d3 ? I would like to add that the following csv file "LoanStats3a.csv" is in the same folder.

 <html>
    <title>Loans</title>
<link href="../css/jquery-ui.css" rel="stylesheet" />
<script src="../Scripts/jquery-1.12.4"></script>  
<script src="../Scripts/jquery-1.12.4.js"></script>
<script src="../Scripts/jquery-ui.js"></script>
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
    <style>
        #LoanStats3a{
           color: blueviolet;   
        }
        </style>
<body>
    <script>
        d3.csv("LoanStats3a", function (file1){
            var bg = d3.select("body").append("svg")
                    .attr("width", 5000)
                    .attr("height", 5000);

        bg.selectAll("rect")
                    .data(file1)
                    .enter()
                    .attr("width", function(d){return d.loan_amnt / 100;})
                    .attr("height", function(d) {return d.term;})
                    .attr("y", function (d,i) {return i *50;})
                    .attr("fill", function (d){"red","blue";});
    }   
    </script>
</body>

This is because after binding the data to your empty selection, you have to append a rect element for each data.

Also, your attribute "fill" is incorrect.

bg.selectAll("rect")
    .data(file1)
    .enter()
    .append("rect") // <= You need to create a rect for each data
    .attr("width", function(d){return d.loan_amnt / 100;})
    .attr("height", function(d) {return d.term;})
    .attr("y", function (d,i) {return i *50;})
    .attr("fill", "blue");

If you want to change the color depending on the data, create a function and return something.

// For example
.attr("fill", function(d){return d.loan_amnt > 25000 ? "blue" : "red"});

Here's a JsFiddle with random data : DEMO

EDIT : If it's still not working, it's probably a problem with your data because the only thing different between our code is that I used custom data in the JsFiddle.

I notice that your csv file doesn't have the extension .csv , it's just LoanStats3a ?
You should do a console.log(file1) , to check if your data are correct.

Take a look at D3 CSV for how to load a csv file.

You are missing a closing ) at the end:

     .attr("fill", function (d){"red","blue";});
   }  
//  ^ Here should be a )
</script>

It helps if you have proper indent:

<script>
d3.csv("LoanStats3a", function(file1) {
    var bg = d3.select("body").append("svg")
        .attr("width", 5000)
        .attr("height", 5000);

    bg.selectAll("rect")
        .data(file1)
        .enter()
        .attr("width", function(d) {
            return d.loan_amnt / 100;
        })
        .attr("height", function(d) {
            return d.term;
        })
        .attr("y", function(d, i) {
            return i * 50;
        })
        .attr("fill", function(d) {
            "red", "blue"; // What is going on here?
                           // Did you for to return?
                           // return "blue";
        });
});
</script>

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