程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

【高级课程笔记】—— Echarts高级应用(二)

发布于2021-10-24 13:00     阅读(888)     评论(0)     点赞(27)     收藏(2)


一、视觉映射

 

注:visualMap 以前叫dataRange,如果你看到了比较老的教程或博客,里面有dataRange,要知道那就是视觉映射 visualMap

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>视觉映射</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 800px;
  10. height: 500px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. // 基于准备好的dom,初始化echarts实例
  21. const myChart = echarts.init(document.getElementById('main'));
  22. //数据源
  23. const source = [
  24. //x y z w
  25. [2, 1, 10, 90],
  26. [4, 2, 20, 80],
  27. [6, 3, 30, 70],
  28. [8, 4, 40, 60],
  29. [10, 5, 50, 50],
  30. [12, 6, 60, 40],
  31. [14, 7, 70, 30],
  32. [16, 8, 80, 20],
  33. [18, 9, 90, 10],
  34. ];
  35. //颜色范围
  36. const color=['#70ad47', '#c00000'];
  37. // 指定图表的配置项和数据
  38. const option = {
  39. tooltip: {},
  40. /*绘图区*/
  41. grid:{
  42. left:100
  43. },
  44. /*x 轴*/
  45. xAxis: {},
  46. /*y 轴*/
  47. yAxis: {},
  48. /*数据集*/
  49. dataset:{source},
  50. /*
  51. * visualMap 视觉映射 {}
  52. * type 映射方式
  53. * continuous 连续型
  54. * piecewise 分段型
  55. * min 映射区间的起始位置,如0
  56. * max 映射区间的结束位置,如90
  57. * range [] 显示此范围内的项目(在连续型中有效),百分百类型,如[0,100]
  58. * calculable 是否显示拖拽用的手柄
  59. * inRange 自定义映射范围
  60. * color[] 颜色映射
  61. * symbolSize[] 大小映射
  62. *
  63. * */
  64. visualMap:{
  65. type:'continuous',
  66. // type:'piecewise',
  67. min:0,
  68. max:100,
  69. range:[0,90],
  70. calculable: true,
  71. inRange:{
  72. color,
  73. symbolSize:[10,60],
  74. },
  75. // dimension:2,
  76. dimension:3
  77. },
  78. /*系列列表*/
  79. series: [
  80. {
  81. name: '视觉映射',
  82. type: 'scatter',
  83. encode:{
  84. tooltip:[0,1,2]
  85. }
  86. },
  87. ]
  88. };
  89. // 使用刚指定的配置项和数据显示图表。
  90. myChart.setOption(option);
  91. </script>
  92. </body>
  93. </html>

二、事件

ECharts 如何监听事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 500px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. // 基于准备好的dom,初始化echarts实例
  21. const myChart = echarts.init(document.getElementById('main'));
  22. // 指定图表的配置项和数据
  23. const option = {
  24. xAxis: {
  25. data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  26. },
  27. yAxis: {},
  28. series: [{
  29. name: '销量',
  30. type: 'bar',
  31. data: [5, 20, 36, 10, 10, 20]
  32. }]
  33. };
  34. // 使用刚指定的配置项和数据显示图表。
  35. myChart.setOption(option);
  36. /*
  37. * 使用on 方法绑定click点击事件
  38. * */
  39. myChart.on('click',function(param){
  40. console.log(param)
  41. })
  42. </script>
  43. </body>
  44. </html>

  

鼠标事件有哪些

