Skip to content

Instantly share code, notes, and snippets.

@joshjordan
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshjordan/9645b43357df13528ec3 to your computer and use it in GitHub Desktop.
Save joshjordan/9645b43357df13528ec3 to your computer and use it in GitHub Desktop.
nv.chartScripts = []
for(var model in nv.models)
{
if(model.match(/[cC]hart$/)) {
(function() {
var currentModel = model;
var chartFunction = nv.models[currentModel];
nv.models[currentModel] = function() {
var chart = chartFunction();
chart.apply = function() {
var d3Selection = arguments[0];
var data = d3Selection[0][0].__data__;
var chartId = "chart-" + new Date().getTime();
var script = "<svg id='" + chartId + "'></svg><script type='text/javascript'>";
script += "nv.addGraph({generate: function() {";
script += "var data = " + JSON.stringify(data) + ";";
script += "var chart = nv.models." + currentModel + "();";
script += "chart";
var optionNames = Object.getOwnPropertyNames(this._options || {}).concat(this._inherited || [])
for(var i = 0; i < optionNames.length && (property = optionNames[i]); i++)
{
ignoredProperties = ['labelFormat', 'valueFormat']
if(ignoredProperties.indexOf(property) == -1)
{
var value = this[property]();
if(property == 'color')
{
if(typeof(value) == 'function')
{
value = [];
for(var i = 0; i < data.length; i++)
{
value.push(this.color()({}, i))
}
}
}
if(value && typeof(value) === 'object')
{
value = JSON.stringify(value)
}
else if(typeof(value) === 'string')
{
value = '"' + value + '"';
}
script += "." + property + "(" + value + ")";
}
}
script += ";";
script += "d3.select('#" + chartId + "').datum(data).call(chart);}});";
script += "<\/script>";
nv.chartScripts.push(script);
return this(d3Selection);
}
return chart;
}
})();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment