echarts 使用示例
*****************
示例
***********
pojo 层
Fruit
@Data
public class Fruit {
private String name;
private Float price;
}
***********
config 层
WebConfig
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
registry.addViewController("/index2").setViewName("index2");
}
}
***********
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public List<Fruit> hello(){
List<Fruit> list = new ArrayList<>();
Fruit apple = new Fruit();
apple.setName("苹果");
apple.setPrice(6.0f);
list.add(apple);
Fruit banana = new Fruit();
banana.setName("香蕉");
banana.setPrice(4.0f);
list.add(banana);
Fruit orange = new Fruit();
orange.setName("橘子");
orange.setPrice(3.6f);
list.add(orange);
Fruit pipeApple = new Fruit();
pipeApple.setName("菠萝");
pipeApple.setPrice(4.8f);
list.add(pipeApple);
return list;
}
}
*****************
前端页面
index.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>echarts 示例</title>
<script src="/echarts/echarts.min.js"></script>
<script src="/jquery/jquery-3.6.0.min.js"></script>
<script>
$(function (){
$("#btn").click(function (){
$.get({
url: "/hello",
success: function (data){
const myChart = echarts.init($("#con")[0]);
let source= [];
$.each(data,function (index,object){
let item = {fruit:object.name, price:object.price }
source.push(item);
});
console.log(source);
const option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['价格']
},
dataset: { //数据集提供数据源
source: source
},
xAxis: {
type: 'category'
},
yAxis: {},
series: [
{
type: 'bar',
seriesLayoutBy: 'row'
}
]
};
myChart.setOption(option);
}
});
})
})
</script>
</head>
<body>
<div th:align="center" id="con" style="width: 600px;height:400px;">
</div>
<div th:align="center">
<button id="btn">加载数据</button>
</div>
</body>
</html>
index2.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>echarts 示例</title>
<script src="/echarts/echarts.min.js"></script>
<script src="/jquery/jquery-3.6.0.min.js"></script>
<script>
$(function (){
$("#btn").click(function (){
$.get({
url: "/hello",
success: function (data){
const myChart = echarts.init($("#con")[0]);
let fruits= [];
let prices=[];
$.each(data,function (index,object){
fruits.push(object.name);
prices.push(object.price);
});
const option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['价格']
},
xAxis: {
data: fruits //在xAxis中提供x轴数据
},
yAxis: {},
series: [
{
type: 'bar',
data: prices //在seris中提供纵坐标数据
}
]
};
myChart.setOption(option);
}
});
})
})
</script>
</head>
<body>
<div th:align="center" id="con" style="width: 600px;height:400px;">
</div>
<div th:align="center">
<button id="btn">加载数据</button>
</div>
</body>
</html>
*****************
使用测试
localhost:8080/index,点击加载数据
localhost:8080/index2,点击加载数据