组件交互事件的监听

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 500px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. // 基于准备好的dom,初始化echarts实例
  21. const myChart = echarts.init(document.getElementById('main'));
  22. // 指定图表的配置项和数据
  23. const option = {
  24. legend:{
  25. data:['销量','库存']
  26. },
  27. xAxis: {
  28. data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  29. },
  30. yAxis: {},
  31. series: [{
  32. name: '销量',
  33. type: 'bar',
  34. data: [5, 20, 36, 10, 10, 20]
  35. },{
  36. name: '库存',
  37. type: 'bar',
  38. data: [3, 4, 8, 3, 7, 5]
  39. }]
  40. };
  41. // 使用刚指定的配置项和数据显示图表。
  42. myChart.setOption(option);
  43. /*
  44. * 使用on 方法绑定legendselectchanged 图例开关点击事件
  45. * */
  46. myChart.on('legendselectchanged',function(param){
  47. console.log(param)
  48. })
  49. </script>
  50. </body>
  51. </html>

 代码触发 ECharts 中组件的行为

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 500px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. // 基于准备好的dom,初始化echarts实例
  21. const myChart = echarts.init(document.getElementById('main'));
  22. // 指定图表的配置项和数据
  23. const option = {
  24. title: {
  25. text: '饼图程序调用高亮示例',
  26. left: 'center'
  27. },
  28. tooltip: {
  29. trigger: 'item',
  30. formatter: '{a} <br/>{b} : {c} ({d}%)'
  31. },
  32. legend: {
  33. orient: 'vertical',
  34. left: 'left',
  35. data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
  36. },
  37. series: {
  38. name: '访问来源',
  39. type: 'pie',
  40. radius: '55%',
  41. center: ['50%', '60%'],
  42. data: [
  43. {value: 335, name: '直接访问'},
  44. {value: 310, name: '邮件营销'},
  45. {value: 234, name: '联盟广告'},
  46. {value: 135, name: '视频广告'},
  47. {value: 1548, name: '搜索引擎'}
  48. ],
  49. /*鼠标划上的状态*/
  50. emphasis: {
  51. itemStyle: {
  52. shadowBlur: 10,
  53. shadowOffsetX: 0,
  54. shadowOffsetY: 10,
  55. shadowColor: 'rgba(0, 0, 0, 0.5)'
  56. }
  57. }
  58. }
  59. };
  60. // 使用刚指定的配置项和数据显示图表。
  61. myChart.setOption(option);
  62. /*当前索引*/
  63. let ind=0;
  64. /*获取系列数据的长度*/
  65. let len =5;
  66. /*使用dispatchAction 方法高亮并提示一个扇形
  67. * type 触发的行为类型
  68. * highlight 高亮
  69. * showTip 显示提示
  70. * downplay 取消高亮
  71. * hideTip 取消提示
  72. * seriesIndex 系列索引,用于寻找系列列表中的某个系列
  73. * dataIndex 数据所有,用于寻找系列中的某个元素
  74. * */
  75. myChart.dispatchAction({
  76. type:'highlight',
  77. seriesIndex:0,
  78. dataIndex:ind
  79. });
  80. myChart.dispatchAction({
  81. type:'showTip',
  82. seriesIndex:0,
  83. dataIndex:ind
  84. });
  85. /*建立时间监听器*/
  86. setInterval(function(){
  87. myChart.dispatchAction({
  88. type:'hideTip',
  89. seriesIndex:0,
  90. dataIndex:ind
  91. });
  92. myChart.dispatchAction({
  93. type:'downplay',
  94. seriesIndex:0,
  95. dataIndex:ind
  96. });
  97. ind++;
  98. if(ind===len){ind=0}
  99. myChart.dispatchAction({
  100. type:'highlight',
  101. seriesIndex:0,
  102. dataIndex:ind
  103. });
  104. myChart.dispatchAction({
  105. type:'showTip',
  106. seriesIndex:0,
  107. dataIndex:ind
  108. });
  109. },1000)
  110. </script>
  111. </body>
  112. </html>

三、富文本标签

富文本标签,就是内容丰富的文本标签。

在许多地方(如图、轴的标签等)都可以使用富文本标签。例如:

文本块和文本片段

  • 文本块(Text Block):文本标签块整体。
  • 文本片段(Text fragment):文本标签块中的部分文本。

文本标签的属性可以参考label:https://www.echartsjs.com/zh/option.html#series-bar.label

富文本的属性:https://www.echartsjs.com/zh/option.html#series-bar.label.rich.%3Cstyle_name%3E

