-1

I am new in d3.js and i am trying to make a d3.js program in JSP. I have a two files 1) HTML 2) javascript

now when i linked javascript in HTML file then it shows pie chart.

Problem: when i am trying to run this code in jsp and its not working? I dont understand that just by changing the extension from html to jsp, why the program was stop showing pie chart?

your suggestions would be helpful...

this is the code-

This is the JSP file

<html>
<head>
<link rel="stylesheet" type="text/css" href="background.css" />
<script type="text/javascript" src="libss/d3.js" charset="UTF-8"></script>
<script type="text/javascript" src="libss/d3.min.js" charset="UTF-8"></script>
<script src="http://d3js.org/d3.v3.js" charset="UTF-8"></script>
</style>
</head>
<body>
<script type="text/javascript" src=" E:\javaa\data_vis\data-vis-new\libss\temp.js">        
 </script>  
<script>     
dataset = [ 7, 66, 20, 45, 6, 25 ];
</script>
 </body>
</html>

and this is temp.js file-

//Width and height
var w = 300;
var h = 300;
var outerRadius = w / 2;
var innerRadius = 0;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie();
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.category10();
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);

//Set up groups
var arcs = svg.selectAll("g.arc")
.data(pie(dataset))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");

//Draw arc paths
arcs.append("path")
.attr("fill", function(d, i) {
    return color(i);
})
.attr("d", arc);

//Labels
arcs.append("text")
.attr("transform", function(d) {
    return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
    return d.value;
});

2 Answers2

2

This is because you didn't put the script tag in the right place.

Solution #1

Simply try move your scripts lib into bottom of body:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>

  </head>

  <body> 

    ...
    // try import it here
    <script type="text/javascript" src="lib/pie.js"></script>

  </body>

</html>

plnkr

Solution #2

If you insists putting the lib into head, try adding a defer attribute in script element:

<head>
    <meta charset="utf-8">
    <title>D3: Pie layout</title>
    <script type="text/javascript" src="http://d3js.org/d3.v3.min.js" defer></script>
    <script type="text/javascript" src="lib/pie.js" defer></script>
    <script>
      dataset = [1,2,3,4,5];
     </script>
</head>

For reasons why your code can not work, see this thread:

Where is the best place to put tags in HTML markup

Community
  • 1
  • 1
huan feng
  • 7,307
  • 2
  • 32
  • 56
1

I get the answer. I did not get the logic my program start working without any problem.

Here is the solution-

just placed your own .js file below the dataset variable like this...

<body>
<script>     
dataset = [ 7, 66, 20, 45, 6, 25 ];
</script>
<script type="text/javascript" src=" E:\javaa\data_vis\data-vis-new\libss\temp.js">        
 </script>  
</body>

if u dont get it just comment above with @ and my name and will tell you later...