Supporting utilities for graphs and scripts
Last active
January 1, 2020 01:15
-
-
Save pinsterdev/a2fcc47fae884342e190 to your computer and use it in GitHub Desktop.
Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Standard blue colour set. | |
*/ | |
function pinColours() { | |
return [ "#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", | |
"#ff8c00", "#ffb010", "#ffff20", "#99ff33", "#ff4000" ]; | |
} | |
/** | |
* Gist data downloaded. | |
*/ | |
var gistData = {}; | |
/** | |
* Array of Google sheets downloaded. | |
*/ | |
var googleSheets = []; | |
function parseParamWithDefault(param, values) { | |
var obj = parseQueryString(); | |
return (typeof obj[param] == 'undefined')? 0 : | |
(values.indexOf(obj[param]) == -1)? 0: values.indexOf(obj[param]); | |
} | |
function parseQueryString() { | |
var str = window.location.search; | |
var objURL = {}; | |
str.replace( | |
new RegExp( "([^?=&]+)(=([^&]*))?", "g" ), | |
function( $0, $1, $2, $3 ){ | |
objURL[ $1 ] = $3; | |
} | |
); | |
return objURL; | |
} | |
/** | |
* Callback function when downloading Gist data as JSONP. | |
* @param json downloaded Gist structure | |
*/ | |
function gistDataLoaded(json) { | |
gistData = json.data.files; | |
} | |
function extractGistData(content) { | |
var data = d3.csv.parse(content); | |
var rowName = content.substring(0,content.indexOf(",")); | |
var colNames = d3.keys(data[0]).filter(function(key) { return key !== rowName; }); | |
return {data: data, colNames: colNames, rowName: rowName}; | |
} | |
function extractGistDataSubset(content, rowStart, rowEnd) { | |
var data = d3.csv.parseRows(content); | |
data = data.slice(rowStart, rowEnd); | |
content = d3.formatRows(data); | |
return extractGistData(content); | |
} | |
/** | |
* Callback function when downloading Google sheets as JSONP. | |
* @param json downloaded sheet structure | |
*/ | |
function sheetLoaded(json) { | |
googleSheets.push(extractSheetData(json, 0, 0)); | |
} | |
function extractSheetData(json, rowStart, rowEnd) { | |
var cells = json.feed.entry; | |
var row = (rowStart > 0)? rowStart : 1, col = 1, cell = 0, csv = ""; | |
for (; (row > +cells[cell].gs$cell.row) && (cell < cells.length); cell++); | |
var rowName = cells[cell].gs$cell.$t; | |
for (; cell < cells.length; cell++) { | |
var nextRow = +cells[cell].gs$cell.row; | |
if ((rowEnd > 0) && (nextRow >= rowEnd)) { | |
break; | |
} | |
if (row != nextRow) { | |
row = nextRow; | |
col = 1; | |
csv = csv + '\n'; | |
} | |
if (col++ > 1) csv = csv + ','; | |
csv = csv + '"' + cells[cell].gs$cell.$t + '"'; | |
} | |
var data = d3.csv.parse(csv); | |
var colNames = d3.keys(data[0]).filter(function(key) { return key !== rowName; }); | |
return {json: json, id: json.feed.id.$t, data: data, colNames: colNames, rowName: rowName}; | |
} | |
function transposeSheet(sheet) { | |
var transpose = []; | |
var transposeCols = []; | |
sheet.colNames.forEach(function (d) { | |
var row = new Object(); | |
row[sheet.rowName] = d; | |
transpose.push(row); | |
}); | |
sheet.data.forEach(function (d, j) { | |
transposeCols[j] = d[sheet.rowName]; | |
sheet.colNames.forEach(function (c,i) { | |
transpose[i][d[sheet.rowName]]=d[c]; | |
}); | |
}); | |
return {data: transpose, rowName : sheet.rowName, colNames : transposeCols}; | |
} | |
/** | |
* Read markdown from README.md, convert to html and set inner html of supplied | |
* element. Requires showdown.js for markdown conversion. | |
* | |
* @param elem | |
* the element that will receive the html | |
*/ | |
function readme(elem) { | |
d3.text("README.md", function(error, rawText) { | |
if (error) | |
throw error; | |
var converter = new showdown.Converter({tables: true}); | |
elem.innerHTML = converter.makeHtml(rawText); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment