经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
echarts设置多条折线不是你想的那样简单
来源:cnblogs  作者:南风晚来晚相识  时间:2023/12/15 8:46:52  对本文有异议

简单的多条折线图

  1. 小伙伴写过多条折线图的都知道,
  2. 常见的折线图是 xAxis 配置项下的 data属性上设置时间或者日期。
  3. series配置项下是对应的 legend中的数据以及该条折线的数据。
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多条折线图</title>
  7. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <div style="width: 900px;height: 400px;"></div>
  11. </body>
  12. <script>
  13. let myChart = echarts.init(document.querySelector('div'))
  14. // 设置X轴的时间
  15. let dataXTime = [
  16. '2023-12-04 09:45:07', '2023-12-04 09:50:07','2023-12-04 09:55:07', '2023-12-04 10:00:07', '2023-12-04 10:05:07',
  17. '2023-12-04 11:05:07','2023-12-04 12:05:07','2023-12-04 13:05:07','2023-12-04 14:05:07','2023-12-04 15:05:07',
  18. ]
  19. let option = {
  20. // 设置的是标题
  21. title: {
  22. text: '折线图'
  23. },
  24. tooltip: {
  25. trigger: 'axis'
  26. },
  27. legend: {
  28. data: ['Email', 'Union Ads']
  29. },
  30. // 网格间距设置
  31. grid: {
  32. left: '30px',
  33. right: '60px',
  34. bottom: '3%',
  35. containLabel: true
  36. },
  37. xAxis: {
  38. type: 'category',
  39. boundaryGap: false,
  40. data: dataXTime,
  41. },
  42. yAxis: {
  43. type: 'value'
  44. },
  45. // 数据
  46. series: [
  47. {
  48. name: 'Email',
  49. type: 'line',
  50. data: [120, 132, 101, 134, 90, 230, 210,90, 230, 210]
  51. },
  52. {
  53. name: 'Union Ads',
  54. type: 'line',
  55. data: [220, 182, 191, 234, 290, 330, 310,9, 30, 110]
  56. }
  57. ]
  58. };
  59. myChart.setOption(option);
  60. </script>
  61. </html>

发现多条折线共享一个时间

  1. 通过上面的小例子,我们发现一个问题:
  2. 多条折线共享的是一个时间(时间与数据是1对多的关系)
  3. 第一个时间匹配第一个数据,第2个时间匹配第2个数据。
  4. 也就是第n个时间匹配第n个数据。
  5. 我们不仅会提出这样一个问题:
  6. 有没有可能让每一条折线拥有自己的时间呢?
  7. 时间不同,也可以显示在一个实例上。

多条折线拥有数据自己的时间

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多条折线图</title>
  7. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <div style="width: 900px;height: 400px;"></div>
  11. </body>
  12. <script>
  13. let myChart = echarts.init(document.querySelector('div'))
  14. let option = {
  15. // 设置的是标题
  16. title: {
  17. text: '折线图'
  18. },
  19. tooltip: {
  20. trigger: 'axis'
  21. },
  22. legend: {
  23. data: ['邮件', '短信']
  24. },
  25. // 网格间距设置
  26. grid: {
  27. left: '30px',
  28. right: '60px',
  29. bottom: '3%',
  30. containLabel: true
  31. },
  32. xAxis: {
  33. // xAxis的下不在设置data属性共享时间
  34. type: 'category',
  35. splitLine: { show: false },
  36. lineStyle: {
  37. width: 2
  38. },
  39. axisTick: {
  40. show: false
  41. },
  42. axisLabel:{
  43. // 更改x轴文字颜色的配置
  44. textStyle: {
  45. color: '#717782'
  46. },
  47. showMaxLabel: true // 固定显示X轴的最后一条数据
  48. },
  49. // 更改x轴线的颜色
  50. axisLine: {
  51. lineStyle: {
  52. color: '#D2DBE6;',
  53. width: 1 // x轴线的宽度
  54. }
  55. },
  56. },
  57. yAxis: {
  58. type: 'value'
  59. },
  60. // 数据
  61. series: [
  62. {
  63. "name": "邮件",
  64. "type": "line",
  65. "symbol": "rect",
  66. "connectNulls": true,
  67. "showAllSymbol": true,
  68. // 让每一条折线拥有数据自己的时间
  69. "data": [
  70. [ "2023-12-04 09:50:07", "0.137"],
  71. [ "2023-12-04 09:55:07", "0.147"],
  72. [ "2023-12-04 10:00:07", "0.137"],
  73. [ "2023-12-04 10:05:07", "0.163"],
  74. [ "2023-12-04 10:10:07", "0.150"],
  75. [ "2023-12-04 10:15:07", "0.143"],
  76. [ "2023-12-04 10:20:07", "0.133"],
  77. [ "2023-12-04 10:25:07", "0.147"],
  78. [ "2023-12-04 10:30:07", "0.147"],
  79. [ "2023-12-04 10:35:07", "0.143"],
  80. [ "2023-12-04 10:40:07", "0.140"],
  81. [ "2023-12-04 10:45:07", "0.150"],
  82. [ "2023-12-04 10:50:07", "0.143"],
  83. ],
  84. "unit": "%",
  85. "markPoint": {
  86. "symbol": "rect",
  87. "symbolSize": "12",
  88. "label": { "show": false },
  89. "tooltip": { "triggerOn": "click", "trigger": "item" },
  90. }
  91. },
  92. {
  93. "name": "短信",
  94. "type": "line",
  95. "symbol": "rect",
  96. "connectNulls": true,
  97. "showAllSymbol": true,
  98. "data": [
  99. [ "2023-12-04 10:35:07", "0.123"],
  100. [ "2023-12-04 10:40:07", "0.140"],
  101. [ "2023-12-04 10:45:07", "0.150"],
  102. [ "2023-12-04 10:50:07", "0.143"],
  103. ],
  104. "unit": "%",
  105. "markPoint": {
  106. "symbol": "circle",
  107. "symbolSize": "12",
  108. "label": { "show": false }
  109. }
  110. }
  111. ]
  112. };
  113. myChart.setOption(option);
  114. </script>
  115. </html>

