From c6030e6edce2207ee13a3b33cde60663367e9d38 Mon Sep 17 00:00:00 2001 From: Boyi C Date: Sat, 10 Feb 2018 14:39:15 +0800 Subject: [PATCH] Replace Google Charts with Chart.js (#429) * chartjs test * [WIP] Modified for Chart.js * Tweaking styles ( tooltips and lines ) * Almost done TODO: Change tooltips to HTML tag Improve color function * More work on Tooltips * Improve update logic Fix linting * resolve conflict * [WIP] Create new tooltip mode hack. Add axis padding to top and botton to prevent axis cutoff * TODO: cleanup * FIXME: tooltip in history graph not working correctly reorganize some code * fix build problem * Fix color and tooltip issue Fix label max width for timeline chart * update dep * Fix strange color after build due to `uglify` bug with reference the minified version. Make line chart behavior more similar to Google Charts. Make the chart honor to `unknown` and other state by manually calculate point value. * fix bugs * Remove label for only one data in timeline chart. Fix bug for infinite loop in some cases * Add HTML legend to chart. * Fix isSingleDevice bug due to calculation. Add isSingleDevice property support. * fix for lint * Replace innerHTML code with polymer node. * Replace tooltip with HTML code * fix tooltip style * move default tooltip mode to plugin * LINTING * fix Move localize history data to Timeline Chart. Fix timeline static color. Rework on chart resize. * Bug fix: Chart may disappear on some case. Timeline chart calculation issue. Change timeline chart hidden logic. * fix tooltip rework for resize event * lint * element * Replace `var` to `let`. Move import and ChartJs injection code to `ha-chart-scripts.html`. * lint: convert more let to const * fix font fix undef * update bower.json * move * Load chart code on demand --- .eslintrc-hound.json | 1 + .eslintrc.json | 3 +- bower.json | 5 +- gulp/tasks/build.js | 1 + gulp/tasks/rollup.js | 1 + js/core.js | 1 + panels/history/ha-panel-history.html | 4 +- polymer.json | 3 +- script/build_frontend | 4 + src/components/entity/ha-chart-base.html | 484 +++++++++++++++++ src/components/state-history-chart-line.html | 485 +++++++++--------- .../state-history-chart-timeline.html | 192 ++++--- src/components/state-history-charts.html | 72 +-- src/data/ha-state-history-data.html | 6 +- src/resources/ha-chart-scripts.html | 74 +++ 15 files changed, 922 insertions(+), 414 deletions(-) create mode 100644 src/components/entity/ha-chart-base.html create mode 100644 src/resources/ha-chart-scripts.html diff --git a/.eslintrc-hound.json b/.eslintrc-hound.json index dc053e517d..3361247052 100644 --- a/.eslintrc-hound.json +++ b/.eslintrc-hound.json @@ -16,6 +16,7 @@ "__DEMO__": false, "__BUILD__": false, "__VERSION__": false, + "__ROOT__": false, "Polymer": true, "webkitSpeechRecognition": false, "ResizeObserver": false diff --git a/.eslintrc.json b/.eslintrc.json index e0c225a1f1..9954ffbaba 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -8,6 +8,7 @@ "browser": true }, "rules": { - "import/no-unresolved": 2 + "import/no-unresolved": 2, + "linebreak-style": 0 } } diff --git a/bower.json b/bower.json index 41c13814f1..50eb05342b 100644 --- a/bower.json +++ b/bower.json @@ -55,7 +55,10 @@ "vaadin-combo-box": "vaadin/vaadin-combo-box#^3.0.2", "vaadin-date-picker": "vaadin/vaadin-date-picker#^2.0.0", "web-animations-js": "^2.2.5", - "webcomponentsjs": "^1.0.10" + "webcomponentsjs": "^1.0.10", + "chart.js": "~2.7.1", + "moment": "^2.20.0", + "chartjs-chart-timeline": "fanthos/chartjs-chart-timeline#^0.1.3" }, "devDependencies": { "web-component-tester": "^6.3.0" diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js index cbd546755c..396a72fb37 100644 --- a/gulp/tasks/build.js +++ b/gulp/tasks/build.js @@ -36,6 +36,7 @@ function build(es6) { stripImportsStrategy([ 'bower_components/font-roboto/roboto.html', 'bower_components/paper-styles/color.html', + 'src/resources/ha-chart-scripts.html', ]), stripAllButEntrypointStrategy('panels/hassio/ha-panel-hassio.html') ]); diff --git a/gulp/tasks/rollup.js b/gulp/tasks/rollup.js index 16f6803208..07d3c5ee16 100644 --- a/gulp/tasks/rollup.js +++ b/gulp/tasks/rollup.js @@ -58,6 +58,7 @@ function getRollupInputOptions(es6) { __DEMO__: JSON.stringify(DEMO), __BUILD__: JSON.stringify(es6 ? 'latest' : 'es5'), __VERSION__: JSON.stringify(VERSION), + __ROOT__: JSON.stringify(es6 ? 'frontend_latest' : 'frontend_es5'), }, }), ], diff --git a/js/core.js b/js/core.js index 31a0434dd4..9367a03597 100644 --- a/js/core.js +++ b/js/core.js @@ -5,6 +5,7 @@ window.HASS_DEMO = __DEMO__; window.HASS_DEV = __DEV__; window.HASS_BUILD = __BUILD__; window.HASS_VERSION = __VERSION__; +window.HASS_ROOT = __ROOT__; const init = window.createHassConnection = function (password) { const proto = window.location.protocol === 'https:' ? 'wss' : 'ws'; diff --git a/panels/history/ha-panel-history.html b/panels/history/ha-panel-history.html index ea6a4703e6..0931617998 100644 --- a/panels/history/ha-panel-history.html +++ b/panels/history/ha-panel-history.html @@ -82,9 +82,11 @@ + end-time="[[endTime]]" + no-single> diff --git a/polymer.json b/polymer.json index eb40c8d42e..f6339c29e7 100644 --- a/polymer.json +++ b/polymer.json @@ -16,7 +16,8 @@ "panels/logbook/ha-panel-logbook.html", "panels/map/ha-panel-map.html", "panels/shopping-list/ha-panel-shopping-list.html", - "panels/mailbox/ha-panel-mailbox.html" + "panels/mailbox/ha-panel-mailbox.html", + "src/resources/ha-chart-scripts.html" ], "sources": [ "src/**/*", diff --git a/script/build_frontend b/script/build_frontend index 047cd11d0c..afb778d8f2 100755 --- a/script/build_frontend +++ b/script/build_frontend @@ -27,6 +27,10 @@ cp build/panels/*.html $OUTPUT_DIR/panels mkdir $OUTPUT_DIR_ES5/panels cp build-es5/panels/*.html $OUTPUT_DIR_ES5/panels +# Chart code +cp build/src/resources/ha-chart-scripts.html $OUTPUT_DIR +cp build-es5/src/resources/ha-chart-scripts.html $OUTPUT_DIR_ES5 + # Translations cp -r build-translations/output $OUTPUT_DIR/translations diff --git a/src/components/entity/ha-chart-base.html b/src/components/entity/ha-chart-base.html new file mode 100644 index 0000000000..4e7b55da31 --- /dev/null +++ b/src/components/entity/ha-chart-base.html @@ -0,0 +1,484 @@ + + + + + + + diff --git a/src/components/state-history-chart-line.html b/src/components/state-history-chart-line.html index 313d95cb25..551f9b89ba 100644 --- a/src/components/state-history-chart-line.html +++ b/src/components/state-history-chart-line.html @@ -1,299 +1,274 @@ - + + + + + diff --git a/src/components/state-history-chart-timeline.html b/src/components/state-history-chart-timeline.html index 15c5f3b9a2..30e87580c8 100644 --- a/src/components/state-history-chart-timeline.html +++ b/src/components/state-history-chart-timeline.html @@ -1,13 +1,25 @@ - + + + + + diff --git a/src/components/state-history-charts.html b/src/components/state-history-charts.html index 315960ebc2..576f6c16c5 100644 --- a/src/components/state-history-charts.html +++ b/src/components/state-history-charts.html @@ -1,27 +1,16 @@ - -