From 2e1d3e1fa70aae5027c9d5bff48ea04ef30e47a0 Mon Sep 17 00:00:00 2001 From: Boyi C Date: Tue, 8 Aug 2017 23:24:34 +0800 Subject: [PATCH] Weatherpanel (#375) * temporary save * revert table to chart add templow and condition support to the chart * fix missing emoji * fix linting * commit, try to migrate table to div * Change the table to div * fix lint --- src/cards/ha-weather-card.html | 242 ++++++++++++++++++--------------- 1 file changed, 130 insertions(+), 112 deletions(-) diff --git a/src/cards/ha-weather-card.html b/src/cards/ha-weather-card.html index 4920bddf2d..a35132ef9d 100644 --- a/src/cards/ha-weather-card.html +++ b/src/cards/ha-weather-card.html @@ -1,9 +1,7 @@ - - + - @@ -120,20 +74,20 @@ 'use strict'; var _WEATHER_TO_ICON = { - cloudy: '\uD83C\uDF2B', - fog: '\uD83C\uDF2B', + cloudy: '\u2601\ufe0f', + fog: '\uD83C\uDF2B\ufe0f', hail: 'Hail', - lightning: '\uD83C\uDF29', - 'lightning-rainy': '\u26c8', - partlycloudy: '\u26c5', - pouring: '\uD83C\uDF22', - rainy: '\uD83C\uDF27', - snowy: '\uD83C\uDF28', - 'snowy-rainy': 'SR', - sunny: '\u2600', - windy: '\uD83C\uDF2C', - 'windy-variant': '[]', - exceptional: '!!', + lightning: '\uD83C\uDF29\ufe0f', + 'lightning-rainy': '\u26c8\ufe0f', + partlycloudy: '\u26c5\ufe0f', + pouring: '\uD83D\uDCA7\ufe0f', + rainy: '\uD83C\uDF27\ufe0f', + snowy: '\uD83C\uDF28\ufe0f', + 'snowy-rainy': '\uD83C\uDF28\ufe0f', + sunny: '\u2600\ufe0f', + windy: '\uD83C\uDF2C\ufe0f', + 'windy-variant': '\uD83C\uDF2C\ufe0f', + exceptional: '\u2b55\ufe0f', }; var _DEGREE_TEXT = [ @@ -158,8 +112,8 @@ computeTitle: function (stateObj) { return stateObj.attributes.friendly_name; }, - getDataArray: function () { + var dataArray = []; var data = this.stateObj.attributes.forecast; var i; @@ -167,18 +121,17 @@ return []; } - var dDate = []; - var dTempHigh = []; - var dTempLow = []; - var dCond = []; for (i = 0; i < data.length; i++) { - dDate.push(new Date(data[i].datetime).getDate()); - dTempHigh.push(data[i].temperature); - dTempLow.push(data[i].templow); - dCond.push(_WEATHER_TO_ICON[data[i].condition]); + var d = data[i]; + if (!d.condition) { + dataArray.push([new Date(d.datetime), null, d.temperature, d.templow]); + } else { + dataArray.push([new Date(d.datetime), _WEATHER_TO_ICON[data[i].condition], + d.temperature, d.templow]); + } } - return { date: dDate, cond: dCond, temphigh: dTempHigh, templow: dTempLow }; + return dataArray; }, checkRequirements: function () { @@ -186,18 +139,83 @@ return; } - if (!this.stateObj.attributes || !this.stateObj.attributes.forecast) { + if (!this.stateObj.attributes) { return; } + this.attr = this.stateObj.attributes; + this.nowCond = _WEATHER_TO_ICON[this.stateObj.state]; - this.windBearing = this.windBearingToText(this.stateObj.attributes.windBearing); + this.windBearing = this.windBearingToText(this.attr.wind_bearing); + + if (!window.google) { + return; + } + + if (!this.chartEngine) { + this.chartEngine = new window.google.visualization.LineChart( + this.$.chart_id); + } + + if (!this.attr.forecast) { + return; + } this.data = this.getDataArray(); + + this.drawChart(); + }, + drawChart: function () { + var dataGrid = new window.google.visualization.DataTable(); + var options = { + legend: { + position: 'top' + }, + interpolateNulls: true, + titlePosition: 'none', + chartArea: { + left: 25, + top: 5, + height: '100%', + width: '90%', + bottom: 25, + }, + curveType: 'function', + focusTarget: 'category', + colors: ['red', 'blue'], + annotations: { + textStyle: { + fontSize: '24', + } + } + }; + + dataGrid.addColumn('datetime', 'Time'); + dataGrid.addColumn({ type: 'string', role: 'annotation' }); + dataGrid.addColumn('number', 'Temperature'); + dataGrid.addColumn('number', 'Temperature Low'); + + var dataArray = this.getDataArray(); + dataGrid.addRows(dataArray); + + this.chartEngine.draw(dataGrid, options); + }, + + googleApiLoaded: function () { + window.google.load('visualization', '1', { + packages: ['corechart'], + callback: function () { + this.checkRequirements(); + }.bind(this), + }); }, windBearingToText: function (degree) { - return _DEGREE_TEXT[((parseInt(degree) + 5.63) / 11.25) | 0]; + var degreenum = parseInt(degree); + if (isFinite(degreenum)) { + return _DEGREE_TEXT[(((degreenum + 11.25) / 22.5) | 0) % 16]; + } + return ''; } }); }());