3

I'm learning to use d3.js with a tutorial.

The following code is entered in the javascript console (Chrome) and it creates a line graph.

How do now take this code and make it run in an html page?

 var lineData = [ { "x": 1,   "y": 5},  { "x": 20,  "y": 20},
                  { "x": 40,  "y": 10}, { "x": 60,  "y": 40},
                  { "x": 80,  "y": 5},  { "x": 100, "y": 60}];

 //This is the accessor function we talked about above
 var lineFunction = d3.svg.line()
                          .x(function(d) { return d.x; })
                          .y(function(d) { return d.y; })
                         .interpolate("linear");

//The SVG Container
var svgContainer = d3.select("body").append("svg")
                                    .attr("width", 200)
                                    .attr("height", 200);

//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
                            .attr("d", lineFunction(lineData))
                            .attr("stroke", "blue")
                            .attr("stroke-width", 2)
                            .attr("fill", "none");
1
  • Put the code in a script tag and put that tag at the end of the body. Commented Feb 5, 2014 at 18:28

1 Answer 1

9
  1. Create a file called yourfile.html.
  2. Add the basic tags like html, head and body.
  3. Include the d3 JavaScript in a script tag with src="http://d3js.org/d3.v3.min.js".
  4. Add your code above into another script and the end of the body tag.
  5. Open yourfile.html in a browser.

All in all, your file may look like this:

<!DOCTYPE html>
<html>
<head>
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
    <script type="text/javascript">
        var lineData = [ { "x": 1,   "y": 5},  { "x": 20,  "y": 20}, { "x": 40,  "y": 10}, { "x": 60,  "y": 40}, { "x": 80,  "y": 5},  { "x": 100, "y": 60}];

        //This is the accessor function we talked about above
        var lineFunction = d3.svg.line().x(function(d) { return d.x; }).y(function(d) { return d.y; }).interpolate("linear");

        //The SVG Container
        var svgContainer = d3.select("body").append("svg").attr("width", 200).attr("height", 200);

        //The line SVG Path we draw
        var lineGraph = svgContainer.append("path").attr("d", lineFunction(lineData)).attr("stroke", "blue").attr("stroke-width", 2).attr("fill", "none");
    </script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

For more on setting up and getting started, this tutorial is a good reference.
Yes, that did the job, thank you. I was linking to my local version of d3.v3.js, and that doesn't work. My local version of d3.v3.min.js works. Is there a difference between the two files other than no spaces, smaller size, etc??
No, they should be totally the same in their functionality.
@Charlie...you need to add <meta charset="utf-8">. Read explanation here and here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.