{"id":84,"date":"2026-06-18T10:40:42","date_gmt":"2026-06-18T02:40:42","guid":{"rendered":"https:\/\/yykcj.com\/?p=84"},"modified":"2026-06-18T10:40:42","modified_gmt":"2026-06-18T02:40:42","slug":"%e5%89%8d%e7%ab%af%e6%95%b0%e6%8d%ae%e5%8f%af%e8%a7%86%e5%8c%96%ef%bc%9ad3-js-vs-echarts-%e9%80%89%e5%9e%8b%e6%8c%87%e5%8d%97%e4%b8%8e%e5%ae%9e%e6%88%98%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=84","title":{"rendered":"\u524d\u7aef\u6570\u636e\u53ef\u89c6\u5316\uff1aD3.js vs ECharts \u9009\u578b\u6307\u5357\u4e0e\u5b9e\u6218\u5bf9\u6bd4"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u6280\u80fd\u3002\u9762\u5bf9 D3.js \u548c ECharts\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u4f1a\u9677\u5165\u9009\u62e9\u56f0\u96be\u3002\u672c\u6587\u5c06\u4ece\u5e95\u5c42\u539f\u7406\u3001API \u8bbe\u8ba1\u3001\u6027\u80fd\u3001\u7075\u6d3b\u6027\u7b49\u7ef4\u5ea6\u8fdb\u884c\u5bf9\u6bd4\uff0c\u5e76\u901a\u8fc7\u4e24\u4e2a\u5b9e\u6218\u9879\u76ee\uff08\u4e00\u4e2a\u7528 D3.js \u5b9e\u73b0\u81ea\u5b9a\u4e49\u56fe\u8868\uff0c\u4e00\u4e2a\u7528 ECharts \u5feb\u901f\u642d\u5efa\u4eea\u8868\u76d8\uff09\u8ba9\u4f60\u4eb2\u8eab\u4f53\u9a8c\u5dee\u5f02\u3002<\/p>\n<h2>\u4e00\u3001\u6838\u5fc3\u5dee\u5f02\u6982\u89c8<\/h2>\n<p>| \u7279\u6027 | D3.js | ECharts | |&#8212;&#8212;|&#8212;&#8212;-|&#8212;&#8212;&#8212;| | \u5e95\u5c42 | \u57fa\u4e8e SVG\/Canvas \u7684 DOM \u64cd\u4f5c\u5e93 | \u57fa\u4e8e Canvas \u7684\u56fe\u8868\u5e93 | | \u7075\u6d3b\u6027 | \u6781\u9ad8\uff0c\u53ef\u5b9a\u5236\u4efb\u4f55\u56fe\u5f62 | \u4e2d\u7b49\uff0c\u63d0\u4f9b\u4e30\u5bcc\u56fe\u8868\u7c7b\u578b\u4f46\u5b9a\u5236\u590d\u6742 | | \u5b66\u4e60\u66f2\u7ebf | \u9661\u5ced | \u5e73\u7f13 | | \u6570\u636e\u91cf | \u9002\u5408\u4e2d\u5c0f\u89c4\u6a21\uff08<10\u4e07\u70b9\uff09 | \u9002\u5408\u5927\u89c4\u6a21\uff08\u767e\u4e07\u7ea7\uff09 | | \u52a8\u753b | \u624b\u52a8\u5b9e\u73b0 | \u5185\u7f6e\u4f18\u96c5\u52a8\u753b | | \u793e\u533a | \u8d44\u6e90\u4e30\u5bcc\u4f46\u5206\u6563 | \u4e2d\u6587\u6587\u6863\u53cb\u597d\uff0c\u793e\u533a\u6d3b\u8dc3 |<\/p>\n<h2>\u4e8c\u3001\u9009\u578b\u539f\u5219<\/h2>\n<ul>\n<li><strong>\u9700\u8981\u9ad8\u5ea6\u5b9a\u5236<\/strong>\uff08\u5982\u7279\u6b8a\u56fe\u8868\u3001\u4ea4\u4e92\uff09\u2192 D3.js<\/li>\n<li><strong>\u5feb\u901f\u5f00\u53d1\u6807\u51c6\u56fe\u8868<\/strong>\uff08\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u5730\u56fe\u7b49\uff09\u2192 ECharts<\/li>\n<li><strong>\u6570\u636e\u91cf\u5927<\/strong>\uff08>10\u4e07\u70b9\uff09\u2192 ECharts\uff08Canvas \u6e32\u67d3\uff09<\/li>\n<li><strong>\u9700\u8981 SVG \u4ea4\u4e92<\/strong>\uff08\u5982\u7f29\u653e\u3001\u5e73\u79fb\uff09\u2192 D3.js<\/li>\n<\/ul>\n<h2>\u4e09\u3001\u5b9e\u6218\u5bf9\u6bd4\uff1a\u76f8\u540c\u6570\u636e\uff0c\u4e0d\u540c\u5b9e\u73b0<\/h2>\n<h3>\u573a\u666f\uff1a\u5c55\u793a\u67d0\u516c\u53f8\u8fd1 6 \u4e2a\u6708\u9500\u552e\u989d\u8d8b\u52bf<\/h3>\n<p>\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst data = [\n  { month: &#x27;2024-01&#x27;, sales: 1200 },\n  { month: &#x27;2024-02&#x27;, sales: 1500 },\n  { month: &#x27;2024-03&#x27;, sales: 1100 },\n  { month: &#x27;2024-04&#x27;, sales: 1800 },\n  { month: &#x27;2024-05&#x27;, sales: 2000 },\n  { month: &#x27;2024-06&#x27;, sales: 1700 }\n];\n<\/code><\/pre>\n<h3>3.1 \u4f7f\u7528 ECharts \u5b9e\u73b0\uff085\u5206\u949f\u5b8c\u6210\uff09<\/h3>\n<pre><code class=\"language-html\">\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;script src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/echarts@5.4.3\/dist\/echarts.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div id=&quot;chart&quot; style=&quot;width: 600px; height: 400px;&quot;&gt;&lt;\/div&gt;\n  &lt;script&gt;\n    const chart = echarts.init(document.getElementById(&#x27;chart&#x27;));\n    const option = {\n      xAxis: {\n        type: &#x27;category&#x27;,\n        data: data.map(d =&gt; d.month)\n      },\n      yAxis: {\n        type: &#x27;value&#x27;\n      },\n      series: [{\n        data: data.map(d =&gt; d.sales),\n        type: &#x27;line&#x27;,\n        smooth: true,\n        areaStyle: {}\n      }],\n      tooltip: { trigger: &#x27;axis&#x27; }\n    };\n    chart.setOption(option);\n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>> \ud83d\udca1 ECharts \u7684\u914d\u7f6e\u9879\u9ad8\u5ea6\u7ed3\u6784\u5316\uff0c\u53ea\u9700\u63d0\u4f9b\u6570\u636e\u548c\u6837\u5f0f\u5373\u53ef\u3002<\/p>\n<h3>3.2 \u4f7f\u7528 D3.js \u5b9e\u73b0\uff08\u9700\u66f4\u591a\u4ee3\u7801\uff09<\/h3>\n<pre><code class=\"language-html\">\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;script src=&quot;https:\/\/d3js.org\/d3.v7.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;svg id=&quot;chart&quot; width=&quot;600&quot; height=&quot;400&quot;&gt;&lt;\/svg&gt;\n  &lt;script&gt;\n    const svg = d3.select(&#x27;#chart&#x27;);\n    const margin = { top: 20, right: 20, bottom: 30, left: 40 };\n    const width = +svg.attr(&#x27;width&#x27;) - margin.left - margin.right;\n    const height = +svg.attr(&#x27;height&#x27;) - margin.top - margin.bottom;\n    const g = svg.append(&#x27;g&#x27;).attr(&#x27;transform&#x27;, `translate(${margin.left},${margin.top})`);\n\n    \/\/ \u7f29\u653e\n    const x = d3.scalePoint()\n      .domain(data.map(d =&gt; d.month))\n      .range([0, width]);\n    const y = d3.scaleLinear()\n      .domain([0, d3.max(data, d =&gt; d.sales)])\n      .range([height, 0]);\n\n    \/\/ \u7ebf\u6761\u751f\u6210\u5668\n    const line = d3.line()\n      .x(d =&gt; x(d.month))\n      .y(d =&gt; y(d.sales))\n      .curve(d3.curveCatmullRom);\n\n    \/\/ \u7ed8\u5236\u6298\u7ebf\n    g.append(&#x27;path&#x27;)\n      .datum(data)\n      .attr(&#x27;fill&#x27;, &#x27;none&#x27;)\n      .attr(&#x27;stroke&#x27;, &#x27;steelblue&#x27;)\n      .attr(&#x27;stroke-width&#x27;, 2)\n      .attr(&#x27;d&#x27;, line);\n\n    \/\/ \u6dfb\u52a0\u5750\u6807\u8f74\n    g.append(&#x27;g&#x27;)\n      .attr(&#x27;transform&#x27;, `translate(0,${height})`)\n      .call(d3.axisBottom(x));\n    g.append(&#x27;g&#x27;)\n      .call(d3.axisLeft(y));\n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>> \ud83d\udca1 D3.js \u9700\u8981\u624b\u52a8\u5904\u7406\u6bd4\u4f8b\u5c3a\u3001\u5750\u6807\u8f74\u3001\u8def\u5f84\u751f\u6210\u7b49\u5e95\u5c42\u7ec6\u8282\u3002<\/p>\n<h2>\u56db\u3001\u6df1\u5ea6\u5bf9\u6bd4\uff1a\u6027\u80fd\u4e0e\u5b9a\u5236<\/h2>\n<h3>4.1 \u6027\u80fd\u6d4b\u8bd5\uff0810\u4e07\u6570\u636e\u70b9\uff09<\/h3>\n<ul>\n<li><strong>ECharts<\/strong>\uff1a\u4f7f\u7528 Canvas \u6e32\u67d3\uff0c10\u4e07\u70b9\u6d41\u7545\u4ea4\u4e92\uff0860fps\uff09\u3002<\/li>\n<li><strong>D3.js<\/strong>\uff1a\u9ed8\u8ba4 SVG \u6e32\u67d3\uff0c10\u4e07\u70b9 DOM \u8282\u70b9\u8fc7\u591a\uff0c\u5361\u987f\u3002\u53ef\u901a\u8fc7 Canvas \u9002\u914d\u5668\u4f18\u5316\uff0c\u4f46\u590d\u6742\u5ea6\u589e\u52a0\u3002<\/li>\n<\/ul>\n<h3>4.2 \u5b9a\u5236\u80fd\u529b<\/h3>\n<ul>\n<li><strong>ECharts<\/strong>\uff1a\u901a\u8fc7 <code>graphic<\/code> \u5143\u7d20\u6216\u81ea\u5b9a\u4e49\u7cfb\u5217\u5b9e\u73b0\u5b9a\u5236\uff0c\u4f46\u53d7\u9650\u4e8e API\u3002<\/li>\n<li><strong>D3.js<\/strong>\uff1a\u5b8c\u5168\u63a7\u5236 DOM\uff0c\u53ef\u521b\u5efa\u4efb\u4f55\u56fe\u5f62\uff08\u5982\u81ea\u5b9a\u4e49 Sankey \u56fe\u3001\u529b\u5bfc\u5411\u56fe\uff09\u3002<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n<ol>\n<li><strong>\u6df7\u5408\u4f7f\u7528<\/strong>\uff1a\u5728 ECharts \u4e2d\u5d4c\u5165 D3.js \u5b9a\u5236\u7684\u56fe\u5f62\u5143\u7d20\uff08\u901a\u8fc7 <code>graphic<\/code>\uff09\u3002<\/li>\n<li><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u65e0\u8bba\u9009\u54ea\u4e2a\u5e93\uff0c\u6570\u636e\u6e05\u6d17\u548c\u805a\u5408\u90fd\u5e94\u5728\u670d\u52a1\u7aef\u5b8c\u6210\u3002<\/li>\n<li><strong>\u54cd\u5e94\u5f0f<\/strong>\uff1aD3.js \u9700\u624b\u52a8\u76d1\u542c resize\uff0cECharts \u5185\u7f6e <code>resize<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<ul>\n<li><strong>\u5feb\u901f\u4e0a\u7ebf\u6807\u51c6\u56fe\u8868<\/strong> \u2192 ECharts<\/li>\n<li><strong>\u9700\u8981\u72ec\u7279\u4ea4\u4e92\u6216\u56fe\u5f62<\/strong> \u2192 D3.js<\/li>\n<li><strong>\u4e24\u8005\u4e0d\u51b2\u7a81<\/strong>\uff1aECharts \u7528\u4e8e 80% \u573a\u666f\uff0cD3.js \u7528\u4e8e 20% \u5b9a\u5236\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\u53ef\u5c1d\u8bd5\uff1a\u7528 D3.js \u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5730\u56fe\u53ef\u89c6\u5316\uff0c\u6216\u7528 ECharts \u96c6\u6210\u5230 React\/Vue \u9879\u76ee\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u5bf9\u6bd4 D3.js \u4e0e ECharts \u4e24\u5927\u53ef\u89c6\u5316\u5e93\uff0c\u4ece\u5e95\u5c42\u539f\u7406\u3001\u9002\u7528\u573a\u666f\u5230\u5b9e\u6218\u4ee3\u7801\uff0c\u5e2e\u4f60\u505a\u51fa\u660e\u667a\u9009\u62e9\uff0c\u5e76\u9644\u5b8c\u6574\u53ef\u8fd0\u884c\u793a\u4f8b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[51,52,36,53,54],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-d3-js","tag-echarts","tag-36","tag-53","tag-54"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}