amserialchart graphs d​uplication calculation - javascript

I'm use amChartSerialChart.
"categoryField": "ymd",
"categoryAxis": {
"dateFormats": [{
"period": "DD",
"format": "DD"
}, {
"period": "WW",
"format": "MMM DD"
}, {
"period": "MM",
"format": "MMM"
}, {
"period": "YYYY",
"format": "YYYY"
}],
"parseDates": true,
"minPeriod": "WW",
"dataProvider": [{
"date": "2016-11-14",
"value": 1
}, {
"date": "2016-11-14",
"value": 2
}, {
"date": "2016-11-15",
"value": 3
}, {
"date": "2012-11-15",
"value": 4
}
If there are multiple data of the same date, I would like to obtain the total result.
i hope result value is 10.
What kind of solution is there?

Reduce
Map
dataProvider= [{
"date": "2016-11-14",
"value": 1
}, {
"date": "2016-11-14",
"value": 2
}, {
"date": "2016-11-15",
"value": 3
}, {
"date": "2012-11-15",
"value": 4
}]
var result=dataProvider.map(function(arr){return arr.value;
}).reduce((a, b) => a + b, 0);;
console.log(result);

The chart doesn't automatically sum up values of the same date, nor does it support duplicate dates. You need to pre-process your data before assigning it to the chart's dataProvider.
var rawData = [{
"date": "2016-11-14",
"value": 1
}, {
"date": "2016-11-14",
"value": 2
}, {
"date": "2016-11-15",
"value": 3
}, {
"date": "2012-11-15",
"value": 4
}]
//aggregate and sum all elements with the same date key
var aggregatedTotals = rawData.reduce(function(accumulator, data) {
if (accumulator.hasOwnProperty(data.date)) {
accumulator[data.date] += data.value;
} else {
accumulator[data.date] = data.value;
}
return accumulator;
}, {});
//create a brand new array containing the summed up values
var chartData = Object.keys(aggregatedTotals).map(function(date) {
return {
"date": date,
"value": aggregatedTotals[date]
};
});
var rawData = [{
"date": "2016-11-15",
"value": 4
}, {
"date": "2016-11-15",
"value": 1
}, {
"date": "2016-11-16",
"value": 3
}, {
"date": "2016-11-16",
"value": 4
}, {
"date": "2016-11-17",
"value": 3
}, {
"date": "2016-11-17",
"value": 3
}, {
"date": "2016-11-18",
"value": 5
}, {
"date": "2016-11-18",
"value": 1
}, {
"date": "2016-11-19",
"value": 4
}, {
"date": "2016-11-19",
"value": 1
}, {
"date": "2016-11-20",
"value": 4
}, {
"date": "2016-11-20",
"value": 3
}, {
"date": "2016-11-21",
"value": 3
}, {
"date": "2016-11-21",
"value": 3
}, {
"date": "2016-11-22",
"value": 5
}, {
"date": "2016-11-22",
"value": 3
}, {
"date": "2016-11-23",
"value": 4
}, {
"date": "2016-11-23",
"value": 4
}, {
"date": "2016-11-24",
"value": 4
}, {
"date": "2016-11-24",
"value": 2
}, {
"date": "2016-11-25",
"value": 5
}, {
"date": "2016-11-25",
"value": 4
}, {
"date": "2016-11-26",
"value": 5
}, {
"date": "2016-11-26",
"value": 1
}, {
"date": "2016-11-27",
"value": 5
}, {
"date": "2016-11-27",
"value": 3
}, {
"date": "2016-11-28",
"value": 5
}, {
"date": "2016-11-28",
"value": 1
}, {
"date": "2016-11-29",
"value": 3
}, {
"date": "2016-11-29",
"value": 3
}, {
"date": "2016-11-30",
"value": 4
}, {
"date": "2016-11-30",
"value": 3
}, {
"date": "2016-12-01",
"value": 4
}, {
"date": "2016-12-01",
"value": 2
}, {
"date": "2016-12-02",
"value": 4
}, {
"date": "2016-12-02",
"value": 1
}, {
"date": "2016-12-03",
"value": 4
}, {
"date": "2016-12-03",
"value": 3
}, {
"date": "2016-12-04",
"value": 3
}, {
"date": "2016-12-04",
"value": 1
}, {
"date": "2016-12-05",
"value": 5
}, {
"date": "2016-12-05",
"value": 2
}, {
"date": "2016-12-06",
"value": 3
}, {
"date": "2016-12-06",
"value": 3
}, {
"date": "2016-12-07",
"value": 5
}, {
"date": "2016-12-07",
"value": 1
}, {
"date": "2016-12-08",
"value": 4
}, {
"date": "2016-12-08",
"value": 1
}, {
"date": "2016-12-09",
"value": 3
}, {
"date": "2016-12-09",
"value": 4
}, {
"date": "2016-12-10",
"value": 5
}, {
"date": "2016-12-10",
"value": 2
}, {
"date": "2016-12-11",
"value": 5
}, {
"date": "2016-12-11",
"value": 1
}, {
"date": "2016-12-12",
"value": 3
}, {
"date": "2016-12-12",
"value": 2
}];
//aggregate and sum all elements with the same date key
var aggregatedTotals = rawData.reduce(function(accumulator, data) {
if (accumulator.hasOwnProperty(data.date)) {
accumulator[data.date] += data.value;
} else {
accumulator[data.date] = data.value;
}
return accumulator;
}, {});
//create a brand new array containing the summed up values
var chartData = Object.keys(aggregatedTotals).map(function(date) {
return {
"date": date,
"value": aggregatedTotals[date]
};
});
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"dataProvider": chartData,
"graphs": [{
"bullet": "round",
"valueField": "value"
}],
"dataDateFormat": "YYYY-MM-DD",
"categoryField": "date",
"categoryAxis": {
"dateFormats": [{
"period": "DD",
"format": "DD"
}, {
"period": "WW",
"format": "MMM DD"
}, {
"period": "MM",
"format": "MMM"
}, {
"period": "YYYY",
"format": "YYYY"
}],
"parseDates": true,
"minPeriod": "7DD",
}
});
#chartdiv {
width: 100%;
height: 300px;
}
<script type="text/javascript" src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="//www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv"></div>
Note that WW is an invalid minPeriod. If you want to group your data in weeks, use 7DD instead. API documentation for minPeriod

Related

Scrollable X-axis in Fusioncharts