成功了吗?多条折线拥有数据自己的时间

  1. 根据上面的图片,我们发现。
  2. 好像确实是每一条折线都拥有数据自己的时间了。
  3. 但是如果你只细看的话。你就会发现端倪
  4. 结束时间都是一样的,但是折线却是在不同的时间上结束的。
  5. 很明显不正确的。

多条折线他们必须有一样的开始时间和结束时间?

  1. 上面我们发现了问题:结束时间都是一样的,但是折线却是在不同的时间上结束的。
  2. 有的机智的小伙伴可能会说:
  3. 是因为:多条折线他们必须有一样的开始时间和结束时间。
  4. 这样echarts在渲染的时候就不会出现上面这样的情况。
  5. 需要有相同的起始点和结束点
  6. 感觉有点道理,我们尝试一下
  1. series: [
  2. {
  3. "name": "邮件",
  4. "data": [
  5. [ "2023-12-04 09:50:07", "0.137"],
  6. [ "2023-12-04 09:55:07", "0.147"],
  7. [ "2023-12-04 10:00:07", "0.137"],
  8. [ "2023-12-04 10:05:07", "0.163"],
  9. [ "2023-12-04 10:10:07", "0.150"],
  10. [ "2023-12-04 10:15:07", "0.143"],
  11. [ "2023-12-04 10:20:07", "0.133"],
  12. [ "2023-12-04 10:25:07", "0.147"],
  13. [ "2023-12-04 10:30:07", "0.147"],
  14. [ "2023-12-04 10:35:07", "0.143"],
  15. [ "2023-12-04 10:40:07", "0.140"],
  16. [ "2023-12-04 10:45:07", "0.150"],
  17. [ "2023-12-04 10:50:07", "0.143"],
  18. ],
  19. },
  20. {
  21. "name": "短信",
  22. "data": [
  23. [ "2023-12-04 09:50:07", "0.8"],
  24. [ "2023-12-04 10:40:07", "0.140"],
  25. [ "2023-12-04 10:45:07", "0.150"],
  26. [ "2023-12-04 10:50:07", "0.143"],
  27. ],
  28. }
  29. ]
  30. 现在都有相同的起始点(2023-12-04 09:50:07)和结束点(2023-12-04 10:50:07)。

