amChart failed to initialize after modal is show - ajax

$.ajax({
cache: false,
type: "POST",
url: route,
data: data,
success: function(response){
$('#evolutieProdus').show();
function showEvProd(data){
var chart = AmCharts.makeChart("chart_2",{
"type": "serial",
"theme": "dark",
"dataProvider" : data,
"valueAxes": [ {
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
} ],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [ {
"balloonText": "[[date]]: <b>[[bucati]] bucati vandute </b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "bucati"
} ],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "date",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
},
"export": {
"enabled": true
}
});
}
showEvProd(response);
console.log(response);
},
error: function(){
toastr.error('Eroare, ceva nu a functionat corect, reveniti mai tarziu');
}
});
This is my ajax call. I receive the callback with the data but in the graph all I see is an undefined. What is wrong with my code .
P.S. If I hard-coding the dataProvider object it works. But I want to load the graph with the data provided by the server via the ajax response.

Related

Loading JSON data into AmCharts using the dataloader plugin

I'm am trying to create a chart using AmCharts by loading JSON output from a webservice using the dataloader plugin from this URL http://portal.eyeo.se/datapump_amchart.ashx. For some reason, only a few items from my data are visible as you can see here: http://portal.eyeo.se/historychart
What am I doing wrong?
Code below:
<script src="amcharts/amcharts.js"></script>
<script src="amcharts//serial.js"></script>
<script src="amcharts/plugins/export/export.min.js"></script>
<script src="amcharts/plugins/dataloader/dataloader.min.js"></script>
<link rel="stylesheet" href="amcharts/plugins/export/export.css" type="text/css" media="all" />
<script src="amcharts/themes/light.js"></script>
<!-- Chart code -->
<script>
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"dataLoader": {
"url": "datapump_amchart.ashx",
"format": "json",
"async": "True"
},
"categoryField": "timestamp",
"dataDateFormat": "YYYY-MM-DD HH:NN:SS",
"rotate": false,
"valueAxes": [{
"position": "left",
"title": "SensorVärde"
}],
"categoryAxis": {
"parseDates": true,
"minPeriod": "ss",dateformat that is wron
},
"graphs": [{
"valueField": "value1",
"id": "g1",
"bullets": "round",
"balloon": {
"drop": false,
"adjustBorderColor": false,
"color": "#ffffff",
"type": "smoothedLine"
},
"fillAlphas": 0.2,
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"bulletSize": 10,
"hideBulletsCount": 50,
"lineThickness": 5,
"title": "red line",
"useLineColorForBulletBorder": true,
"valueField": "value1",
"fillAlphas": 0.4,
"balloonText": "<div style='margin:5px; font-size:10px;'>Sensorvärde:<b>[[value1]]</b></div>"
}],
"chartCursor": {
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha": 0,
"zoomable": false,
"valueZoomable": true,
"valueLineAlpha": 0.5,
"categoryBalloonDateFormat": "HH:NN:SS, DD MMMM",
"cursorPosition": "mouse"
},
"chartScrollbar": {
"graph": "g1",
"scrollbarHeight": 80,
"backgroundAlpha": 0,
"selectedBackgroundAlpha": 0.1,
"selectedBackgroundColor": "#888888",
"graphFillAlpha": 0,
"graphLineAlpha": 0.5,
"selectedGraphFillAlpha": 0,
"selectedGraphLineAlpha": 1,
"autoGridCount": true,
"color": "#AAAAAA"
},
"export": {
"enabled": true
},
});
</script>
<!-- HTML -->
<div id="chartdiv"></div>
The issue is that your date-based data is in the wrong order. It must be in ascending order when you're using parseDates as mentioned in the documentation.
Important: If this is set to true, the data points needs to come pre-ordered in ascending order. Data with incorrect order might result in visual and functional glitches on the chart.
Fixing the order of your data should solve your problem.

Amchart not initialized with data