How can I create Scrollable X-axis using Fusioncharts? I'm trying to create a scrollbar like this for stacked bar and line graph combination.
Can I add scrollbar by using any inbuilt functions provided by Fusioncharts library?
Can someone please help me with a solution?
/StackOverflow didn't accepted my question, so I added this comment to the increase question length/
const dataSource = {
"chart": {
"subcaption": "2016 - 2021",
"syaxisname": "YoY growth in %",
"formatnumberscale": "0",
"numberprefix": "$",
"numbersuffix": "M",
"snumbersuffix": "%",
"showvalues": "0",
"plottooltext": "Market size for $seriesName in $label is <b>$dataValue</b>",
"theme": "fusion"
},
"categories": [
{
"category": [
{
"label": "2016"
},
{
"label": "2017"
},
{
"label": "2018"
},
{
"label": "2019"
},
{
"label": "2020"
}
]
}
],
"dataset": [
{
"seriesname": "RPA Software",
"data": [
{
"value": "73"
},
{
"value": "113"
},
{
"value": "153"
},
{
"value": "192"
},
{
"value": "232"
}
]
},
{
"seriesname": "RPA Services",
"data": [
{
"value": "198"
},
{
"value": "330"
},
{
"value": "476"
},
{
"value": "630"
},
{
"value": "790"
}
]
},
{
"seriesname": "YoY Growth",
"parentyaxis": "S",
"plottooltext": "$dataValue growth expected in $label",
"renderas": "line",
"data": [
{
"value": "73"
},
{
"value": "63"
},
{
"value": "42"
},
{
"value": "31"
},
{
"value": "24"
},
{
"value": "20"
}
]
}
]
};
FusionCharts.ready(function() {
var myChart = new FusionCharts({
type: "stackedcolumn3dlinedy",
renderAt: "chart-container",
width: "100%",
height: "100%",
dataFormat: "json",
dataSource
}).render();
});
The chart which you are looking for is scrollcombidy2d
Here is a demo - http://jsfiddle.net/nqt410Lk/
const dataSource = {
"chart": {
"caption": "Analysing Subsidies by Youth Population",
"subcaption": "By province",
"yaxisname": "Population Count",
"syaxisname": "Subsidies % of Income",
"labeldisplay": "rotate",
"snumbersuffix": "%",
"scrollheight": "10",
"numvisibleplot": "10",
"drawcrossline": "1",
"theme": "fusion"
},
"categories": [{
"category": [{
"label": "Matzikama"
},
{
"label": "Cederberg"
},
{
"label": "Bergrivier"
},
{
"label": "Saldanha Bay"
},
{
"label": "Swartland"
},
{
"label": "Witzenberg"
},
{
"label": "Drakenstein"
},
{
"label": "Stellenbosch"
},
{
"label": "Breede Valley"
},
{
"label": "Langeberg"
},
{
"label": "Swellendam"
},
{
"label": "Theewaterskloof"
},
{
"label": "Overstrand"
},
{
"label": "Cape Agulhas"
},
{
"label": "Kannaland"
},
{
"label": "Hessequa"
},
{
"label": "Mossel Bay"
},
{
"label": "George"
},
{
"label": "Oudtshoorn"
},
{
"label": "Bitou"
},
{
"label": "Knysna"
},
{
"label": "Laingsburg"
},
{
"label": "Prince Albert"
},
{
"label": "Beaufort West"
}
]
}],
"dataset": [{
"seriesname": "Total Population",
"plottooltext": "Population: $dataValue",
"data": [{
"value": "71045"
},
{
"value": "52949"
},
{
"value": "67474"
},
{
"value": "111173"
},
{
"value": "133762"
},
{
"value": "130548"
},
{
"value": "280195"
},
{
"value": "173419"
},
{
"value": "176578"
},
{
"value": "105483"
},
{
"value": "40211"
},
{
"value": "117109"
},
{
"value": "93466"
},
{
"value": "36000"
},
{
"value": "24168"
},
{
"value": "54237"
},
{
"value": "94135"
},
{
"value": "208237"
},
{
"value": "97509"
},
{
"value": "59157"
},
{
"value": "73835"
},
{
"value": "8895"
},
{
"value": "14272"
},
{
"value": "51080"
}
]
},
{
"seriesname": "Youth",
"renderas": "area",
"showanchors": "0",
"plottooltext": "Youth: $dataValue",
"data": [{
"value": "24598"
},
{
"value": "18302"
},
{
"value": "22162"
},
{
"value": "40696"
},
{
"value": "47420"
},
{
"value": "49981"
},
{
"value": "97230"
},
{
"value": "73162"
},
{
"value": "60668"
},
{
"value": "34594"
},
{
"value": "12567"
},
{
"value": "39907"
},
{
"value": "30681"
},
{
"value": "11323"
},
{
"value": "7801"
},
{
"value": "15785"
},
{
"value": "31478"
},
{
"value": "72762"
},
{
"value": "32301"
},
{
"value": "21401"
},
{
"value": "27863"
},
{
"value": "3254"
},
{
"value": "5562"
},
{
"value": "19047"
}
]
},
{
"seriesname": "Subsidies received %",
"parentyaxis": "S",
"renderas": "line",
"plottooltext": "$dataValue subsidies received",
"showvalues": "0",
"data": [{
"value": "28.0"
},
{
"value": "35.2"
},
{
"value": "23.9"
},
{
"value": "11.8"
},
{
"value": "18.0"
},
{
"value": "26.9"
},
{
"value": "11.1"
},
{
"value": "11.2"
},
{
"value": "24.0"
},
{
"value": "18.9"
},
{
"value": "35.6"
},
{
"value": "37.9"
},
{
"value": "12.9"
},
{
"value": "27.6"
},
{
"value": "40.5"
},
{
"value": "19.9"
},
{
"value": "15.6"
},
{
"value": "28.2"
},
{
"value": "23.3"
},
{
"value": "26.2"
},
{
"value": "16.9"
},
{
"value": "41.9"
},
{
"value": "62.1"
},
{
"value": "31.2"
}
]
}
]
};
FusionCharts.ready(function() {
var myChart = new FusionCharts({
type: "scrollcombidy2d",
renderAt: "chart-container",
width: "100%",
height: "400",
dataFormat: "json",
dataSource
}).render();
});

Amcharts refresh or validate data every seconds