富文本的实现步骤

 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>富文本</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 500px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. // 基于准备好的dom,初始化echarts实例
  21. const myChart = echarts.init(document.getElementById('main'));
  22. // 数据
  23. const data=[
  24. {name:'杨戬',value:80,img:'./images/yj.jpg'},
  25. {name:'鲁班',value:60,img:'./images/lb.jpg'},
  26. {name:'沈梦溪',value:40,img:'./images/smx.jpg'},
  27. {name:'诸葛亮',value:30,img:'./images/zgl.jpg'}
  28. ];
  29. //获取hero的数据
  30. let hero=data[0];
  31. /*自定义标签 label
  32. * formatter 文本块
  33. * '{样式名|文字内容}\n 换行'
  34. * 文本块的样式
  35. * textBorderColor 文本描边颜色
  36. * textBorderWidth 文本描边宽度
  37. * ...
  38. * rich 富文本,在其中写入样式
  39. * width 宽
  40. * height 高
  41. * backgroundColor 背景色
  42. * image 背景图
  43. * fontSize 文字大小
  44. * lineHeight 行高
  45. * fontWeight 文本加粗
  46. * ...
  47. * */
  48. data.forEach((hero,ind)=>{
  49. hero.label={
  50. formatter:'{img|}\n{name|'+hero.name+'}\n{val|战力:'+hero.value+'}',
  51. textBorderColor:'yellow',
  52. textBorderWidth:2,
  53. rich:{
  54. img:{
  55. width:60,
  56. height:60,
  57. backgroundColor:{
  58. image:hero.img
  59. }
  60. },
  61. name:{
  62. fontSize:16,
  63. lineHeight:28,
  64. fontWeight:'bold'
  65. },
  66. val:{}
  67. }
  68. };
  69. });
  70. /*配置项*/
  71. const option = {
  72. title:{text:'英雄战力'},
  73. series: {
  74. type: 'pie',
  75. data,
  76. radius:'70%',
  77. }
  78. };
  79. myChart.setOption(option);
  80. </script>
  81. </body>
  82. </html>  

四、原生图形组件

原生图形组件就是可以自定义图形的组件。

原生图形组件里绘制的图形,可以绑定鼠标事件、拖拽事件等。

echarts 有两种点位:坐标位,像素位。

坐标位有直角坐标位、地理坐标位等。

原生图形的位置就是基于像素位定位的。

echarts 实例对象提供了坐标位和像素位的转换方法:

  • convertToPixel(坐标系,[x,y]) 坐标位转像素位
  • convertFromPixel(坐标系,[x,y]) 像素位转坐标位

