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 item; function addItem { item = $('#extraItem').val; if (item&&item.length) { if ($('#addedItems [data-item="'+item+'"]').length) { alert('That item is already in the graph.') $('#extraItem').val(''); return false; }		$('#extraItem').prop('disabled',true); var req = $.get('/wiki/Exchange:'+item+'/Data?action=render', function(data) {			var prices = data.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]) ]);				}			}			if (chart.series[2].name == 'Volume') {loadChart(true)}			chart.addSeries({name: item, data: data, id: item, lineWidth: 2})			$('#extraItem').prop('disabled',false).val();			if (!$('#addedItems').html.replace(/\s+/,).length) {				$('#addedItems').html('Remove items from graph: '+item+'');			} else {				$('#addedItems').append(', '+item+'');			}		}); req.statusCode({			404: function {				alert("That item doesn't exist on our Grand Exchange database.")				$('#extraItem').prop('disabled',false).val('');			}		}); } else {loadChart} }

function removeGraphItem(item) { chart.get(item).remove(true); chart.redraw $('#addedItems [data-item="'+item+'"]').remove $('#addedItems').html($('#addedItems').html.replace(/,, /g,', ').replace(/, $/,'')) if ($('#addedItems').html.match(/^\s*Remove items from graph: *\s*/)) {$('#addedItems').empty} }

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

//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(noVol) { 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 extremes = []; for (var i=0;i<prices.length;i++) { thisprice = prices[i].split(':'); if (prices[i].replace(/\s/g,'').length) { data.push([ parseInt(thisprice[0])*1000, parseInt(thisprice[1]) ]); if (thisprice[2]&&(!isSmall||cLoaded)) { volumes.push([ parseInt(thisprice[0])*1000, parseFloat(thisprice[2])*1000*1000 ]); }	} }

//generating the dataList

var dataList = [{ name: itemName, data: data, lineWidth: isSmall?2:3 }] if ((!isSmall||cLoaded)&&!noVol) { $('#itemstats').show 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

var yAxis = {}; if (volumes.length&&(!isSmall||cLoaded)&&!noVol) { yAxis = [{ title: { text: 'Price history', offset: 60, 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', offset: 60, style: { color: 'black', fontSize: '12px', },		},		labels: { align: 'right', x: -8, y: 4, },               showEmpty: 0, showLastLabel: 1, offset: 0, lineWidth: 1, lineColor: '#E0E0E0', height: 75, top: 325, min: 0 }]; } 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', offset: 60, 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: {                rangeSelectorZoom: isSmall?'':'Zoom',                numericSymbols: ['k', 'M', 'B', 'T', 'Qd', 'Qt'],	} }); //generating the chart window.chart = new Highcharts.StockChart({	chart: {		renderTo: cLoaded&&isSmall?'GEpopupchart':'GEdatachart',		backgroundColor: 'none'	},	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: 2, //3-month zoom		inputBoxStyle: {			display: isSmall&&!cLoaded?'none':'block'		},                inputDateFormat: "%e-%b-%y",                buttons: [{                        type: 'month', count: 1, text: '1m' }, {	               type: 'month', count: 2, text: '2m' }, {	               type: 'month', count: 3, text: '3m' }, {	               type: 'month', count: 6, text: '6m' }, {	               type: 'year', count: 1, text: '1y' }, {	               type: 'all', text: 'All' }]	},	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", },                       minTickInterval: 24 * 3600 * 1000, //1 day },		enabled: !isSmall||cLoaded },	credits: { enabled: isSmall&&!cLoaded, href: 'javascript:popupChart', text: 'Enlarge chart', position: { y: -2 },		style: { color: '#666', fontSize: '11px' }	},       xAxis: [{ lineColor: '#666', tickColor: '#666', dateTimeLabelFormats: { day: "%e-%b", week: "%e-%b", month: "%b-%y", year: "%Y", },               minTickInterval: 24 * 3600 * 1000, //1 day events: { //adjusting the Y-axis extremes (at different zoom levels) afterSetExtremes: function { extremes = this.chart.yAxis[0].getExtremes; this.chart.yAxis[0].setExtremes(extremes.dataMin, extremes.dataMax); if (volumes.length&&(!isSmall||cLoaded)&&!noVol) { extremes = this.chart.yAxis[1].getExtremes; this.chart.yAxis[1].setExtremes(extremes.dataMin, extremes.dataMax * 0.35); }                   }                },        }],	yAxis: yAxis, series: dataList });

//adjusting the Y-axis extremes (initial load) extremes = this.chart.yAxis[0].getExtremes; this.chart.yAxis[0].setExtremes(extremes.dataMin, extremes.dataMax); if (volumes.length&&(!isSmall||cLoaded)&&!noVol) { extremes = this.chart.yAxis[1].getExtremes; this.chart.yAxis[1].setExtremes(extremes.dataMin, extremes.dataMax * 0.35); }

if (!cLoaded&&isSmall) { cLoaded = true; loadChart; }

}

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

addOnloadHook(loadChart); if (isSmall&&cLoaded) { 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); }); }

}