【发布时间】:2021-12-09 15:33:27
【问题描述】:
我是 js 和 spring 的新手,现在我想创建一个 html 仪表板,这个页面将有一个带有饼图的小 div。但我无法创建饼图。 我在 youtube 上尝试了一些教程,但现在我想将值传递给 ajax 或类似的东西来获取饼图。 这是我的代码: admin_homepage.html:
<div class="col-xl-4 col-lg-5">
<div class="card shadow mb-4">
<!-- Thay chart vào thẻ div này -->
<div class="card-body">
<div class="chart-pie pt-4 pb-2">
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$.ajax({
/* for pie chart */
url: "admin_home",
success: function(result){
/* pie chart starts here */
var series = [];
var data = [];
for(var i = 0; i < result.length; i++){
var object = {};
object.name = result[i].catName.toUpperCase();
object.y = result[i].catCount;
data.push(object);
}
var seriesObject = {
name: 'Course By Category',
colorByPoint: true,
data: data
};
series.push(seriesObject);
drawPieChart(series);
/* pie chart ends here */
}
});
/* for pie chart */
function drawPieChart(series){
Highcharts.chart('chartContainer', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
formatter: function() {
return '<strong>'+this.key+': </strong>'+ this.y;
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.y}'
}
}
},
series: series
});
}
</script>
我的控制器
@GetMapping("/admin_home")
public String viewHomePage(){
// Get list of course and count
List<CountCourse> pieChart = dashBoardRepository.countCourseByCategory();
model.addAttribute("pieChart",pieChart);
return "Admin_Homepage";
}
我想要的只是将 catName、catCount 的值传递给饼图,但我不能 任何人帮助我。非常感谢。
【问题讨论】:
-
这个问题我不清楚。 (1) admin_homepage.html 是否设置为 Thymeleaf 模板? (您已将该问题标记为 Thymeleaf 问题)。 (2) url: "admin_home" 在其 Ajax 响应中返回什么数据(以及以什么格式)?
-
另外,请注意您的控制器中有
return "Admin_Homepage";,它与您的 HTML 文件名中的“admin_homepage”不匹配。 -
@andrewJames 嗨,詹姆斯,感谢您的回复。 (1) 我的 admin_homepage.html 当然设置为 thymleaf 模板,我想在这个页面中创建一个饼图。我的饼图将包含课程名称和课程数量等数据。而这个数据输出就是饼图。我
return "admin_homepage"匹配html文件名但没有帮助:((
标签: javascript spring spring-boot highcharts thymeleaf