I'm using amcharts4 for my data visualization,and I need to validate my data every seconds, my problem here is that it keeps refreshing the whole chart not the data. I used amcharts3 and I tried this method but there is no problem. Is there any way to refresh and validate the data and not the whole chart?
setInterval(function() {
refresh();
}, 1000);
function refresh(){
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [ {
"date": "2012-07-27",
"value": 13
}, {
"date": "2012-07-28",
"value": 11
}, {
"date": "2012-07-29",
"value": 15
}, {
"date": "2012-07-30",
"value": 16
}, {
"date": "2012-07-31",
"value": 18
}, {
"date": "2012-08-01",
"value": 13
}, {
"date": "2012-08-02",
"value": 22
}, {
"date": "2012-08-03",
"value": 23
}, {
"date": "2012-08-04",
"value": 20
}, {
"date": "2012-08-05",
"value": 17
}, {
"date": "2012-08-06",
"value": 16
}, {
"date": "2012-08-07",
"value": 18
}, {
"date": "2012-08-08",
"value": 21
}, {
"date": "2012-08-09",
"value": 26
}, {
"date": "2012-08-10",
"value": 24
}, {
"date": "2012-08-11",
"value": 29
}, {
"date": "2012-08-12",
"value": 32
}, {
"date": "2012-08-13",
"value": 18
}, {
"date": "2012-08-14",
"value": 24
}, {
"date": "2012-08-15",
"value": 22
}, {
"date": "2012-08-16",
"value": 18
}, {
"date": "2012-08-17",
"value": 19
}, {
"date": "2012-08-18",
"value": 14
}, {
"date": "2012-08-19",
"value": 15
}, {
"date": "2012-08-20",
"value": 12
}, {
"date": "2012-08-21",
"value": 8
}, {
"date": "2012-08-22",
"value": 9
}, {
"date": "2012-08-23",
"value": 8
}, {
"date": "2012-08-24",
"value": 7
}, {
"date": "2012-08-25",
"value": 5
}, {
"date": "2012-08-26",
"value": 11
}, {
"date": "2012-08-27",
"value": 13
}, {
"date": "2012-08-28",
"value": 18
}, {
"date": "2012-08-29",
"value": 20
}, {
"date": "2012-08-30",
"value": 29
}, {
"date": "2012-08-31",
"value": 33
}, {
"date": "2012-09-01",
"value": 42
}, {
"date": "2012-09-02",
"value": 35
}, {
"date": "2012-09-03",
"value": 31
}, {
"date": "2012-09-04",
"value": 47
}, {
"date": "2012-09-05",
"value": 52
}, {
"date": "2012-09-06",
"value": 46
}, {
"date": "2012-09-07",
"value": 41
}, {
"date": "2012-09-08",
"value": 43
}, {
"date": "2012-09-09",
"value": 40
}, {
"date": "2012-09-10",
"value": 39
}, {
"date": "2012-09-11",
"value": 34
}, {
"date": "2012-09-12",
"value": 29
}, {
"date": "2012-09-13",
"value": 34
}, {
"date": "2012-09-14",
"value": 37
}, {
"date": "2012-09-15",
"value": 42
}, {
"date": "2012-09-16",
"value": 49
}, {
"date": "2012-09-17",
"value": 46
}, {
"date": "2012-09-18",
"value": 47
}, {
"date": "2012-09-19",
"value": 55
}, {
"date": "2012-09-20",
"value": 59
}, {
"date": "2012-09-21",
"value": 58
}, {
"date": "2012-09-22",
"value": 57
}, {
"date": "2012-09-23",
"value": 61
}, {
"date": "2012-09-24",
"value": 59
}, {
"date": "2012-09-25",
"value": 67
}, {
"date": "2012-09-26",
"value": 65
}, {
"date": "2012-09-27",
"value": 61
}, {
"date": "2012-09-28",
"value": 66
}, {
"date": "2012-09-29",
"value": 69
}, {
"date": "2012-09-30",
"value": 71
}, {
"date": "2012-10-01",
"value": 67
}, {
"date": "2012-10-02",
"value": 63
}, {
"date": "2012-10-03",
"value": 46
}, {
"date": "2012-10-04",
"value": 32
}, {
"date": "2012-10-05",
"value": 21
}, {
"date": "2012-10-06",
"value": 18
}, {
"date": "2012-10-07",
"value": 21
}, {
"date": "2012-10-08",
"value": 28
}, {
"date": "2012-10-09",
"value": 27
}, {
"date": "2012-10-10",
"value": 36
}, {
"date": "2012-10-11",
"value": 33
}, {
"date": "2012-10-12",
"value": 31
}, {
"date": "2012-10-13",
"value": 30
}, {
"date": "2012-10-14",
"value": 34
}, {
"date": "2012-10-15",
"value": 38
}, {
"date": "2012-10-16",
"value": 37
}, {
"date": "2012-10-17",
"value": 44
}, {
"date": "2012-10-18",
"value": 49
}, {
"date": "2012-10-19",
"value": 53
}, {
"date": "2012-10-20",
"value": 57
}, {
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}, {
"date": "2012-10-25",
"value": 72
}, {
"date": "2012-10-26",
"value": 77
}, {
"date": "2012-10-27",
"value": 75
}, {
"date": "2012-10-28",
"value": 70
}, {
"date": "2012-10-29",
"value": 72
}, {
"date": "2012-10-30",
"value": 70
}, {
"date": "2012-10-31",
"value": 72
}, {
"date": "2012-11-01",
"value": 73
}, {
"date": "2012-11-02",
"value": 67
}, {
"date": "2012-11-03",
"value": 68
}, {
"date": "2012-11-04",
"value": 65
}, {
"date": "2012-11-05",
"value": 71
}, {
"date": "2012-11-06",
"value": 75
}, {
"date": "2012-11-07",
"value": 74
}, {
"date": "2012-11-08",
"value": 71
}, {
"date": "2012-11-09",
"value": 76
}, {
"date": "2012-11-10",
"value": 77
}, {
"date": "2012-11-11",
"value": 81
}, {
"date": "2012-11-12",
"value": 83
}, {
"date": "2012-11-13",
"value": 80
}, {
"date": "2012-11-14",
"value": 81
}, {
"date": "2012-11-15",
"value": 87
}, {
"date": "2012-11-16",
"value": 82
}, {
"date": "2012-11-17",
"value": 86
}, {
"date": "2012-11-18",
"value": 80
}, {
"date": "2012-11-19",
"value": 87
}, {
"date": "2012-11-20",
"value": 83
}, {
"date": "2012-11-21",
"value": 85
}, {
"date": "2012-11-22",
"value": 84
}, {
"date": "2012-11-23",
"value": 82
}, {
"date": "2012-11-24",
"value": 73
}, {
"date": "2012-11-25",
"value": 71
}, {
"date": "2012-11-26",
"value": 75
}, {
"date": "2012-11-27",
"value": 79
}, {
"date": "2012-11-28",
"value": 70
}, {
"date": "2012-11-29",
"value": 73
}, {
"date": "2012-11-30",
"value": 61
}, {
"date": "2012-12-01",
"value": 62
}, {
"date": "2012-12-02",
"value": 66
}, {
"date": "2012-12-03",
"value": 65
}, {
"date": "2012-12-04",
"value": 73
}, {
"date": "2012-12-05",
"value": 79
}, {
"date": "2012-12-06",
"value": 78
}, {
"date": "2012-12-07",
"value": 78
}, {
"date": "2012-12-08",
"value": 78
}, {
"date": "2012-12-09",
"value": 74
}, {
"date": "2012-12-10",
"value": 73
}, {
"date": "2012-12-11",
"value": 75
}, {
"date": "2012-12-12",
"value": 70
}, {
"date": "2012-12-13",
"value": 77
}, {
"date": "2012-12-14",
"value": 67
}, {
"date": "2012-12-15",
"value": 62
}, {
"date": "2012-12-16",
"value": 64
}, {
"date": "2012-12-17",
"value": 61
}, {
"date": "2012-12-18",
"value": 59
}, {
"date": "2012-12-19",
"value": 53
}, {
"date": "2012-12-20",
"value": 54
}, {
"date": "2012-12-21",
"value": 56
}, {
"date": "2012-12-22",
"value": 59
}, {
"date": "2012-12-23",
"value": 58
}, {
"date": "2012-12-24",
"value": 55
}, {
"date": "2012-12-25",
"value": 52
}, {
"date": "2012-12-26",
"value": 54
}, {
"date": "2012-12-27",
"value": 50
}, {
"date": "2012-12-28",
"value": 50
}, {
"date": "2012-12-29",
"value": 51
}, {
"date": "2012-12-30",
"value": 52
}, {
"date": "2012-12-31",
"value": 58
}, {
"date": "2013-01-01",
"value": 60
}, {
"date": "2013-01-02",
"value": 67
}, {
"date": "2013-01-03",
"value": 64
}, {
"date": "2013-01-04",
"value": 66
}, {
"date": "2013-01-05",
"value": 60
}, {
"date": "2013-01-06",
"value": 63
}, {
"date": "2013-01-07",
"value": 61
}, {
"date": "2013-01-08",
"value": 60
}, {
"date": "2013-01-09",
"value": 65
}, {
"date": "2013-01-10",
"value": 75
}, {
"date": "2013-01-11",
"value": 77
}, {
"date": "2013-01-12",
"value": 78
}, {
"date": "2013-01-13",
"value": 70
}, {
"date": "2013-01-14",
"value": 70
}, {
"date": "2013-01-15",
"value": 73
}, {
"date": "2013-01-16",
"value": 71
}, {
"date": "2013-01-17",
"value": 74
}, {
"date": "2013-01-18",
"value": 78
}, {
"date": "2013-01-19",
"value": 85
}, {
"date": "2013-01-20",
"value": 82
}, {
"date": "2013-01-21",
"value": 83
}, {
"date": "2013-01-22",
"value": 88
}, {
"date": "2013-01-23",
"value": 85
}, {
"date": "2013-01-24",
"value": 85
}, {
"date": "2013-01-25",
"value": 80
}, {
"date": "2013-01-26",
"value": 87
}, {
"date": "2013-01-27",
"value": 84
}, {
"date": "2013-01-28",
"value": 83
}, {
"date": "2013-01-29",
"value": 84
}, {
"date": "2013-01-30",
"value": 81
} ];
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 50;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.strokeWidth = 3;
series.fillOpacity = 0.5;
// Add vertical scrollbar
chart.scrollbarY = new am4core.Scrollbar();
chart.scrollbarY.marginLeft = 0;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomY";
chart.cursor.lineX.disabled = true;
}
#chartdiv {
width: 100%;
height: 500px;
}
<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
Here is my correct code for rendering my chart but not real time
//get data from database every 2secs
$interval(function () {
Blooddonation.donationAdmin().then(function(data) {
app.date = data.data.date; //store the items here
});
}, 2000);
//initialize chart
function initChart() {
var data = [];
for(let key in app.date) {
data.push({
"date": app.date[key]._id,
"value": app.date[key].count
});
}
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
}
Here is my items from data.data.date:
[ { _id: '2018-12-01', count: 1 },
{ _id: '2018-12-09', count: 2 },
{ _id: '2018-12-15', count: 3 },
{ _id: '2018-12-30', count: 5 },
{ _id: '2019-01-10', count: 2 },
{ _id: '2019-01-21', count: 3 } ]
I would recommend you to check the dynamic updates section in the documentaion.
Your chart creation is encapsulated in the refresh function. That means if will try to draw the chart every time refresh is called. Instead you need to instantiate the chart once and only update the data using the addData method:
chart.addData(dataItem, 1);
Based on your updated code, you could try something like shown below assuming app exists in the context:
chart.events.on('ready', function (e) {
$interval(function () {
Blooddonation.donationAdmin().then(function(data) {
app.date = data.data.date; //store the items here
e.target.addData(app.date.map(function (item) {
return {
date: item._id,
value: item.count
};
}), app.date.length - 1);
});
}, 2000);
});
Check the updated example below:
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [ {
"date": "2012-07-27",
"value": 13
}, {
"date": "2012-07-28",
"value": 11
}, {
"date": "2012-07-29",
"value": 15
}, {
"date": "2012-07-30",
"value": 16
}, {
"date": "2012-07-31",
"value": 18
}, {
"date": "2012-08-01",
"value": 13
}, {
"date": "2012-08-02",
"value": 22
}, {
"date": "2012-08-03",
"value": 23
}, {
"date": "2012-08-04",
"value": 20
}, {
"date": "2012-08-05",
"value": 17
}, {
"date": "2012-08-06",
"value": 16
}, {
"date": "2012-08-07",
"value": 18
}, {
"date": "2012-08-08",
"value": 21
}, {
"date": "2012-08-09",
"value": 26
}, {
"date": "2012-08-10",
"value": 24
}, {
"date": "2012-08-11",
"value": 29
}, {
"date": "2012-08-12",
"value": 32
}, {
"date": "2012-08-13",
"value": 18
}, {
"date": "2012-08-14",
"value": 24
}, {
"date": "2012-08-15",
"value": 22
}, {
"date": "2012-08-16",
"value": 18
}, {
"date": "2012-08-17",
"value": 19
}, {
"date": "2012-08-18",
"value": 14
}, {
"date": "2012-08-19",
"value": 15
}, {
"date": "2012-08-20",
"value": 12
}, {
"date": "2012-08-21",
"value": 8
}, {
"date": "2012-08-22",
"value": 9
}, {
"date": "2012-08-23",
"value": 8
}, {
"date": "2012-08-24",
"value": 7
}, {
"date": "2012-08-25",
"value": 5
}, {
"date": "2012-08-26",
"value": 11
}, {
"date": "2012-08-27",
"value": 13
}, {
"date": "2012-08-28",
"value": 18
}, {
"date": "2012-08-29",
"value": 20
}, {
"date": "2012-08-30",
"value": 29
}, {
"date": "2012-08-31",
"value": 33
}, {
"date": "2012-09-01",
"value": 42
}, {
"date": "2012-09-02",
"value": 35
}, {
"date": "2012-09-03",
"value": 31
}, {
"date": "2012-09-04",
"value": 47
}, {
"date": "2012-09-05",
"value": 52
}, {
"date": "2012-09-06",
"value": 46
}, {
"date": "2012-09-07",
"value": 41
}, {
"date": "2012-09-08",
"value": 43
}, {
"date": "2012-09-09",
"value": 40
}, {
"date": "2012-09-10",
"value": 39
}, {
"date": "2012-09-11",
"value": 34
}, {
"date": "2012-09-12",
"value": 29
}, {
"date": "2012-09-13",
"value": 34
}, {
"date": "2012-09-14",
"value": 37
}, {
"date": "2012-09-15",
"value": 42
}, {
"date": "2012-09-16",
"value": 49
}, {
"date": "2012-09-17",
"value": 46
}, {
"date": "2012-09-18",
"value": 47
}, {
"date": "2012-09-19",
"value": 55
}, {
"date": "2012-09-20",
"value": 59
}, {
"date": "2012-09-21",
"value": 58
}, {
"date": "2012-09-22",
"value": 57
}, {
"date": "2012-09-23",
"value": 61
}, {
"date": "2012-09-24",
"value": 59
}, {
"date": "2012-09-25",
"value": 67
}, {
"date": "2012-09-26",
"value": 65
}, {
"date": "2012-09-27",
"value": 61
}, {
"date": "2012-09-28",
"value": 66
}, {
"date": "2012-09-29",
"value": 69
}, {
"date": "2012-09-30",
"value": 71
}, {
"date": "2012-10-01",
"value": 67
}, {
"date": "2012-10-02",
"value": 63
}, {
"date": "2012-10-03",
"value": 46
}, {
"date": "2012-10-04",
"value": 32
}, {
"date": "2012-10-05",
"value": 21
}, {
"date": "2012-10-06",
"value": 18
}, {
"date": "2012-10-07",
"value": 21
}, {
"date": "2012-10-08",
"value": 28
}, {
"date": "2012-10-09",
"value": 27
}, {
"date": "2012-10-10",
"value": 36
}, {
"date": "2012-10-11",
"value": 33
}, {
"date": "2012-10-12",
"value": 31
}, {
"date": "2012-10-13",
"value": 30
}, {
"date": "2012-10-14",
"value": 34
}, {
"date": "2012-10-15",
"value": 38
}, {
"date": "2012-10-16",
"value": 37
}, {
"date": "2012-10-17",
"value": 44
}, {
"date": "2012-10-18",
"value": 49
}, {
"date": "2012-10-19",
"value": 53
}, {
"date": "2012-10-20",
"value": 57
}, {
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}, {
"date": "2012-10-25",
"value": 72
}, {
"date": "2012-10-26",
"value": 77
}, {
"date": "2012-10-27",
"value": 75
}, {
"date": "2012-10-28",
"value": 70
}, {
"date": "2012-10-29",
"value": 72
}, {
"date": "2012-10-30",
"value": 70
}, {
"date": "2012-10-31",
"value": 72
}, {
"date": "2012-11-01",
"value": 73
}, {
"date": "2012-11-02",
"value": 67
}, {
"date": "2012-11-03",
"value": 68
}, {
"date": "2012-11-04",
"value": 65
}, {
"date": "2012-11-05",
"value": 71
}, {
"date": "2012-11-06",
"value": 75
}, {
"date": "2012-11-07",
"value": 74
}, {
"date": "2012-11-08",
"value": 71
}, {
"date": "2012-11-09",
"value": 76
}, {
"date": "2012-11-10",
"value": 77
}, {
"date": "2012-11-11",
"value": 81
}, {
"date": "2012-11-12",
"value": 83
}, {
"date": "2012-11-13",
"value": 80
}, {
"date": "2012-11-14",
"value": 81
}, {
"date": "2012-11-15",
"value": 87
}, {
"date": "2012-11-16",
"value": 82
}, {
"date": "2012-11-17",
"value": 86
}, {
"date": "2012-11-18",
"value": 80
}, {
"date": "2012-11-19",
"value": 87
}, {
"date": "2012-11-20",
"value": 83
}, {
"date": "2012-11-21",
"value": 85
}, {
"date": "2012-11-22",
"value": 84
}, {
"date": "2012-11-23",
"value": 82
}, {
"date": "2012-11-24",
"value": 73
}, {
"date": "2012-11-25",
"value": 71
}, {
"date": "2012-11-26",
"value": 75
}, {
"date": "2012-11-27",
"value": 79
}, {
"date": "2012-11-28",
"value": 70
}, {
"date": "2012-11-29",
"value": 73
}, {
"date": "2012-11-30",
"value": 61
}, {
"date": "2012-12-01",
"value": 62
}, {
"date": "2012-12-02",
"value": 66
}, {
"date": "2012-12-03",
"value": 65
}, {
"date": "2012-12-04",
"value": 73
}, {
"date": "2012-12-05",
"value": 79
}, {
"date": "2012-12-06",
"value": 78
}, {
"date": "2012-12-07",
"value": 78
}, {
"date": "2012-12-08",
"value": 78
}, {
"date": "2012-12-09",
"value": 74
}, {
"date": "2012-12-10",
"value": 73
}, {
"date": "2012-12-11",
"value": 75
}, {
"date": "2012-12-12",
"value": 70
}, {
"date": "2012-12-13",
"value": 77
}, {
"date": "2012-12-14",
"value": 67
}, {
"date": "2012-12-15",
"value": 62
}, {
"date": "2012-12-16",
"value": 64
}, {
"date": "2012-12-17",
"value": 61
}, {
"date": "2012-12-18",
"value": 59
}, {
"date": "2012-12-19",
"value": 53
}, {
"date": "2012-12-20",
"value": 54
}, {
"date": "2012-12-21",
"value": 56
}, {
"date": "2012-12-22",
"value": 59
}, {
"date": "2012-12-23",
"value": 58
}, {
"date": "2012-12-24",
"value": 55
}, {
"date": "2012-12-25",
"value": 52
}, {
"date": "2012-12-26",
"value": 54
}, {
"date": "2012-12-27",
"value": 50
}, {
"date": "2012-12-28",
"value": 50
}, {
"date": "2012-12-29",
"value": 51
}, {
"date": "2012-12-30",
"value": 52
}, {
"date": "2012-12-31",
"value": 58
}, {
"date": "2013-01-01",
"value": 60
}, {
"date": "2013-01-02",
"value": 67
}, {
"date": "2013-01-03",
"value": 64
}, {
"date": "2013-01-04",
"value": 66
}, {
"date": "2013-01-05",
"value": 60
}, {
"date": "2013-01-06",
"value": 63
}, {
"date": "2013-01-07",
"value": 61
}, {
"date": "2013-01-08",
"value": 60
}, {
"date": "2013-01-09",
"value": 65
}, {
"date": "2013-01-10",
"value": 75
}, {
"date": "2013-01-11",
"value": 77
}, {
"date": "2013-01-12",
"value": 78
}, {
"date": "2013-01-13",
"value": 70
}, {
"date": "2013-01-14",
"value": 70
}, {
"date": "2013-01-15",
"value": 73
}, {
"date": "2013-01-16",
"value": 71
}, {
"date": "2013-01-17",
"value": 74
}, {
"date": "2013-01-18",
"value": 78
}, {
"date": "2013-01-19",
"value": 85
}, {
"date": "2013-01-20",
"value": 82
}, {
"date": "2013-01-21",
"value": 83
}, {
"date": "2013-01-22",
"value": 88
}, {
"date": "2013-01-23",
"value": 85
}, {
"date": "2013-01-24",
"value": 85
}, {
"date": "2013-01-25",
"value": 80
}, {
"date": "2013-01-26",
"value": 87
}, {
"date": "2013-01-27",
"value": 84
}, {
"date": "2013-01-28",
"value": 83
}, {
"date": "2013-01-29",
"value": 84
}, {
"date": "2013-01-30",
"value": 81
} ];
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 50;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.strokeWidth = 3;
series.fillOpacity = 0.5;
// Add vertical scrollbar
chart.scrollbarY = new am4core.Scrollbar();
chart.scrollbarY.marginLeft = 0;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomY";
chart.cursor.lineX.disabled = true;
chart.events.on('ready', function (e) {
setInterval(function () {
var data = e.target.data,
item = data[data.length - 1],
lastDate = new Date(item.date),
newDate = new Date(lastDate.setDate(lastDate.getDate() + 2)),
fullYear = newDate.getFullYear(),
month = newDate.getMonth() + 1,
day = newDate.getDate();
if (month < 10)
month = '0' + month;
if (day < 10)
day = '0' + day;
chart.addData({
date: [fullYear, month, day].join('-'),
value: item.value + Math.floor(Math.random() * 9) - 5
}, 1);
}, 1000);
});
// chart.events.on('ready', function (e) {
//
// $interval(function () {
// Blooddonation.donationAdmin().then(function(data) {
// app.date = data.data.date; //store the items here
//
// e.target.addData(app.date.map(function (item) {
// return {
// date: item._id,
// value: item.count
// };
// }), app.date.length - 1);
// });
// }, 2000);
//
// });
#chartdiv {
width: 100%;
height: 400px;
}
<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

