MediaWiki:Common.js/GECharts.js

var itemsParam = unescape(getUrlParam('items')).split(','); var isSmall = $('#GEdatachart').is('.smallChart');

//general functions

function getUrlParam(param) { param = document.location.search.substring(1).match(new RegExp(param+'=([^&#=]*)')); return param?param[1]:''; }

Array.prototype.avg = function(amt) { amt = amt?amt:this.length; var avgs = [], list = []; for (var i=0;i=amt) { avgs.push([this[i][0], Math.round(sum(list)/list.length)]); }		list[i % amt] = this[i][1] }	return avgs; }

function sum(arr) { var tot = 0; for (var i=0;i<arr.length;i++) { tot += parseFloat(arr[i])||0; }	return tot }

//chart-related general functions

if ($('#GEdataprices').length) { var items,vals,id,n,datalist,lastFilled,req,item,itemlist; function getExtraItem { items = []; item = {}; vals = ''; $('[id^="item"]').each(function {		items.push($(this).val);		vals += $(this).val;	}); if (vals.length) { datalist = [] n = 2; var i = 2; lastFilled = -1 itemlist = $('#GEdataprices').attr('data-item'); itemlist = itemlist || wgPageName.replace(/_/g,' ').split('/')[0].replace('Exchange:',''); $('[id^="item"]').each(function(i) {			id = $(this).attr('id')			item[id] = $(this).val;			if (item[id]&&item[id].length&&lastFilled==i-1) {				if (!itemlist.match(item[id])) {					req = $.get('/wiki/Exchange:'+item[id]+'/Data?action=render', function(a) { var prices = a.replace(/(.|\n)*]*>\n?|<\/p>(.|\n)*/g, '').split(/,\s/); var data = []; var thisprice; for (var i=0;i<prices.length;i++) { if (prices[i].replace(/\s/g,'').length) { thisprice = prices[i].split(':'); data.push([ parseInt(thisprice[0])*1000, parseInt(thisprice[1]) ]); }						}						itemlist += ','+item['item'+n] datalist.push({name:item['item'+n], data:data, lineWidth: 2}); n++; });					lastFilled = i;				}				if (!$('[id^="item"]').eq(i+1).length && req) { req.success(function {loadChart(datalist)}); req.statusCode({404: function {loadChart(datalist)}}) } else if (!req) {loadChart}			} else if (req) { req.success(function {loadChart(datalist)}); req.statusCode({404: function {loadChart(datalist)}}) } else { loadChart }		}); if (!req) { loadChart } } else {loadChart} }

function popupChart { if (!$('#GEchartpopup').length) return false; if ($('#overlay').length) { $('#overlay').toggle; } else { $('#GEchartpopup').before(' '); }	document.getElementById('overlay').onclick = popupChart $('#GEchartpopup').toggle; getExtraItem; }

//setting up the form and chart elements

if (isSmall) { $('body').append('\nClose popup '); }

$(isSmall?'#GEpopupchart':'#GEdatachart').before(' Average:  days Add more item(s):  Note: If you add more items, the averages will not be shown. Submit  ');

$('[id^="item"]').keyup(function {	if ($(this).val.length) $(this).next.removeAttr('disabled');	else if (!$(this).val.length) $(this).nextAll('input[id^="item"]').attr('disabled','disabled'); }); $('[id^="item"]:not(:disabled)').next.removeAttr('disabled');

//the chart function

var cLoaded = false; var data = [];

function loadChart(item2) { var itemName = $('#GEdataprices').attr('data-item'); itemName = itemName || wgPageName.replace(/_/g,' ').split('/')[0].replace('Exchange:',''); var prices = $('#GEdataprices').html.split(/,\s/); data = []; var thisprice; var volumes = []; var minmax = [0,0]; for (var i=0;i prices.length-30) { if (!minmax[0] || parseInt(thisprice[1]) < minmax[0]) { minmax[0] = parseInt(thisprice[1]); }			if (parseInt(thisprice[1]) > minmax[1]) { minmax[1] = parseInt(thisprice[1]); }		}	} }

//generating the dataList

var dataList = [{ name: itemName, data: data, lineWidth: isSmall?2:4 }] if (item2&&item2.length&&(!isSmall||cLoaded)) { for (i=0;i<item2.length;i++) { dataList.push(item2[i]); } } else if (!isSmall||cLoaded) { var inputAvg = parseInt($('input#average').val); if (inputAvg) { dataList.push({			name: inputAvg+'-day average',			data: data.avg(inputAvg),			lineWidth: 2,			dashStyle: 'shortdash',		}); } else { dataList.push({			name: '30-day average',			data: data.avg(30),			lineWidth: 2,			dashStyle: 'shortdash',		}); }	if (volumes.length) dataList.push({			name: 'Volume',			data: volumes,                       type : 'area',                        color: 'orange',                                               fillColor: {                        	linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },				stops: 0, 'orange'], [1, 'white',                	},			yAxis: 1,		}); }

