当前位置:   article > 正文

若依分离版 —引入echart连接Springboot后端_若依框架echarts

若依框架echarts

1. vue引入echart

(1)首先安装ECharts库。可以通过npm

npm install echarts --save

(2)在vue页面中添加一个容器元素来显示图表

  1. <el-card class="mt20">
  2. <div id="ha" ref="main"></div>
  3. </el-card>

(3)在vue页面中的script引入echart,使用import * as echarts from 'echarts'

并且连接后端数据

  1. <script>
  2. import * as echarts from 'echarts'
  3. import { getlineChartData} from "@/api/echartdata";
  4. export default {
  5. name: "Index",
  6. data() {
  7. return {
  8. };
  9. },
  10. mounted() {
  11. //加载后初始化图表
  12. this.initEcarts();
  13. },
  14. methods: {
  15. initEcarts() {
  16. // 初始化echarts实例
  17. let myChart = echarts.init(this.$refs.main);
  18. getlineChartData().then(res => {
  19. console.log(res);
  20. console.log(res.axisData);
  21. console.log(res.seriesData1);
  22. console.log(res.seriesData2);
  23. let option = {
  24. title: {
  25. text: '每天数据'
  26. },
  27. //鼠标悬浮显示数字
  28. tooltip: {
  29. trigger: 'axis',
  30. axisPointer: {
  31. type: 'cross',
  32. label: {
  33. backgroundColor: '#6a7985'
  34. }
  35. }
  36. },
  37. legend: {
  38. data: ['联盟广告1', '联盟广告2']
  39. },
  40. xAxis: [
  41. {
  42. type: 'category',
  43. data: res.axisData,
  44. }
  45. ],
  46. yAxis: [
  47. {
  48. type: 'value'
  49. }
  50. ],
  51. series: [
  52. {
  53. name: '联盟广告1',
  54. type: 'line',
  55. data: res.seriesData1,
  56. },
  57. {
  58. name: '联盟广告2',
  59. type: 'line',
  60. data: res.seriesData2,
  61. }
  62. ]
  63. };
  64. if (option && myChart.setOption) {
  65. // 使用刚指定的配置项和数据显示图表。
  66. myChart.setOption(option);
  67. }
  68. });
  69. }
  70. }
  71. };
  72. </script>
(5)样式
  1. <style scoped lang="scss">
  2. #ha {
  3. width: 80%;
  4. height: 360px;
  5. }
  6. </style>
2. 后端Controller模拟接口
  1. @GetMapping("/lineChartData")
  2. public Map<String, Object> getLineChartData() {
  3. Map<String, Object> model = new LinkedHashMap<>();
  4. model.put("axisData", new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"});
  5. model.put("seriesData1", new Integer[]{120, 132, 101, 134, 190, 130, 120});
  6. model.put("seriesData2", new Integer[]{220, 232, 101, 234, 290, 330, 220});
  7. return model;
  8. }
3. 后端查询数据

定义 :

public class EchartCount {

    public String name;
    public String count;

}

controller内容如下:

  1. @GetMapping("/lineChartData")
  2. public Map<String, Object> getLineChartData() {
  3. List<EchartCount> saimaCountList=tEchartServcie.getLiXiangData();
  4. Map<String, Object> model = new LinkedHashMap<>();
  5. model.put("axisData", saimaCountList.stream().map(EchartCount::getName).collect(Collectors.toList()));
  6. model.put("seriesData1", saimaCountList.stream().map(EchartCount::getCount).collect(Collectors.toList()));
  7. model.put("seriesData2",
  8. return model;
  9. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/769369
推荐阅读
相关标签
  

闽ICP备14008679号