• jQuery使用echarts循环插入图表


    目录

    jQuery动态循环插入echarts图表

    y轴显示最大值和最小值

    x轴只显示两个值,开始日期和结束日期


    jQuery动态循环插入echarts图表

    html

    .center_img_list 是我们循环数据的地方

    1. <div class="center_img shadow">
    2. <div class="center_img_border">
    3. <div class="center_img_list">
    4. <div class="canvas">div>
    5. <div class="img_title">一站式 HarmonyOS/OpenHarmo…div>
    6. <div class="label">计算机行业专题研究:华为算力进展不断div>
    7. div>
    8. div>
    9. div>

    css

    1. .center_img_border {
    2. display: flex;
    3. justify-content: space-around;
    4. padding: 0.3rem;
    5. }
    6. .center_img_list {
    7. width: 30%;
    8. }
    9. .center_img_list .canvas {
    10. border: solid green 1px;
    11. border-radius: 10px;
    12. width: 100%;
    13. height: 206px;
    14. }

    js

    1. var newList = [{
    2. "id": "1",
    3. "title": "计算机行业专题研究:华为算力进展不断",
    4. "content": "图表 1:双十一美妆销量",
    5. 'list': [1, 20, 200, 3, 52, 99],
    6. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    7. },
    8. {
    9. "id": "2",
    10. "title": "计算机行业专题研究:华为算力进展不断",
    11. "content": "图表 2:双十一家电销量",
    12. 'list': [1000, 20, 200, 3, 52, 99],
    13. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    14. },
    15. {
    16. "id": "3",
    17. "title": "计算机行业专题研究:华为算力进展不断",
    18. "content": "图表 3:双十一家具销量",
    19. 'list': [100, 200, 220, 300, 2, 9],
    20. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    21. },
    22. ]
    23. this.getEcharts(newList);
    24. // 图表数据
    25. function getEcharts(newList) {
    26. let echartsHtml = ''
    27. $.each(newList, function(i, v) {
    28. echartsHtml += `
    29. `" >
    30. ${v.content}
    31. ${v.title}
    32. `
  • $(document).ready(function() {
  • changeECharts(v.list, v.time, 'canvas' + i);
  • })
  • })
  • $(".center_img_border").html(echartsHtml);
  • }
  • function changeECharts(changePCTRYsList, x, idname) {
  • var chartDom = document.getElementById(idname);
  • let objDom = {}
  • objDom[idname] = echarts.init(chartDom);
  • let option = {
  • xAxis: [{
  • type: 'category',
  • boundaryGap: true,
  • data: x,
  • axisLabel: {
  • interval: x.length - 2,
  • fontSize: 12,
  • },
  • axisLine: {
  • show: false, //隐藏x轴
  • },
  • axisTick: {
  • show: false, //刻度线
  • },
  • }],
  • grid: {
  • left: '',
  • right: 30,
  • bottom: 20,
  • top: 20,
  • containLabel: true
  • },
  • tooltip: {
  • show: true,
  • trigger: 'axis'
  • },
  • yAxis: {
  • show: true,
  • scale: true,
  • alignTicks: true,
  • axisTick: {
  • inside: true
  • },
  • type: 'value',
  • min: Math.min(...changePCTRYsList),
  • max: Math.max(...changePCTRYsList)
  • },
  • series: [{
  • name: '收盘价',
  • data: changePCTRYsList,
  • type: 'line',
  • itemStyle: {
  • color: '#649E92',
  • },
  • symbol: 'none',
  • },
  • {
  • name: '成交量',
  • data: changePCTRYsList,
  • type: 'line',
  • itemStyle: {
  • color: '#649E92',
  • },
  • symbol: 'none',
  • }
  • ]
  • };
  • objDom[idname].setOption(option);
  • }
  • y轴显示最大值和最小值

    y轴设置刻度最大和最小值

    min: Math.min(...changePCTRYsList),
    max: Math.max(...changePCTRYsList)

    x轴只显示两个值,开始日期和结束日期

    在xAxis中的axisLabel设置 interval: x.length - 2 即可

    1. axisLabel: {
    2. interval: x.length - 2,
    3. fontSize: 12,
    4. },

    全部代码,可以直接运行

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>echartstitle>
    8. <style>
    9. *,
    10. html,
    11. body {
    12. padding: 0;
    13. margin: 0;
    14. box-sizing: border-box;
    15. }
    16. .center_img_border {
    17. display: flex;
    18. justify-content: space-around;
    19. padding: 0.3rem;
    20. }
    21. .center_img_list {
    22. width: 30%;
    23. }
    24. .center_img_list .canvas {
    25. border: solid green 1px;
    26. border-radius: 10px;
    27. width: 100%;
    28. height: 206px;
    29. }
    30. style>
    31. head>
    32. <body>
    33. <div class="center_img_border">
    34. <div class="center_img_list">
    35. <div class="canvas">div>
    36. <div class="img_title">一站式 HarmonyOS/OpenHarmo…div>
    37. <div class="label">计算机行业专题研究:华为算力进展不断div>
    38. div>
    39. div>
    40. body>
    41. <script type="text/javascript" src="js/echarts.js">script>
    42. <script type="text/javascript" src="js/jquery-2.1.4.min.js">script>
    43. <script>
    44. var newList = [{
    45. "id": "1",
    46. "title": "计算机行业专题研究:华为算力进展不断",
    47. "content": "图表 1:双十一美妆销量",
    48. 'list': [1, 20, 200, 3, 52, 99],
    49. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    50. },
    51. {
    52. "id": "2",
    53. "title": "计算机行业专题研究:华为算力进展不断",
    54. "content": "图表 2:双十一家电销量",
    55. 'list': [1000, 20, 200, 3, 52, 99],
    56. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    57. },
    58. {
    59. "id": "3",
    60. "title": "计算机行业专题研究:华为算力进展不断",
    61. "content": "图表 3:双十一家具销量",
    62. 'list': [100, 200, 220, 300, 2, 9],
    63. 'time': ['2023/01/02', '2023/01/03', '2023/02/02', '2023/02/22', '2023/03/02', '2023/04/02', ]
    64. },
    65. ]
    66. this.getEcharts(newList);
    67. // 图表数据
    68. function getEcharts(newList) {
    69. let echartsHtml = ''
    70. $.each(newList, function(i, v) {
    71. echartsHtml += `
    72. `" >
    73. ${v.content}
    74. ${v.title}
    75. `
  • $(document).ready(function() {
  • changeECharts(v.list, v.time, 'canvas' + i);
  • })
  • })
  • $(".center_img_border").html(echartsHtml);
  • }
  • function changeECharts(changePCTRYsList, x, idname) {
  • var chartDom = document.getElementById(idname);
  • let objDom = {}
  • objDom[idname] = echarts.init(chartDom);
  • let option = {
  • xAxis: [{
  • type: 'category',
  • boundaryGap: true,
  • data: x,
  • axisLabel: {
  • interval: x.length - 2,
  • fontSize: 12,
  • },
  • axisLine: {
  • show: false, //隐藏x轴
  • },
  • axisTick: {
  • show: false, //刻度线
  • },
  • }],
  • grid: {
  • left: '',
  • right: 30,
  • bottom: 20,
  • top: 20,
  • containLabel: true
  • },
  • tooltip: {
  • show: true,
  • trigger: 'axis'
  • },
  • yAxis: {
  • show: true,
  • scale: true,
  • alignTicks: true,
  • axisTick: {
  • inside: true
  • },
  • type: 'value',
  • min: Math.min(...changePCTRYsList),
  • max: Math.max(...changePCTRYsList)
  • },
  • series: [{
  • name: '收盘价',
  • data: changePCTRYsList,
  • type: 'line',
  • itemStyle: {
  • color: '#649E92',
  • },
  • symbol: 'none',
  • },
  • {
  • name: '成交量',
  • data: changePCTRYsList,
  • type: 'line',
  • itemStyle: {
  • color: '#649E92',
  • },
  • symbol: 'none',
  • }
  • ]
  • };
  • objDom[idname].setOption(option);
  • }
  • script>
  • html>
  • 相关阅读:
    LeetCode 1235. 规划兼职工作
    内存管理下及模板初阶
    h2database BTree 设计实现与查询优化思考
    进一步了解视频美颜SDK:美颜SDK的技术原理
    【Hack The Box】linux练习-- Networked
    【2024最新】Spring面试题
    pytorch基本流程
    MySQL InnoDB 是怎么使用 B+ 树存数据的?
    数据结构篇——栈和队列
    智能多价格-面向电商的全栈开发利器
  • 原文地址:https://blog.csdn.net/Hero_rong/article/details/134426733