//adding in the chart properties and the minmax lines

var yAxis = {}; if (volumes.length&&(!isSmall||cLoaded)&&!(item2&&item2.length)) { yAxis = [{ title: { text: 'Price history', style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               minTickInterval: 1,  //1 coin showLastLabel: 1, lineWidth: 1, lineColor: '#E0E0E0', height: 225, }, {		title: { text: 'Trade volume', style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               min: 0, showEmpty: 0, showLastLabel: 1, offset: 0, lineWidth: 1, lineColor: '#E0E0E0', height: 75, top: 318, }]; } else if (isSmall||cLoaded) { yAxis = { labels: { align: 'right', x: -8, y: 4, },               minTickInterval: 1,  //1 coin showLastLabel: 1, lineWidth: 1, lineColor: '#E0E0E0', } } else { yAxis = { title: { text: 'Price history', style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               minTickInterval: 1,  //1 coin showLastLabel: 1, lineWidth: 1, lineColor: '#E0E0E0', }       } Highcharts.setOptions({	lang: {                numericSymbols: ['k', 'M', 'B', 'T', 'Qd', 'Qt'],	} }); //generating the chart window.chart = new Highcharts.StockChart({	credits: {		enabled: isSmall&&!cLoaded,		href: 'javascript:popupChart',		text: 'Enlarge chart'	},	chart: {		renderTo: cLoaded&&isSmall?'GEpopupchart':'GEdatachart',		backgroundColor: 'transparent'	},	legend: {		enabled: !isSmall||cLoaded,		align: 'right',		layout: 'vertical',		verticalAlign: 'top',		y: 55	},       title: {		text: isSmall&&!cLoaded?null:'Grand Exchange Market Watch',		style: {                	color: 'black',                	fontSize: '18px',                },	},	subtitle: {		text: isSmall&&!cLoaded?null:''+itemName+'',		y: 35,		style: {                	color: '#666',                	fontSize: '15px',                },                	},	rangeSelector: {		selected: 1,		inputBoxStyle: {			display: isSmall&&!cLoaded?'none':'block'		}, inputDateFormat: "%e-%b-%y", },	tooltip: { enabled: !isSmall||cLoaded, valueDecimals: 0, headerFormat: ' {point.key} ', xDateFormat: "%A, %e %B %Y", },       navigator: { xAxis: { dateTimeLabelFormats: { day: "%e-%b", week: "%e-%b", month: "%b-%y", year: "%Y", }               },		enabled: !isSmall||cLoaded },       xAxis: [{ lineColor: 'black', tickColor: 'black', dateTimeLabelFormats: { day: "%e-%b", week: "%e-%b", month: "%b-%y", year: "%Y", },               minTickInterval: 864000000, //1 day }],	yAxis: yAxis, series: dataList }); if (!cLoaded&&isSmall) {	cLoaded = true;	getExtraItem; }

}

//loading the chart and additional price info when the page is ready

addOnloadHook(getExtraItem); addOnloadHook(function {	var prices = $('#GEdataprices').html.split(/,\s/);	var data = [];	var curprice;	for (var i=0;i<prices.length;i++) {		if (prices[i].replace(/\s/g,'').length) {			curprice = prices[i].split(':');			data.push(parseInt(curprice[1]));		}	}	var datal = data.length	var curprice = data[datal-1]	var priceDiffs = [		curprice - data[datal-2],		curprice - data[datal-8],		curprice - data[datal-31],		curprice - data[datal-91],		curprice - data[datal-181]	];	var percentDiffs = [		Math.round(priceDiffs[0] / data[datal-2]*1000)/10,		Math.round(priceDiffs[1] / data[datal-8]*1000)/10,		Math.round(priceDiffs[2] / data[datal-31]*1000)/10,		Math.round(priceDiffs[3] / data[datal-91]*1000)/10,		Math.round(priceDiffs[4] / data[datal-180]*1000)/10	];	var dayDiffs = [1,7,30,90,180];	function rg(n) {		return n>0?'green':(n==0?'blue':'red');	}	var itemStats = ' ';	$('#chartProperties').after(itemStats); });

}