JSON data not loading into Google Charts Geo Map Visual - java

I am trying to pass a JSON file into a Google Charts Geo Map but nothing is return after running the script.
Here is the JSON for states and the amount of orders (orders.json):
{"columns":["state","orders"],"data":[["US-SC",245],["US-CA",2866],["US-MD",411],["US-VA",552], ["US-CO",390],["US-NY",1529],["US-FL",1534],["US-TX",1588],["US-ID",76],["US-CT",275],["US-PA",926],["US-WV",88],["US-HI",141],["US-NJ",833],["US-MI",507],["US-KS",158],["US-WA",476],["US-MN",289],["US-OH",682],["US-GA",574],["US-KY",209],["US-WI",282],["US-IL",866],["US-OK",170],["US-MA",643],["US-RI",85],["US-NC",536],["US-MS",103],["US-OR",207],["US-AZ",382],["US-NV",168],["US-TN",374],["US-AR",131],["US-AL",233],["US-MO",353],["US-WY",14],["US-IA",143],["US-IN",300],["US-LA",237],["US-ME",65],["US-AK",44],["US-UT",134],["US-ND",44],["US-NE",105],["US-BC",29],["US-MT",52],["US-DE",68],["US-SD",31],[null,91],["US-ON",69],["US-DC",68],["US-Jersey",1],["US-Taipei",1],["US-NH",89],["US-VI",5],["US-NM",63],["US-Dubai",5],["US-AB",22],["US-AP",8],["US-Wan Chai",1],["US-VT",18],["US-AE",11],["US-West Midlands",2],["US-Stafford",1],["US-Glos",1],["US-QC",19],["US-SK",4],["US-VIC",8],["US-London",3],["US-QLD",5],["US-Nottinghamshire",1],["US-England",11],["US-NRW",2],["US-PR",15],["US-Kwun Tong",1],["US-Isle of Wight",1],["US-NSW",14],["US-N\/A",3],["US-CORK",1],["US-Oxfordshire",1],["US-MEX",1],["US-NT",1],["US-Guam",1],["US-Powys",1],["US-NS",9],["US-NB",1],["US-Tenessee",1],["US-AS",1],["US-BAYERN",1],["US-Co. Dublin",2],["US-Central and Western",3],["US-MB",3],["US-Tamuning",1],["US-Kowloon City",1],["US-PE",1],["US-COUNTY DURHAM",1],["US-3892",1],["US-GU",2],["US-ALICANTE",1],["US-Cambridgeshire",1],["US-AA",1],["US-WEST GLAMORGAN",1],["US-Warwickshire",1],["US-Gelderland",1],["US-NL",3],["US-Gujarat",2],["US-Tokyo",1],["US-MOR",1],["US-Abu Dhabi",1],["US-Co. Clare",1],["US-West lothian",1],["US-Worcestershire",1],["US-Northamptonshire",1],["US-Essex",1],["US-PI",1],["US-Z\u00fcrich",1],["US-Merseyside",2],["US-Durham",1],["US-Co. Kildare",2],["US-Hamilton",1],["US-TO",2],["US-Buenos Aires",1],["US-Lagos",1],["US-Greater London",1],["US-Shetland Islands",1],["US-HK",1],["US-Co. Waterford",1],["US-SA",2],["US-West Yorkshire",1],["US-3906",1],["US-Surrey",1],["US-FM",1],["US-Warrington",1],["US-ACT",1],["US-BW",1],["US-PUE",1],["US-Ash Sh\u0101riqah",1],["US-Co. Kerry",1]]}
Here is the script for creating the Geo Map:
<!DOCTYPE html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
google.charts.load('visualization', '1', {'packages': ['geochart']});
google.charts.setOnLoadCallback(drawStatesMap);
function drawStatesMap() {
var jsonData = $.ajax({
data: 'orders.json',
dataType: "json"
success: function(json) {
console.log(jsonData);
}
}).responseText;
var statesData = google.visualization.DataTable(jsonData);
// var statesData = google.visualization.DataTable(jsonData);
var chart = google.visualization.GeoChart(document.getElementById('chart_div'));
var options = {region: 'US', resolution: 'provinces'};
var dimension = "orders";
chart.draw(statesData, options);
};
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
I was expecting to have an interactive Google Geo Map but nothing is returned to Google CHrome when running the above script.

Related

