On hover, multiple nested donut charts not showing balloon text- amcharts - javascript

Multiple nested donut charts on the same page not working properly. I have two nested donut charts. One is showing ball0on text and another one is not. If I have 4 charts in first nested donut and two charts in second nested donut chart, balloon text will appear for last two outer charts in first and for both inner charts of the second donut.
Reference : Simulating nested donut chart using multiple pie chart instances
"Plugin: Manipulate z-index of the chart" is also there.
JSFIDDLE : PROBLEM STATEMENT
Hover on first nested donut chart inner pie charts, then hover on pie charts of second nested donut chart.
I didn't get any helpful data here : Documentation.
How to make balloon appear for all charts?
/**
* Plugin: Manipulate z-index of the chart
*/
AmCharts.addInitHandler(function(chart) {
// init holder for nested charts
if (AmCharts.nestedChartHolder === undefined)
AmCharts.nestedChartHolder = {};
if (chart.bringToFront === true) {
chart.addListener("init", function(event) {
// chart inited
var chart = event.chart;
var div = chart.div;
var parent = div.parentNode;
// add to holder
if (AmCharts.nestedChartHolder[parent] === undefined)
AmCharts.nestedChartHolder[parent] = [];
AmCharts.nestedChartHolder[parent].push(chart);
// add mouse mouve event
chart.div.addEventListener('mousemove', function() {
// calculate current radius
var x = Math.abs(chart.mouseX - (chart.realWidth / 2));
var y = Math.abs(chart.mouseY - (chart.realHeight / 2));
var r = Math.sqrt(x * x + y * y);
// check which chart smallest chart still matches this radius
var smallChart;
var smallRadius;
for (var i = 0; i < AmCharts.nestedChartHolder[parent].length; i++) {
var checkChart = AmCharts.nestedChartHolder[parent][i];
if ((checkChart.radiusReal < r) || (smallRadius < checkChart.radiusReal)) {
checkChart.div.style.zIndex = 1;
} else {
if (smallChart !== undefined)
smallChart.div.style.zIndex = 1;
checkChart.div.style.zIndex = 2;
smallChart = checkChart;
smallRadius = checkChart.radiusReal;
}
}
}, false);
});
}
}, ["pie"]);
/**
* Create the charts
*/
var pie1 = AmCharts.makeChart("chart1", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "$",
"value": 100,
"color": "#090E0F"
}],
"startDuration": 0,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 14,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -25,
"labelColor": "#fff",
"radius": 25,
"innerRadius": 0,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie2 = AmCharts.makeChart("chart2", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Marketing",
"value": 33,
"color": "#BA3233"
}, {
"title": "Production",
"value": 33,
"color": "#624B6A"
}, {
"title": "R&D",
"value": 33,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 9,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -28,
"labelColor": "#fff",
"radius": 80,
"innerRadius": 27,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie3 = AmCharts.makeChart("chart3", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Online",
"value": 11,
"color": "#BA3233"
}, {
"title": "Print",
"value": 11,
"color": "#BA3233"
}, {
"title": "Other",
"value": 11,
"color": "#BA3233"
}, {
"title": "Equipment",
"value": 16.5,
"color": "#624B6A"
}, {
"title": "Materials",
"value": 16.5,
"color": "#624B6A"
}, {
"title": "Labs",
"value": 16.5,
"color": "#6179C0"
}, {
"title": "Patents",
"value": 16.5,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 8,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -27,
"labelColor": "#fff",
"radius": 135,
"innerRadius": 82,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pi4 = AmCharts.makeChart("chart4", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Design",
"value": 5.5,
"color": "#BA3233"
}, {
"title": "P&P",
"value": 5.5,
"color": "#BA3233"
}, {
"title": "Magazines",
"value": 11,
"color": "#BA3233"
}, {
"title": "Outdoor",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Promo",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Endorsement",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Maintenance",
"value": 8.25,
"color": "#624B6A"
}, {
"title": "Acquisition",
"value": 8.25,
"color": "#624B6A"
}, {
"title": "Raw",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "Recycling",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "Logistics",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "LAB1",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "LAB2",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "LAB3",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Supply",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Disposal",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Application",
"value": 5.5,
"color": "#6179C0"
}, {
"title": "Acquisition",
"value": 5.5,
"color": "#6179C0"
}, {
"title": "Settlement",
"value": 5.5,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 8,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -27,
"labelColor": "#fff",
"radius": 190,
"innerRadius": 137,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]",
"allLabels": [{
"text": "ACME Inc. Spending Chart",
"bold": true,
"size": 18,
"color": "#404040",
"x": 0,
"align": "center",
"y": 20
}]
});
var pie5 = AmCharts.makeChart("chart11", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "$",
"value": 100,
"color": "#090E0F"
}],
"startDuration": 0,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 14,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -25,
"labelColor": "#fff",
"radius": 25,
"innerRadius": 0,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie6 = AmCharts.makeChart("chart22", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Marketing",
"value": 33,
"color": "#BA3233"
}, {
"title": "Production",
"value": 33,
"color": "#624B6A"
}, {
"title": "R&D",
"value": 33,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 9,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -28,
"labelColor": "#fff",
"radius": 80,
"innerRadius": 27,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
#charts,
#charts1 {
width: 200px;
height: 500px;
position: relative;
margin: 0 auto;
font-size: 8px;
flex: 1;
}
#charts {
width: 400px;
}
.chartdiv {
width: 400px;
height: 500px;
position: absolute;
top: 0;
left: 0;
}
#parent {
display: flex;
width: 700px;
border: 2px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/pie.js"></script>
<div id="parent">
<div id="charts">
<div id="chart1" class="chartdiv"></div>
<div id="chart2" class="chartdiv"></div>
<div id="chart3" class="chartdiv"></div>
<div id="chart4" class="chartdiv"></div>
</div>
<div id="charts1">
<div id="chart11" class="chartdiv"></div>
<div id="chart22" class="chartdiv"></div>
<div id="chart33" class="chartdiv"></div>
<div id="chart44" class="chartdiv"></div>
</div>
</div>

