Cannot read property 'length' of undefined - angularjs

I have a problem with my angular JS function
when I first call this function, everything is good. But my next attempts cause an error and stops it from working.
My js code:
$scope.updateUptimeCalendar = function() {
$scope.loadHeatMap();
$scope.getMonthHeatMap(new Date().getFullYear(), new Date().getMonth());
};
$scope.loadHeatMapData = function (date) {
var time = date.getTime();
var isMonthLoaded = false;
var eps = 1000 * 60 * 60 * 6; // epsilont +-6 hours
angular.forEach($scope.heatMatData, function (record) {
if (!isMonthLoaded) {
if (Math.abs(record.date - time) < eps) {
isMonthLoaded = true;
}
}
});
if (!isMonthLoaded) {
$scope.getMonthHeatMap(date.getFullYear(), date.getMonth());
};
};
$scope.getMonthHeatMap = function(fullYear, month) {
$scope.setLoadingState(true);
var monthStart = new Date(Date.UTC(fullYear, month, 1));
var monthEnd;
if (month == 12) {
monthEnd = new Date(Date.UTC(fullYear + 1, 1, 1));
} else {
monthEnd = new Date(Date.UTC(fullYear, month + 1, 1));
};
monthStart = monthStart.getTime();
monthEnd = monthEnd.getTime();
var turnOnDuration;
$http.post('Statistic/GetClientStateStatisticByDays', {
clientId: $scope.$parent.clientForStatistic.ClientId,
stateId: 1,
beginPeriod: monthStart,
endPeriod: monthEnd
}).success(function(data) {
$scope.$parent.processResponse(data);
angular.forEach(data, function(record) {
turnOnDuration = record.Duration / (1000 * 60 * 60);
$scope.heatMatData.push({
date: record.AvarageTime,
value: Number((turnOnDuration).toFixed(0)),
});
});
$scope.heatMatData.sort(function(a, b) {
return a.date - b.date;
});
//todo: fixed this when try to get null data values DK
$scope.setLoadingState(false);
$scope.cal.update($scope.heatMatData);
});
};
$scope.heatMapDateParser = function (data) {
var stats = {};
for (var d in data) {
stats[data[d].date / 1000] = data[d].value;
}
return stats;
};
$scope.loadHeatMap = function () {
$('#statistic-modal-chart-container').empty();
$('#statistic-modal-chart-container-calendar').empty();
$scope.cal = new CalHeatMap();
$scope.cal.init({
data: $scope.heatMatData,
afterLoadData: $scope.heatMapDateParser,
considerMissingDataAsZero: true,
//todo: use to set visible dates interval. MR
//minDate: new Date(2014, 9),
//maxDate: new Date(),
itemSelector: "#statistic-modal-chart-container-calendar",
domain: "month",
domainLabelFormat: "%b %Y",
subDomain: "x_day",
subDomainTextFormat: "%d",
itemName: "Hour",
cellSize: 35,
range: 1,
displayLegend: true,
legend: [0, 4, 8, 12, 16, 20, 25],
legendColors: {
base: '#ededed',
empty: '#ededed',
min: '#FFFFFF',
max: '#3366FF'
},
legendHorizontalPosition: "center",
previousSelector: "#uptime-calendar-prev",
nextSelector: "#uptime-calendar-next",
afterLoadNextDomain: function (date) {
$scope.loadHeatMapData(date);
},
afterLoadPreviousDomain: function (date) {
$scope.loadHeatMapData(date);
},
itemNamespace: "animationDuration-a",
tooltip: true
});
};
If I use debug, the error occurs in this row $scope.cal.update($scope.heatMatData);
I get this error
my error:
TypeError: Cannot read property 'length' of undefined
at e (http:/Scripts/d3.min.js:3:10447)
at Array.pa.data (http://Scripts/d3.min.js:3:11196)
at Object.CalHeatMap.fill (http://Scripts/cal-heatmap.min.js:8:20667)
at http://Scripts/cal-heatmap.min.js:9:4615
at h (http:/Scripts/cal-heatmap.min.js:9:1605)
at Object.CalHeatMap.getDatas (http:/Scripts/cal-heatmap.min.js:9:1940)
at Object.CalHeatMap.update (http://Scripts/cal-heatmap.min.js:9:4537)
at http://Scripts/Controllers/statistic-ctrl.js:355:24
at http://Scripts/angular.min.js:72:45
at L (http:/Scripts/angular.min.js:99:469) angular.min.js:92
(anonymous function) angular.min.js:92
(anonymous function) angular.min.js:68
L angular.min.js:99
L angular.min.js:99
(anonymous function) angular.min.js:101
k.$eval angular.min.js:111
k.$digest angular.min.js:108
k.$apply angular.min.js:112
h angular.min.js:72
w angular.min.js:77
A.onreadystatechange
please anybody help me!

Put
$scope.heatMatData = [];
as first instruction

Related

Sum javascript array based on timestamp

I have a 6 JSON with:
timestamp
value
from a power meter
I need to aggregrate the values in 15min timestamps.
{
"data": [
[
[
1616968800000,
3159.1404000000007
],
[
1616968886400,
1288.7799999999997
],
[
1616968972800,
522.8924999999999
],
[
1616969059200,
446.1015
],
[
1616969145600,
2340.1559999999995
]
and so on...
When I aggregrate then I start with timestamp:
1616968800000
So I need to loop all other JSON arrays to get the values:
1616968800000 to 1616969700000
But when the timestamp is in the middle of 2 timestamps, he has to calculate the value based of the timespan.
It is to calculate a power meter.
Thank you!
so far I have no aggregation, because not sure how I can do.
I´m drawing a google chart and add the values with this:
function drawChart() {
// 487 = Bezug
// 486 = Lieferung
// 504 = PV
// 214 = Verbrauch
var json_day = 'http://192.168.0.202:8080/db/getdata?id=dlc&eid=487,486,504,214&range=yesterday';
var http_request_day = new XMLHttpRequest();
try {
// Opera 8.0+, Firefox, Chrome, Safari
http_request_day = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
http_request_day = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request_day = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request_day.onreadystatechange = function() {
if (http_request_day.readyState == 4) {
var data_line = new google.visualization.DataTable();
data_line.addColumn('date', 'Date');
data_line.addColumn('number', 'Verbrauch');
data_line.addColumn('number', 'PV');
data_line.addColumn('number', 'Bezug');
data_line.addColumn('number', 'Lieferung');
var jsonObj = JSON.parse(http_request_day.responseText);
var rows = [];
// Verbrauch
for (var i = 0; i < jsonObj.data[3].length; i++) {
// console.log(i);
var date = new Date(jsonObj.data[3][i][0]);
var consumption = parseFloat(jsonObj.data[3][i][1]);
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
dateNew = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var pv = 0;
// search PV
for (var ii = 0; ii < jsonObj.data[2].length; ii++) {
var date = new Date(jsonObj.data[2][ii][0]);
if ( jsonObj.data[2][ii][0] >= jsonObj.data[3][i][0]) {
// console.log(i + "/" + ii);
pv = parseFloat(jsonObj.data[2][ii][1]);
break;
}
}
var bezug = 0;
// search Bezug
for (var ii = 0; ii < jsonObj.data[0].length; ii++) {
var date = new Date(jsonObj.data[0][ii][0]);
if ( jsonObj.data[0][ii][0] >= jsonObj.data[3][i][0]) {
// console.log(i + "/" + ii);
bezug = parseFloat(jsonObj.data[0][ii][1]);
break;
}
}
var lieferung = 0;
// search Lieferung
for (var ii = 0; ii < jsonObj.data[1].length; ii++) {
var date = new Date(jsonObj.data[1][ii][0]);
if ( jsonObj.data[1][ii][0] >= jsonObj.data[3][i][0]) {
// console.log(i + "/" + ii);
lieferung = parseFloat(jsonObj.data[1][ii][1]) * -1;
break;
}
}
data_line.addRow([date, consumption, pv, bezug, lieferung]);
}
var optionsLine = {
title: 'Energieverbrauch',
animation: {
duration: 1000,
easing: 'out',
startup: true
},
hAxis: {
gridlines: {
color: 'none'
},
gridThickness: 4,
format: 'HH:MM',
color: '#AAA',
fontName: 'Arial',
fontSize: '1'
},
interpolateNulls: true,
backgroundColor: {
fillOpacity: 0
},
legendTextStyle: {
color: '#AAA'
},
titleTextStyle: {
color: '#AAA'
},
series: {
0: {
color: '#0699fa',
lineWidth: 1
},
1: {
color: '#edd409',
lineWidth: 1
},
2: {
color: '#f1ca3a'
},
3: {
color: '#6f9654'
},
4: {
color: '#1c91c0'
},
5: {
color: '#43459d'
},
},
vAxis: {
gridlines: {
count: -1,
color: 'none'
},
gridThickness: 4,
format: '#,###W',
color: '#AAA',
fontName: 'Arial',
fontSize: '5',
minValue: 0
},
explorer: {
actions: ['dragToZoom', 'rightClickToReset'],
axis: 'horizontal',
keepInBounds: true,
maxZoomIn: 4.0
},
};
var container = document.getElementById('current');
var chartArea = new google.visualization.AreaChart(container);
But here, I search for the next timestamp and added to the first ... this is not exact or correct.
I need to calc the power over 15min.
I want it for a 15min Intervall
Example2:
00:00, 5kW
00:15, 12kW
00:30, 12kW
Here is for the first 15min 5kW, for the second 12kW
00:00, 5kW
00:07, 10kw
00:15, 12kW
00:30, 12kW
Here is for the first 15min 7.7kW,
(5*(760) + 10(860))/(1560)
(2100 + 4800)/900 = 7.7kW
for the second 12kW
But I need to loop from timestamp to timestamp ... and calc the in the past and future, because which timestamp I found.

do not show 0 value on google line chart

var data = google.visualization.arrayToDataTable([
['Year', 'Massachusetts', 'National'],
['2010', 88, 76],
['2011', 0, 82],
['2012', 96, 86],
['2013', 100, 91],
['2014', 0, 94],
['2015', -1, 98],
['2016', 100, 99],
['2017', 124, 100],
['2018', 125, 102]
]);
This is the data I am using to create a line chart, I do not want to show 0's and negative values on the chart. I just want to skip these values.
my current chart looks like this
Expected chart is like this
we can use a data view with calculated columns,
to replace values less than or equal to zero with null
var view = new google.visualization.DataView(data);
var viewColumns = [0];
for (var i = 1; i < data.getNumberOfColumns(); i++) {
addViewColumn(i);
}
view.setColumns(viewColumns);
function addViewColumn(columnIndex) {
viewColumns.push({
calc: function (dt, row) {
var valNew = null;
var valOrig = dt.getValue(row, columnIndex);
if (valOrig > 0) {
valNew = valOrig;
}
return valNew;
},
label: data.getColumnLabel(columnIndex),
type: data.getColumnType(columnIndex)
});
}
then use the following configuration option, which skips null values
interpolateNulls: true
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Year', 'Massachusetts', 'National'],
['2010', 88, 76],
['2011', 0, 82],
['2012', 96, 86],
['2013', 100, 91],
['2014', 0, 94],
['2015', -1, 98],
['2016', 100, 99],
['2017', 124, 100],
['2018', 125, 102]
]);
var view = new google.visualization.DataView(data);
var viewColumns = [0];
for (var i = 1; i < data.getNumberOfColumns(); i++) {
addViewColumn(i);
}
view.setColumns(viewColumns);
function addViewColumn(columnIndex) {
viewColumns.push({
calc: function (dt, row) {
var valNew = null;
var valOrig = dt.getValue(row, columnIndex);
if (valOrig > 0) {
valNew = valOrig;
}
return valNew;
},
label: data.getColumnLabel(columnIndex),
type: data.getColumnType(columnIndex)
});
}
var options = {
title: 'Average Home Insurance Premium',
interpolateNulls: true
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(view, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

Dots persist on D3 graph for angularjs

I am using NVD3 for my graphs and I have this issue where the dots that show up on hover will start to persist as you over over the line. Does anyone have any idea on how to make sure these disappear when the move moves away from them?
Here is the component:
;(function() {
angular.module('canopy.common.components.largeStandardChart')
.component('largeStandardChart', {
templateUrl: 'app/common/components/chart-components/large-standard-chart/large-standard-chart.html',
controller: LargeStandardChartController,
controllerAs: 'vm',
bindings: {
kpi: "<",
updateGraph: '=',
frequency: '<'
}
});
LargeStandardChartController.$inject = ['$rootScope', 'BanyanUtilsService', 'ConfigurationService', '$timeout'];
function LargeStandardChartController($rootScope, UtilsService, CS, $timeout) {
var vm = this;
vm.kpiTrend = [];
vm.kpiTargetTrend = [];
vm.kpiProjectedTrend = [];
vm.predictedDate = null;
var allTrends = vm.kpi.trend.length && vm.kpi.trend[0].values.length ? vm.kpi.trend[0].values : [];
vm.chart = {
chartOptions: {
chart: {
type: 'lineChart',
height: 250,
area: CS.getOrgConfig().graph.general.fillArea,
margin : {
top: 15,
right: 40,
bottom: 50,
left: 70
},
x: (function(d) { return d.time }),
y: (function(d) { return d.value }),
clipVoronoi: false,
xAxis: {
showMaxMin: false,
staggerLabels: vm.frequency === 'DAY' ? false : true,
tickFormat: function(d) {
return vm.frequency === 'DAY'
? d3.time.format(CS.getOrgConfig().dateTime.d3DateFormat)(new Date(d))
: d3.time.format(CS.getOrgConfig().dateTime.d3DateTimeFormat)(new Date(d));
}
},
yAxis: {
showMaxMin: true,
tickFormat: function (d) {
return vm.kpi.kpiMeasure === 'NUMBER' && d ? d.toFixed(1) : UtilsService.getFormattedData(d, vm.kpi.kpiMeasure);
}
},
tooltip: {
hideDelay: 0
},
showXAxis: CS.getOrgConfig().graph.xAxis.showXAxis,
showYAxis: CS.getOrgConfig().graph.yAxis.showYAxis,
showLegend: false,
transitionDuration: 350,
useInteractiveGuideline: false
}
}
};
vm.$onInit = function() {
if(vm.updateGraph) { vm.updateGraph.handler = vm.updateGraphData; }
if (!vm.kpi) { vm.kpi = { trend: vm.kpiTrend, kpiMeasure: "PERCENTAGE" } }
setTrends();
d3.select(window).on('mouseout', function () {
d3.selectAll('.nvtooltip').style('opacity', '0');
});
};
function setTrends() {
_.set(vm.chart, 'chartData', []);
vm.kpiTrend = [];
vm.kpiProjectedTrend = [];
_.forEach(allTrends, function(kpi) {
if (_.has(kpi, 'predict')) {
vm.kpiProjectedTrend.push(kpi);
} else {
if (CS.getOrgConfig().graph.general.showNullValues) {
vm.kpiTrend.push(kpi);
} else {
if (kpi.value) { vm.kpiTrend.push(kpi) }
}
}
});
if (!vm.kpi.hideTarget && !vm.kpiProjectedTrend.length) {
vm.chart.chartData.push({
key: CS.getOrgConfig().labels.target.single,
classed: "dashed",
color: $rootScope.branding.canopyBrandColor,
seriesIndex: 2,
strokeWidth: 1,
values: getTargetValues()
});
}
if (vm.kpiTrend.length) {
vm.chart.chartData.push({
key: 'Value',
classed: "solid",
area: CS.getOrgConfig().graph.general.fillArea,
color: $rootScope.branding.canopyBrandColor,
seriesIndex: 0,
strokeWidth: 2,
values: vm.kpiTrend
});
}
if (vm.kpiProjectedTrend.length) {
vm.chart.chartOptions.chart.useInteractiveGuideline = false;
var lastCurrentValue = angular.copy(vm.kpiTrend).pop();
var firstPredictedValue = angular.copy(vm.kpiTrend).pop();
vm.kpiProjectedTrend.unshift(firstPredictedValue);
vm.endDate = moment.unix(allTrends[ allTrends.length - 1 ].time / 1000).format(CS.getOrgConfig().dateTime.dateFormat); // Divide by 1000 for miliseconds coming from server
vm.chart.chartData.push({
key:'Projected',
classed: "dashed",
color: $rootScope.branding.canopyBrandColor,
strokeWidth: 1,
seriesIndex: 3,
values: vm.kpiProjectedTrend
});
var top = 0, bottom = 0;
if (allTrends.length) {
var top = _.maxBy(allTrends, function(tr) { return tr.value }).value;
var bottom = _.minBy(allTrends, function(tr) { return tr.value }).value;
}
var yTop = vm.kpi.kpiMeasure === 'PERCENTAGE' ? 103 : top + ((top - bottom) * 0.07);
var yBottom = vm.kpi.kpiMeasure === 'PERCENTAGE' ? 0 : bottom - ((top - bottom) * 0.04);
vm.chart.chartData.push({
classed: "solid",
strokeWidth: 1,
seriesIndex: 4,
values: [
{time: lastCurrentValue.time, value: yTop},
{time: lastCurrentValue.time, value: yBottom},
],
color: '#ff0005'
});
}
setDomain();
}
function setDomain () {
var top = 0, bottom = 0;
if (allTrends.length) {
top = _.maxBy(allTrends, function(tr) { return tr.value }).value;
bottom = _.minBy(allTrends, function(tr) { return tr.value }).value;
}
bottom = bottom < 1 && bottom > 0 ? 0 : bottom;
if (top === bottom) { bottom = top - bottom; }
if (top + bottom === 0) { top = 1; }
var yTop = vm.kpi.kpiMeasure === 'PERCENTAGE' ? 103 : top + ((top - bottom) * 0.05);
var yBottom = vm.kpi.kpiMeasure === 'PERCENTAGE' ? 0 : bottom - ((top - bottom) * 0.05);
vm.chart.chartOptions.chart.yDomain = [yBottom, yTop];
}
vm.updateGraphData = function(trend) {
allTrends = trend.length && trend[0].values.length ? trend[0].values : [];
setTrends();
vm.api.updateWithOptions(vm.chart.chartOptions);
vm.api.updateWithData(trend);
vm.api.refresh();
};
function getTargetValues() {
var trend = angular.copy(allTrends);
_.forEach(trend, function(t) {
t.value = vm.kpi.targetValue;
});
return trend;
}
}
})();
and here is what it looks like when I hover:
For anyone having this issue I have finally found the solution. I had to add a listener on the mouseout event and remove the hover class that is added to the .nv-point which is the dot. It looks like this:
d3.select(window).on('mouseout', function () {
d3.selectAll('.nv-point').classed('hover', false);
});

Dynamically change zoom

I need to change zoom of Google Maps. Code:
function _initMap(vm) {
vm.windowOptions = {
show: false
};
var latSum = 0;
var longSum = 0;
for (var i = 0, length = vm.markers.length; i < length; i++) {
//calculate center of map (1st part)
latSum += vm.markers[i].coords.latitude;
longSum += vm.markers[i].coords.longitude;
//assign an icon
vm.markers[i].iconUrl = getIconUrl(vm.markers[i].deviceType);
}
var centeredLatitude = latSum / vm.markers.length;
var centeredLongitude = longSum / vm.markers.length;
vm.control = {};
vm.map = {
center: setCenterMap(),
options: getMapOptions(),
zoom: setMapZoom(),
events: {
click: function (mapModel, eventName, originalEventArgs) {
var e = originalEventArgs[0];
$log.log('gz', e.latLng.lat() + ' ' + e.latLng.lng());
console.log('xxx', mapModel);
}
},
show: true,
refresh: function (a, b, c, d) {
vm.map.control.refresh();
}
};
vm.clusterOptions = {
minimumClusterSize: 2,
zoomOnClick: true,
styles: [{
url: 'assets/images/markers/m1.png',
width: 53,
height: 53,
textColor: 'white',
textSize: 17,
fontFamily: 'Open Sans'
}],
averageCenter: true,
clusterClass: 'cluster-icon'
};
vm.window = {
location: undefined,
templateUrl: 'app/components/maps/maps.info.template.html',
show: false,
options: getMapWindowOptions()
};
vm.clickMarker = function (marker, event, object) {
vm.window.show = false;
vm.window.details = object.details;
vm.window.location = object.coords;
vm.window.show = true;
vm.sendChoosenDeviceToController(object);
angular.element('#right-menu').focus();
};
vm.closeClick = function () {
vm.window.show = false;
}
}
but the code:
center: setCenterMap()
zoom: setMapZoom()
when I call the methods center and zoom does not change center and zoom. How to update center and zoom dynamically ? The methods are properly exectued during initiation of map but after initialization does not want to change.
The solution was just simply:
scope.map.center = {
'latitude': scope.markers[i].coords.latitude,
'longitude': scope.markers[i].coords.longitude
};
GoogleMaps knows about that change and works nice.

how can i draw dynamic highchart. the y-axis and the number of charts are dynamic from json

im new in angular js and i need to use highchart in my angular page . the problem is that i must draw chart with dynamic data from json and the number of charts will be dynamic too , maybe it should draw 3 or 4 different chart from one json . I searched alot but couldnt solve my problem.
this code works but show the data in one chart in different series. I need to show each series in different charts, and in this case the json send 4 data but it will be changed .
1. List item
$scope.draw_chart = function(){
Highcharts.chart('container2', {
chart:{
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
var this_chart = this;
$scope.ws = ngSocket('ws://#');
$scope.ws.onOpen(function () {
});
var k = 0 ;
var time=0;
$scope.points_avarage = [];
$scope.ws.onMessage(function (message) {
listener(JSON.parse(message.data));
var z = JSON.parse(message.data);
var line_to_draw = z.result.length;
var j = 0 ;
for(i=0 ; i < line_to_draw*2 ; i+=2)
{
$scope.data_to_draw[i] = {
name : z.result[j][0]['name'] ,
y : z.result[j][0]['rx-bits-per-second']
}
$scope.data_to_draw[i+1] = {
name : z.result[j][0]['name'] ,
y : z.result[j][0]['tx-bits-per-second']
}
j++;
}
this_chart.series[0].name= $scope.data_to_draw[0].name;
this_chart.series[1].name= $scope.data_to_draw[1].name;
this_chart.series[2].name= $scope.data_to_draw[2].name;
this_chart.series[3].name= $scope.data_to_draw[3].name;
for(i=0; i < line_to_draw*2; i++) {
var x = (new Date()).getTime(); // current time
var y = parseInt($scope.data_to_draw[i].y);
this_chart.series[i].addPoint([x, y], true, true);
}
});
var d = new Date().toTimeString();
}
}
},
global: {
useUTC: false
},
title: {
text: 'Live data'
},
xAxis: {
type: 'datetime'//,
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
width: 1,
color: '#808080'
}
]
}
plotOptions: {
series: {
marker: {
enabled: false
}
}
},
series: [{
data: (function () {
var data = [],
time = (new Date()).getTime(),
i;
for (i = -5; i <= 0; i += 1) {
data.push({
x: time ,
y: 0
});
}
return data;
}())
},
{
data: (function () {
var data = [],
time = (new Date()).getTime(),
i;
for (i = -5; i <= 0; i += 1) {
data.push({
x: time ,
y: 0
});
}
return data;
}())
},
{
data: (function () {
var data = [],
time = (new Date()).getTime(),
i;
for (i = -5; i <= 0; i += 1) {
data.push({
x: time ,
y: 0
});
}
return data;
}())
},
{
data: (function () {
var data = [],
time = (new Date()).getTime(),
i;
for (i = -5; i <= 0; i += 1) {
data.push({
x: time ,
y: 0
});
}
return data;
}())
}
]
});
};
<div id="containet" ng-init="draw_chart()"></div>

Resources