如果每条折线的时间都没有交集会怎么样?

  1. 我们发现只要有相同的起始点和结束点;
  2. 就会可以达到我们的预期效果。
  3. 此时,有的小伙伴说:
  4. "如果他们的时间如果没有交集会怎么样(有相同的起始点和结束点)"
  5. "data": [
  6. [ "2023-12-04 09:50:07", "0.137"],
  7. [ "2023-12-04 09:55:07", "0.147"],
  8. [ "2023-12-04 10:00:07", "0.137"],
  9. [ "2023-12-04 10:05:07", "0.163"],
  10. [ "2023-12-04 10:10:07", "0.150"],
  11. [ "2023-12-04 10:15:07", "0.143"],
  12. [ "2023-12-04 10:20:07", "0.133"],
  13. [ "2023-12-04 10:25:07", "0.147"],
  14. [ "2023-12-04 10:30:07", "0.147"],
  15. [ "2023-12-04 10:35:07", "0.143"],
  16. [ "2023-12-04 10:40:07", "0.140"],
  17. [ "2023-12-04 10:45:07", "0.150"],
  18. [ "2023-12-04 10:50:07", "0.143"],
  19. ],
  20. "data": [
  21. [ "2023-12-04 09:50:07", "0.8"],
  22. [ "2023-12-04 09:52:07", "1.23"],
  23. [ "2023-12-04 10:41:07", "0.140"],
  24. [ "2023-12-04 10:49:07", "0.150"],
  25. [ "2023-12-04 10:50:07", "0.143"],
  26. ],

xAxis 的 type值设置time

  1. 时间绘制的折线图不对,怎么会有返回去的折线?
  2. 怎么去解决这个问题呢?
  3. 有些小伙伴又提出了。我们可以将 xAxis type值设置time
  4. 就可以解决这个问题。

在 ECharts 中,type的值是 time 和 category 的区别

  1. 1.数据类型:'time' 表示时间类型的数据,适用于连续的时序数据。
  2. 通常返回的是时间戳。我们为了方便这里写的是yyyy-mm-dd hh:mm:ss
  3. 'category' 表示类目类型的数据,适用于离散的类目数据。
  4. 2.显示方式:在 'time' 类型下,
  5. ECharts 会根据时间跨度自动切换显示的时间粒度,例如从月、星期、日到小时等。
  6. 而在 'category' 类型下,坐标轴只会显示类目列表,并且坐标轴内仅包含这些指定类目的坐标。

时间格式又不对

  1. 有眼尖的小伙伴发现了一个小问题。
  2. 我们给的时间是 yyyy-mm-dd hh:mm:ss的格式
  3. 但是刚刚发现展示的是 hh:ss mm-dd
  4. 格式和我们预期的不符合

xAxis 配置项 axisLabel下的formatter 转换时间格式

  1. 通过查询echarts的文档。
  2. 我们发现 xAxis.axisLabel.formatter 可以做到对格式进行转换。
  3. formatter:刻度标签的内容格式器,支持字符串模板和回调函数两种形式。
  4. 对于时间轴(type: 'time'),formatter 的字符串模板支持3种形式:
  5. 1.字符串模板:简单快速实现常用日期时间模板,string 类型
  6. 2.回调函数:自定义 formatter,可以用来实现复杂高级的格式,Function 类型
  7. 3.分级模板:为不同时间粒度的标签使用不同的 formatterobject 类型
  8. 我发现使用 字符串模板 模板是不行的。分级模板没有试过。
  9. 官网推荐使用字符串模板,如果可以使用成功。
  10. 我们就不需要在写一个方法进行转化了。
  11. 但是很遗憾,失败了。可能是用的方式错误吧

字符串模板是失败的

字符串模板是失败的原因

  1. 本来我已经失望了。
  2. 结果小脑袋灵光一闪,猜测有没有可能是版本的原因。
  3. 我果断去切换到5的版本
  4. <script src="https://cdn.staticfile.org/echarts/5.3.0/echarts.min.js"></script>
  5. 果然字符串模板显示正常了
  6. 模板字符串的详细使用地址是:https://echarts.apache.org/zh/option.html#xAxis.axisLabel.formatter

字符串模板转化时间格式【推荐】

  1. xAxis: {
  2. // xAxis的下不在设置data属性共享时间`
  3. type: 'time',
  4. splitLine: { show: false },
  5. lineStyle: {
  6. width: 2
  7. },
  8. axisTick: {
  9. show: false
  10. },
  11. axisLabel:{
  12. // 更改x轴文字颜色的配置
  13. textStyle: {
  14. color: '#717782'
  15. },
  16. // 设置坐标轴上的时间格式 --使用的是模板字符串
  17. // formatter: "{yyyy}-{MM}-{dd}", 得到的 label 形如:'2020-12-02'
  18. formatter: '{yyyy}-{MM}-{dd} \n{HH}:{mm}:{ss}',
  19. showMinLabel: true,
  20. showMaxLabel: true // 固定显示X轴的最后一条数据
  21. },
  22. // 更改x轴线的颜色
  23. axisLine: {
  24. lineStyle: {
  25. color: '#D2DBE6;',
  26. width: 1 // x轴线的宽度
  27. }
  28. },
  29. },