There's a bug with the nested plugin where it's using a reference to an element to hold a nest, which doesn't work when you have multiple sets of nested pie charts. A quick fix for this is to have it use the chart div's parent element ID to ensure that each set of pie charts are grouped correctly:
var parent = div.parentNode.id;
Of course this will add a dependency that the container div must have an ID but that's a small requirement.
You'll also want to remove the other divs if they don't have pie charts as they will prevent the mouseover event from firing on the second column's charts.
Updated code below:
/**
* Plugin: Manipulate z-index of the chart
*/
AmCharts.addInitHandler(function(chart) {
// init holder for nested charts
if (AmCharts.nestedChartHolder === undefined)
AmCharts.nestedChartHolder = {};
if (chart.bringToFront === true) {
chart.addListener("init", function(event) {
// chart inited
var chart = event.chart;
var div = chart.div;
var parent = div.parentNode.id; //changed to use the parent's ID instead of a reference.
// add to holder
if (AmCharts.nestedChartHolder[parent] === undefined) {
AmCharts.nestedChartHolder[parent] = [];
}
AmCharts.nestedChartHolder[parent].push(chart);
// add mouse mouve event
chart.div.addEventListener('mousemove', function() {
// calculate current radius
var x = Math.abs(chart.mouseX - (chart.realWidth / 2));
var y = Math.abs(chart.mouseY - (chart.realHeight / 2));
var r = Math.sqrt(x * x + y * y);
// check which chart smallest chart still matches this radius
var smallChart;
var smallRadius;
for (var i = 0; i < AmCharts.nestedChartHolder[parent].length; i++) {
var checkChart = AmCharts.nestedChartHolder[parent][i];
if ((checkChart.radiusReal < r) || (smallRadius < checkChart.radiusReal)) {
checkChart.div.style.zIndex = 1;
} else {
if (smallChart !== undefined)
smallChart.div.style.zIndex = 1;
checkChart.div.style.zIndex = 2;
smallChart = checkChart;
smallRadius = checkChart.radiusReal;
}
}
}, false);
});
}
}, ["pie"]);
/**
* Create the charts
*/
var pie1 = AmCharts.makeChart("chart1", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "$",
"value": 100,
"color": "#090E0F"
}],
"startDuration": 0,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 14,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -25,
"labelColor": "#fff",
"radius": 25,
"innerRadius": 0,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie2 = AmCharts.makeChart("chart2", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Marketing",
"value": 33,
"color": "#BA3233"
}, {
"title": "Production",
"value": 33,
"color": "#624B6A"
}, {
"title": "R&D",
"value": 33,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 9,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -28,
"labelColor": "#fff",
"radius": 80,
"innerRadius": 27,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie3 = AmCharts.makeChart("chart3", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Online",
"value": 11,
"color": "#BA3233"
}, {
"title": "Print",
"value": 11,
"color": "#BA3233"
}, {
"title": "Other",
"value": 11,
"color": "#BA3233"
}, {
"title": "Equipment",
"value": 16.5,
"color": "#624B6A"
}, {
"title": "Materials",
"value": 16.5,
"color": "#624B6A"
}, {
"title": "Labs",
"value": 16.5,
"color": "#6179C0"
}, {
"title": "Patents",
"value": 16.5,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 8,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -27,
"labelColor": "#fff",
"radius": 135,
"innerRadius": 82,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pi4 = AmCharts.makeChart("chart4", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Design",
"value": 5.5,
"color": "#BA3233"
}, {
"title": "P&P",
"value": 5.5,
"color": "#BA3233"
}, {
"title": "Magazines",
"value": 11,
"color": "#BA3233"
}, {
"title": "Outdoor",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Promo",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Endorsement",
"value": 3.66,
"color": "#BA3233"
}, {
"title": "Maintenance",
"value": 8.25,
"color": "#624B6A"
}, {
"title": "Acquisition",
"value": 8.25,
"color": "#624B6A"
}, {
"title": "Raw",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "Recycling",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "Logistics",
"value": 5.5,
"color": "#624B6A"
}, {
"title": "LAB1",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "LAB2",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "LAB3",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Supply",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Disposal",
"value": 3.3,
"color": "#6179C0"
}, {
"title": "Application",
"value": 5.5,
"color": "#6179C0"
}, {
"title": "Acquisition",
"value": 5.5,
"color": "#6179C0"
}, {
"title": "Settlement",
"value": 5.5,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 8,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -27,
"labelColor": "#fff",
"radius": 190,
"innerRadius": 137,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]",
"allLabels": [{
"text": "ACME Inc. Spending Chart",
"bold": true,
"size": 18,
"color": "#404040",
"x": 0,
"align": "center",
"y": 20
}]
});
var pie5 = AmCharts.makeChart("chart11", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "$",
"value": 100,
"color": "#090E0F"
}],
"startDuration": 0,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 14,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -25,
"labelColor": "#fff",
"radius": 25,
"innerRadius": 0,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
var pie6 = AmCharts.makeChart("chart22", {
"type": "pie",
"bringToFront": true,
"dataProvider": [{
"title": "Marketing",
"value": 33,
"color": "#BA3233"
}, {
"title": "Production",
"value": 33,
"color": "#624B6A"
}, {
"title": "R&D",
"value": 33,
"color": "#6179C0"
}],
"startDuration": 1,
"pullOutRadius": 0,
"color": "#fff",
"fontSize": 9,
"titleField": "title",
"valueField": "value",
"colorField": "color",
"labelRadius": -28,
"labelColor": "#fff",
"radius": 80,
"innerRadius": 27,
"outlineAlpha": 1,
"outlineThickness": 4,
"labelText": "[[title]]",
"balloonText": "[[title]]: [[value]]"
});
#charts,
#charts1 {
width: 200px;
height: 500px;
position: relative;
margin: 0 auto;
font-size: 8px;
flex: 1;
}
#charts {
width: 400px;
}
.chartdiv {
width: 400px;
height: 500px;
position: absolute;
top: 0;
left: 0;
}
#parent {
display: flex;
width: 700px;
border: 2px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/pie.js"></script>
<div id="parent">
<div id="charts">
<div id="chart1" class="chartdiv"></div>
<div id="chart2" class="chartdiv"></div>
<div id="chart3" class="chartdiv"></div>
<div id="chart4" class="chartdiv"></div>
</div>
<div id="charts1">
<div id="chart11" class="chartdiv"></div>
<div id="chart22" class="chartdiv"></div>
<!--
commented out as the mouseover event doesn't fire if there are empty divs overlapping
<div id="chart33" class="chartdiv"></div>
<div id="chart44" class="chartdiv"></div>
-->
</div>
</div>