How to handle Enums from backend in frontend

I use at frontend AngularJS 1.4 and at Backend Java and I have a lot of options which can be selected in frontend, e.g. the country:
GERMANY
FRANCE
USA
RUSSIA
Enums are written in upper case and I will customize this in frontend (e.g. FRANCE will become to France).
My question now would be if there is a directive or a any other support doing this in frontend.
In view:
<select ng-model="selectedCountry" ng-options="country as country.name for country in countries | filter:filterUpperCamelCase"></select>
In controller:
$scope.countries= [
{name:"SPAIN"},
{name:"GERMANY"}
];
$scope.filterUpperCamelCase = function(element){
element.name = element.name.toLowerCase();
element.name = element.name.charAt(0).toUpperCase() + element.name.slice(1);
return element;
};
Check out this link
http://hello-angularjs.appspot.com/angularjs-create-custom-filter
It has a custom filter written which can help you out.
Here's code from link given above.
<!DOCTYPE html>
<html ng-app="HelloApp">
<head>
<title></title>
</head>
<body ng-controller="HelloCtrl">
<form>
<input type="text" ng-model="name"/>
</form>
<div>{\{name|titlecase}}</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script type="text/javascript">
// Code defining custom module consisting of a filter
// The module needs to be included as dependency for using the filter, titlecase
//
angular.module('CustomFilterModule', [])
.filter( 'titlecase', function() {
return function( input ) {
return input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
});
// Angular App on this page
// Included CustomFilterModule as dependency
//
angular.module('HelloApp', [ 'CustomFilterModule'])
.controller('HelloCtrl', ['$scope', function($scope){
$scope.name = '';
}])
</script>
</body>
</html>

Is it possible to get Javascript array in JSP page

I am developing a web application in which I which I have a JavaScript array and I want this array in my JSP page and iterate it save the data in database.
var value = response;
for (var key in value) {
if (value.hasOwnProperty(key)) {
alert(key + " -> " + value[key]);
var sample = new Array();
sample=value[key];
console.log(sample);
// document.getElementById('');
}
}
});
I want this data on my JSP page is it possible. If it is possible, then how?
You can have javascript in jsp but that will always execute on client side. So if you want to save some stuff on trigger of some event inside browser you can do that by making ajax call or form submission. Ajax is preferred way because its faster and better experience for user
But if you have intention of execution of javascript on server side thats not possible
Java script client side script whereas jsp is server side script so you can't simply do this.
What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server to store in database.
Client Side:
<script type="text/javascript">
var n = document.getElementById("data");
var f=new Array( "apple", "orange", "mango" );
n.value = f;
</script>
<form action="Your_JSP.jsp" method="POST">
<input type="hidden" id="data" name="data" value="" />
<input type="submit" />
</form>
Server Side:
<%
String val = request.getParameter("data");
String aa[]=val.split(",");
for(String x : aa )
out.println(x);
//now hereyou can use aa array tostore in database or do whatever you want
%>
Try this if you have two different jsp:
first.jsp
<script>
var response = [];
...
var put = function(form) {
form.resp.value = response.join(','); //using comma as separator
};
</script>
<form onsubmit='put(this)' method='next.jsp' method='post'>
<input type='hidden' name='resp' />
<button>Submit</button>
</form>
next.jsp
<%
String[] resp = request.getParameter("resp").split(",");
%>
Response is: ${param.resp} <!-- debugging -->
yes , its possible to do that. you can show array in HTML tag
<!DOCTYPE html>
<html>
<head>
<script>
var value=response;
for (var key in value) {
if (value.hasOwnProperty(key)) {
alert(key + " -> " + value[key]);
var sample = new Array();
sample=value[key];
console.log(sample);
// do here as
document.getElementById("array").innerHTML = sample;
// here array is <p> tag in html and sample is array which will be shown in jsp page.
}
}
</script>
</head>
<body>
<form action="get.jsp" method="POST">
<p id="array" name="array" type="hidden"></p>
<input type="submit" />
</body>
</html>
in get.jsp you will able to get value from array as:
<%
String []array = request.getParameter("array").split(",");
//this String array you can use to store data in DB
%>

Initialize JavaScript variable with java

