I am encountering a common issue with my code while constructing a Treemap layout using d3.js. The code seems to only work when an alert statement is included in it. Let me briefly outline the structure of the code below,
default.HTML file and FetchData.java
- The default HTML file contains a form with 3 radio buttons and 2 text boxes (used for date filtering).
- There is a button that triggers the loadNewData() function upon clicking.
- The loadNewData() function makes an AJAX call to a servlet named "FetchData.java" with query parameters from the text boxes.
- The servlet, "FetchData.java", connects to the database on each click event, extracts data, and writes it to a file called "OutputJSON". A unique random number is generated for every click event.
- The filename is then sent back to the AJAX call as input to another JS function called wrapperFunc(str).
- This function utilizes d3.json by taking the file URL (json data file) and generates a treemap visualization.
Step 6 functions correctly when an alert statement precedes the call to wrapperFunc(str). If the alert is removed, the functionality fails.
<!-- DEFAULT.HTML -->
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
<script type="text/javaScript" src="d3/d3.js"></script>
<script>
[JavaScript code...]
</script>
</head>
<body>
[Form elements and JavaScript code...]
</body>
</html>
FetchData.java - is a servlet that retrieves the required data from the database and writes it to a file
/*
* Java Servlet code snippet
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aol.poc.d3js.dbConnect.NetezzaJdbcConnection;
import com.aol.poc.d3js.properties.*;
import com.aol.poc.d3js.treemap.CsvToJson_V2;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet(name = "FetchData", urlPatterns = {"/FetchData"})
public class FetchData extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String from="",to="",filetod3="";
from = request.getParameter("from");
to = request.getParameter("to");
response.setContentType("text/html");
response.getWriter().write(generateJSONDataFile(from,to));
response.getWriter().flush();
}
public String generateJSONDataFile(String from,String to){
NetezzaJdbcConnection njc = new NetezzaJdbcConnection();
CsvToJson_V2 readCsv = new CsvToJson_V2();
if(from == null || to == null){
System.out.println("Inside if null");
njc.extractData();
}
else{
System.out.println("Inside else");
njc.extractData(from,to);
}
String filetod3 = readCsv.readCsv();
return filetod3;
}
}