PHP - amcharts.js:34 Uncaught TypeError: Cannot read property 'call' of undefined

Please clear me this error i don't know what to do with this error? I really don't know where the error is? I want to create a graph like this but the
values i give is not working. The JSON values I am giving had not been
plotte
i want the result as plotted graph with all scroll and zoom options for realtime dataset in json format friends. Here i am using amchart js and normal javascript.
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 40,
"marginLeft": 40,
"autoMarginOffset": 20,
"mouseWheelZoomEnabled":true,
"dataDateFormat": "YYYY-MM-DD",
"valueAxes": [{
"id": "v1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth":true
}],
"balloon": {
"borderThickness": 1,
"shadowAlpha": 0
},
"graphs": [{
"id": "g1",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"bulletSize": 5,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "red line",
"useLineColorForBulletBorder": true,
"valueField": "value",
"balloonText": "<span style='font-size:18px;'>[[value]]</span>"
}],
"chartScrollbar": {
"graph": "g1",
"oppositeAxis":false,
"offset":30,
"scrollbarHeight": 80,
"backgroundAlpha": 0,
"selectedBackgroundAlpha": 0.1,
"selectedBackgroundColor": "#888888",
"graphFillAlpha": 0,
"graphLineAlpha": 0.5,
"selectedGraphFillAlpha": 0,
"selectedGraphLineAlpha": 1,
"autoGridCount":true,
"color":"#AAAAAA"
},
"chartCursor": {
"pan": true,
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha":1,
"cursorColor":"#258cbb",
"limitToGraph":"g1",
"valueLineAlpha":0.2,
"valueZoomable":true
},
"valueScrollbar":{
"oppositeAxis":false,
"offset":50,
"scrollbarHeight":10
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"dashLength": 1,
"minorGridEnabled": true
},
"export": {
"enabled": true
},
"dataProvider": [
{
"date":"2017-08-08",
"value":28
},
{
"date":"2017-08-07",
"value":26
},
{
"date":"2017-08-06",
"value":36
},
{
"date":"2017-08-05",
"value":31
},
{
"date":"2017-08-04",
"value":30
},
{
"date":"2017-08-03",
"value":27
},
{
"date":"2017-08-02",
"value":28
},
{
"date":"2017-08-01",
"value":25
},
{
"date":"2017-07-31",
"value":27
},
{
"date":"2017-07-30",
"value":27
},
{
"date":"2017-07-29",
"value":36
},
{
"date":"2017-07-28",
"value":30
},
{
"date":"2017-07-27",
"value":39
},
{
"date":"2017-07-26",
"value":36
},
{
"date":"2017-07-25",
"value":32
},
{
"date":"2017-08-09",
"value":33
},
{
"date":"2017-08-09",
"value":40
},
{
"date":"2017-08-09",
"value":33
},
{
"date":"2017-08-09",
"value":25
},
{
"date":"2017-08-09",
"value":33
},
{
"date":"2017-08-09",
"value":26
},
{
"date":"2017-07-24",
"value":38
},
{
"date":"2017-07-23",
"value":33
},
{
"date":"2017-07-22",
"value":37
},
{
"date":"2017-07-21",
"value":35
},
{
"date":"2017-07-20",
"value":39
},
{
"date":"2017-07-19",
"value":26
},
{
"date":"2017-07-18",
"value":29
},
{
"date":"2017-07-17",
"value":40
},
{
"date":"2017-07-16",
"value":40
},
{
"date":"2017-07-15",
"value":37
},
{
"date":"2017-07-14",
"value":28
},
{
"date":"2017-07-13",
"value":26
},
{
"date":"2017-07-12",
"value":32
},
{
"date":"2017-07-11",
"value":34
},
{
"date":"2017-07-10",
"value":31
},
{
"date":"2017-07-09",
"value":34
},
{
"date":"2017-07-08",
"value":37
},
{
"date":"2017-07-07",
"value":31
},
{
"date":"2017-07-06",
"value":36
},
{
"date":"2017-07-05",
"value":40
},
{
"date":"2017-07-04",
"value":27
},
{
"date":"2017-07-03",
"value":26
},
{
"date":"2017-07-02",
"value":38
},
{
"date":"2017-07-01",
"value":39
},
{
"date":"2017-06-30",
"value":33
},
{
"date":"2017-06-29",
"value":31
},
{
"date":"2017-06-28",
"value":38
},
{
"date":"2017-06-27",
"value":26
},
{
"date":"2017-06-26",
"value":32
},
{
"date":"2017-06-25",
"value":30
},
{
"date":"2017-06-24",
"value":27
},
{
"date":"2017-06-23",
"value":29
},
{
"date":"2017-06-22",
"value":39
},
{
"date":"2017-06-21",
"value":40
},
{
"date":"2017-06-20",
"value":39
},
{
"date":"2017-06-19",
"value":38
},
{
"date":"2017-06-18",
"value":25
},
{
"date":"2017-06-17",
"value":28
},
{
"date":"2017-06-16",
"value":37
},
{
"date":"2017-06-15",
"value":40
},
{
"date":"2017-06-14",
"value":40
},
{
"date":"2017-06-13",
"value":40
},
{
"date":"2017-06-12",
"value":25
},
{
"date":"2017-06-11",
"value":32
},
{
"date":"2017-06-10",
"value":34
},
{
"date":"2017-06-09",
"value":32
},
{
"date":"2017-06-08",
"value":25
},
{
"date":"2017-06-07",
"value":31
},
{
"date":"2017-06-06",
"value":39
},
{
"date":"2017-06-05",
"value":37
},
{
"date":"2017-06-04",
"value":30
},
{
"date":"2017-06-03",
"value":26
},
{
"date":"2017-06-02",
"value":38
},
{
"date":"2017-06-01",
"value":28
},
{
"date":"2017-05-31",
"value":40
},
{
"date":"2017-05-30",
"value":31
},
{
"date":"2017-05-29",
"value":34
},
{
"date":"2017-05-28",
"value":37
},
{
"date":"2017-05-27",
"value":33
},
{
"date":"2017-05-26",
"value":25
},
{
"date":"2017-05-25",
"value":27
},
{
"date":"2017-05-24",
"value":35
},
{
"date":"2017-05-23",
"value":30
},
{
"date":"2017-05-22",
"value":25
},
{
"date":"2017-05-21",
"value":35
},
{
"date":"2017-05-20",
"value":29
},
{
"date":"2017-05-19",
"value":38
},
{
"date":"2017-05-18",
"value":36
},
{
"date":"2017-05-17",
"value":32
},
{
"date":"2017-05-16",
"value":35
},
{
"date":"2017-05-15",
"value":35
},
{
"date":"2017-05-14",
"value":32
},
{
"date":"2017-05-13",
"value":35
},
{
"date":"2017-05-12",
"value":36
},
{
"date":"2017-05-11",
"value":39
},
{
"date":"2017-05-10",
"value":28
},
{
"date":"2017-05-09",
"value":28
},
{
"date":"2017-05-08",
"value":40
},
{
"date":"2017-05-07",
"value":35
},
{
"date":"2017-05-06",
"value":26
},
{
"date":"2017-05-05",
"value":36
},
{
"date":"2017-05-04",
"value":25
},
{
"date":"2017-05-03",
"value":28
},
{
"date":"2017-05-02",
"value":34
},
{
"date":"2017-05-01",
"value":28
},
{
"date":"2017-04-30",
"value":27
},
{
"date":"2017-04-29",
"value":25
},
{
"date":"2017-04-28",
"value":37
},
{
"date":"2017-04-27",
"value":39
},
{
"date":"2017-04-26",
"value":33
},
{
"date":"2017-04-25",
"value":38
},
{
"date":"2017-04-24",
"value":25
},
{
"date":"2017-04-23",
"value":28
},
{
"date":"2017-04-22",
"value":27
},
{
"date":"2017-04-21",
"value":26
},
{
"date":"2017-04-20",
"value":38
},
{
"date":"2017-04-19",
"value":32
},
{
"date":"2017-04-18",
"value":39
},
{
"date":"2017-04-17",
"value":33
},
{
"date":"2017-04-16",
"value":39
},
{
"date":"2017-04-15",
"value":34
},
{
"date":"2017-04-14",
"value":28
},
{
"date":"2017-04-13",
"value":31
},
{
"date":"2017-04-12",
"value":28
},
{
"date":"2017-04-11",
"value":40
},
{
"date":"2017-04-10",
"value":29
},
{
"date":"2017-04-09",
"value":32
},
{
"date":"2017-04-08",
"value":27
},
{
"date":"2017-04-07",
"value":28
},
{
"date":"2017-04-06",
"value":26
},
{
"date":"2017-04-05",
"value":29
},
{
"date":"2017-04-04",
"value":40
},
{
"date":"2017-04-03",
"value":26
},
{
"date":"2017-04-02",
"value":32
},
{
"date":"2017-04-01",
"value":34
},
{
"date":"2017-03-31",
"value":29
},
{
"date":"2017-03-30",
"value":35
},
{
"date":"2017-03-29",
"value":34
},
{
"date":"2017-03-28",
"value":26
},
{
"date":"2017-03-27",
"value":33
},
{
"date":"2017-03-26",
"value":27
},
{
"date":"2017-03-25",
"value":39
},
{
"date":"2017-03-24",
"value":34
},
{
"date":"2017-03-23",
"value":30
}
]
});
chart.addListener("rendered", zoomChart);
zoomChart();
function zoomChart() {
chart.zoomToIndexes(chart.dataProvider.length - 40, chart.dataProvider.length - 1);
}
.amcharts-chart-div a {display:none !important;}
#drop {
margin-top: 5%;
margin-left: 30%;
margin-right: 30%;
}
#chartdiv {
margin-top: 2%;
margin-left: 20%;
margin-right: 20%;
width : auto;
height : 500px;
}
<html>
<head>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<script>
</script>
<style>
</style>
</head>
<body>
<div class="row" id="drop">
<div class="offset-m3 offset-l4 col s12 m6 l4">
<ul id = "dropdown" class = "dropdown-content">
<li>Department2</li>
<li>Department3</li>
<li>Department4</li>
<li>Department5</li>
<li>Department6</li>
<li>Department7</li>
<li>Department8</li>
<li>Department9</li>
<li>Department10</li>
<li>Department11</li>
<li>Department12</li>
<li>Department13</li>
<li>Department14</li>
<li>Department15</li>
<li>Department16</li>
<li>Department17</li>
</ul>
<a class = "btn dropdown-button" href = "graph.html" data-activates = "dropdown">Department1
<i class = "mdi-navigation-arrow-drop-down right"></i></a>
</div>
</div>
<div class="offset-m2 offset-l3 col s12 m8 l6">
<div class="" id="chartdiv"></div>
</div>
</body>
</html>
I'm not able to reproduce that exact console error, but I am getting issues when zooming due to your dates being out of order. As mentioned in the parseDates documentation, your date-based data must be sorted in ascending order. Your data was mostly in descending order with duplicate 2017-08-09 dates out of order, which should also be removed.
Here's your code in sorted order with the duplicates removed:
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 40,
"marginLeft": 40,
"autoMarginOffset": 20,
"mouseWheelZoomEnabled": true,
"dataDateFormat": "YYYY-MM-DD",
"valueAxes": [{
"id": "v1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth": true
}],
"balloon": {
"borderThickness": 1,
"shadowAlpha": 0
},
"graphs": [{
"id": "g1",
"balloon": {
"drop": true,
"adjustBorderColor": false,
"color": "#ffffff"
},
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"bulletSize": 5,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "red line",
"useLineColorForBulletBorder": true,
"valueField": "value",
"balloonText": "<span style='font-size:18px;'>[[value]]</span>"
}],
"chartScrollbar": {
"graph": "g1",
"oppositeAxis": false,
"offset": 30,
"scrollbarHeight": 80,
"backgroundAlpha": 0,
"selectedBackgroundAlpha": 0.1,
"selectedBackgroundColor": "#888888",
"graphFillAlpha": 0,
"graphLineAlpha": 0.5,
"selectedGraphFillAlpha": 0,
"selectedGraphLineAlpha": 1,
"autoGridCount": true,
"color": "#AAAAAA"
},
"chartCursor": {
"pan": true,
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha": 1,
"cursorColor": "#258cbb",
"limitToGraph": "g1",
"valueLineAlpha": 0.2,
"valueZoomable": true
},
"valueScrollbar": {
"oppositeAxis": false,
"offset": 50,
"scrollbarHeight": 10
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"dashLength": 1,
"minorGridEnabled": true
},
"export": {
"enabled": true
},
"dataProvider": [{
"date": "2017-03-23",
"value": 30
}, {
"date": "2017-03-24",
"value": 34
}, {
"date": "2017-03-25",
"value": 39
}, {
"date": "2017-03-26",
"value": 27
}, {
"date": "2017-03-27",
"value": 33
}, {
"date": "2017-03-28",
"value": 26
}, {
"date": "2017-03-29",
"value": 34
}, {
"date": "2017-03-30",
"value": 35
}, {
"date": "2017-03-31",
"value": 29
}, {
"date": "2017-04-01",
"value": 34
}, {
"date": "2017-04-02",
"value": 32
}, {
"date": "2017-04-03",
"value": 26
}, {
"date": "2017-04-04",
"value": 40
}, {
"date": "2017-04-05",
"value": 29
}, {
"date": "2017-04-06",
"value": 26
}, {
"date": "2017-04-07",
"value": 28
}, {
"date": "2017-04-08",
"value": 27
}, {
"date": "2017-04-09",
"value": 32
}, {
"date": "2017-04-10",
"value": 29
}, {
"date": "2017-04-11",
"value": 40
}, {
"date": "2017-04-12",
"value": 28
}, {
"date": "2017-04-13",
"value": 31
}, {
"date": "2017-04-14",
"value": 28
}, {
"date": "2017-04-15",
"value": 34
}, {
"date": "2017-04-16",
"value": 39
}, {
"date": "2017-04-17",
"value": 33
}, {
"date": "2017-04-18",
"value": 39
}, {
"date": "2017-04-19",
"value": 32
}, {
"date": "2017-04-20",
"value": 38
}, {
"date": "2017-04-21",
"value": 26
}, {
"date": "2017-04-22",
"value": 27
}, {
"date": "2017-04-23",
"value": 28
}, {
"date": "2017-04-24",
"value": 25
}, {
"date": "2017-04-25",
"value": 38
}, {
"date": "2017-04-26",
"value": 33
}, {
"date": "2017-04-27",
"value": 39
}, {
"date": "2017-04-28",
"value": 37
}, {
"date": "2017-04-29",
"value": 25
}, {
"date": "2017-04-30",
"value": 27
}, {
"date": "2017-05-01",
"value": 28
}, {
"date": "2017-05-02",
"value": 34
}, {
"date": "2017-05-03",
"value": 28
}, {
"date": "2017-05-04",
"value": 25
}, {
"date": "2017-05-05",
"value": 36
}, {
"date": "2017-05-06",
"value": 26
}, {
"date": "2017-05-07",
"value": 35
}, {
"date": "2017-05-08",
"value": 40
}, {
"date": "2017-05-09",
"value": 28
}, {
"date": "2017-05-10",
"value": 28
}, {
"date": "2017-05-11",
"value": 39
}, {
"date": "2017-05-12",
"value": 36
}, {
"date": "2017-05-13",
"value": 35
}, {
"date": "2017-05-14",
"value": 32
}, {
"date": "2017-05-15",
"value": 35
}, {
"date": "2017-05-16",
"value": 35
}, {
"date": "2017-05-17",
"value": 32
}, {
"date": "2017-05-18",
"value": 36
}, {
"date": "2017-05-19",
"value": 38
}, {
"date": "2017-05-20",
"value": 29
}, {
"date": "2017-05-21",
"value": 35
}, {
"date": "2017-05-22",
"value": 25
}, {
"date": "2017-05-23",
"value": 30
}, {
"date": "2017-05-24",
"value": 35
}, {
"date": "2017-05-25",
"value": 27
}, {
"date": "2017-05-26",
"value": 25
}, {
"date": "2017-05-27",
"value": 33
}, {
"date": "2017-05-28",
"value": 37
}, {
"date": "2017-05-29",
"value": 34
}, {
"date": "2017-05-30",
"value": 31
}, {
"date": "2017-05-31",
"value": 40
}, {
"date": "2017-06-01",
"value": 28
}, {
"date": "2017-06-02",
"value": 38
}, {
"date": "2017-06-03",
"value": 26
}, {
"date": "2017-06-04",
"value": 30
}, {
"date": "2017-06-05",
"value": 37
}, {
"date": "2017-06-06",
"value": 39
}, {
"date": "2017-06-07",
"value": 31
}, {
"date": "2017-06-08",
"value": 25
}, {
"date": "2017-06-09",
"value": 32
}, {
"date": "2017-06-10",
"value": 34
}, {
"date": "2017-06-11",
"value": 32
}, {
"date": "2017-06-12",
"value": 25
}, {
"date": "2017-06-13",
"value": 40
}, {
"date": "2017-06-14",
"value": 40
}, {
"date": "2017-06-15",
"value": 40
}, {
"date": "2017-06-16",
"value": 37
}, {
"date": "2017-06-17",
"value": 28
}, {
"date": "2017-06-18",
"value": 25
}, {
"date": "2017-06-19",
"value": 38
}, {
"date": "2017-06-20",
"value": 39
}, {
"date": "2017-06-21",
"value": 40
}, {
"date": "2017-06-22",
"value": 39
}, {
"date": "2017-06-23",
"value": 29
}, {
"date": "2017-06-24",
"value": 27
}, {
"date": "2017-06-25",
"value": 30
}, {
"date": "2017-06-26",
"value": 32
}, {
"date": "2017-06-27",
"value": 26
}, {
"date": "2017-06-28",
"value": 38
}, {
"date": "2017-06-29",
"value": 31
}, {
"date": "2017-06-30",
"value": 33
}, {
"date": "2017-07-01",
"value": 39
}, {
"date": "2017-07-02",
"value": 38
}, {
"date": "2017-07-03",
"value": 26
}, {
"date": "2017-07-04",
"value": 27
}, {
"date": "2017-07-05",
"value": 40
}, {
"date": "2017-07-06",
"value": 36
}, {
"date": "2017-07-07",
"value": 31
}, {
"date": "2017-07-08",
"value": 37
}, {
"date": "2017-07-09",
"value": 34
}, {
"date": "2017-07-10",
"value": 31
}, {
"date": "2017-07-11",
"value": 34
}, {
"date": "2017-07-12",
"value": 32
}, {
"date": "2017-07-13",
"value": 26
}, {
"date": "2017-07-14",
"value": 28
}, {
"date": "2017-07-15",
"value": 37
}, {
"date": "2017-07-16",
"value": 40
}, {
"date": "2017-07-17",
"value": 40
}, {
"date": "2017-07-18",
"value": 29
}, {
"date": "2017-07-19",
"value": 26
}, {
"date": "2017-07-20",
"value": 39
}, {
"date": "2017-07-21",
"value": 35
}, {
"date": "2017-07-22",
"value": 37
}, {
"date": "2017-07-23",
"value": 33
}, {
"date": "2017-07-24",
"value": 38
}, {
"date": "2017-07-25",
"value": 32
}, {
"date": "2017-07-26",
"value": 36
}, {
"date": "2017-07-27",
"value": 39
}, {
"date": "2017-07-28",
"value": 30
}, {
"date": "2017-07-29",
"value": 36
}, {
"date": "2017-07-30",
"value": 27
}, {
"date": "2017-07-31",
"value": 27
}, {
"date": "2017-08-01",
"value": 25
}, {
"date": "2017-08-02",
"value": 28
}, {
"date": "2017-08-03",
"value": 27
}, {
"date": "2017-08-04",
"value": 30
}, {
"date": "2017-08-05",
"value": 31
}, {
"date": "2017-08-06",
"value": 36
}, {
"date": "2017-08-07",
"value": 26
}, {
"date": "2017-08-08",
"value": 28
}, {
"date": "2017-08-09",
"value": 33
}/*
remove extra duplicate dates
, {
"date": "2017-08-09",
"value": 40
}, {
"date": "2017-08-09",
"value": 33
}, {
"date": "2017-08-09",
"value": 25
}, {
"date": "2017-08-09",
"value": 33
}, {
"date": "2017-08-09",
"value": 26
}
*/
]
});
chart.addListener("rendered", zoomChart);
zoomChart();
function zoomChart() {
chart.zoomToIndexes(chart.dataProvider.length - 40, chart.dataProvider.length - 1);
}
.amcharts-chart-div a {
display: none !important;
}
#drop {
margin-top: 5%;
margin-left: 30%;
margin-right: 30%;
}
#chartdiv {
margin-top: 2%;
margin-left: 20%;
margin-right: 20%;
width: auto;
height: 500px;
}
<html>
<head>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<script>
</script>
<style>
</style>
</head>
<body>
<div class="row" id="drop">
<div class="offset-m3 offset-l4 col s12 m6 l4">
<ul id="dropdown" class="dropdown-content">
<li>Department2</li>
<li>Department3</li>
<li>Department4</li>
<li>Department5</li>
<li>Department6</li>
<li>Department7</li>
<li>Department8</li>
<li>Department9</li>
<li>Department10</li>
<li>Department11</li>
<li>Department12</li>
<li>Department13</li>
<li>Department14</li>
<li>Department15</li>
<li>Department16</li>
<li>Department17</li>
</ul>
<a class="btn dropdown-button" href="graph.html" data-activates="dropdown">Department1
<i class = "mdi-navigation-arrow-drop-down right"></i></a>
</div>
</div>
<div class="offset-m2 offset-l3 col s12 m8 l6">
<div class="" id="chartdiv"></div>
</div>
</body>
</html>

