Jump to content

User:Cobaltcigs/HistoryGraph.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cobaltcigs (talk | contribs) at 09:36, 11 October 2019 (js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function AddGraphLink() {
	if(wgAction != "history") return;
	var div = document.getElementById("histlegend");
	if(div == null) return;
	var ul = div.getElementsByTagName("ul")[0];
	if(ul == null) return;
	ul.innerHTML += '<li><a onclick="ShowGraph()">Show graph</a></li>';
	}
function HideGraph() {
	var old = document.getElementById("history-graph");
	if(old) old.style.visibility = "hidden";
	}
function ShowGraph() {
	var old = document.getElementById("history-graph");
	if(old) {
		old.style.visibility = "visible";
		return;
		}
	var width = 640, height = 480, color = "red", stroke_width = 2;
	var b = [];
	var e = document.getElementsByClassName("history-size");
	if(e.length == 0) return;
	for(var i = 0; i < e.length; i++) {
		var v = parseInt(e[i].innerText.replace(/\D/g, ""));
		if(isNaN(v)) continue;
		b.push(v);
		}
	if(b.length == 1) b.push(b[0]); // flat line for single rev
	b.reverse();
	var max = Math.max.apply(null, b), min = Math.min.apply(null, b);
	var dx = width/(b.length - 1);
	var db = max-min;
	var dy = height/db;
	var om = Math.floor(Math.log10(db)) - 1;
	var dd = Math.pow(10, om);
	var da = Math.ceil(min/dd)*dd;
	var db = Math.floor(max/dd)*dd;
	var pts = [];
	for(var i = 0; i < b.length; i++)
		pts.push(`${(i * dx)}, ${height - (b[i]-min) * dy}`);
	var gpath = [], gtext = [];
	for(var i = da; i <= db; i += dd) {
		var y = height - (i - min) * dy;
		gpath.push(`<path d="M0,${y} L${width},${y}" />`);
		gtext.push(`<text x="40" y="${y}">${i}</text>`);
		}
	var path = `<path d="M${pts.join(' L')}" stroke="${color}" stroke-width="${stroke_width}" fill="none" />`;
	var g1 = `<g stroke="gray" stroke-width="1" fill="none">${gpath.join('')}</g>`;
	var g2 = `<g font-family="monospace" font-size="12px" text-align="right" color="black">${gtext.join('')}</g>`;
	var svg = `<svg style="width:${width}px; height:${height}px;">${g1}${path}${g2}</svg>`;
	var divStyle = `position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); display:inline-block; border: 1px solid black; background-color:white;`;
	var xbox = `<button style="float:right; height: 25px; width: 25px; font-size:20px; line-height:100%; font-weight: bolder; background-color:red; color:white;" onclick="HideGraph()">×</button>`;
	var div = `<div id="history-graph" style="${divStyle}">${xbox}${svg}</div>`;
	document.body.innerHTML += div;
	}