使用回调函数转化时间格式

  1. function backTime(value){
  2. let date = new Date(value);
  3. // 获取年份、月份和日期
  4. let year = date.getFullYear();
  5. let month = date.getMonth() + 1; // 月份从 0 开始,需要加 1
  6. let day = date.getDate();
  7. let hours = date.getHours();
  8. let minutes = date.getMinutes();
  9. let seconds = date.getSeconds();
  10. // 格式化月份和日期为两位数(不足两位时补零)
  11. month = month < 10 ? '0' + month : month;
  12. day = day < 10 ? '0' + day : day;
  13. hours = hours < 10 ? '0' + hours : hours;
  14. minutes = minutes < 10 ? '0' + minutes : minutes;
  15. seconds = seconds < 10 ? '0' + seconds : seconds;
  16. // 返回格式化后的字符串
  17. return year + '-' + month + '-' + day + ' ' +
  18. hours + ':' + minutes + ':' + seconds;
  19. }
  20. xAxis: {
  21. // xAxis的下不在设置data属性共享时间
  22. type: 'time',
  23. splitLine: { show: false },
  24. lineStyle: {
  25. width: 2
  26. },
  27. axisTick: {
  28. show: false
  29. },
  30. axisLabel:{
  31. // 更改x轴文字颜色的配置
  32. textStyle: {
  33. color: '#717782'
  34. },
  35. // 设置坐标轴上的时间格式
  36. formatter: function (value) {
  37. console.log('时间戳',value )
  38. // 将时间转换为 我们需要的格式 ,这里的value是一个时间戳
  39. return backTime(value)
  40. },
  41. showMinLabel: true,
  42. showMaxLabel: true // 固定显示X轴的最后一条数据
  43. },
  44. // 更改x轴线的颜色
  45. axisLine: {
  46. lineStyle: {
  47. color: '#D2DBE6;',
  48. width: 1 // x轴线的宽度
  49. }
  50. },
  51. },
  1. 特别提醒: type: 'time'的时候,
  2. formatter : function (value) { }
  3. 中的value是一个时间戳

更改tooltip的时间格式

  1. tooltip: {
  2. trigger: 'axis',
  3. formatter: (c) => {
  4. let str = ''
  5. let temp = {
  6. showTime: '', // 时间
  7. marker: '', // 颜色
  8. seriesName: '', // legend名称
  9. valueData: '', // 数值
  10. setWidthSpan: '',
  11. }
  12. c.forEach((item) => {
  13. temp.showTime = item.data[0]
  14. temp.marker = item.marker
  15. temp.seriesName = item.seriesName
  16. temp.valueData = item.value[1]
  17. temp.setWidthSpan = '<span style="display:inline-block;width:10px;height:10px;"></span>'
  18. str += temp.marker + temp.seriesName + temp.setWidthSpan + temp.valueData + '<br />'
  19. })
  20. return temp.showTime + '<br />' + str
  21. },
  22. },

