MediaWiki:Common.js/GECharts.js

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

//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], avg(list)]); }		list[i % amt] = this[i][1] }	return avgs; }

function avg(arr) { var tot = 0 for (var i=0;i<arr.length;i++) { tot += parseFloat(arr[i])?parseFloat(arr[i]):0; }	tot = Math.round(tot); return Math.round(tot/arr.length); }

if ($('#GEdataprices').length) {

//chart-related general functions

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; $('#GEpopupchart').height($('#GEchartpopup').height - $('#chartProperties').height - 10); getExtraItem; }

var timeOut = null; window.onresize = function{ if (timeOut != null) clearTimeout(timeOut); timeOut = setTimeout(function {		$('#GEpopupchart').height($('#GEchartpopup').height - $('#chartProperties').height - 10);		getExtraItem;	}, 250); };

//setting up the form and chart elements

if (isSmall) { $('#GEdatachart').after('\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;

function loadChart(item2) { var itemName = $('#GEdataprices').attr('data-item'); itemName = itemName || wgPageName.replace(/_/g,' ').split('/')[0].replace('Exchange:',''); var prices = $('#GEdataprices').html.split(/,\s/); var 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: 3 }] 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: '7-day average',			data: data.avg(7),			lineWidth: 2,			dashStyle: 'shortdash',		},{			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 = {}; var plotLines = [{ color: 'orange', dashStyle: 'longdash', width: isSmall?1:1.5, value: minmax[0], label: { align: 'right', x: 0, y: 12, text: '30-day min'+(isSmall?'':'imum')+': '+minmax[0] },	//zIndex: 6 }, {	color: 'orange', dashStyle: 'longdash', width: isSmall?1:1.5, value: minmax[1], label: { align: 'right', x: 0, y: -5, text: '30-day max'+(isSmall?'':'imum')+': '+minmax[1] },	//zIndex: 6 }] var chartHeight = $(cLoaded&&isSmall?'#GEpopupchart':'#GEdatachart').height; 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, },               showLastLabel: 1, height: chartHeight*2/5, lineWidth: 1, lineColor: '#E0E0E0', plotLines: plotLines, }, {		title: { text: 'Trade volume', style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               showLastLabel: 1, height: chartHeight/5, top: chartHeight*2/5+100, offset: 0, lineWidth: 1, lineColor: '#E0E0E0', }]; } else if (isSmall||cLoaded) { yAxis = { labels: { align: 'right', x: -8, y: 4, },               showLastLabel: 1, lineWidth: 1, lineColor: '#E0E0E0', plotLines: plotLines } } else { yAxis = { title: { text: 'Price history', style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               showLastLabel: 1, lineWidth: 1, lineColor: '#E0E0E0', plotLines: item2&&item2.length?null:plotLines }       } 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'	},	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", }               }        },        xAxis: [{ lineColor: 'black', tickColor: 'black', dateTimeLabelFormats: { day: "%e-%b", week: "%e-%b", month: "%b-%y", year: "%Y", },       }],	yAxis: yAxis, series: dataList }); if (!cLoaded&&isSmall) {	cLoaded = true;	getExtraItem; }

}

//loading the chart when the page is ready

$(function {getExtraItem});

}