案例 – 折线图标记点的拖拽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖拽</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 700px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. /*尺寸*/
  21. const symbolSize = 20;
  22. /*点位*/
  23. const data = [[0,0], [20, 20], [40,40]];
  24. /*实例化echarts*/
  25. const myChart = echarts.init(document.getElementById('main'));
  26. /*1.线绘制折线图*/
  27. myChart.setOption({
  28. /*tooltip 提示
  29. * triggerOn 提示的触发方式
  30. * mousemove 鼠标移动时触发
  31. * click 鼠标点击时触发
  32. * mousemove|click 移动点击皆可触发
  33. * none 不被鼠标移动、点击所触发。可再以后手动触发。
  34. * formatter 格式化提示内容
  35. * */
  36. tooltip: {
  37. triggerOn: 'none',
  38. formatter: function (params) {
  39. return Math.round(params.data[0]) + ' , ' + Math.round(params.data[1]);
  40. }
  41. },
  42. /*x轴
  43. * min,max 刻度区间
  44. * type 坐标轴的类型
  45. * value 数值轴
  46. * */
  47. xAxis: {
  48. min: 0,
  49. max: 50,
  50. type: 'value',
  51. },
  52. /*y 轴
  53. * 属性同x
  54. * */
  55. yAxis: {
  56. min: 0,
  57. max: 50,
  58. type: 'value',
  59. },
  60. /*系列 series
  61. * id 用于在 option 或者 API 中引用组件
  62. * type 类型
  63. * smooth 圆弧
  64. * symbolSize 标记点尺寸
  65. * data 数据
  66. * */
  67. series: [
  68. {
  69. id: 'a',
  70. type: 'line',
  71. smooth: true,
  72. symbolSize: symbolSize,
  73. data: data,
  74. /*itemStyle:{
  75. opacity:0
  76. }*/
  77. }
  78. ],
  79. });
  80. /*2.为标记点添加拖拽功能*/
  81. /*graphic 原生图形组件
  82. * type 图形类型,image, text, circle, sector, ring...
  83. * position 位置
  84. * shape 相关于图形的属性,不同类型的图形,其中的属性不同
  85. * onclick 点击事件
  86. * onmousemove 鼠标移动
  87. * */
  88. /*myChart.convertToPixel 将直角坐标系上的点转换为像素*/
  89. /*echarts.util.curry(函数,参数) 函数的柯里化,来自zrender
  90. * 函数中,this 便是event.target
  91. * */
  92. const graphic=data.map((ele,ind)=>{
  93. return {
  94. type: 'circle',
  95. position: myChart.convertToPixel('grid', ele),
  96. shape: {
  97. r: symbolSize / 2
  98. },
  99. invisible: true,
  100. draggable: true,
  101. ondrag: echarts.util.curry(onPointDragging, ind),
  102. onmousemove: echarts.util.curry(showTooltip, ind),
  103. onmouseout: echarts.util.curry(hideTooltip, ind),
  104. z: 100
  105. };
  106. });
  107. myChart.setOption({graphic});
  108. /*鼠标拖拽时,让折线中的点位随拖拽点变化
  109. * convertFromPixel(grid,pos)将拖拽点的像素位解析为直角坐标系的位置
  110. * setOption() 更新数据
  111. * */
  112. function onPointDragging(dataIndex) {
  113. data[dataIndex] = myChart.convertFromPixel('grid', this.position);
  114. myChart.setOption({
  115. series: [{
  116. id: 'a',
  117. data: data
  118. }]
  119. });
  120. }
  121. /*鼠标在标记点上移动时,触发显示提示事件
  122. * type 事件类型,如showTip
  123. * seriesIndex 系列在系列集合中的索引位置
  124. * dataIndex 标记点在系列中的索引位置
  125. * */
  126. function showTooltip(dataIndex) {
  127. myChart.dispatchAction({
  128. type: 'showTip',
  129. seriesIndex: 0,
  130. dataIndex: dataIndex
  131. });
  132. }
  133. /*鼠标在标记点上移动时,触发隐藏提示事件*/
  134. function hideTooltip(dataIndex) {
  135. myChart.dispatchAction({
  136. type: 'hideTip'
  137. });
  138. }
  139. </script>
  140. </body>
  141. </html>

  

原生图形 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>原生图形</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 800px;
  10. height: 700px;
  11. background: antiquewhite;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!--建立dom 容器-->
  17. <div id="main"></div>
  18. <!--引入echarts-->
  19. <script src="./js/echarts.min.js"></script>
  20. <script>
  21. /*实例化echarts*/
  22. const myChart = echarts.init(document.getElementById('main'));
  23. /*配置项*/
  24. let option={
  25. /*graphic 原生图形组件
  26. * type 图形类型,image, text, circle, sector, ring...
  27. * position 位置
  28. * shape 相关于图形的属性
  29. * style 图形样式
  30. * draggable 可否拖拽
  31. * onmouseover 鼠标划上
  32. * onmouseup 鼠标抬起
  33. * onmouseout 鼠标划出
  34. * */
  35. graphic:{
  36. id:'c',
  37. type:'circle',
  38. shape:{
  39. r:100
  40. },
  41. position:[300,300],
  42. style:{
  43. fill:'green'
  44. },
  45. draggable:true,
  46. onmouseover:overFn,
  47. onmouseout:outFn
  48. }
  49. };
  50. /*绘图*/
  51. myChart.setOption(option);
  52. /*事件*/
  53. function overFn(){
  54. myChart.setOption({
  55. graphic:{
  56. id:'c',
  57. style:{
  58. fill:'yellow'
  59. },
  60. }
  61. });
  62. }
  63. function outFn(){
  64. myChart.setOption({
  65. graphic:{
  66. id:'c',
  67. style:{
  68. fill:'green'
  69. },
  70. }
  71. });
  72. }
  73. </script>
  74. </body>
  75. </html>

 原生图形的位置

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>原生图形</title>
  6. <style>
  7. #main{
  8. margin: 20px;
  9. width: 700px;
  10. height: 700px;
  11. background: antiquewhite;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!--建立dom 容器-->
  17. <div id="main"></div>
  18. <!--引入echarts-->
  19. <script src="./js/echarts.min.js"></script>
  20. <script>
  21. /*实例化echarts*/
  22. const myChart = echarts.init(document.getElementById('main'));
  23. /*绘制坐标系*/
  24. myChart.setOption({
  25. /*x轴
  26. * min,max 刻度区间
  27. * type 坐标轴的类型
  28. * value 数值轴
  29. * */
  30. xAxis:{
  31. min:0,
  32. max:50,
  33. },
  34. /*y 轴
  35. * 属性同x
  36. * */
  37. yAxis:{
  38. min:0,
  39. max:50,
  40. }
  41. });
  42. /*绘制原生图形*/
  43. myChart.setOption({
  44. graphic:{
  45. id:'c',
  46. type:'circle',
  47. shape:{
  48. r:100
  49. },
  50. // position:[10,10],
  51. position:myChart.convertToPixel('grid',[20,10]),
  52. style:{
  53. fill:'green'
  54. },
  55. }
  56. });
  57. </script>
  58. </body>
  59. </html>

