<script type="text/javascript">
var chart;
var data = [];
for (var i = 0; i < '${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>
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)
}
Find more questions by tags JavaScriptMySQLDatabasesJava
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
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
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 collection receives the request. - della_Dicki commented on July 8th 19 at 12:15
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