当前位置:   article > 正文

在vue项目中使用echarts(echarts不显示,echarts使用详细)_echarts按需引入镜像cnpm

echarts按需引入镜像cnpm

简述:我们在写大屏项目和vue项目时经常会用到echarts,用于数据统计和可视化,它是一款基于JavaScript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表,下面分享下它在项目中的使用方式;

1、安装,下载相关依赖(可用镜像cnpm);

  1. npm install echarts --save
  2. 下载较慢时,可以使用镜像cnpm
  3. cnpm install echarts --save

2、在项目中引入(全局引入);

  1. 全局引入Echarts
  2. import * as echarts from "echarts";
  3. 然后挂载在vue原型上
  4. Vue.prototype.$echarts = echarts;

  可按需引入 >>

Echarts按需引入详细https://echarts.apache.org/handbook/zh/basics/import/https://echarts.apache.org/handbook/zh/basics/import/

3、在vue页面中使用;

      3.1、首先定义一个容器,设置宽高,也就是echarts的大小(这一步很重要,一定要设置容器大小,不然显然不出来

  1. 定义一个容器
  2. <template>
  3. <div id="echarts_demo"></div>
  4. </template>
  5. 并设置大小
  6. <style lang="scss" scoped>
  7. #echarts_demo {
  8. width: 80%;
  9. height: 80vh;
  10. margin: auto;
  11. border: 1px solid #8080ff;
  12. }
  13. </style>

     3.2、定义一个方法,在方法中获取容器实例,初始化echarts,并配置相关属性,然后在mounted函数中调用(注意这里调用时候使用的是this.$echarts.init,全局方法);

  1. methods: {
  2. 定义一个echarts方法,把代码粘贴进去(官网实例有提供代码)
  3. defineEcharts() {
  4. 获取容器实例
  5. var chartDom = document.getElementById("echarts_demo");
  6. 赋值并初始化ecahrts
  7. var myChart = this.$echarts.init(chartDom);
  8. 配置相关属性
  9. var option;
  10. setTimeout(function () {
  11. option = {
  12. legend: {},
  13. tooltip: {
  14. trigger: "axis",
  15. showContent: false,
  16. },
  17. dataset: {
  18. source: [
  19. ["product", "2012", "2013", "2014", "2015", "2016", "2017"],
  20. ["Milk Tea", 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
  21. ["Matcha Latte", 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
  22. ["Cheese Cocoa", 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
  23. ["Walnut Brownie", 25.2, 37.1, 41.2, 18, 33.9, 49.1],
  24. ],
  25. },
  26. xAxis: { type: "category" },
  27. yAxis: { gridIndex: 0 },
  28. grid: { top: "55%" },
  29. series: [
  30. {
  31. type: "line",
  32. smooth: true,
  33. seriesLayoutBy: "row",
  34. emphasis: { focus: "series" },
  35. },
  36. {
  37. type: "line",
  38. smooth: true,
  39. seriesLayoutBy: "row",
  40. emphasis: { focus: "series" },
  41. },
  42. {
  43. type: "line",
  44. smooth: true,
  45. seriesLayoutBy: "row",
  46. emphasis: { focus: "series" },
  47. },
  48. {
  49. type: "line",
  50. smooth: true,
  51. seriesLayoutBy: "row",
  52. emphasis: { focus: "series" },
  53. },
  54. {
  55. type: "pie",
  56. id: "pie",
  57. radius: "30%",
  58. center: ["50%", "25%"],
  59. emphasis: {
  60. focus: "self",
  61. },
  62. label: {
  63. formatter: "{b}: {@2012} ({d}%)",
  64. },
  65. encode: {
  66. itemName: "product",
  67. value: "2012",
  68. tooltip: "2012",
  69. },
  70. },
  71. ],
  72. };
  73. myChart.on("updateAxisPointer", function (event) {
  74. const xAxisInfo = event.axesInfo[0];
  75. if (xAxisInfo) {
  76. const dimension = xAxisInfo.value + 1;
  77. myChart.setOption({
  78. series: {
  79. id: "pie",
  80. label: {
  81. formatter: "{b}: {@[" + dimension + "]} ({d}%)",
  82. },
  83. encode: {
  84. value: dimension,
  85. tooltip: dimension,
  86. },
  87. },
  88. });
  89. }
  90. });
  91. myChart.setOption(option);
  92. });
  93. option && myChart.setOption(option);
  94. // 给window添加resize监听事件,当平怒尺寸发生变化时重置
  95. window.addEventListener("resize", () => {
  96. myChart.resize();
  97. });
  98. },
  99. },
  100. mounted() {
  101. this.defineEcharts();
  102. },

4、实现的效果;

 5、这里给echarts加上页面重置效果,就是当页面尺寸发生变化的时候,跟随页面大小变化而变化(也就是加一个监听事件resize,上面代码有提到);

  1. option && myChart.setOption(option);
  2. // 给window添加resize监听事件,当屏幕尺寸发生变化时重置
  3. window.addEventListener("resize", () => {
  4. myChart.resize();
  5. });

  加监听事件之前:

  加监听事件之后:

 更多,Echarts大小重置,缩小等问题 >>

echarts重置、缩小等问题,解决方案https://blog.csdn.net/weixin_65793170/article/details/127608700?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167984448316800180676689%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167984448316800180676689&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-127608700-null-null.blog_rank_default&utm_term=echarts&spm=1018.2226.3001.4450https://blog.csdn.net/weixin_65793170/article/details/127608700?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167984448316800180676689%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167984448316800180676689&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-127608700-null-null.blog_rank_default&utm_term=echarts&spm=1018.2226.3001.4450

更多操作 >>

Echarts官网https://echarts.apache.org/zh/index.htmlhttps://echarts.apache.org/zh/index.html

相关镜像(Echarts访问加速) >>

PPCharthttp://ppchart.com/#/

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