-2

I am a beginner in coding. I have met a scenario to pass the variable values from a java class to javascript and that need to be updated as values of a high chart in javascript.

[java code: I need to pass this values of float variables from this java class to JS

These values need to be updated in the below chart in JS.

java scripts

Kindly help me to code this scenario.

3

3 Answers 3

0
$.ajax({
  url: "YourSeveletPathName",
  type: 'GET',
  success: function(response) {
    //console.log(response);
    //map your data here -  to  series parameter in your highchart method.
  },
  error: function(error) {
    errorFunction(error, parameter);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

In your controller class you can put the data in an array and send to your jsp :

request.setAttribute("mydata", mydataarray);

In your Electricity.jsp you can get parameters as :

var jsvarlist=<%=request.getAttribute("mydata") %>;

Note: javascript doesn't want explicit data types


In your JS file you can pass the list at your function , as it is an array you can get data in this way :

jsvarlist[0] //first value etc.. 

if you need to import such java class in your jsp , you should use :

 <%@ page import ="yourpackage.yourclass" %>

In the end import the JS file to your jsp and call your function who accept the list now :

<script type="text/javascript" src="yourfile.js"></script>
... call your function 

5 Comments

you also need to set something in request also.
Hi Frank, The code for chart has written in java scripts (Electricity.js) which internally called by 'Electricity.jsp'. I am calling the 'container1' from the 'Electricity.jsp' and displays the chart in JSP.
<div class="content-wrapper less-margin"> <div class="content-area"> <div id="container1" style="width:100%; height: 500px; float:left;display:inline-block;margin-bottom: 50px"></div></div> </div> <script src="javascripts/jquery-1.11.3.min.js"></script> <script src="code.highcharts.com/highcharts.js"></script> <script src="code.highcharts.com/highcharts-3d.js"></script> <script src="javascripts/bootstrap-3.3.2.min.js"></script> <script src="javascripts/jquery.mCustomScrollbar.js"></script> <script src="javascripts/Electricity.js"></script>
Pls provide me the information to insert the values in to the chart. -Bibin
Edited , hope may can help
0

Maybe you should use ajax for doing that.

 var series = this.series[0];
 $.get("YourServletName",function(data){
                        series.addPoint([data.x, data.y], true, true);
                    });
                 }, 1000);

In the above code: the var series = this.series[0]; is your array, the $get method is the method which retrieves the data from your servlet, the series.addPoint([data.x, data.y], true, true); is adding each point to the series. And before all these in your servlet you must have something like this:

JsonObjectBuilder j = Json.createObjectBuilder();
        //Create code for your getting what you want and set this result to "y".

        j.add("x", System.currentTimeMillis());
        j.add("y", YourResult);
        out.println(j.build());

4 Comments

Hi Fortis, Thank you for your valuable information.
If my question helped you please accepted and give an up vote!
I am done & i am getting this message. 'Thanks for the feedback. Votes cast by those less than 15 reputation are recorded, but do not change the publicity displayed post score'
Thank you me dear @Bibin Vs. You can accepted also by pressing the grey tick. :)

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.