Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to convert a MySQL result-set written in a Java Server Page (.jsp), into a Javascript array that I can graph.

My current MYSQL result-set returns this in the web browser:

I want to return this in Javascript. It must be in this format so I can graph it with d3 or Highcharts :

       var ValueArray = [ {x:0, y:110}, {x:15, y:113}, {x:30, y:90} ] 

After my MYSQL query is run, I create a table using the following java server tags...

<script>
var ValueArray=  [
 <c:forEach var="row" items="${meanvalue.rowsByIndex}">
         <c:forEach var="column" items="${row}">, 
                     {<c:out value= "${column}"/>}
 </c:forEach>
 </c:forEach>
 ]
</script>

This is the corresponding HTML code that is generated and is the current output of my MYSQL query:

<table border="1" id = "meanvalue">
<!-- column headers -->
<tr>

    <th>timestamp</th>

    <th>Value</th>

<tr>

  <td>  0</td>  

  <td>  110</td>  

</tr>

<tr>

  <td>  15 </td>  

  <td>  113</td>  

</tr>

<tr>

  <td>  30</td>  

  <td>  90</td>  

</tr>

I've searched around stackoverflow and have found similar questions, but none of them have the formatting constraint that I'm looking for; converting a raw number array into a .js array with the format

  var Array = [{x: val1, y: val2}, {x: val3, y: val4} ] 
share|improve this question
    
From your current attempts what are you getting as output? It also looks like you're missing a comma after your c:out section. –  Lance Jan 23 '14 at 19:30
    
Thanks for the speedy response. Currently my results are of the form [ {0},{10}, {15},{113},{30},{90}] so not exactly what I want... –  CodeinCafes Jan 23 '14 at 19:41

1 Answer 1

It looks like you need to edit your foreach statements. Something like the following

 <c:forEach var="row" items="${meanvalue.rowsByIndex}">
         { // <-- begin the object
         <c:forEach var="column" items="${row}" varStatus="status">, 
                 ${status.index} : <c:out value= "${column}"/>
         </c:forEach>
         } // <-- end the object
 </c:forEach>
share|improve this answer
    
Ah, I see. This then appends "0:" and "1:" in front of the variables I want . Is there a way to convert the 0 and 1 to "X" and "Y"? –  CodeinCafes Jan 23 '14 at 20:16
    
Yes, there is a way to do that. stackoverflow.com/a/5935934/577264 I believe that answer will show you how to do if and JSP's choose statements (choose will simulate if/else statements). –  Lance Jan 23 '14 at 20:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.