Related

ClickMarker is not working in XY Amcharts

I have a requirement where I need to use customlegends in XY Amcharts.
I have implemented them, but When I have added event listeners to those legends, the function wasnt triggered. I dont know the reason, can anyone correct me what I have made a mistake.
You can check the following link for the output:
jsfiddle.net/u371jyjs/3/
clickMarker is a legend-specific event, not a top-level chart event. Put the listener for it inside the legend object:
legend: {
// ...
listeners: [{
"event": "clickMarker",
"method": function(event) {
// toggle the marker state
event.dataItem.hidden = !event.dataItem.hidden;
event.chart.validateNow();
}
}]
}
Demo:
var chart = AmCharts.makeChart("chartdiv", {
"type": "xy",
"path": "https://www.amcharts.com/lib/3/",
"theme": "light",
"dataProvider": [{
"y": 10,
"x": 2,
"value": 59,
"y2": 5,
"x2": 3,
"value2": 44,
"label": "Hello",
"category": "0",
"column-1": 32
}, {
"y": 5,
"x": 8,
"value": 50,
"y2": 15,
"x2": 8,
"value2": 12,
"label": "Hi",
"category": "1000",
"column-1": 14
}, {
"y": 10,
"x": 8,
"value": 19,
"y2": 4,
"x2": 6,
"value2": 35,
"label": "Yo"
}, {
"y": 6,
"x": 5,
"value": 65,
"y2": 5,
"x2": 6,
"value2": 168,
"label": "Howdy"
}, {
"y": 15,
"x": 4,
"value": 92,
"y2": 13,
"x2": 8,
"value2": 102,
"label": "Hi there"
}, {
"y": 13,
"x": 1,
"value": 8,
"y2": 2,
"x2": 0,
"value2": 41,
"label": "Morning"
}, {
"y": 1,
"x": 6,
"value": 35,
"y2": 0,
"x2": 3,
"value2": 16,
"label": "Afternoon"
}],
"valueAxes": [{
"position": "bottom",
"axisAlpha": 0,
"integersOnly": true,
//"labelRotation": 45,
"labelFunction": function(value) {
// define categories
var cats = [
"Nick",
"Sarah",
"Kevin",
"Dominick",
"Christy",
"Kate",
"Julian",
"Anita",
"Mike",
"Kyle",
"Tyrese"
];
return cats[value];
}
}, {
"axisAlpha": 0,
"position": "left"
}],
"startDuration": 1.5,
"graphs": [{
"balloonText": "[[label]]",
"bullet": "circle",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value",
"xField": "x",
"yField": "y",
"maxBulletSize": 100
}, {
"balloonText": "[[label]]",
"bullet": "diamond",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value2",
"xField": "x2",
"yField": "y2",
"maxBulletSize": 100
}],
"legend": {
"switchable": true,
"textClickEnabled": true,
"data": [{
title: "One",
color: "#3366CC",
hidden: true
}, {
title: "Two",
color: "#FFCC33"
}],
"listeners": [{
"event": "clickMarker",
"method": function(event) {
event.dataItem.hidden = !event.dataItem.hidden;
event.chart.validateNow()
}
}]
}
});
html, body {
width: 100%;
height: 100%;
margin: 0;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/xy.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
<div id="clicked">
</div>

AmCharts serial chart date ordering issue

I have 4 different data sets data1, data2, data3, data4, each data set contain different dates, so date is not in order, hence graph is not displaying according to date
Here is my code
AmCharts.makeChart("chartdiv", {
"type": "serial",
"addClassNames": true,
"startDuration": 0.4,
"theme": "light",
"dataDateFormat": "YYYY-MM-DD",
"trendLines": [],
"applyGapValue": 0,
"graphs": [{
"bullet": "round",
"type": "smoothedLine",
"valueField": "data2",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data1",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data3",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data4",
}],
"guides": [],
"valueAxes": [{
"id": "ValueAxis-1",
}],
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"equalSpacing": true,
"minorGridEnabled": true,
"gridPosition": "start",
},
"allLabels": [],
"balloon": {
"borderThickness": 3,
"horizontalPadding": 17,
"offsetX": 50,
"offsetY": 8
},
"chartCursor": {
"cursorAlpha": 0,
"cursorPosition": "mouse",
"graphBulletSize": 1,
"zoomable": false
},
"legend": {
"enabled": true,
"useGraphSettings": true,
"position": "top",
},
"dataProvider": [
{
"date": "2017-06-02",
"data1": 202,
}, {
"date": "2017-06-04",
"data1": 420,
}, {
"date": "2017-06-05",
"data1": 910,
}, {
"date": "2017-06-07",
"data1": 60,
},
{
"date": "2017-06-02",
"data2": 110,
}, {
"date": "2017-06-04",
"data2": 920,
}, {
"date": "2017-06-05",
"data2": 320,
}, {
"date": "2017-06-07",
"data2": 355,
},
{
"date": "2017-06-02",
"data3": 80,
}, {
"date": "2017-06-04",
"data3": 350,
}, {
"date": "2017-06-05",
"data3": 710,
}, {
"date": "2017-06-07",
"data3": 710,
},
{
"date": "2017-06-02",
"data4": 580,
}, {
"date": "2017-06-04",
"data4": 510,
}, {
"date": "2017-06-05",
"data4": 702,
}, {
"date": "2017-05-07",
"data4": 940,
}, {
"date": "2017-06-09",
"data4": 940,
},
]
});
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
in the output I want chart to be displayed according to ordered date, thank you
You can sort the data like this:
chartData.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
});
Let's say you do this before passing it to the dataProvider property.
While you do have to sort the data by dates as previously mentioned, it's not enough for AmCharts. You also need to group the data by date as well or your chart will not render or behave correctly, i.e. the array element with the date "2017-06-02" needs to have the "data1", "data2", "data3", "data4" properties in the same element.
Here's one way to group it (assuming your data is in a variable called chartData):
var dataMap = {}; //map to group data by date
var newChartData = []; //new chart data array
chartData.forEach(function(dataItem) {
if (!dataMap[dataItem.date]) {
dataMap[dataItem.date] = {};
}
Object.keys(dataItem).forEach(function(key) { //assign keys to map
if (key !== "date") {
dataMap[dataItem.date][key] = dataItem[key];
dataMap[dataItem.date].date = dataItem.date;
}
});
});
//sort the dates and add the grouped objects to the new array.
Object.keys(dataMap).sort(function(lhs, rhs) {
return new Date(lhs) - new Date(rhs);
}).forEach(function(date) {
newChartData.push(dataMap[date]);
});
Demo:
var chartData = [
{
"date": "2017-06-02",
"data1": 202,
}, {
"date": "2017-06-04",
"data1": 420,
}, {
"date": "2017-06-05",
"data1": 910,
}, {
"date": "2017-06-07",
"data1": 60,
},
{
"date": "2017-06-02",
"data2": 110,
}, {
"date": "2017-06-04",
"data2": 920,
}, {
"date": "2017-06-05",
"data2": 320,
}, {
"date": "2017-06-07",
"data2": 355,
},
{
"date": "2017-06-02",
"data3": 80,
}, {
"date": "2017-06-04",
"data3": 350,
}, {
"date": "2017-06-05",
"data3": 710,
}, {
"date": "2017-06-07",
"data3": 710,
},
{
"date": "2017-06-02",
"data4": 580,
}, {
"date": "2017-06-04",
"data4": 510,
}, {
"date": "2017-06-05",
"data4": 702,
}, {
"date": "2017-05-07",
"data4": 940,
}, {
"date": "2017-06-09",
"data4": 940,
},
];
var dataMap = {}; //map to group data by date
var newChartData = []; //new chart data array
chartData.forEach(function(dataItem) {
if (!dataMap[dataItem.date]) {
dataMap[dataItem.date] = {};
}
Object.keys(dataItem).forEach(function(key) { //assign keys to map
if (key !== "date") {
dataMap[dataItem.date][key] = dataItem[key];
dataMap[dataItem.date].date = dataItem.date;
}
});
});
//sort the dates and add the grouped objects to the new array.
Object.keys(dataMap).sort(function(lhs, rhs) {
return new Date(lhs) - new Date(rhs);
}).forEach(function(date) {
newChartData.push(dataMap[date]);
});
AmCharts.makeChart("chartdiv", {
"type": "serial",
"addClassNames": true,
"startDuration": 0.4,
"theme": "light",
"dataDateFormat": "YYYY-MM-DD",
"trendLines": [],
"applyGapValue": 0,
"graphs": [{
"bullet": "round",
"type": "smoothedLine",
"valueField": "data2",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data1",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data3",
}, {
"bullet": "round",
"type": "smoothedLine",
"valueField": "data4",
}],
"guides": [],
"valueAxes": [{
"id": "ValueAxis-1",
}],
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"equalSpacing": true,
"minorGridEnabled": true,
"gridPosition": "start",
},
"allLabels": [],
"balloon": {
"borderThickness": 3,
"horizontalPadding": 17,
"offsetX": 50,
"offsetY": 8
},
"chartCursor": {
"cursorAlpha": 0,
"cursorPosition": "mouse",
"graphBulletSize": 1,
"zoomable": false
},
"legend": {
"enabled": true,
"useGraphSettings": true,
"position": "top",
},
"dataProvider": newChartData
});
html,
body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>

