经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
vue使用ECharts实现折线图和饼图
来源:jb51  时间:2021/9/27 11:06:06  对本文有异议

在开发后台管理项目时,需要统计后台用户数据,以折线图和饼图的形式显示,这边采用apache-echarts来实现需求。

1.安装echarts和引入

  1. npm install echarts --save
  2. import * as echarts from 'echarts';

2.使用echarts实现pie图

  1. <template>
  2. <div id="chartPie" class="pie-wrap"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. require('echarts/theme/macarons');//引入主题
  7. export default {
  8. data() {
  9. return {
  10. chartPie: null
  11. }
  12. },
  13. mounted() {
  14. this.$nextTick(() => {
  15. this.drawPieChart();
  16. })
  17. },
  18. methods: {
  19. drawPieChart() {
  20. let mytextStyle = {
  21. color: "#333",
  22. fontSize: 18,
  23. };
  24. let mylabel = {
  25. show: true,
  26. position: "right",
  27. offset: [30, 40],
  28. formatter: '{b} : {c} ({d}%)',
  29. textStyle: mytextStyle
  30. };
  31. this.chartPie = echarts.init(document.getElementById('chartPie'),'macarons');
  32. this.chartPie.setOption({
  33. title: {
  34. text: 'Pie Chart',
  35. subtext: '纯属虚构',
  36. x: 'center'
  37. },
  38. tooltip: {
  39. trigger: 'item',
  40. formatter: "{a} <br/>{b} : {c} ({d}%)",
  41. },
  42. legend: {
  43. data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'],
  44. left:"center",
  45. top:"bottom",
  46. orient:"horizontal",
  47. },
  48. series: [
  49. {
  50. name: '访问来源',
  51. type: 'pie',
  52. radius: ['50%', '70%'],
  53. center: ['50%', '50%'],
  54. data: [
  55. {value: 335, name: '直接访问'},
  56. {value: 310, name: '邮件营销'},
  57. {value: 234, name: '联盟广告'},
  58. {value: 135, name: '视频广告'},
  59. {value: 1548, name: '搜索引擎'}
  60. ],
  61. animationEasing: 'cubicInOut',
  62. animationDuration: 2600,
  63. label: {
  64. emphasis: mylabel
  65. }
  66. }
  67. ]
  68. });
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang='less' scope>
  74. .pie-wrap{
  75. width:100%;
  76. height:400px;
  77. }
  78. </style>

3.使用echarts实现line图

  1. <template>
  2. <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
  3. <div id="chartLine" class="line-wrap"></div>
  4. </template>
  5. <script>
  6. import * as echarts from 'echarts';
  7. require('echarts/theme/shine');//引入主题
  8. export default {
  9. data() {
  10. return {
  11. chartLine: null
  12. }
  13. },
  14. mounted() {
  15. this.$nextTick(() => {
  16. this.drawLineChart();
  17. })
  18. },
  19. methods: {
  20. drawLineChart() {
  21. this.chartLine = echarts.init(this.$el,'shine');// 基于准备好的dom,初始化echarts实例
  22. let option = {
  23. tooltip : {
  24. trigger: 'axis'
  25. },
  26. legend: {
  27. data:['邮件营销','联盟广告','视频广告','直接访问','搜索引擎']
  28. },
  29. calculable : true,
  30. xAxis : [
  31. {
  32. type : 'category',
  33. boundaryGap : false,
  34. axisTick: {
  35. show: false
  36. },
  37. data : ['周一','周二','周三','周四','周五','周六','周日']
  38. }
  39. ],
  40. yAxis : [
  41. {
  42. type : 'value',
  43. axisTick: {
  44. show: false
  45. },
  46. name: '(人)'
  47. }
  48. ],
  49. series : [
  50. {
  51. name:'邮件营销',
  52. type:'line',
  53. stack: '总量',
  54. data:[120, 132, 101, 134, 90, 230, 210]
  55. },
  56. {
  57. name:'联盟广告',
  58. type:'line',
  59. stack: '总量',
  60. data:[220, 182, 191, 234, 290, 330, 310]
  61. },
  62. {
  63. name:'视频广告',
  64. type:'line',
  65. stack: '总量',
  66. data:[150, 232, 201, 154, 190, 330, 410]
  67. },
  68. {
  69. name:'直接访问',
  70. type:'line',
  71. stack: '总量',
  72. data:[320, 332, 301, 334, 390, 330, 320]
  73. },
  74. {
  75. name:'搜索引擎',
  76. type:'line',
  77. stack: '总量',
  78. data:[820, 932, 901, 934, 1290, 1330, 1320]
  79. }
  80. ]
  81. };
  82. // 使用刚指定的配置项和数据显示图表
  83. this.chartLine.setOption(option);
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang='less' scope>
  89. .line-wrap{
  90. width:50%;
  91. height:400px;
  92. }
  93. </style>

4.echarts基础概念

1)echarts实例

一个网页中可以创建多个 echarts 实例,每个 echarts 实例 中可以创建多个图表和坐标系等等(用 option 来描述)。准备一个 DOM 节点(作为 echarts 的渲染容器),就可以在上面创建一个 echarts 实例。

2)系列(series)

一组数值以及他们映射成的图,一个 系列 包含的要素至少有:一组数值、图表类型(series.type)、以及其他的关于这些数据如何映射成图的参数。
echarts 里系列类型(series.type)就是图表类型。系列类型(series.type)至少有:line(折线图)、bar(柱状图)、pie(饼图)

3)组件(component)

echarts 中至少有这些组件:xAxis(直角坐标系 X 轴)、yAxis(直角坐标系 Y 轴)、grid(直角坐标系底板)、dataZoom(数据区缩放组件)、visualMap(视觉映射组件)、tooltip(提示框组件)、toolbox(工具栏组件)、series(系列)、...

5.echarts常用配置项和API

1)title标题组件,包含主标题和副标题

  1. title: {
  2. text: 'Pie Chart',//主标题文本
  3. subtext: '纯属虚构',//副标题文本
  4. x: 'center',
  5. textStyle:{//主标题样式
  6. color: "#333",
  7. fontSize: 18
  8. },
  9. subtextStyle:{},//副标题样式
  10. itemGap: 10,//主副标题之间的间距
  11. }

2)legend图例组件

  1. legend: {
  2. left:"center",//图例组件离容器左侧的距离
  3. top:"bottom",//图例组件离容器上侧的距离
  4. orient:"horizontal",//图例列表的布局朝向
  5. itemGap :20,//图例每项之间的间隔
  6. formatter: function (name) {//用来格式化图例文本,支持字符串模板和回调函数两种形式~~~~
  7. return 'Legend ' + name;
  8. },
  9. icon:'circle',//图例项的 icon
  10. data: [
  11. {
  12. icon: 'triangle',
  13. textStyle: {
  14. color: 'red'
  15. },
  16. name: '直接访问'
  17. },
  18. '邮件营销', '联盟广告', '视频广告', '搜索引擎'],//图例的数据数组
  19. }

3)xAxis直角坐标系 grid 中的 x 轴

  1. xAxis : [
  2. {
  3. type : 'category',//坐标轴类型
  4. boundaryGap : false,
  5. axisTick: {//坐标轴刻度相关设置
  6. show: false
  7. },
  8. data : ['周一','周二','周三','周四','周五','周六','周日']//类目数据
  9. }
  10. ]

