{"id":3237,"date":"2024-11-10T13:55:44","date_gmt":"2024-11-10T05:55:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3237\/"},"modified":"2024-11-10T13:55:44","modified_gmt":"2024-11-10T05:55:44","slug":"chart-js-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3237\/","title":{"rendered":"chart.js \u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 chart.js\uff1f\u5b89\u88c5 chart.js\uff08cdn \u6216 npm\uff09\u521b\u5efa\u56fe\u8868\u5bf9\u8c61\uff08\u83b7\u53d6\u753b\u5e03\u5143\u7d20\u3001\u8bbe\u7f6e\u56fe\u8868\u7c7b\u578b\u3001\u6570\u636e\u548c\u9009\u9879\uff09\u81ea\u5b9a\u4e49\u56fe\u8868\uff08\u4f7f\u7528\u5e7f\u6cdb\u7684\u9009\u9879\uff0c\u5982\u6807\u9898\u3001\u7f51\u683c\u7ebf\u3001\u56fe\u4f8b\u548c\u4ea4\u4e92\u6027\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808032534372.jpg\" class=\"aligncenter\" title=\"chart.js \u5982\u4f55\u4f7f\u7528\u63d2\u56fe\" alt=\"chart.js \u5982\u4f55\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Chart.js<\/strong><\/p>\n<p>Chart.js \u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5f00\u6e90 JavaScript \u5e93\uff0c\u7528\u4e8e\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684 API\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u6761\u5f62\u56fe\u3001\u6298\u7ebf\u56fe\u3001\u997c\u56fe\u548c\u96f7\u8fbe\u56fe\u3002<\/p>\n<p><strong>\u5b89\u88c5 Chart.js<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 Chart.js\uff0c\u60a8\u9700\u8981\u5c06\u5176\u5305\u542b\u5230\u60a8\u7684 HTML \u6587\u4ef6\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4e4b\u4e00\u8fdb\u884c\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528 CDN\uff1a<\/strong><\/p>\n<pre>&lt;script src=\"https:\/\/unpkg.com\/chart.js@latest\/dist\/Chart.min.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u4ece NPM \u5b89\u88c5\uff1a<\/strong><\/p>\n<pre>npm install chart.js<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u56fe\u8868\uff0c\u60a8\u9700\u8981\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u753b\u5e03\u5143\u7d20\uff1a<\/strong><\/p>\n<pre>const canvas = document.getElementById('myChart');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u8868\u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>const chart = new Chart(canvas, {\n  type: 'bar',\n  data: {\n    labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],\n    datasets: [{\n      label: 'Sales',\n      data: [10, 20, 30, 40, 50]\n    }]\n  },\n  options: {\n    \/\/ \u56fe\u8868\u9009\u9879\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ol>\n<p><strong>\u81ea\u5b9a\u4e49\u56fe\u8868<\/strong><\/p>\n<p>Chart.js \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u9009\u9879\uff0c\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u56fe\u8868\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002\u8fd9\u4e9b\u9009\u9879\u5728 options \u5bf9\u8c61\u4e2d\u8bbe\u7f6e\u3002\u4e00\u4e9b\u5e38\u89c1\u7684\u9009\u9879\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u6807\u9898\uff1a<\/strong> title<\/li>\n<li> <strong>\u7f51\u683c\u7ebf\uff1a<\/strong> scales<\/li>\n<li> <strong>\u56fe\u4f8b\uff1a<\/strong> legend<\/li>\n<li> <strong>\u5de5\u5177\u63d0\u793a\uff1a<\/strong> tooltips<\/li>\n<li> <strong>\u4ea4\u4e92\u6027\uff1a<\/strong> interaction<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u6761\u5f62\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>const canvas = document.getElementById('myChart');\nconst chart = new Chart(canvas, {\n  type: 'bar',\n  data: {\n    labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],\n    datasets: [{\n      label: 'Sales',\n      data: [100, 200, 300, 400, 500]\n    }]\n  },\n  options: {\n    title: {\n      text: 'Monthly Sales'\n    },\n    scales: {\n      yAxes: [{\n        ticks: {\n          beginAtZero: true\n        }\n      }]\n    }\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fchart.js \u5982\u4f55\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 chart.js\uff1f\u5b89\u88c5 chart.js\uff08cdn \u6216 npm\uff09\u521b\u5efa\u56fe\u8868\u5bf9\u8c61\uff08\u83b7\u53d6\u753b\u5e03\u5143\u7d20\u3001\u8bbe\u7f6e\u56fe\u8868\u7c7b\u578b\u3001\u6570\u636e\u548c\u9009\u9879\uff09\u81ea\u5b9a\u4e49\u56fe\u8868\uff08\u4f7f\u7528\u5e7f\u6cdb\u7684\u9009\u9879\uff0c\u5982\u6807\u9898\u3001\u7f51\u683c\u7ebf\u3001\u56fe\u4f8b\u548c\u4ea4\u4e92\u6027\uff09 \u5982\u4f55\u4f7f\u7528 Chart.js Chart.js \u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5f00\u6e90 JavaScript \u5e93\uff0c\u7528\u4e8e\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684 API\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u6761\u5f62\u56fe\u3001\u6298\u7ebf\u56fe\u3001\u997c\u56fe\u548c\u96f7\u8fbe\u56fe\u3002 \u5b89\u88c5 Chart.js \u8981\u4f7f\u7528 Chart.js\uff0c\u60a8\u9700\u8981\u5c06\u5176\u5305\u542b\u5230\u60a8\u7684 HTML \u6587\u4ef6\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4e4b\u4e00\u8fdb\u884c\uff1a \u4f7f\u7528 CDN\uff1a &lt;script src=&#8221;https:\/\/unpkg.com\/chart.js@latest\/dist\/Chart.min.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u4ece NPM \u5b89\u88c5\uff1a npm install chart.js \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u56fe\u8868 \u8981\u521b\u5efa\u4e00\u4e2a\u56fe\u8868\uff0c\u60a8\u9700\u8981\uff1a \u83b7\u53d6\u753b\u5e03\u5143\u7d20\uff1a const canvas = document.getElementById(&#8216;myChart&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u56fe\u8868\u5bf9\u8c61\uff1a const chart = new Chart(canvas, { type: &#8216;bar&#8217;, data: { labels: [&#8216;Monday&#8217;, &#8216;Tuesday&#8217;, &#8216;Wednesday&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3237","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3237"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3237\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}