User:Cobaltcigs/HistoryGraph.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Cobaltcigs/HistoryGraph. |
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;
}