Am Chart showing incorrectly, Here is following ajax code, incorrect thumb of chart.
I am using ajax to get the response and initialized the amchart (area)
Ajax json response data
{"success":true,"graph_data":[{"date":"2018-06-21","value":121.65},{"date":"2018-06-20","value":121.65},{"date":"2018-06-19","value":121.5},{"date":"2018-06-18","value":121.3},{"date":"2018-06-17","value":120.55},{"date":"2018-06-16","value":119.84},{"date":"2018-06-14","value":119.88}],"min_period":"DD","message":null}
Here code which I am initializing for amchart
var chart = AmCharts.makeChart( "graph_chart_div", {
"type": "serial",
"theme": "light",
"marginRight": 5,
"marginLeft": 5,
"minPeriod":response.min_period,
"autoMarginOffset": 5,
"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",
"type": "smoothedLine"
},
"fillAlphas": 0.2,
"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>"
} ],
"chartCursor": {
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha": 0,
"zoomable": false,
"valueZoomable": true,
"valueLineAlpha": 0.5
},
"valueScrollbar": {
"autoGridCount": true,
"color": "#000000",
"scrollbarHeight": 50
},
"categoryField": "date",
"categoryAxis": {
"gridPosition": "start",
"labelRotation": 45,
"minorGridEnabled": true,
/* ENSURE 2 LINES BELOW ARE ADDED */
"autoGridCount": false,
"gridCount": 12,
"parseDates": true,
"dashLength": 1,
// "minorGridEnabled": true
},
"export": {
"enabled": false
},
// "dataProvider": graph_data
"dataProvider": response.graph_data
} );
This is currently initialized chart
I want to show the following chart for my data
Your date data is out of order. AmCharts requires your date-based data to be in date-ascending order when using parseDates.
Important: If this is set to true, the data points needs to come pre-ordered in ascending order. Data with incorrect order might result in visual and functional glitches on the chart.
Once you fix the sort order, your chart will render correctly.

AmCharts zoom on render with dataLoader

I have several amCharts charts where I am trying to set up the default zoom. I can get it to work just fine with my code when using dataProvider such as here: http://jsfiddle.net/f2s32ojL/7/
When I try to do it on my own site but using dataLoader from a database, it does not work. It just gives me a default zoom of the entire dataset instead of the timeframe I want to use. Here is the code:
var chart = AmCharts.makeChart("chartdivFC", {
"type": "serial",
"dataLoader": {
"url": "includes/fc-chart-data2.php",
"format": "json"
},
"theme": "light",
"marginRight": 40,
"marginLeft": 40,
"autoMarginOffset": 20,
"mouseWheelZoomEnabled":true,
"dataDateFormat": "YYYY-MM-DD HH:NN:SS",
"valueAxes": [{
"id": "v1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth":true,
"guides": [{
"value": 0,
"toValue": "<?=$min_FC?>",
"fillColor": "#CC0000",
"inside": true,
"fillAlpha": 0.1,
"lineAlpha": 0
}]
}],
"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": "FC",
"balloonText": "<span style='font-size:18px;'>[[FC]]</span>"
}],
"chartScrollbar": {
"graph": "g1",
"oppositeAxis":false,
"offset":30,
"scrollbarHeight": 50,
"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
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"dashLength": 1,
"minorGridEnabled": true
},
"export": {
"enabled": true
},
"listeners": [{
"event": "rendered",
"method": function(event) {
event.chart.zoom(new Date(2017, 10, 1), new Date(2017, 11, 1));
}
}]
});
The json of the dataLoader looks like this:
[{"date":"2016-08-04 14:18:49","FC":"9"},{"date":"2016-08-04 14:44:30","FC":"9"},{"date":"2016-08-04 20:33:11","FC":"7.5"},{.....
Does anyone have any ideas how to make this work?
Thank you.
The reason why it doesn't work is because rendered fires before the dataLoader finishes loading your data; rendered fires when the chart area is rendered, regardless of whether data actually exists in the chart, so by the time you see the "Loading data" message pop up, the event has already fired.
You have two options:
1) Use the complete callback in the dataLoader to call the zoom method after the data has been loaded. Note that you have to wrap the zoom in a setTimeout call:
"dataLoader": {
"url": "includes/fc-chart-data2.php",
"complete": function(chart) {
setTimeout(function() {
chart.zoom(new Date(2017, 10, 1), new Date(2017, 11, 1))
});
}
},
2) Use the dataUpdated event instead. Note that this will fire every time you call validateData, which might not be ideal.
"listeners": [{
"event": "dataUpdated",
"method": function(event) {
event.chart.zoom(new Date(2017, 10, 1), new Date(2017, 11, 1));
}
}]

Missing date on Amcharts x-axis

