Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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