完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多条折线图</title>
  7. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <div style="width: 900px;height: 400px;"></div>
  11. </body>
  12. <script>
  13. function backTime(value){
  14. let date = new Date(value);
  15. // 获取年份、月份和日期
  16. let year = date.getFullYear();
  17. let month = date.getMonth() + 1; // 月份从 0 开始,需要加 1
  18. let day = date.getDate();
  19. let hours = date.getHours();
  20. let minutes = date.getMinutes();
  21. let seconds = date.getSeconds();
  22. // 格式化月份和日期为两位数(不足两位时补零)
  23. month = month < 10 ? '0' + month : month;
  24. day = day < 10 ? '0' + day : day;
  25. hours = hours < 10 ? '0' + hours : hours;
  26. minutes = minutes < 10 ? '0' + minutes : minutes;
  27. seconds = seconds < 10 ? '0' + seconds : seconds;
  28. // 返回格式化后的字符串
  29. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
  30. }
  31. let myChart = echarts.init(document.querySelector('div'))
  32. let option = {
  33. // 设置的是标题
  34. title: {
  35. text: '折线图'
  36. },
  37. tooltip: {
  38. trigger: 'axis',
  39. formatter: (c) => {
  40. let str = ''
  41. let temp = {
  42. showTime: '', // 时间
  43. marker: '', // 颜色
  44. seriesName: '', // legend名称
  45. valueData: '', // 数值
  46. setWidthSpan: '',
  47. }
  48. c.forEach((item) => {
  49. temp.showTime = item.data[0]
  50. temp.marker = item.marker
  51. temp.seriesName = item.seriesName
  52. temp.valueData = item.value[1]
  53. temp.setWidthSpan = '<span style="display:inline-block;width:10px;height:10px;"></span>'
  54. str += temp.marker + temp.seriesName + temp.setWidthSpan + temp.valueData + '<br />'
  55. })
  56. return temp.showTime + '<br />' + str
  57. },
  58. },
  59. legend: {
  60. data: ['邮件', '短信']
  61. },
  62. // 网格间距设置
  63. grid: {
  64. left: '30px',
  65. right: '60px',
  66. bottom: '3%',
  67. containLabel: true
  68. },
  69. xAxis: {
  70. // xAxis的下不在设置data属性共享时间`
  71. type: 'time',
  72. splitLine: { show: false },
  73. lineStyle: {
  74. width: 2
  75. },
  76. axisTick: {
  77. show: false
  78. },
  79. axisLabel:{
  80. // 更改x轴文字颜色的配置
  81. textStyle: {
  82. color: '#717782'
  83. },
  84. // 设置坐标轴上的时间格式
  85. formatter: function (value) {
  86. console.log('时间戳',value )
  87. // 将时间转换为 JavaScript 日期对象
  88. return backTime(value)
  89. },
  90. showMinLabel: true,
  91. showMaxLabel: true // 固定显示X轴的最后一条数据
  92. },
  93. // 更改x轴线的颜色
  94. axisLine: {
  95. lineStyle: {
  96. color: '#D2DBE6;',
  97. width: 1 // x轴线的宽度
  98. }
  99. },
  100. },
  101. yAxis: {
  102. type: 'value'
  103. },
  104. // 数据
  105. series: [
  106. {
  107. "name": "邮件",
  108. "type": "line",
  109. "symbol": "rect",
  110. "connectNulls": true,
  111. "showAllSymbol": true,
  112. // 让每一条折线拥有数据自己的时间
  113. "data": [
  114. [ "2023-12-04 09:50:07", "0.137"],
  115. [ "2023-12-04 09:55:07", "0.147"],
  116. [ "2023-12-04 10:00:07", "0.137"],
  117. [ "2023-12-04 10:05:07", "0.163"],
  118. [ "2023-12-04 10:10:07", "0.150"],
  119. [ "2023-12-04 10:15:07", "0.143"],
  120. [ "2023-12-04 10:20:07", "0.133"],
  121. [ "2023-12-04 10:25:07", "0.147"],
  122. [ "2023-12-04 10:30:07", "0.147"],
  123. [ "2023-12-04 10:35:07", "0.143"],
  124. [ "2023-12-04 10:40:07", "0.140"],
  125. [ "2023-12-04 10:45:07", "0.150"],
  126. [ "2023-12-04 10:50:07", "0.143"],
  127. ],
  128. "unit": "%",
  129. "markPoint": {
  130. "symbol": "rect",
  131. "symbolSize": "12",
  132. "label": { "show": false },
  133. "tooltip": { "triggerOn": "click", "trigger": "item" },
  134. }
  135. },
  136. {
  137. "name": "短信",
  138. "type": "line",
  139. "symbol": "rect",
  140. "connectNulls": true,
  141. "showAllSymbol": true,
  142. "data": [
  143. [ "2023-12-04 09:50:07", "0.8"],
  144. [ "2023-12-04 09:52:07", "1.23"],
  145. [ "2023-12-04 10:41:07", "0.140"],
  146. [ "2023-12-04 10:42:07", "0.140"],
  147. [ "2023-12-04 10:45:07", "0.140"],
  148. [ "2023-12-04 10:49:07", "0.150"],
  149. [ "2023-12-04 10:50:07", "0.143"],
  150. ],
  151. "unit": "%",
  152. "markPoint": {
  153. "symbol": "circle",
  154. "symbolSize": "12",
  155. "label": { "show": false }
  156. }
  157. }
  158. ]
  159. };
  160. myChart.setOption(option);
  161. </script>
  162. </html>

原文链接:https://www.cnblogs.com/IwishIcould/p/17900950.html

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

本站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号