-
Notifications
You must be signed in to change notification settings - Fork 0
/
45513c12.html
1 lines (1 loc) · 105 KB
/
45513c12.html
1
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>React多个echarts图表在一个页面的使用 | 梦洁小站-属于你我的小天地</title><meta name="author" content="梦洁"><meta name="copyright" content="梦洁"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前景 很多情况下图标都是一个,我们大概率会像下面代码一样的做法 大概流程就是获取到数据后执行初始化,因为先初始化后异步请求再设置state里面的数据回导致无法正常显示echarts(除非再次调用setOption) 下面就记录下自己解决过程 源码 https://github.com/superBiuBiuMan/react-class-test 根据ID获取DOM进行初始化1234"><meta property="og:type" content="article"><meta property="og:title" content="React多个echarts图表在一个页面的使用"><meta property="og:url" content="https://www.dreamlove.top/45513c12.html"><meta property="og:site_name" content="梦洁小站-属于你我的小天地"><meta property="og:description" content="前景 很多情况下图标都是一个,我们大概率会像下面代码一样的做法 大概流程就是获取到数据后执行初始化,因为先初始化后异步请求再设置state里面的数据回导致无法正常显示echarts(除非再次调用setOption) 下面就记录下自己解决过程 源码 https://github.com/superBiuBiuMan/react-class-test 根据ID获取DOM进行初始化1234"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181528746.png"><meta property="article:published_time" content="2024-03-02T21:48:01.000Z"><meta property="article:modified_time" content="2024-03-02T21:48:01.000Z"><meta property="article:author" content="梦洁"><meta property="article:tag" content="HTML"><meta property="article:tag" content="echarts"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181528746.png"><link rel="shortcut icon" href="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png"><link rel="canonical" href="https://www.dreamlove.top/45513c12.html"><link rel="preconnect" href="https://lib.baomitu.com"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://lib.baomitu.com/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.min.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:{appId:"G7A2D1SYPY",apiKey:"49056313a9be02852c7dbedfcb772831",indexName:"blog",hits:{per_page:20},languages:{input_placeholder:"搜索文章",hits_empty:"找不到您查询的内容:${query}",hits_stats:"找到 ${hits} 条结果,用时 ${time} 毫秒"}},localSearch:void 0,translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlight.js",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"复制成功",error:"复制错误",noSupport:"浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"天",dateSuffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:void 0,lightbox:"fancybox",Snackbar:void 0,infinitegrid:{js:"https://lib.baomitu.com/egjs-infinitegrid/4.11.1/infinitegrid.min.js",buttonText:"加载更多"},isPhotoFigcaption:!1,islazyload:!1,isAnchor:!1,percent:{toc:!0,rightside:!1},autoDarkmode:!1}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"React多个echarts图表在一个页面的使用",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2024-03-02 21:48:01"}</script><script>(e=>{e.saveToLocal={set:(e,t,o)=>{if(0===o)return;const a={value:t,expiry:Date.now()+864e5*o};localStorage.setItem(e,JSON.stringify(a))},get:e=>{const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!(Date.now()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=(e,t={})=>new Promise((o,a)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},Object.keys(t).forEach(e=>{n.setAttribute(e,t[e])}),document.head.appendChild(n)}),e.getCSS=(e,t=!1)=>new Promise((o,a)=>{const n=document.createElement("link");n.rel="stylesheet",n.href=e,t&&(n.id=t),n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},document.head.appendChild(n)}),e.activateDarkMode=()=>{document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=()=>{document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><meta name="generator" content="Hexo 7.1.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='onerror=null,src="/img/404.png"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><hr class="custom-hr"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181528746.png)"><nav id="nav"><span id="blog-info"><a href="/" title="梦洁小站-属于你我的小天地"><span class="site-name">梦洁小站-属于你我的小天地</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React多个echarts图表在一个页面的使用</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-03-02T21:48:01.000Z" title="发表于 2024-03-02 21:48:01">2024-03-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-03-02T21:48:01.000Z" title="更新于 2024-03-02 21:48:01">2024-03-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/HTML/">HTML</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/HTML/echarts/">echarts</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" data-flag-title="React多个echarts图表在一个页面的使用"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前景"><a href="#前景" class="headerlink" title="前景"></a>前景</h1><ul><li><p>很多情况下图标都是一个,我们大概率会像下面代码一样的做法</p><ul><li><strong>大概流程就是获取到数据后执行初始化,因为先初始化后异步请求再设置state里面的数据回导致无法正常显示echarts(除非再次调用setOption)</strong></li><li>下面就记录下自己解决过程</li><li>源码<ul><li><a target="_blank" rel="noopener" href="https://github.com/superBiuBiuMan/react-class-test">https://github.com/superBiuBiuMan/react-class-test</a></li></ul></li></ul><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403022009666.png"></p></li></ul><h2 id="根据ID获取DOM进行初始化"><a href="#根据ID获取DOM进行初始化" class="headerlink" title="根据ID获取DOM进行初始化"></a>根据ID获取DOM进行初始化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span> } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">'echarts'</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">TestEcharts</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params">props</span>) {</span><br><span class="line"> <span class="variable language_">super</span>(props);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span> = {</span><br><span class="line"> <span class="attr">testData</span>: <span class="string">'测试数据'</span>,</span><br><span class="line"> <span class="attr">list1</span>: [],</span><br><span class="line"> };</span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">componentDidMount</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initEchartsData</span>();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 请求并初始化echarts数据</span></span><br><span class="line"> <span class="title function_">initEchartsData</span>(<span class="params"></span>) {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>(</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">list1</span>: res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>)),</span><br><span class="line"> },</span><br><span class="line"> );</span><br><span class="line"> <span class="comment">// 初始化echarts</span></span><br><span class="line"> <span class="keyword">const</span> myChart = echarts.<span class="title function_">init</span>(<span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">'myChart'</span>));</span><br><span class="line"> myChart.<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>: <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">list1</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"myChart"</span> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">width:</span> '<span class="attr">400px</span>', <span class="attr">height:</span> '<span class="attr">400px</span>' }}></span><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span>></span>{this.state.testData}<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">TestEcharts</span>;</span><br></pre></td></tr></table></figure><h2 id="根据ref获取DOM进行初始化"><a href="#根据ref获取DOM进行初始化" class="headerlink" title="根据ref获取DOM进行初始化"></a>根据ref获取DOM进行初始化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span>,createRef } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">'echarts'</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">TestEcharts</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params">props</span>) {</span><br><span class="line"> <span class="variable language_">super</span>(props);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span> = {</span><br><span class="line"> <span class="attr">testData</span>: <span class="string">'测试数据'</span>,</span><br><span class="line"> <span class="attr">list1</span>: [],</span><br><span class="line"> };</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">echart1</span> = <span class="title function_">createRef</span>();</span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">componentDidMount</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initEchartsData</span>();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 请求并初始化echarts数据</span></span><br><span class="line"> <span class="title function_">initEchartsData</span>(<span class="params"></span>) {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>(</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">list1</span>: res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>)),</span><br><span class="line"> },</span><br><span class="line"> );</span><br><span class="line"> <span class="comment">// 初始化echarts</span></span><br><span class="line"> <span class="comment">// const myChart = echarts.init(document.getElementById('myChart'));</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">echart1</span>.<span class="property">current</span>);</span><br><span class="line"> <span class="keyword">const</span> myChart = echarts.<span class="title function_">init</span>(<span class="variable language_">this</span>.<span class="property">echart1</span>.<span class="property">current</span>);</span><br><span class="line"> myChart.<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>: <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">list1</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> {/* id="myChart" */}</span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> <span class="attr">ref</span>=<span class="string">{this.echart1}</span> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">width:</span> '<span class="attr">400px</span>', <span class="attr">height:</span> '<span class="attr">400px</span>' }}></span><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span>></span>{this.state.testData}<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">TestEcharts</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure><h1 id="如果有1个页面有多个echart图标怎么解决呢"><a href="#如果有1个页面有多个echart图标怎么解决呢" class="headerlink" title="如果有1个页面有多个echart图标怎么解决呢?"></a>如果有1个页面有多个echart图标怎么解决呢?</h1><h2 id="方式1-一个图表就是一个组件"><a href="#方式1-一个图表就是一个组件" class="headerlink" title="方式1-一个图表就是一个组件"></a>方式1-一个图表就是一个组件</h2><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403022100849.png"></p><h3 id="数据获取到后再setOption"><a href="#数据获取到后再setOption" class="headerlink" title="数据获取到后再setOption"></a>数据获取到后再setOption</h3><ul><li>主index.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span> } <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData</span> <span class="keyword">from</span> <span class="string">"./component/showData/index"</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span>></span>显示多个图表<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">display:</span>'<span class="attr">flex</span>' }}></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>图表ShowData.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span>, createRef } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">'echarts'</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params">props</span>) {</span><br><span class="line"> <span class="variable language_">super</span>(props);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span> = {</span><br><span class="line"> <span class="attr">data</span>: [],</span><br><span class="line"> };</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">echartDOM</span> = <span class="title function_">createRef</span>();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 请求数据</span></span><br><span class="line"> <span class="title function_">initData</span>(<span class="params"></span>) {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> <span class="attr">data</span>: res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>)),</span><br><span class="line"> },<span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initEchartsData</span>();</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//初始化echart图表</span></span><br><span class="line"> <span class="title function_">initEchartsData</span>(<span class="params"></span>){</span><br><span class="line"> <span class="keyword">const</span> myChart = echarts.<span class="title function_">init</span>(<span class="variable language_">this</span>.<span class="property">echartDOM</span>.<span class="property">current</span>);</span><br><span class="line"> myChart.<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>: <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">data</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">componentDidMount</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initData</span>(); <span class="comment">//初始化数据</span></span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{this.echartDOM}</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">width:</span> '<span class="attr">400px</span>', <span class="attr">height:</span> '<span class="attr">400px</span>' }}</span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> ></span><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h3 id="先setOption后再更新"><a href="#先setOption后再更新" class="headerlink" title="先setOption后再更新"></a>先setOption后再更新</h3><ul><li>通过createRef获取DOM再保存echarts实例方式</li><li>主index.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span> } <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData</span> <span class="keyword">from</span> <span class="string">"./component/showData/index"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData2</span> <span class="keyword">from</span> <span class="string">"./component/showData/index2"</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span>></span>显示多个图表<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">display:</span>'<span class="attr">flex</span>' }}></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>图表ShowData2.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span>, createRef } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">'echarts'</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params">props</span>) {</span><br><span class="line"> <span class="variable language_">super</span>(props);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span> = {</span><br><span class="line"> <span class="attr">data</span>: [],</span><br><span class="line"> <span class="attr">myEchart</span>: <span class="string">''</span>,</span><br><span class="line"> };</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">echartDOM</span> = <span class="title function_">createRef</span>();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 请求数据</span></span><br><span class="line"> <span class="title function_">initData</span>(<span class="params"></span>) {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> <span class="attr">data</span>: res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>)),</span><br><span class="line"> },<span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">myEchart</span>.<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>: <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">data</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//初始化echart图表</span></span><br><span class="line"> <span class="title function_">initEchartsData</span>(<span class="params"></span>){</span><br><span class="line"> <span class="keyword">const</span> myEchart = echarts.<span class="title function_">init</span>(<span class="variable language_">this</span>.<span class="property">echartDOM</span>.<span class="property">current</span>);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> myEchart,</span><br><span class="line"> })</span><br><span class="line"> myEchart.<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>: <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">data</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">componentDidMount</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initData</span>(); <span class="comment">//初始化数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initEchartsData</span>();<span class="comment">//初始化数据</span></span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{this.echartDOM}</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">width:</span> '<span class="attr">400px</span>', <span class="attr">height:</span> '<span class="attr">400px</span>' }}</span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> ></span><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="方式2-都写在一个jsx-tsx文件中"><a href="#方式2-都写在一个jsx-tsx文件中" class="headerlink" title="方式2-都写在一个jsx\tsx文件中"></a>方式2-都写在一个jsx\tsx文件中</h2><ul><li>比较难的地方在于怎么获取到这么多个的DOM再进行统一的初始化,并且还可能后续不同操作需要更新不同的DOM</li><li>这里只是数组简单的存储,你也可以通过Map来进行存储</li></ul><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403022140538.png"></p><ul><li>点击更新</li></ul><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403022141680.gif"></p><ul><li>主index.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span> } <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData</span> <span class="keyword">from</span> <span class="string">"./component/showData/index"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData2</span> <span class="keyword">from</span> <span class="string">"./component/showData/index2"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ShowData3</span> <span class="keyword">from</span> <span class="string">"./component/showData/index3"</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span>></span>显示多个图表<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> {/* 方式1 */}</span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">h1</span>></span>方式1<span class="tag"></<span class="name">h1</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">display:</span>'<span class="attr">flex</span>' }}></span></span></span><br><span class="line"><span class="language-xml"> {/* <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData2</span>/></span> */}</span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> {/* 方式2 */}</span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">h1</span>></span>方式2<span class="tag"></<span class="name">h1</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span> ></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">ShowData3</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>图表ShowData.jsx</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, { <span class="title class_">Component</span>, createRef } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> echarts <span class="keyword">from</span> <span class="string">'echarts'</span>;</span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> <span class="title class_">index3</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> {</span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params">props</span>) {</span><br><span class="line"> <span class="variable language_">super</span>(props);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span> = {</span><br><span class="line"> <span class="attr">myEchartList</span>: [], <span class="comment">//实例化echart列表</span></span><br><span class="line"> <span class="attr">data1</span>: [],</span><br><span class="line"> <span class="attr">data2</span>: [],</span><br><span class="line"> <span class="attr">data3</span>: [],</span><br><span class="line"> <span class="attr">data4</span>: [],</span><br><span class="line"> };</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">allRef</span> = []; <span class="comment">//存储所有的ref</span></span><br><span class="line"> }</span><br><span class="line"> <span class="title function_">componentDidMount</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> <span class="comment">//实例化echart</span></span><br><span class="line"> <span class="attr">myEchartList</span>: <span class="variable language_">this</span>.<span class="property">allRef</span>.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =></span> echarts.<span class="title function_">init</span>(item)),</span><br><span class="line"> });</span><br><span class="line"> <span class="comment">//获取数据A</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initDataA</span>();</span><br><span class="line"> <span class="comment">//获取数据B</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initDataB</span>();</span><br><span class="line"> }</span><br><span class="line"> initDataA = <span class="function">() =></span> {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res,<span class="string">'来自initDataA'</span>);</span><br><span class="line"> <span class="keyword">let</span> data1 = res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">() =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>));</span><br><span class="line"> <span class="keyword">let</span> data2 = res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">() =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>));</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> data1,</span><br><span class="line"> data2,</span><br><span class="line"> });</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">myEchartList</span>[<span class="number">0</span>].<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>:data1,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">myEchartList</span>[<span class="number">1</span>].<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>:data2,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> };</span><br><span class="line"> initDataB = <span class="function">() =></span> {</span><br><span class="line"> axios.<span class="title function_">get</span>(<span class="string">'https://api.oick.cn/api/lishi'</span>).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res,<span class="string">'来自initDataB'</span>);</span><br><span class="line"> <span class="keyword">let</span> data3 = res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">() =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>));</span><br><span class="line"> <span class="keyword">let</span> data4 = res.<span class="property">data</span>.<span class="property">result</span>.<span class="title function_">map</span>(<span class="function">() =></span> <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100</span>));</span><br><span class="line"> <span class="comment">//处理数据</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setState</span>({</span><br><span class="line"> data3,</span><br><span class="line"> data4,</span><br><span class="line"> });</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">myEchartList</span>[<span class="number">2</span>].<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>:data3,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">state</span>.<span class="property">myEchartList</span>[<span class="number">3</span>].<span class="title function_">setOption</span>({</span><br><span class="line"> <span class="attr">xAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'category'</span>,</span><br><span class="line"> <span class="attr">data</span>: [<span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>],</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">yAxis</span>: {</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'value'</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">series</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">data</span>:data4,</span><br><span class="line"> <span class="attr">type</span>: <span class="string">'line'</span>,</span><br><span class="line"> },</span><br><span class="line"> ],</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line"> };</span><br><span class="line"> handleUpdate = <span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">initDataA</span>();</span><br><span class="line"> };</span><br><span class="line"> <span class="title function_">render</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <span class="language-xml"><span class="tag"><></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">{this.handleUpdate}</span>></span>点击我更新2个图表数据<span class="tag"></<span class="name">button</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">style</span>=<span class="string">{{</span> <span class="attr">display:</span> '<span class="attr">flex</span>' }}</span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> ></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{(node)</span> =></span> {</span></span><br><span class="line"><span class="language-xml"> this.allRef.push(node);</span></span><br><span class="line"><span class="language-xml"> }}</span></span><br><span class="line"><span class="language-xml"> style={{ width: '401px', height: '400px' }}</span></span><br><span class="line"><span class="language-xml"> ><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{(node)</span> =></span> {</span></span><br><span class="line"><span class="language-xml"> this.allRef.push(node);</span></span><br><span class="line"><span class="language-xml"> }}</span></span><br><span class="line"><span class="language-xml"> style={{ width: '402px', height: '400px' }}</span></span><br><span class="line"><span class="language-xml"> ><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{(node)</span> =></span> {</span></span><br><span class="line"><span class="language-xml"> this.allRef.push(node);</span></span><br><span class="line"><span class="language-xml"> }}</span></span><br><span class="line"><span class="language-xml"> style={{ width: '403px', height: '400px' }}</span></span><br><span class="line"><span class="language-xml"> ><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">ref</span>=<span class="string">{(node)</span> =></span> {</span></span><br><span class="line"><span class="language-xml"> this.allRef.push(node);</span></span><br><span class="line"><span class="language-xml"> }}</span></span><br><span class="line"><span class="language-xml"> style={{ width: '404px', height: '400px' }}</span></span><br><span class="line"><span class="language-xml"> ><span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></></span></span></span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><ul><li><p>可以了解下setOption的其他用法<a target="_blank" rel="noopener" href="https://www.jb51.net/article/269766.htm">https://www.jb51.net/article/269766.htm</a></p></li><li><p>也可以使用在react中</p><ul><li>echarts-for-react: <a target="_blank" rel="noopener" href="https://www.npmjs.com/package/echarts-for-react">https://www.npmjs.com/package/echarts-for-react</a></li></ul></li><li><p>参考文章</p><ul><li><a target="_blank" rel="noopener" href="https://blog.csdn.net/A15029296293/article/details/130500081">https://blog.csdn.net/A15029296293/article/details/130500081</a></li></ul></li></ul></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top">梦洁</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top/45513c12.html">https://www.dreamlove.top/45513c12.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.dreamlove.top" target="_blank">梦洁小站-属于你我的小天地</a>!</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/HTML/">HTML</a><a class="post-meta__tags" href="/tags/echarts/">echarts</a></div><div class="post_share"><div class="social-share" data-image="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181528746.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/4f2816f0.html" title="前端canvas的学习和将网页生成canvas图片"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181529884.png" onerror='onerror=null,src="/img/404.png"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">前端canvas的学习和将网页生成canvas图片</div></div></a></div><div class="next-post pull-right"><a href="/73c88c12.html" title="前端取图片相同颜色作为遮罩或者背景"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181527872.png" onerror='onerror=null,src="/img/404.png"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">前端取图片相同颜色作为遮罩或者背景</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/4561a0ef.html" title="Echarts图表的基本使用"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205162354315.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-16</div><div class="title">Echarts图表的基本使用</div></div></a></div><div><a href="/75bf0d7.html" title="webpack构建工具的学习"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205291452280.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-28</div><div class="title">webpack构建工具的学习</div></div></a></div><div><a href="/e4e505ca.html" title="使用vite的社区模板来创建对应的项目(比如React17,vue+electron)"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101645867.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-12-06</div><div class="title">使用vite的社区模板来创建对应的项目(比如React17,vue+electron)</div></div></a></div><div><a href="/e031a88b.html" title="前端面试可能会问到的知识点记录"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202207181536689.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-18</div><div class="title">前端面试可能会问到的知识点记录</div></div></a></div><div><a href="/36fd9db3.html" title="前端SVG的学习"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181531348.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-03-16</div><div class="title">前端SVG的学习</div></div></a></div><div><a href="/4687ae16.html" title="canvas基本使用"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205132349743.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-13</div><div class="title">canvas基本使用</div></div></a></div></div></div><hr class="custom-hr"><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="artalk-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="avatar"></div><div class="author-info__name">梦洁</div><div class="author-info__description">小小的字,有大大的梦想~分享我的前端学习过程,经历,错误,和一些其他折腾过程</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/superBiuBiuMan"><i class="fab fa-github"></i><span>关注下我(* ̄▽ ̄*)</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:[email protected]" target="_blank" title="Email"><i class="fas fa-envelope" style="color:#4a7dbe"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">不断更新中,有问题请留言回复(会通过邮箱提醒~)</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E6%99%AF"><span class="toc-number">1.</span> <span class="toc-text">前景</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B9%E6%8D%AEID%E8%8E%B7%E5%8F%96DOM%E8%BF%9B%E8%A1%8C%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.1.</span> <span class="toc-text">根据ID获取DOM进行初始化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B9%E6%8D%AEref%E8%8E%B7%E5%8F%96DOM%E8%BF%9B%E8%A1%8C%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.2.</span> <span class="toc-text">根据ref获取DOM进行初始化</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A6%82%E6%9E%9C%E6%9C%891%E4%B8%AA%E9%A1%B5%E9%9D%A2%E6%9C%89%E5%A4%9A%E4%B8%AAechart%E5%9B%BE%E6%A0%87%E6%80%8E%E4%B9%88%E8%A7%A3%E5%86%B3%E5%91%A2"><span class="toc-number">2.</span> <span class="toc-text">如果有1个页面有多个echart图标怎么解决呢?</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F1-%E4%B8%80%E4%B8%AA%E5%9B%BE%E8%A1%A8%E5%B0%B1%E6%98%AF%E4%B8%80%E4%B8%AA%E7%BB%84%E4%BB%B6"><span class="toc-number">2.1.</span> <span class="toc-text">方式1-一个图表就是一个组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%8E%B7%E5%8F%96%E5%88%B0%E5%90%8E%E5%86%8DsetOption"><span class="toc-number">2.1.1.</span> <span class="toc-text">数据获取到后再setOption</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%88setOption%E5%90%8E%E5%86%8D%E6%9B%B4%E6%96%B0"><span class="toc-number">2.1.2.</span> <span class="toc-text">先setOption后再更新</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F2-%E9%83%BD%E5%86%99%E5%9C%A8%E4%B8%80%E4%B8%AAjsx-tsx%E6%96%87%E4%BB%B6%E4%B8%AD"><span class="toc-number">2.2.</span> <span class="toc-text">方式2-都写在一个jsx\tsx文件中</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">总结</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202410091600476.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="大家好我是夯大力,这是我的影视,大力金刚等系列"></a><div class="content"><a class="title" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列">大家好我是夯大力,这是我的影视,大力金刚等系列</a><time datetime="2024-10-21T08:12:21.000Z" title="发表于 2024-10-21 08:12:21">2024-10-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/91ff580f.html" title="Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖">Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖</a><time datetime="2024-10-18T21:22:02.000Z" title="发表于 2024-10-18 21:22:02">2024-10-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241535616.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="独角数卡,打开商品列表出现Undefined variable form的解决办法"></a><div class="content"><a class="title" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法">独角数卡,打开商品列表出现Undefined variable form的解决办法</a><time datetime="2024-08-24T15:31:38.000Z" title="发表于 2024-08-24 15:31:38">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241528979.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="前端常用npm库大全-vue,react,通用(持续更新)"></a><div class="content"><a class="title" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)">前端常用npm库大全-vue,react,通用(持续更新)</a><time datetime="2024-08-24T15:24:59.000Z" title="发表于 2024-08-24 15:24:59">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241539662.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="基于vue-pdf-embed的二开PDF预览的通用组件"></a><div class="content"><a class="title" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件">基于vue-pdf-embed的二开PDF预览的通用组件</a><time datetime="2024-08-11T08:53:00.000Z" title="发表于 2024-08-11 08:53:00">2024-08-11</time></div></div></div></div></div></div></main><footer id="footer" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181528746.png)"><div id="footer-wrap"><div class="copyright">©2021 - 2024 By 梦洁</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>(()=>{let t=null;const e=()=>{if(t=Artalk.init(Object.assign({el:"#artalk-wrap",server:"https://artalk.dreamlove.top",site:"梦洁小站",pageKey:location.pathname,darkMode:"dark"===document.documentElement.getAttribute("data-theme")},null)),"null"===GLOBAL_CONFIG.lightbox)return;t.on("list-loaded",()=>{t.ctx.get("list").getCommentNodes().forEach(t=>{const e=t.getRender().$content;btf.loadLightbox(e.querySelectorAll("img:not([atk-emoticon])"))})});btf.addGlobalFn("pjax",()=>{t.destroy()},"destroyArtalk")},a=async()=>{"object"==typeof Artalk||(await getCSS("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.css"),await getScript("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.js")),e()};btf.addGlobalFn("themeChange",e=>{const a=document.getElementById("artalk-wrap");if(!a||!a.children.length)return;const l="dark"===e;t.setDarkMode(l)},"artalk"),a()})()</script></div><script id="canvas_nest" defer color="0,0,255" opacity="0.7" zindex="-1" count="99" mobile="false" src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://lib.baomitu.com/algoliasearch/4.22.1/algoliasearch-lite.umd.min.js"></script><script src="https://lib.baomitu.com/instantsearch.js/4.65.0/instantsearch.production.min.js"></script><script src="/js/search/algolia.js?v=4.13.0"></script></div></div></body></html>