Can we initialize JavaScript variables with java in jsp page?
like,
<script type="text/javascript">
var refreshId = setInterval(function() {
var nv=<% out.print(num_voted); %>;
var tv=<%out.print(totalvoted); %>;
var m=<% out.print(month);%>;
var y=<% out.print(year);%>;
alert("refreshed");
$('#alertmessagebox').text("Total members voted "+nv+" out of "+tv+" for "+m+" " +y);
}, 9000);
$.ajaxSetup({ cache: false });
</script>
Code is not working as expected. :(
Is there way to do this? Why it is not possible by this way? shall we need to create header to do this?
Here is an example of setting Javascript variables in a JSP.
<head>
<%
String numVotedStr = request.getParameter("numvoted");
int numVoted = 0;
if (numVotedStr != null) {
numVoted = Integer.parseInt(numVotedStr);
}
%>
<script type="text/javascript">
function setInterval() {
alert("hello " + <%= numVoted %>);
}
</script>
</head>
<body>
<form>
<input type="button" onclick="setInterval()" value="Press Me"/>
</form>
</body>
</html>
To test, use the appropriate version of this URL:
http://localhost:8080/sandbox/example.jsp?numvoted=99
This will popup an alert box with the integral value of "numvoted" on the HTTP request, by producing an HTML page where the value is initialized in Javascript. (The code should have at least a try-catch for the parseInt() call, but this will serve as simple example.)

on click show Iframe

I have a map that you can click on locations to get information for that area and I would like to show that information in a Iframe on the same page when clicked
My page has this for the link now
`<AREA SHAPE="CIRCLE" COORDS="555,142,6" HREF="http://www.Page.com" TITLE="" />`
Any suggestions
The beauty of AJAX is that you don't really need an IFRAME to do this.
You've got a server that will return to you information about a certain area. Each of the AREA tags simply needs an onclick attribute that calls a JavaScript function to retrieve that information and display it in a location you set aside on your page.
Here is a sample HTML page that will retrieve information from the server using AJAX
<html>
<head>
<script type="text/javascript">
function getAreaInfo(id)
{
var infoBox = document.getElementById("infoBox");
if (infoBox == null) return true;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) alert(xhr.status);
infoBox.innerHTML = xhr.responseText;
};
xhr.open("GET", "info.php?id=" + id, true);
xhr.send(null);
return false;
}
</script>
<style type="text/css">
#infoBox {
border:1px solid #777;
height: 400px;
width: 400px;
}
</style>
</head>
<body onload="">
<p>AJAX Test</p>
<p>Click a link...
Area One
Area Two
Area Three
</p>
<p>Here is where the information will go.</p>
<div id="infoBox"> </div>
</body>
</html>
And here is the info.php that returns the information back to the HTML page:
<?php
$id = $_GET["id"];
echo "You asked for information about area #{$id}. A real application would look something up in a database and format that information using XML or JSON.";
?>
Hope this helps!

include external java script file in jsp page

I have an external JavaScript file named paging.js. Following are the contents of the file:
function Pager(tableName,itemPerPage){
this.tableName = tableName;
this.itemPerPage = itemPerPage;
this.currentPage = 1;
this.pages = 0;
this.init()= function(){
alert("init called ");
var rows = document.getElementById(tableName).rows;
var records = (rows.length - 1);
this.pages = Math.ceil(records / itemPerPage);
}
this.showPageNav = function(pagerName,positionId){
alert("show page navi call");
var element = document.getElementById(positionId);
var pagerHtml = '<input src = "next.jpg" type="image">';
pagerHtml += '<input src = "next.jpg" type="image">' ;
element.innerHTML = pagerHtml;
}
}
Now I tried to call init from my jsp page like .
<script type="text/javascript">
var pager = new Pager('results',7);
pager.init();
</script>
This code i put before complete my body part in my jsp page.
For including this page I put line like
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/paging.js"></script>
But i can't able to call init method. Is there anyone to help me for finding problem?
This line of code is the problem:
this.init()= function(){
Change it to:
this.init=function() {
Try
<script type="text/javascript"
src="js/paging.js"></script>
With .jsp 2.+ technology, I place all my links and scripts in a separate file that I reference using the <jsp:include> directive:
<jsp:include page="//path to your links_and_scripts page">
My links_and_scripts page has this meta and the path to my script:
<meta http-equiv="Content-Script-Type" content="application/javascript; charset=utf-8" />
<script src="// path to your scripts js"></script>
//...your other scripts and links here

Categories

Resources