简体   繁体   中英

How to make a chart in D3 work in node.js

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="d3/d3.min.js"></script>
</head>
<body>

<script type="text/javascript">
    var dataA=[10,20];
    d3.select("body").style("background-color", "green");

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

    var bars = canvas.selectAll("rect")
                     .data(dataA)
                     .enter()
                     .append("rect")
                     .attr("width",function (d){ return d*5;})
                     .attr("height",10)
                     .attr("y",function(d,i){ return i*20;});

</script>

</body>

This is the code I used for drawing a bar char in d3. how can I use node.js to do the same chart?

The question is not clear enough, whether you want to just use node or node as a server and a client code. But still, you can go ahead with the two use cases given below.

  1. Server side scripting to generate a chart and take a snapshot: I think yes, you can use node and phantomjs and node_phantomjs
  2. If you have both client and server side code: You can render you web page using node and expressjs

You can keep your D3 code as it is. But, if you want to play with dynamic data being sent from the server, just have a look at full fledged MEAN framework.

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