Extension:Plotters/Plotkit scripts

From MediaWiki.org

Jump to: navigation, search

Please feel free to add your own scripts, or to improve these.

Contents

[edit] MediaWiki:Plotters-definition for the below scripts

== Plotkit-scripts ==

* bar|tofloat.js|addticks.js|plotkitlabels.js|bar.js
* pie|tofloat.js|addticks.js|plotkitlabels.js|pie.js
* line|tofloat.js|addticks.js|plotkitlabels.js|line.js

== Preprocessors ==

* labelrow|labelrow.js
* labelcolumn|labelcolumn.js

[edit] Helpers

[edit] tofloat.js

The extension sends all data and arguments as strings. Some scripts require floats, not strings. This will turn a single column of data from strings to floats. This should be improved to convert all data.

function plotter_tofloat( data ) {
    for ( var i = 0; i < data.length; ++i ) {
        num = "" + data[i];
        data[i] = parseInt(num.replace(",", ""));
    }
}

[edit] addticks.js

Plotkit requires data to be in a format like so:

0,1
1,1.2
2,3484
3,1
4,1

Notice that the first column of data is sequential, this helper script will transform data like:

1
1.2
3483
1
1

into the format above.

function plotter_addticks( data ) {
    for ( var i = 0; i < data.length; ++i ) {
        temp = [i];
        data[i] = temp.concat(data[i]);
    }
}

[edit] plotkitlabels.js

This script will take labels given as an argument and convert them into a format that plotkit wants:

function plotter_plotkitlabels( labels ) {
    for ( var i = 0; i < labels.length; ++i ) {
        labels[i] = { v:i, label:labels[i] };
    }
}

[edit] Scripts

[edit] bar.js

This is a simple bar graph. This script should be improved to not require labels (at minimum):

function plotter_bar_draw( name, data, labels, arguments ) {
    plotter_tofloat( data );
    plotter_addticks( data );
    plotter_plotkitlabels( labels );
    var options = { "xTicks": labels };
    var layout = new PlotKit.Layout("bar", options);
    layout.addDataset("data", data);
    layout.evaluate();
    var canvas = MochiKit.DOM.getElement(name);
    var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
    plotter.render();
}

[edit] pie.js

This is a simple bar graph. This script should be improved to not require labels (at minimum):

function plotter_pie_draw( name, data, labels, arguments ) {
    plotter_tofloat( data );
    plotter_addticks( data );
    plotter_plotkitlabels( labels );
    var options = { "xTicks": labels, "pieRadius": arguments[0] };
    var layout = new PlotKit.Layout("pie", options);
    layout.addDataset("data", data);
    layout.evaluate();
    var canvas = MochiKit.DOM.getElement(name);
    var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
    plotter.render();
}

[edit] line.js

This is a simple bar graph. This script should be improved to not require labels (at minimum):

function plotter_line_draw( name, data, labels, arguments ) {
    plotter_tofloat( data );
    plotter_addticks( data );
    plotter_plotkitlabels( labels );
    var options = { "xTicks": labels };
    var layout = new PlotKit.Layout("line", options);
    layout.addDataset("data", data);
    layout.evaluate();
    var canvas = MochiKit.DOM.getElement(name);
    var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
    plotter.render();
}

[edit] Preprocessors

[edit] labelcolumn.js

This preprocessor script removes the first column of data, and uses it as labels.

function plotter_labelcolumn_process( name, data, labels, arguments ) {
    for ( var i = 0; i < data.length; ++i ) {
        line = data[i];
        labels[i] = line.shift();
    }
}

[edit] labelrow.js

This preprocessor script removes the first roe of data, and uses it as labels.

function plotter_labelrow_process( name, data, labels, arguments ) {
    labels.push(data.shift());
}