Create Nested JSON object Javascript

Hello I want to create a JSON Object for storage resources in a post request in java script I have an input array value disk sizes for example below:
request1
input = [10, 20, 30]
"storageResources": [
{
"stats": [
{
"name": "diskSize",
"units": "GB",
"value": 10
},
{
"name": "diskIopsConsumed",
"value": 0
},
{
"name": "diskConsumedFactor",
"value": 1
}
]
},
{
"stats": [
{
"name": "diskSize",
"units": "GB",
"value": 20
},
{
"name": "diskIopsConsumed",
"value": 1
},
{
"name": "diskConsumedFactor",
"value": "NaN"
}
]
},
{
"stats": [
{
"name": "diskSize",
"units": "GB",
"value": 30
},
{
"name": "diskIopsConsumed",
"value": 0
},
{
"name": "diskConsumedFactor",
"value": 1
}
]
},
],
request2:
input [10,20]
"storageResources": [
{
"stats": [
{
"name": "diskSize",
"units": "GB",
"value": 10
},
{
"name": "diskIopsConsumed",
"value": 0
},
{
"name": "diskConsumedFactor",
"value": 1
}
]
},
{
"stats": [
{
"name": "diskSize",
"units": "GB",
"value": 20
},
{
"name": "diskIopsConsumed",
"value": 1
},
{
"name": "diskConsumedFactor",
"value": "NaN"
}
]
}
],
Is the best way to do this with a function or can you send it by properties?
Use Array.prototype.map to return a modified response
$$text.oninput = evt => {
let json = JSON.parse($$text.value)
let result = json.storageResources.map(resource =>
resource.stats.find(e => e.name == 'diskSize').value
)
console.log(result)
}
$$text.oninput()
<textarea id="$$text">{"storageResources":[{"stats":[{"name":"diskSize","units":"GB","value":10},{"name":"diskIopsConsumed","value":0},{"name":"diskConsumedFactor","value":1}]},{"stats":[{"name":"diskSize","units":"GB","value":20},{"name":"diskIopsConsumed","value":1},{"name":"diskConsumedFactor","value":"NaN"}]},{"stats":[{"name":"diskSize","units":"GB","value":30},{"name":"diskIopsConsumed","value":0},{"name":"diskConsumedFactor","value":1}]}]}</textarea>

