Problem getting data from database for Amcharts JavaScript?

Good evening, colleagues.

The challenge is to get data from DB and put them in the form of charts using AmCharts.

Getting data from DB has already been made, but there was Oding bug.

To transfer data in AmCharts use the following code:
<script type="text/javascript">
 var chart;

 var data = [];

 for (var i = 0; i &lt; '${countryMB.list.size()}'; i++) {
data.push(
{
 country: '${countryMB.list.get(i).getName()}',
 gdp: '${countryMB.list.get(i).getGdp()}'
}
);
}

 var chartData = data;

 AmCharts.ready(function () {
 // SERIAL CHART
 chart = new AmCharts.AmSerialChart();
 chart.dataProvider = chartData;
 chart.categoryField = "country";
 chart.startDuration = 1;

 // AXES
 // category
 var categoryAxis = chart.categoryAxis;
 categoryAxis.labelRotation = 90;
 categoryAxis.gridPosition = "start";

 // GRAPH
 var graph = new AmCharts.AmGraph();
 graph.valueField = "gdp";
 graph.balloonText = "[[category]]: [[value]]";
 graph.type = "column";
 graph.lineAlpha = 0;
 graph.fillAlphas = 0.8;
chart.addGraph(graph);

chart.write("chartdiv");
});</code></pre><br><br>
countryMB is a component of Primefaces.<br><br>
The catch is that the chart displays only data for the first list item, which is repeated as many times as elements in the database.<br><br><img src="https://habrastorage.org/files/29b/806/b6e/29b806b6e5254c34a8fa3a259ba09ad4.png" alt="29b806b6e5254c34a8fa3a259ba09ad4.png"><br><br>
Tried a lot of options and it is believed that the error is extremely silly, but find it does not work.<br><br>
Thanks in advance.</script>
July 8th 19 at 11:49
1 answer
July 8th 19 at 11:51
var countryList = `${countryMB.list.toJson()}` // convert to json

for (var i = 0; i < countryList.length; i++) {
 (function (count){
data.push({
 country: countryList[count].name
 gdp: countryList[count].gdp
});
})(i)
}
Unfortunately, did not help.
the point is still that if you modify i inside get(i) any digit, it prints the element with this index.
Therefore, everything works.
Why it returns only the first element is unclear. - della_Dicki commented on July 8th 19 at 11:54
: Try the second option, I corrected it a bit. - Rene.DAmore commented on July 8th 19 at 11:57
:
Unchanged, colleague.
I'm not strong in JS, but as I understand it - in the get() method is passed only the first in i.
Don't know prinicipe work with memory in JS, but maybe is only a reference to the first value.
As not only twisted my brain to come up with why it is so :) - della_Dicki commented on July 8th 19 at 12:00
: And check console.log(count) gthtl data.push and see what is displayed in the console. - Rene.DAmore commented on July 8th 19 at 12:03
I prolongiruetsa console.log(chartData) after the line var chartData = data; Maybe it's not in a loop. - Rene.DAmore commented on July 8th 19 at 12:06
:
Displays 0,1,2,3,4,5,

After
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

....

I'll keep thinking, thanks. - della_Dicki commented on July 8th 19 at 12:09
: This is the server code, I understand `${countryMB.list.get(i).getName ()}` interpolation. If so, then you have of course, the entire array will consist of the first element. View the compiled code, what is there in this cycle. It is suspected that i there did not change. - Rene.DAmore commented on July 8th 19 at 12:12
[Country{name='Ukraine', gdp=132}, Country{name='Russia' gdp=1861}, Country{name='Belarus' gdp=76}, Country{name='United Kingdom', gdp=2988}, Country{name='Switzerland', gdp=701}, Country{name='Japan' gdp=3500}]

This collection receives the request. - della_Dicki commented on July 8th 19 at 12:15
And it's a thought... Thank you. - della_Dicki commented on July 8th 19 at 12:18
You need a collection countryMB.list to convert to a json object. JS is not typed, it will not understand your collection. And this json object to assign to a variable before the loop. - Rene.DAmore commented on July 8th 19 at 12:21
: Updated solution - Rene.DAmore commented on July 8th 19 at 12:24
Thank you.

The code in this form will not work (it's Java).
But the idea is correct.
Yesterday I processed and completely forgot about JSON.
Today plan to complete the module.

Thank you. - della_Dicki commented on July 8th 19 at 12:27

Find more questions by tags JavaScriptMySQLDatabasesJava