I'm trying to build a simple graph. X-axis is month, Y-axis is value. Here is the demo of my graph
var chart = AmCharts.makeChart("chartdiv", {
"type": "xy",
"theme": "light",
"dataDateFormat": "DD-MM-YYYY",
"graphs": [
{
"id":"g8",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet":"round",
"bulletBorderAlpha":1,
"bulletColor":"#FFFFFF",
"bulletSize":5,
"dashLength":0,
"hideBulletsCount":50,
"lineThickness":2,
"lineColor":"#67b7dc",
"title":"Store 8",
"useLineColorForBulletBorder":true,
"xField":"d1",
"yField":"p1",
"xAxis":"g8",
"balloonText":"<span style='font-size:18px;'>$[[d2]]</span><br>07/1/2017-12/31/2017"
}
],
"valueAxes": [
{
"id": "g8",
"axisAlpha": 1,
"gridAlpha": 1,
"axisColor": "#b0de09",
"color": "#b0de09",
"dashLength": 5,
"centerLabelOnFullp": true,
"position": "bottom",
"type": "date",
"minp": "DD-MM-YYYY",
"markPeriodChange": false,
}
],
"dataProvider": [
{
"d1":"01/01/2017",
"p1":"5353.9000"
},{
"d1":"02/01/2017",
"p1":"5353.9000"
},{
"d1":"01/02/2017",
"p1":"5288.9500"
},{
"d1":"01/03/2017",
"p1":"6850.9900"
},{
"d1":"01/04/2017",
"p1":"5543.1900"
},{
"d1":"01/05/2017",
"p1":"5519.0100"
},{
"d1":"01/06/2017",
"p1":"6191.7500"
}
]
});
https://jsfiddle.net/noroots/xru967ha/
I don't know why, but X-axis labels missing June and all labels looks like having left offset. How can I move it to the left and show missing month?
You could add data items before and after without drawing points:
"dataProvider": [{
"d1":"01/12/2016"
}, {
"d1":"01/01/2017",
"p1":"5353.9000"
}, ...
Please check the example here: https://jsfiddle.net/xru967ha/5/
Old Answer
Please check the example below. It's using AmSerialChart and then the datePadding plugin to set 15 extra days at the beginning and the end of your data.
"categoryAxis": {
"parseDates": true,
"minPeriod": "MM",
"prependPeriods": 0.5, // add 15 days start
"appendPeriods": 0.5 // add 15 days to end
}
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 40,
"marginLeft": 60,
"dataDateFormat": "DD-MM-YYYY",
"valueAxes": [{
"id": "v1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth": true,
"dashLength": 5
}],
"graphs": [{
"id": "g1",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"bulletSize": 5,
"hideBulletsCount": 50,
"lineThickness": 2,
"lineColor":"#67b7dc",
"title": "red line",
"useLineColorForBulletBorder": true,
"valueField": "p1",
"balloonText": "<span style='font-size:18px;'>[[value]]</span>"
}],
"chartCursor": {
"pan": true,
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha":1,
"cursorColor":"#258cbb",
"limitToGraph":"g1",
"valueLineAlpha":0.2,
"valueZoomable":true
},
"categoryField": "d1",
"categoryAxis": {
"parseDates": true,
"minorGridEnabled": true,
"axisColor": "#b0de09",
"color": "#b0de09",
"dashLength": 5,
"boldPeriodBeginning": false,
"markPeriodChange": false,
"minPeriod": "MM",
"prependPeriods": 0.5,
"appendPeriods": 0.5
},
"export": {
"enabled": true
},
"dataProvider": [
{
"d1":"01/01/2017",
"p1":"5353.9000"
},{
"d1":"02/01/2017",
"p1":"5353.9000"
},{
"d1":"01/02/2017",
"p1":"5288.9500"
},{
"d1":"01/03/2017",
"p1":"6850.9900"
},{
"d1":"01/04/2017",
"p1":"5543.1900"
},{
"d1":"01/05/2017",
"p1":"5519.0100"
},{
"d1":"01/06/2017",
"p1":"6191.7500"
}
]
});
#chartdiv {
width : 800px;
height : 500px;
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/plugins/tools/datePadding/datePadding.min.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>
<div id="chartdiv"></div>

Amcharts add Euro symbol to value in balloon

