{"id":761,"date":"2020-04-26T22:14:30","date_gmt":"2020-04-26T14:14:30","guid":{"rendered":"http:\/\/www.growlove.cn\/blog\/?p=761"},"modified":"2020-04-26T22:14:30","modified_gmt":"2020-04-26T14:14:30","slug":"%e4%bd%bf%e7%94%a8e-chartg2%ef%bc%8c%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%8f%af%e8%a7%86%e5%8c%96","status":"publish","type":"post","link":"https:\/\/growlove.cn\/index.php\/2020\/04\/26\/%e4%bd%bf%e7%94%a8e-chartg2%ef%bc%8c%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%8f%af%e8%a7%86%e5%8c%96\/","title":{"rendered":"\u4f7f\u7528E Chart\/G2\uff0c\u8ba9\u6570\u636e\u53ef\u89c6\u5316"},"content":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0<br \/>\n1.\u4ec0\u4e48\u662fE chart\uff1f<\/p>\n<blockquote><p><span style=\"font-size: 1rem;\">ECharts\uff0capache\u516c\u53f8\u5f00\u6e90\u9879\u76ee\u3002\u4e00\u4e2a\u4f7f\u7528 JavaScript \u5b9e\u73b0\u7684\u5f00\u6e90\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u6d41\u7545\u7684\u8fd0\u884c\u5728 PC \u548c\u79fb\u52a8\u8bbe\u5907\u4e0a\uff0c\u517c\u5bb9\u5f53\u524d\u7edd\u5927\u90e8\u5206\u6d4f\u89c8\u5668\uff08IE8\/9\/10\/11\uff0cChrome\uff0cFirefox\uff0cSafari\u7b49\uff09\uff0c\u5e95\u5c42\u4f9d\u8d56\u77e2\u91cf\u56fe\u5f62\u5e93 ZRender\uff0c\u63d0\u4f9b\u76f4\u89c2\uff0c\u4ea4\u4e92\u4e30\u5bcc\uff0c\u53ef\u9ad8\u5ea6\u4e2a\u6027\u5316\u5b9a\u5236\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\u3002ECharts \u63d0\u4f9b\u4e86\u5e38\u89c4\u7684\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u3001\u997c\u56fe\u3001K\u7ebf\u56fe\uff0c\u7528\u4e8e\u7edf\u8ba1\u7684\u76d2\u5f62\u56fe\uff0c\u7528\u4e8e\u5730\u7406\u6570\u636e\u53ef\u89c6\u5316\u7684\u5730\u56fe\u3001\u70ed\u529b\u56fe\u3001\u7ebf\u56fe\uff0c\u7528\u4e8e\u5173\u7cfb\u6570\u636e\u53ef\u89c6\u5316\u7684\u5173\u7cfb\u56fe\u3001treemap\u3001\u65ed\u65e5\u56fe\uff0c\u591a\u7ef4\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e73\u884c\u5750\u6807\uff0c\u8fd8\u6709\u7528\u4e8e BI \u7684\u6f0f\u6597\u56fe\uff0c\u4eea\u8868\u76d8\uff0c\u5e76\u4e14\u652f\u6301\u56fe\u4e0e\u56fe\u4e4b\u95f4\u7684\u6df7\u642d\u3002\u9664\u4e86\u5df2\u7ecf\u5185\u7f6e\u7684\u5305\u542b\u4e86\u4e30\u5bcc\u529f\u80fd\u7684\u56fe\u8868\uff0cECharts \u8fd8\u63d0\u4f9b\u4e86\u81ea\u5b9a\u4e49\u7cfb\u5217\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2arenderItem\u51fd\u6570\uff0c\u5c31\u53ef\u4ee5\u4ece\u6570\u636e\u6620\u5c04\u5230\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u56fe\u5f62\uff0c\u66f4\u68d2\u7684\u662f\u8fd9\u4e9b\u90fd\u8fd8\u80fd\u548c\u5df2\u6709\u7684\u4ea4\u4e92\u7ec4\u4ef6\u7ed3\u5408\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u64cd\u5fc3\u5176\u5b83\u4e8b\u60c5\u3002<\/span><br \/>\n\u5b98\u7f51\uff1a<a href=\"https:\/\/www.echartsjs.com\/zh\/index.html\">https:\/\/www.echartsjs.com\/zh\/index.html<\/a><\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>2.\u4ec0\u4e48\u662fG2\uff1f<\/p>\n<blockquote><p>G2\u662fAntV \u662f\u8682\u8681\u91d1\u670d\u5168\u65b0\u4e00\u4ee3\u6570\u636e\u53ef\u89c6\u5316\u89e3\u51b3\u65b9\u6848,\u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u5957\u7b80\u5355\u65b9\u4fbf\u3001\u4e13\u4e1a\u53ef\u9760\u3001\u65e0\u9650\u53ef\u80fd\u7684\u6570\u636e\u53ef\u89c6\u5316\u6700\u4f73\u5b9e\u8df5\u3002<br \/>\n\u5b98\u7f51\uff1a<a href=\"https:\/\/g2.antv.vision\/zh\" target=\"_blank\" rel=\"noopener\">https:\/\/g2.antv.vision\/zh<\/a><\/p><\/blockquote>\n<p>3.\u80fd\u505a\u4ec0\u4e48\uff1f<br \/>\n<strong>\u7b80\u5355\u6765\u8bf4\uff0c\u4f7f\u7528E chart\u6216\u8005G2\uff0c\u53ef\u4ee5\u5c06Excel\u8868\u683c\u4ee5\u53ca\u56fe\u6807\uff0c\u5b9e\u73b0\u5728PC\u7aef\u6216\u8005\u624b\u673a\u7aef\u5c55\u793a\u3002\u76f4\u89c2\u4e00\u70b9\u8bf4\uff0c\u5728\u7f51\u9875\uff08html\uff09\u4e0a\u5c55\u793a\u3002<\/strong><br \/>\n4.\u6548\u679c<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-762\" src=\"http:\/\/bestgrow-bestgrow.stor.applinzi.com\/uploads\/2020\/04\/\u622a\u5c4f2020-04-26-\u4e0b\u53489.37.40.png\" alt=\"\" width=\"1368\" height=\"1152\" \/><br \/>\n\u4e8c\u3001\uff08G2\uff09\u5b89\u88c5<\/p>\n<blockquote><p>npm install @antv\/g2<br \/>\n\u5feb\u901f\u4e0a\u624b<br \/>\n\u5728\u7ed8\u56fe\u524d\u6211\u4eec\u9700\u8981\u4e3a G2 \u51c6\u5907\u4e00\u4e2a DOM \u5bb9\u5668\uff1a<\/p>\n<div id=\"c1\"><\/div>\n<p>import { Chart } from &#8216;@antv\/g2&#8217;;<\/p>\n<p>const data = [<br \/>\n{ genre: &#8216;Sports&#8217;, sold: 275 },<br \/>\n{ genre: &#8216;Strategy&#8217;, sold: 115 },<br \/>\n{ genre: &#8216;Action&#8217;, sold: 120 },<br \/>\n{ genre: &#8216;Shooter&#8217;, sold: 350 },<br \/>\n{ genre: &#8216;Other&#8217;, sold: 150 },<br \/>\n];<\/p>\n<p>\/\/ Step 1: \u521b\u5efa Chart \u5bf9\u8c61<br \/>\nconst chart = new Chart({<br \/>\ncontainer: &#8216;c1&#8217;, \/\/ \u6307\u5b9a\u56fe\u8868\u5bb9\u5668 ID<br \/>\nwidth: 600, \/\/ \u6307\u5b9a\u56fe\u8868\u5bbd\u5ea6<br \/>\nheight: 300, \/\/ \u6307\u5b9a\u56fe\u8868\u9ad8\u5ea6<br \/>\n});<\/p>\n<p>\/\/ Step 2: \u8f7d\u5165\u6570\u636e\u6e90<br \/>\nchart.data(data);<\/p>\n<p>\/\/ Step 3: \u521b\u5efa\u56fe\u5f62\u8bed\u6cd5\uff0c\u7ed8\u5236\u67f1\u72b6\u56fe<br \/>\nchart.interval().position(&#8216;genre*sold&#8217;);<\/p>\n<p>\/\/ Step 4: \u6e32\u67d3\u56fe\u8868<br \/>\nchart.render();<br \/>\n\u2328\ufe0f \u672c\u5730\u5f00\u53d1<\/p>\n<p># \u5b89\u88c5\u4f9d\u8d56<br \/>\n$ npm install<\/p>\n<p># \u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b<br \/>\n$ npm run test<\/p>\n<p># \u6253\u5f00 electron \u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\uff0c\u76d1\u542c\u6587\u4ef6\u53d8\u5316\u6784\u5efa<br \/>\n$ npm run test-live<\/p>\n<p># \u8fd0\u884c CI<br \/>\n$ npm run ci<\/p>\n<p># \u8fd0\u884c\u7f51\u7ad9<br \/>\n$ npm start<\/p><\/blockquote>\n<h1>\u4e09\u3001\u5229\u7528js\uff0c\u76f4\u63a5\u7531\u6d4f\u89c8\u5668\u5f15\u5165<\/h1>\n<blockquote><p>\u65e2\u53ef\u4ee5\u901a\u8fc7\u5c06\u811a\u672c\u4e0b\u8f7d\u5230\u672c\u5730\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5f15\u5165\u5728\u7ebf\u8d44\u6e90\uff1a<br \/>\n\u4f60\u4e5f\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 unpkg \u4e0b\u8f7d\u3002<\/p><\/blockquote>\n<p><strong>\u7b80\u5355\u6765\u8bf4\uff0c\u5728html\u4e2d\u52a0\u5165\u4e0a\u8fb9\u7684\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u5f88\u65b9\u4fbf\u5427\u3002<\/strong><\/p>\n<h1>\u56db\u3001\u5f00\u53d1\u793a\u4f8b<\/h1>\n<blockquote><p>\u5728 G2 \u5f15\u5165\u9875\u9762\u540e\uff0c\u6211\u4eec\u5c31\u5df2\u7ecf\u505a\u597d\u4e86\u521b\u5efa\u7b2c\u4e00\u4e2a\u56fe\u8868\u7684\u51c6\u5907\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4ee5\u4e00\u4e2a\u57fa\u7840\u7684\u67f1\u72b6\u56fe\u4e3a\u4f8b\u5f00\u59cb\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u56fe\u8868\u521b\u5efa\u3002<\/p>\n<p>1. \u521b\u5efa div \u56fe\u8868\u5bb9\u5668<\/p>\n<p>\u5728\u7ed8\u56fe\u524d\u6211\u4eec\u9700\u8981\u4e3a G2 \u51c6\u5907\u4e00\u4e2a DOM \u5bb9\u5668\uff1a<\/p>\n<div id=\"c1\"><\/div>\n<p>2. \u7f16\u5199\u56fe\u8868\u7ed8\u5236\u4ee3\u7801<\/p>\n<p>\u521b\u5efa div \u5bb9\u5668\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u8868\u7ed8\u5236\uff1a<\/p>\n<p>\u521b\u5efa Chart \u56fe\u8868\u5bf9\u8c61\uff0c\u6307\u5b9a\u56fe\u8868\u6240\u5728\u7684\u5bb9\u5668 ID\u3001\u56fe\u8868\u7684\u5bbd\u9ad8\u3001\u8fb9\u8ddd\u7b49\u4fe1\u606f\uff1b<br \/>\n\u8f7d\u5165\u56fe\u8868\u6570\u636e\u6e90\uff1b<br \/>\n\u4f7f\u7528\u56fe\u5f62\u8bed\u6cd5\u8fdb\u884c\u56fe\u8868\u7684\u7ed8\u5236\uff1b<br \/>\n\u6e32\u67d3\u56fe\u8868\u3002<\/p>\n<p>\u67f1\u72b6\u56fe<br \/>\n<!-- \u521b\u5efa\u56fe\u8868\u5bb9\u5668 --><\/p>\n<p>\u8fd9\u6837\uff0c\u4f60\u7684\u7b2c\u4e00\u4e2a\u67f1\u72b6\u56fe\u5c31\u7ed8\u5236\u5b8c\u6210\u4e86!<\/p><\/blockquote>\n<p><strong>\u4e94\u3001\u5c0f\u767d\u5efa\u8bbe\u65b9\u6848<\/strong><br \/>\n<strong> \u4ee5g2\u4e3a\u4f8b\uff0c\u5b8c\u6210\u201c\u5c0f\u767d\u201d\u4e5f\u4f1a\u7528\u7684\u5c55\u793a\u53ef\u89c6\u5316\u6570\u636e\u3002<\/strong><br \/>\n<strong> \u7b2c\u4e00\u6b65\uff1a\u5230<a href=\"https:\/\/g2.antv.vision\/zh\/examples\/gallery\" target=\"_blank\" rel=\"noopener\">\u8682\u8681G2\u5b98\u7f51<\/a>\u627e\u5230\u81ea\u5df1\u60f3\u8981\u7528\u5230\u7684\u56fe\u6807\uff0c\u83b7\u53d6\u6e90\u4ee3\u7801\uff08\u5f00\u6e90\u9879\u76ee\uff09<\/strong><br \/>\n<strong> \u7b2c\u4e8c\u6b65\uff1a\u65b0\u5efa\u4ee5UTF-8\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\uff08\u811a\u672c\uff09\u6587\u6863\uff0c\u5f15\u5165\u6e90\u4ee3\u7801\u3002<\/strong><br \/>\n<strong> \u7b2c\u4e09\u6b65\uff1a\u5c06data\u6570\u636e\u6539\u4e3a\u81ea\u5df1\u771f\u5b9e\u6570\u636e\uff0c\u5176\u4ed6\u6807\u7b7e\u6539\u4e3a\u81ea\u5df1\u9700\u8981\u7684\u5185\u5bb9\u3002<\/strong><br \/>\n<strong> \u7b2c\u56db\u6b65\uff1a\u6570\u636e\u6bd4\u8f83\u591a\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528fetch\u51fd\u6570\u76f4\u63a5\u8c03\u7528json\u6587\u4ef6\u3002<\/strong><br \/>\n<strong> \u7b2c\u4e94\u6b65\uff1a\u5c06\u540e\u7f00\u6539\u4e3ahtml\uff0c\u8fd0\u884c-\u5b8c\u6210\u3002<\/strong><br \/>\n<strong> \u7b2c\u516d\u6b65\uff0c\u5c06\u7f51\u9875\u6216\u8005\u6539\u597d\u7684\u4ee3\u7801\u6302\u5230\u670d\u52a1\u5668\u3002<\/strong><br \/>\n<strong> \u7b2c\u4e03\u90e8\uff1b\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u5b8c\u6210\u3002<\/strong><br \/>\n<strong> \u516d\u3001\u5c06Excel\u6570\u636e\u5c55\u793a\u65b9\u6848<\/strong><br \/>\n<strong> \u6b64\u65b9\u6848\u4e3a\u7b80\u6613\u578b\u65b9\u6848\u3002\u9996\u5148\uff0c\u5c06Excel\u8868\u53e6\u5b58\u4e3acsv\u6216\u8005xml\u683c\u5f0f\u6587\u4ef6\uff1b\u5176\u6b21\uff0c\u767e\u5ea6json\u683c\u5f0f\u8f6c\u5316\uff0c\u5c06csv\u6216\u8005xml\u683c\u5f0f\u6587\u4ef6\u8f6c\u6362\u4e3ajson\u6587\u4ef6\uff1b\u518d\u6b21\uff0c\u5c06json\u6587\u4ef6\u6302\u5230\u670d\u52a1\u5668\u6216\u8005\u4e91\u5b58\u50a8\u91cc\uff08\u53ef\u8bbf\u95ee\uff09\uff0c\u4f7f\u7528fetch\u51fd\u6570\u8c03\u7528\uff1b\u6700\u540e\uff0c\u6d4f\u89c8\u5668\u5f15\u5165\uff0c\u5c55\u793a\u3002<\/strong><\/p>\n<p><strong>\u4e03\u3001\u5b98\u7f51\uff1a<\/strong><\/p>\n<p><strong>\u767e\u5ea6\uff1aapache E chart\uff0c\u8682\u8681\u91d1\u670dG2\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0 1.\u4ec0\u4e48\u662fE chart\uff1f ECharts\uff0capa&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"footnotes":""},"categories":[8,10],"tags":[17,18,19],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-write","category-ict","tag-17","tag-18","tag-19"],"_links":{"self":[{"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":0,"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"wp:attachment":[{"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/growlove.cn\/index.php\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}