五、响应式布局

在html 中使用css 中的flex 可以轻松实现响应式布局。

在echarts 里,如何适配不同尺寸的屏幕呢?

  • 简单点的可以通过为尺寸、位置等属性设置百分比来实现。
  • 复杂些的就需要自定义响应规则。

自定义响应规则的方法

  • 建立基础配置项 baseOption
  • 建立规则配置列表 media
  1. 建立query
  2. 建立此规则下的配置信息option
  • echarts 实例基于baseOption 和media 绘制图表
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>响应式布局</title>
  6. <style>
  7. html,body{margin: 0;height: 100%}
  8. #main{
  9. height: 100%;
  10. background: antiquewhite;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!--建立dom 容器-->
  16. <div id="main"></div>
  17. <!--引入echarts-->
  18. <script src="./js/echarts.min.js"></script>
  19. <script>
  20. /*实例化echarts*/
  21. const myChart = echarts.init(document.getElementById('main'));
  22. /*基础配置项 baseOption,建立两个绘图区
  23. * grid 绘图区
  24. * xAxis yAxis
  25. * min 最小刻度值
  26. * max 最大刻度值
  27. * gridIndex 绘图区的索引位置
  28. *
  29. * */
  30. const baseOption={
  31. grid:[
  32. {left:'10%',right:'55%',top:'10%',bottom:'10%'},
  33. {left:'55%',right:'10%',top:'10%',bottom:'10%'},
  34. ],
  35. xAxis:[
  36. {min:0,max:50,gridIndex:0},
  37. {min:0,max:50,gridIndex:1},
  38. ],
  39. yAxis:[
  40. {min:0,max:50,gridIndex:0},
  41. {min:0,max:50,gridIndex:1},
  42. ],
  43. };
  44. /*media 规则配置列表
  45. * query 规则,如maxWidth: 768
  46. * option 配置项
  47. * */
  48. const media=[
  49. {
  50. option:{
  51. grid:[
  52. {left:'10%',right:'55%',top:'10%',bottom:'10%'},
  53. {left:'55%',right:'10%',top:'10%',bottom:'10%'},
  54. ],
  55. }
  56. },
  57. {
  58. query:{
  59. maxWidth:768
  60. },
  61. option:{
  62. grid:[
  63. {left:'10%',right:'10%',top:'10%',bottom:'55%'},
  64. {left:'10%',right:'10%',top:'55%',bottom:'10%'},
  65. ],
  66. }
  67. }
  68. ];
  69. /*绘图*/
  70. myChart.setOption({baseOption,media});
  71. /*窗口尺寸发生变化resize 时,echarts 实例使用resize() 方法重置尺寸*/
  72. window.addEventListener('resize',function(){
  73. myChart.resize();
  74. })
  75. </script>
  76. </body>
  77. </html>


注:课程来自开课吧

原文链接:https://blog.csdn.net/qq_34235864/article/details/120878688




所属网站分类: 技术文章 > 博客

作者:js是天下最好的语言

链接:http://www.qianduanheidong.com/blog/article/211006/52655c6cbee28f172331/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)