4)yAxis直角坐标系 grid 中的 y 轴

  1. yAxis : [
  2. {
  3. type : 'value',//坐标轴类型,`'value'` 数值轴,适用于连续数据
  4. axisTick: {
  5. show: false
  6. },
  7. name: '(人)'
  8. }
  9. ]

5)tooltip提示框组件

  1. tooltip : {
  2. trigger: 'axis',//触发类型,'axis'坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用
  3. formatter: "{a} <br/>{b} : {c} ({d}%)",//模板变量有 `{a}`, `{b}`,`{c}`,`{d}`,`{e}`,分别表示系列名,数据名,数据值等
  4. }

6)series系列列表,每个系列通过 type 决定自己的图表类型

  1. A.series-line
  2. series : [
  3. {
  4. name:'邮件营销',
  5. type:'line',
  6. stack: '总量',
  7. data:[120, 132, 101, 134, 90, 230, 210],//系列中的数据内容数组
  8. areaStyle:{//区域填充样式
  9. color:'red'
  10. },
  11. lineStyle:{//线条样式
  12. color:'#000'
  13. },
  14. emphasis:{//图形的高亮样式
  15. label:{
  16. color:'red'
  17. }
  18. }
  19. }
  20. ]
  21. B.series-pie
  22. series : [
  23. {
  24. name: '访问来源',
  25. type: 'pie',
  26. radius: ['50%', '70%'],//饼图的半径,数组的第一项是内半径,第二项是外半径
  27. center: ['50%', '50%'],//饼图的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标。支持设置成百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度
  28. roseType: false,//是否展示成南丁格尔图,通过半径区分数据大小
  29. data: [//系列中的数据内容数组
  30. {value: 335, name: '直接访问'},
  31. {value: 310, name: '邮件营销'},
  32. {value: 234, name: '联盟广告'},
  33. {value: 135, name: '视频广告'},
  34. {value: 1548, name: '搜索引擎'}
  35. ],
  36. animationEasing: 'cubicInOut',//初始动画的缓动效果
  37. animationDuration: 2600,//初始动画的时长
  38. label: {
  39. emphasis: mylabel//高亮的扇区和标签样式
  40. }
  41. }
  42.  
  43. ]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号