This is my Json response, I can't figure out how I can add Euro symbol to my values. Ofcourse this is an idea of response and not the actual array that is being sent.
{"graphAxis":[{"valueAxis":"v0","lineColor":"#00969e","bullet":"round","bulletBorderAlpha":1,"bulletSize":"8","bulletBorderThickness":1,"bulletColor":"#FFFFFF","lineThickness":"2","hideBulletsCount":"50","title":"Prolongatie brandstoftermijn","valueField":"Prolongatie brandstoftermijn","costTypeId":15,"useLineColorForBulletBorder":true}],"dataProvider":[{"Prolongatie brandstoftermijn":4163.82,"date":"2016-01-01"},{"Prolongatie brandstoftermijn":7297.77,"date":"2016-02-01"},{"Prolongatie brandstoftermijn":7297.77,"date":"2016-03-01"},{"Prolongatie brandstoftermijn":162.43,"date":"2016-04-01"}]}
Can anyone provide any inputs on how to add Eur symbol to balloon text
This is my code in js file
//ajax request to show graph
$("#diaplayGraph").click(function(event) {
event.preventDefault();
$.ajax
({
url: $("#frmCostViewGraph").attr('action'),
data: $("#frmCostViewGraph").serialize(),
type: 'post',
beforeSend: function( xhr ) {
var message = '';
var selectedCostAllocations = $("#costAllocations").val();
var selectedCostTypes = $("#costTypes").val();
if(selectedCostAllocations == null) {
errorMessage($("#costAllocationError").html());
return false;
}
if(selectedCostTypes == null) {
errorMessage($("#costTypeError").html());
return false;
}
if($('input[name=reportType]:checked').length<=0)
{
errorMessage($("#reportTypeError").html());
return false
}
showLoading();
},
dataType:"JSON",
success: function(result)
{
hideMessage();
chartData = result.dataProvider;
graphAxis = result.graphAxis;
if(chartData == '') {
$("#chartdiv").html($("#noRecordFound").html());
hideLoading();
return false;
}
var chart = AmCharts.makeChart("chartdiv", {
"fontFamily": "Helvetica Neue,Helvetica,Arial,Open Sans,sans-serif",
"fontSize":12,
"type": "serial",
"theme": "default",
"mouseWheelZoomEnabled":true,
"dataDateFormat": "YYYY-MM-DD",
"valueAxes": [{
"id": "v1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth":true
}],
"balloon": {
"borderThickness": 1,
"shadowAlpha": 0
},
"legend": {
"useGraphSettings": false,
},
"dataProvider": chartData,
"graphs":graphAxis,
"chartScrollbar": {
"graph": "g1",
"oppositeAxis":false,
"offset":30,
"scrollbarHeight": 20,
"backgroundAlpha": 0,
"selectedBackgroundAlpha": 0.1,
"selectedBackgroundColor": "#888888",
"graphFillAlpha": 0,
"graphLineAlpha": 0.5,
"selectedGraphFillAlpha": 0,
"selectedGraphLineAlpha": 1,
"autoGridCount":true,
"color":"#AAAAAA",
"autoGridCount": true,
"resizeEnabled":true
},
"chartCursor": {
"valueLineEnabled": true,
"valueLineBalloonEnabled": true,
"cursorAlpha":1,
"cursorColor":"#258cbb",
"limitToGraph":"g1",
"valueLineAlpha":0.2,
"valueZoomable":true,
"cursorPosition": "mouse",
"oneBalloonOnly": true,
"categoryBalloonDateFormat": "DD-MMM-YYYY"
},
"valueScrollbar":{
"oppositeAxis":false,
"offset":75,
"scrollbarHeight":10,
"backgroundAlpha":1,
},
"valueAxes": [{
"axisColor": "#23272D",
"axisAlpha": 1,
"position": "left",
"unit": "$",
"unitPosition": "left"
}],
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"dashLength": 1,
"minorGridEnabled": true,
"minPeriod":"MM",
"axisColor": "#23272D"
},
"export": {
"enabled": true,
},
"numberFormatter": {
"precision": -1,
"decimalSeparator": ",",
"thousandsSeparator": "."
},
});
chart.addListener("clickGraphItem", handleClick);
zoomChart();
function zoomChart() {
chart.zoomToIndexes(chart.dataProvider.length - 40, chart.dataProvider.length - 1);
}
hideLoading();
}
});
})
This should be done with the graphs array, within your graphAxis.
If for instance you have an graph implementation like this, you can add the euro sign to the balloonText property:
success: function(result) {
hideMessage();
chartData = result.dataProvider;
graphAxis = result.graphAxis;
if(graphAxis && graphAxis[0]) {
graphAxis[0].balloonText = "<b>€ [[Prolongatie brandstoftermijn]]</b>";
}
//...
}

Resources