$(function() {
	AmCharts.addInitHandler(function(chart) {
		// check if there are graphs with autoColor: true set
		for(var i = 0; i < chart.graphs.length; i++) {
			var graph = chart.graphs[i];
			if(graph.autoColor !== true)
				continue;
			var colorKey = "autoColor-" + i;
			graph.lineColorField = colorKey;
			graph.fillColorsField = colorKey;
			for(var x = 0; x < chart.dataProvider.length; x++) {
				chart.dataProvider[x][colorKey] = chart.colors[x];
			}
		}
	}, ["serial"]);
});

//TODO line charta bak
function MakeLineChart(selector, data, label) {
	Morris.Line({
		element: selector,
		data: [
			{
				y: '2017-01-01',
				a: 100,
				b: 90
			},
			{
				y: '2017-02-01',
				a: 75,
				b: 65
			},
			{
				y: '2017-03-01',
				a: 50,
				b: 40
			},
			{
				y: '2017-04-01',
				a: 75,
				b: 65
			},
			{
				y: '2017-05-01',
				a: 50,
				b: 40
			},
			{
				y: '2017-06-01',
				a: 75,
				b: 65
			},
			{
				y: '2017-07-01',
				a: 100,
				b: 90
			}
		],
		xkey: 'y',
		xLabels: 'day',
		xLabelAngle: 25,
		xLabelFormat: function(d) {
			return ("0" + (d.getDate() + 1)).slice(-2) + '/' + ("0" + (d.getMonth() + 1)).slice(-2) + '/' + d.getFullYear();
		},
		resize: true,
		ykeys: ['a', 'b'],
		labels: ['Series A', 'Series B']
	});
}

function MakePieChart(selector, data, label) {
	var chart = AmCharts.makeChart(selector, {
		"type": "pie",
		"theme": "light",
		"language": "tr",
		"fontFamily": 'Open Sans',
		"color": '#888',
		"dataProvider": data,
		"valueField": "y",
		"titleField": "x",
		"outlineAlpha": 0.4,
		"depth3D": 15,
		"balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>",
		"angle": 30,
		"exportConfig": {
			menuItems: [{
//									icon: 'lib/3/images/export.png',
				format: 'png'
			}]
		}
	});
	if(label)
		chart.addLabel("0", '20', label, 'center');
	if(data.length == 0) {
		chart.addLabel(0, '50%', 'Veri Yok', 'center');
	}
	jQuery('.chart_7_chart_input').off().on('input change', function() {
		var property = jQuery(this).data('property');
		var target = chart;
		var value = Number(this.value);
		chart.startDuration = 0;
		if(property == 'innerRadius') {
			value += "%";
		}
		target[property] = value;
		chart.validateNow();
	});
	$('#' + selector).closest('.portlet').find('.fullscreen').click(function() {
		chart.invalidateSize();
	});
}

function MakeBarChart(selector, data, label) {
	var chart = AmCharts.makeChart(selector, {
		"theme": "light",
		"type": "serial",
		"startDuration": 2,
		"fontFamily": 'Open Sans',
		"color": '#888',
		"dataProvider": data,
		"graphs": [{
			"balloonText": "[[category]]: <b>[[value]]</b>",
//								"colorField": "color",
			"fillAlphas": 0.85,
			"lineAlpha": 0.1,
			"type": "column",
			"topRadius": 1,
			"valueField": "y",
			"autoColor": true
		}],
		"depth3D": 40,
		"angle": 45,
		"chartCursor": {
			"categoryBalloonEnabled": false,
			"cursorAlpha": 0,
			"zoomable": false
		},
		"categoryField": "x",
		"categoryAxis": {
			"gridPosition": "start",
			"axisAlpha": 0,
			"gridAlpha": 0,
			"tickLength": 0,
			"labelRotation": 25
		},
		"exportConfig": {
			"menuTop": "20px",
			"menuRight": "20px",
			"menuItems": [{
//										"icon": 'lib/3/images/export.png',
				"format": 'png'
			}]
		}
	}, 0);
	if(label)
		chart.addLabel("0", '20', label, 'center');
	if(data.length == 0) {
		chart.addLabel(0, '50%', 'Veri Yok', 'center');
	}
	jQuery('.chart_5_chart_input').off().on('input change', function() {
		var property = jQuery(this).data('property');
		var target = chart;
		chart.startDuration = 0;
		if(property == 'topRadius') {
			target = chart.graphs[0];
		}
		target[property] = this.value;
		chart.validateNow();
	});
	$('#' + selector).closest('.portlet').find('.fullscreen').click(function() {
		chart.invalidateSize();
	});
}