Is this JSON structure suitable for a stacked bar chart using d3.js?

I need to create a stacked bar chart plus line graph. I'm thinking d3.js is the tool for the job, but I'm having problems binding my data. Is the following JSON formatted in a way that will work for the stack method?
{
"response": {
"qtime": 11,
"params": {
"id": "jb8wp1rw41v",
"format": "json"
}
},
"series": {
"twitter": [{
"date": "2013-08-20",
"value": 3
}, {
"date": "2013-08-21",
"value": 1
}, {
"date": "2013-08-22",
"value": 4
}, {
"date": "2013-08-23",
"value": 1
}, {
"date": "2013-08-24",
"value": 5
}, {
"date": "2013-08-25",
"value": 9
}, {
"date": "2013-08-26",
"value": 2
}, {
"date": "2013-08-27",
"value": 6
}, {
"date": "2013-08-28",
"value": 5
}, {
"date": "2013-08-29",
"value": 3
}, {
"date": "2013-08-30",
"value": 5
}, {
"date": "2013-08-31",
"value": 8
}, {
"date": "2013-09-01",
"value": 9
}, {
"date": "2013-09-02",
"value": 7
}],
"facebook": [{
"date": "2013-08-20",
"value": 0
}, {
"date": "2013-08-21",
"value": 1
}, {
"date": "2013-08-22",
"value": 3
}, {
"date": "2013-08-23",
"value": 6
}, {
"date": "2013-08-24",
"value": 10
}, {
"date": "2013-08-25",
"value": 21
}, {
"date": "2013-08-26",
"value": 28
}, {
"date": "2013-08-27",
"value": 21
}, {
"date": "2013-08-28",
"value": 10
}, {
"date": "2013-08-29",
"value": 6
}, {
"date": "2013-08-30",
"value": 0
}, {
"date": "2013-08-31",
"value": 15
}, {
"date": "2013-09-01",
"value": 21
}, {
"date": "2013-09-02",
"value": 1
}],
"email": [{
"date": "2013-08-20",
"value": 0
}, {
"date": "2013-08-21",
"value": 1
}, {
"date": "2013-08-22",
"value": 1
}, {
"date": "2013-08-23",
"value": 2
}, {
"date": "2013-08-24",
"value": 3
}, {
"date": "2013-08-25",
"value": 5
}, {
"date": "2013-08-26",
"value": 8
}, {
"date": "2013-08-27",
"value": 13
}, {
"date": "2013-08-28",
"value": 5
}, {
"date": "2013-08-29",
"value": 8
}, {
"date": "2013-08-30",
"value": 1
}, {
"date": "2013-08-31",
"value": 1
}, {
"date": "2013-09-01",
"value": 2
}, {
"date": "2013-09-02",
"value": 13
}],
"amazon_rank": [{
"date": "2013-08-20",
"value": 1001
}, {
"date": "2013-08-21",
"value": 2312
}, {
"date": "2013-08-22",
"value": 2300
}, {
"date": "2013-08-23",
"value": 5179
}, {
"date": "2013-08-24",
"value": 5112
}, {
"date": "2013-08-25",
"value": 2305
}, {
"date": "2013-08-26",
"value": 1902
}, {
"date": "2013-08-27",
"value": 1221
}, {
"date": "2013-08-28",
"value": 1010
}, {
"date": "2013-08-29",
"value": 2588
}, {
"date": "2013-08-30",
"value": 4093
}, {
"date": "2013-08-31",
"value": 4432
}, {
"date": "2013-09-01",
"value": 5002
}, {
"date": "2013-09-02",
"value": 3902
}],
"pinterest": [{
"date": "2013-08-20",
"value": 17
}, {
"date": "2013-08-21",
"value": 23
}, {
"date": "2013-08-22",
"value": 11
}, {
"date": "2013-08-23",
"value": 13
}, {
"date": "2013-08-24",
"value": 19
}, {
"date": "2013-08-25",
"value": 5
}, {
"date": "2013-08-26",
"value": 17
}, {
"date": "2013-08-27",
"value": 11
}, {
"date": "2013-08-28",
"value": 2
}, {
"date": "2013-08-29",
"value": 3
}, {
"date": "2013-08-30",
"value": 5
}, {
"date": "2013-08-31",
"value": 7
}, {
"date": "2013-09-01",
"value": 19
}, {
"date": "2013-09-02",
"value": 0
}]
}
}
You'll need to wrangle the data a little bit to get in a format that d3 likes, but there are built in functions to do all the work for you:
var companies = d3.layout.stack()(d3.values(json.series))
will give you a data structure similar to causes in the stacked bar chart example.
Basically, d3.values is taking your object with several arrays and turning in into an array of arrays. d3.layout.stack takes that two dimensional array, re structures it a a little bit and adds convenience functions that make it much simpler create a stacked bar chart.
I would recommend you to not use JSON at all since it is not streamable. Indeed, charts are often representing a big amount of datas and it is recommended to load their content progressively with the help of the XHR progress event.
Since most of the charts are tabular datas, i assume CSV is the better format for that purpose. If you still want be able to manage trees like with JSON, take a look at VarStream https://github.com/nfroidure/VarStream.
Here is an example of loading charts with a streamable format versus with JSON : http://server.elitwork.com/experiments/chartstream/index.html
The badest is your connection, the more you see how usefull is XHR Streaming.

Categories

Resources