amCharts group by Date

I use amCharts but I need to group data results by Date:
Demo example
I have 5 visits by one day and chart need to group results.
My code:
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"
},
"categoryAxesSettings": {
"maxSeries": 1,
"groupToPeriods": ["DD"]
},
"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": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
]
});
chart.addListener("rendered", zoomChart);
zoomChart();
function zoomChart() {
chart.zoomToIndexes(chart.dataProvider.length - 40, chart.dataProvider.length - 1);
}
By documentation I use grouping settings without success.
"categoryAxesSettings": {
"maxSeries": 1,
"groupToPeriods": ["DD"]
},
Any Help?
According to the documentation, categoryAxesSettings only applies to stock type charts. You're using a serial here. An alternative option is to translate the data yourself:
function translateData(data){
var newData = [], dates = [];
data.map(function(item){
var index = dates.indexOf(item.date);
if(index > -1){
newData[index].value += item.value;
}else{
newData.push(item);
dates.push(item.date);
}
});
return newData;
}
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"
},
"categoryAxesSettings": {
"maxSeries": 1,
"groupToPeriods": ["DD"]
},
"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": translateData([
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
},
{
"date": "2016-12-01",
"value": 1
}
])
});
chart.addListener("rendered", zoomChart);
zoomChart();
function zoomChart() {
chart.zoomToIndexes(chart.dataProvider.length - 40, chart.dataProvider.length - 1);
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv" style="height: 300px;"></div>

how to make graph section of amcharts dynamic

I am creating amcharts from database fields.Here is my code
function Categoriesline(data) {
debugger;
var databind = JSON.parse(data);
var chart = AmCharts.makeChart("chartdiv2", {
"theme": "light",
"type": "serial",
"legend": {
"position": "right",
"fontSize": 9
},
"marginRight": 80,
"autoMarginOffset": 20,
"marginTop": 20,
"dataProvider": databind,
"valueAxes": [{
"id": "v1",
"axisAlpha": 0.1
}],
"graphs": [{
"balloonText": "[[category]]<br><b>value: [[value]]</b>",
"bullet": "round",
"bulletBorderAlpha": 1,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "section1",
"valueField": "section1"
}, {
"balloonText": "[[category]]<br><b>value: [[value]]</b>",
"bullet": "round",
"bulletBorderAlpha": 1,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "section2",
"valueField": "section2"
}, {
"balloonText": "[[category]]<br><b>value: [[value]]</b>",
"bullet": "round",
"bulletBorderAlpha": 1,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "section3",
"valueField": "section3"
}],
"categoryField": "categorydescription",
"categoryAxis": {
"axisAlpha": 0,
"minHorizontalGap": 60
},
"export": {
"enabled": true
}
});
}
All required data from database i am fetching JSON format.
I want to specify the value axis from json itself.Here in above code instead of hardcoding "section1", "section2","section3" i want to assign it from databind object which is having json key name section.
How can i do this ?
I need to add something like
var graph = [];
var v;
for (var j = 0; j < 6; j++) {
graph[j] = new AmCharts.AmGraph();
graph[j].valueAxis =databind['section'] ; // we have to indicate which value axis should be used
graph[j].title = "Value" + j;
v = j.toString();
graph[j].valueField = databind['value'];
graph[j].bullet = "round";
chart.addGraph(graph[j]);
}
Json format
In above pic foreach section i need different lines to draw(multiple line chart).
[
{
"Section": "Section1",
"duration": "1MCount",
"value": 15},
{
"Section": "Section2",
"duration": "1MCount",
"value": 20},
{
"Section": "Section1",
"duration": "3MCount",
"value": 10},
{
"Section": "Section2",
"duration": "3MCount",
"value": 7},
{
"Section": "Section3",
"duration": "3MCount",
"value": 12}
]
i have added the json details.

AmCharts Legend

I am trying to replicate the following chart
The problem I have is how to make the legend in the left and in the middle of every box that stacks (Series word in each box). I have searched amCharts but no luck. Is there a way to do it or how to override or extend amCharts in order to achieve this behavior?
Thanks a lot.
For displaying value labels, use labelText proeprty.
Just set it to "[[value]]" in your graphs. I.e.:
"graphs": [{
...
"labelText": "[[value]]"
}]
For the "legend" on the left, you will need to use guides. It allows placing lines (optional) and labels at some preset values. You will need to pre-calculate the values at which to place them, though.
Here's a complete working example:
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"dataProvider": [{
"year": 2011,
"value1": 13,
"value2": 5,
"value3": 4
}, {
"year": 2012,
"value1": 22,
"value2": 4,
"value3": 5
}, {
"year": 2013,
"value1": 35,
"value2": 7,
"value3": 4
}],
"valueAxes": [{
"stackType": "regular",
"axisAlpha": 0,
"gridAlpha": 0,
"labelsEnabled": false,
"tickLength": 0,
"totalText": "[[total]]",
"guides": [{
"value": 6.5,
"label": "Series #1",
"fontSize": 15
}, {
"value": 15.5,
"label": "Series #2",
"fontSize": 15
}, {
"value": 20,
"label": "Series #3",
"fontSize": 15
}]
}],
"graphs": [{
"fillAlphas": 1,
"fillColors": "#a0b1cf",
"labelText": "[[value]]",
"lineAlpha": 1,
"lineColor": "#000",
"title": "value1",
"type": "column",
"color": "#000000",
"valueField": "value1"
}, {
"fillAlphas": 1,
"fillColors": "#c5cde0",
"labelText": "[[value]]",
"lineAlpha": 1,
"lineColor": "#000",
"title": "value1",
"type": "column",
"color": "#000000",
"valueField": "value2"
}, {
"fillAlphas": 1,
"fillColors": "#dde6eb",
"labelText": "[[value]]",
"lineAlpha": 1,
"lineColor": "#000",
"title": "value1",
"type": "column",
"color": "#000000",
"valueField": "value3"
}],
"categoryField": "year",
"categoryAxis": {
"gridPosition": "start",
"axisAlpha": 0,
"gridAlpha": 0,
"position": "left"
}
});
#chartdiv {
width: 400px;
height: 400px;
}
<script src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="http://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv"></div>

Categories