【发布时间】:2026-01-13 17:15:02
【问题描述】:
我有一个问题,我不知道如何将包含我数据库中的数据的地图添加到 Thymeleaf 模板中的 google.visualization.arrayToDataTable。
我成功地使用控制台日志在视图中显示了地图,但我的问题是如何将其添加到 google.visualization.arrayToDataTable。
这是我来自@Controller 的代码
@GetMapping("/pourcentageAges")
public String ageDesClients(Model model){
Map<String,Integer>listeAgesTranches =
utilisateurMetier.agesClientsClub();
System.out.println("je suis la tailme de la liste
"+listeAgesTranches.size());
model.addAttribute("liste",listeAgesTranches);
model.addAttribute("tailleListe",listeAgesTranches.size());
return "utilisateur/agePourcentage";
}
从我的角度来看,这是我的代码: 所以我再说一遍,我只需要将“var ageList”分配给 google.visualization.arrayToDataTable
<script type="text/javascript" th:inline="javascript" >
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
/*<![CDATA[*/
var ageList=new Map();
var tailleListe= /*[[${tailleListe}]]*/
ageList= /*[[${liste}]]*/ 'default';
/*]]>*/
// Draw the chart and set the chart values
// for (i=0; i<tailleListe;i++){
// console.log('je suis la'+[Object.keys(message)[i],Object.values(message)[i]])
// }
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'ages between 16 and 90 group by 10'],
['age betweeen 20-30',5 ]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Ages en pourcent de 16 a 90 ans', 'width':550,
'height':400,is3D: true};
// Display the chart inside the <div> element with id="piechart"
var chart = new
google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
非常感谢您的任何建议。
【问题讨论】:
标签: javascript spring-boot thymeleaf google-visualization