{"id":230,"date":"2022-01-23T01:09:13","date_gmt":"2022-01-22T17:09:13","guid":{"rendered":"http:\/\/yanhui.pro\/?p=230"},"modified":"2022-02-04T09:20:25","modified_gmt":"2022-02-04T01:20:25","slug":"elementor-230","status":"publish","type":"post","link":"https:\/\/yanhui.pro\/?p=230","title":{"rendered":"\u57fa\u4e8eChart.js\u5b9e\u73b0\u7684\u201c\u51cf\u80a5\u66f2\u7ebf\u201d\u5e94\u7528"},"content":{"rendered":"\n<p><style>\/*! elementor - v3.5.3 - 28-12-2021 *\/<br \/>\n.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}<\/style><\/p>\n\n\n<p>\u8fd1\u671f\u56e0\u4e3a\u75ab\u60c5\uff0c\u4e0d\u80fd\u5230\u5904\u6d3b\u52a8\uff0c\u4e5f\u5c11\u4e86\u4e00\u4e9b\u5e94\u916c\uff0c\u996e\u98df\u548c\u8fd0\u52a8\u90fd\u53d7\u81ea\u5df1\u63a7\u5236\uff0c\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u51cf\u80a5\u673a\u4f1a\u3002<\/p>\n\n\n<p>\u4e4b\u524d\u81ea\u5df1\u7684\u4f53\u91cd\u4e00\u76f4\u8bb0\u5f55\u5728\u534e\u4e3a\u8fd0\u52a8\u5065\u5eb7APP\u4e0a\uff0c\u4f46\u662f\u8fd9\u4e2a\u7cfb\u7edf\u4e0d\u80fd\u751f\u6210\u4e00\u4e2a\u6bd4\u8f83\u76f4\u89c2\u7684\u66f2\u7ebf\u6765\u5448\u73b0\u6211\u7684\u51cf\u80a5\u6570\u636e\u8d8b\u52bf\uff0c\u6240\u4ee5\u5c31\u60f3\u81ea\u5df1\u52a8\u624b\u5199\u4e00\u4e2a\u3002<\/p>\n\n\n<p>\u9700\u6c42\uff1a\u652f\u6301\u4f53\u91cd\u6570\u636e\u5f55\u5165\u548c\u5220\u9664\uff0c\u6570\u636e\u8bb0\u5f55\u5728S\u7aef\uff0c\u6570\u636e\u5728C\u7aef\u4ee5\u66f2\u7ebf\u7684\u5f62\u52bf\u5448\u73b0\u3002<\/p>\n\n\n<p>\u67b6\u6784\uff1a\u540e\u7aefNodeJs + JSON\u6570\u636e\u4fdd\u5b58\uff0c \u524d\u7aef\u4f7f\u7528Chart.js\u5e93\u6765\u8fdb\u884c\u6570\u636e\u7684\u5448\u73b0\u3002<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/yanhui.pro\/wp-content\/uploads\/2022\/01\/Screenshot_20220123_160010.jpg?resize=640%2C1410\" alt=\"\" class=\"wp-image-241\" width=\"640\" height=\"1410\" srcset=\"https:\/\/i0.wp.com\/yanhui.pro\/wp-content\/uploads\/2022\/01\/Screenshot_20220123_160010.jpg?resize=465%2C1024&amp;ssl=1 465w, https:\/\/i0.wp.com\/yanhui.pro\/wp-content\/uploads\/2022\/01\/Screenshot_20220123_160010.jpg?resize=136%2C300&amp;ssl=1 136w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption>\u624b\u673a\u7aef\u67e5\u770b\u5230\u7684\u4f53\u91cd\u66f2\u7ebf<\/figcaption><\/figure>\n\n\n<ul class=\"wp-block-list\"><li>\u524d\u53f0index.html<\/li><li>\u524d\u53f0app.js<\/li><\/ul>\n\n\n<pre class=\"wp-block-code\"><code><strong>Index.html<\/strong>\n&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;script src=\"https:\/\/cdn.staticfile.org\/jquery\/1.10.2\/jquery.min.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/chart.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"app.js\"&gt;    &lt;\/script&gt;\n    &lt;title&gt;Weight&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h2&gt;Welcome to My Weight Recoder Page!&lt;\/h2&gt;\n    &lt;canvas id=\"myChart\" width=\"50px\" height=\"50px\"&gt;&lt;\/canvas&gt;\n    &lt;!-- &lt;div class=\"chart-container\" style=\"position: relative; height:40vh; width:80vw\"&gt;\n        &lt;canvas id=\"myChart\"&gt;&lt;\/canvas&gt;\n    &lt;\/div&gt; --&gt;\n\n    &lt;div&gt;\n        \u65e5\u671f\uff1a\n        &lt;input type=\"text\" name=\"date\" id=\"date\"&gt;\n        &lt;script&gt;\n            var today = new Date();\n            document.getElementById(\"date\").value = today.getFullYear() + \".\" + (today.getMonth() + 1) + \".\" + today.getDate();\n        &lt;\/script&gt;\n    &lt;\/div&gt;\n\n    &lt;div&gt;\n        \u4f53\u91cd\uff1a\n        &lt;input type=\"text\" name=\"weight\" id=\"weight\"&gt;\n    &lt;\/div&gt;\n\n    &lt;div&gt;\n        \u64cd\u4f5c\uff1a\n        &lt;button id=\"add\"&gt;\u6dfb\u52a0\u6570\u636e&lt;\/button&gt;\n        &lt;button id=\"delete\"&gt;\u5220\u9664\u6570\u636e&lt;\/button&gt;\n    &lt;\/div&gt;\n\n    &lt;div id=\"datashow\"&gt;&lt;\/div&gt;\n\n    &lt;!-- \u521b\u5efa\u56fe\u8868  --&gt;\n    &lt;script&gt;\n        const myChart = new Chart(\n            document.getElementById(\"myChart\").getContext('2d'),\n            config\n        );\n    &lt;\/script&gt;\n    &lt;!-- jquery\u5b9e\u73b0\u7684ajax post--&gt;\n    &lt;script&gt;\n        $(document).ready(function () {\n            PostData({option:\"update\"});\n            console.log(\"document ready callback!\");\n\n            $(\"#add\").click(function () {\n                \/\/verification \n                if(DataVerification($(\"#date\").val(), $(\"#weight\").val())) {\n                    console.log(\"Error Weight Data!\")\n                    alert(\"input error!\")\n                    return;\n                }\n                var para = {option:\"add\",date:$(\"#date\").val(),weight:$(\"#weight\").val()};\n                console.log(para);\n                PostData(para);\n            });\n\n            $(\"#delete\").click(function () {\n                var para = {option:\"delete\"};\n                console.log(para);\n                PostData(para);\n            });\n        })\n    &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n<pre class=\"wp-block-code\"><code><strong>app.js<\/strong>\n\/\/ for Chart.js\nconst labels = &#91;\n    '2022.2.9'\n    ];\n\nconst data = {\n    labels: labels,\n    datasets: &#91;{\n        label: 'Weight(Kg)',\n        backgroundColor: 'rgb(255, 99, 132)',\n        borderColor: 'rgb(255, 99, 132)',\n        data: &#91;82],\n        tension: 0.2\n    }]\n};\n\nconst config = {\n    type: 'line',\n    data: data,\n    options: {\n        responsive: true,\n        scales: {\n            \/\/ y: {\n            \/\/     suggestedMin: 70,\n            \/\/     suggestedMax: 86\n            \/\/ }\n        }\n    }\n};\n\nconst OK = 0;\nconst ERR = -1;\n\n\/**\n * \n * @param {*} data from server\n * {\"date\":&#91;\"2022.1.9\",\"2022.1.5\",\"2022.1.6\"],\"weight\":&#91;85,\"65\",\"83\"]}\n * @returns \n *\/\n\nfunction AddData(chart, data) {\n    chart.data.labels.splice(0,  chart.data.labels.length);\n    chart.data.datasets&#91;0].data.splice(0, chart.data.datasets&#91;0].data.length);\n    for (let index = 0; index &lt; data.date.length; index++) {\n        chart.data.labels.push(data.date&#91;index]);\n        chart.data.datasets&#91;0].data.push(data.weight&#91;index]);\n    }\n\n    chart.update('active');\n\n    return OK;\n}\n\n\/\/ \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\n\/\/ {\n\/\/     \"option\": \"add\", \/* add , delete, update*\/\n\/\/     \"date\":\"2022.1.9\",\n\/\/     \"weight\":\"83.2\"\n\/\/ }\n\/\/\u8bf7\u6c42\u6570\u636e\nfunction PostData(para) {\n    $.post(\"\/update\", para,\n        function (data, textStatus, jqXHR) {\n            $(\"#datashow\").text(JSON.stringify(data));\n            \/\/update Chart\n            AddData(myChart, data);\n           \/\/console.log(JSON.stringify(config));\n        },\n        \"json\"\n    );    \n\n    return OK;\n}\n\nfunction DataVerification(date, weight) {\n\n    if ((\"\" == date) || (null == date) || (date.length &gt; 10) || (date.length &lt; 8) || (\"\") == weight || (null == weight)) {\n        return ERR;\n    }\n\n    var w = parseFloat(weight);\n    if (NaN == w) {\n        return ERR;\n    }\n\n    \/\/ more verification item need to be added: to do \n\n    return OK;\n}<\/code><\/pre>\n\n\n<p>\u770b\u4e0b\u6700\u7ec8\u7684\u6548\u679c\uff0c\u4e0d\u5f97\u4e0d\u8bf4\u66f2\u7ebf\u51cf\u80a5\u6cd5\u597d\u5904\u591a\u591a\uff0c\u80fd\u591f\u5f88\u660e\u663e\u5730\u77e5\u9053\u81ea\u5df1\u5f53\u524d\u7684\u4f53\u91cd\u8d8b\u52bf\uff0c\u53ca\u65f6\u5730\u63a7\u5236\u6444\u5165\u6216\u8005\u52a0\u5927\u8fd0\u52a8\u91cf\uff0c\u56e0\u4e3a\u5f53\u524d\u53ea\u6709\u6211\u4e00\u4e2a\u4eba\u7528\uff0c\u8fd8\u662f\u6309\u7167\u5355\u7528\u6237\u6765\u505a\u505aserver\u7aef\uff0c\u4f46\u5df2\u7ecf\u9884\u7559\u4e86\u63a5\u53e3\uff0c\u53ef\u4ee5\u4fee\u6539\u652f\u6301\u591a\u7528\u6237\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u671f\u56e0\u4e3a\u75ab\u60c5\uff0c\u4e0d\u80fd\u5230\u5904\u6d3b\u52a8\uff0c\u4e5f\u5c11\u4e86\u4e00\u4e9b\u5e94\u916c\uff0c\u996e\u98df\u548c<\/p>\n","protected":false},"author":1,"featured_media":280,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-9"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/yanhui.pro\/wp-content\/uploads\/2022\/01\/94cad1c8a786c917bd703ba99bc73bc83ac75734.jpeg?fit=640%2C360&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/posts\/230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanhui.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=230"}],"version-history":[{"count":8,"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/posts\/230\/revisions\/281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yanhui.pro\/index.php?rest_route=\/wp\/v2\/media\/280"}],"wp:attachment":[{"href":"https:\/\/yanhui.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanhui.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanhui.pro\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}