From b8540821183cf6c9dc83486eaefbbdd11a429283 Mon Sep 17 00:00:00 2001 From: Bob Monteverde Date: Thu, 21 Jun 2012 15:27:50 -0400 Subject: [PATCH] Slowely getting rid of options object.. down to jsut the columns --- examples/indentedtree.html | 4 +- src/models/indentedTree.js | 116 ++++++++++++++++++++++++------------- 2 files changed, 78 insertions(+), 42 deletions(-) diff --git a/examples/indentedtree.html b/examples/indentedtree.html index e301e49..d1a3c3f 100644 --- a/examples/indentedtree.html +++ b/examples/indentedtree.html @@ -108,9 +108,7 @@ var testOptions = { width: '25%', type: 'text' } - ], - header: true, - noResults: "Nothing portfolios found." + ] }; diff --git a/src/models/indentedTree.js b/src/models/indentedTree.js index f97d95e..504c279 100644 --- a/src/models/indentedTree.js +++ b/src/models/indentedTree.js @@ -2,21 +2,26 @@ //TODO: Finish merging this chart into the NVD3 style! nv.models.indentedTree = function() { //Default Settings - var margin = {top: 0, right: 0, bottom: 0, left: 0}, + var margin = {top: 0, right: 0, bottom: 0, left: 0}, //TODO: implement, maybe as margin on the containing div width = 960, height = 500, - color = d3.scale.category20().range(), // array of colors to be used in order - id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one + color = d3.scale.category20().range(), + id = Math.floor(Math.random() * 10000), + header = true, + noResultsText = 'No Results found.' childIndent = 20, - berHeight = 20, - options = {columns:[{key:'key', label: 'Name', type:'text'}]}, - tableClass = null; + options = {columns:[{key:'key', label: 'Name', type:'text'}]}, //TODO: get rid of + tableClass = null, + iconOpen = 'images/grey-plus.png', //TODO: consider removing this and replacing with a '+' or '-' unless user defines images + iconClose = 'images/grey-minus.png'; + var dispatch = d3.dispatch('elementClick', 'elementDblclick', 'elementMouseover', 'elementMouseout'); + function chart(selection) { selection.each(function(data) { - var availableWidth = width - margin.left - margin.right, + var availableWidth = width - margin.left - margin.right, //TODO: decide if there is any use for these availableHeight = height - margin.top - margin.bottom; @@ -30,7 +35,7 @@ nv.models.indentedTree = function() { .size([height, childIndent]); //Not sure if this is needed now that the result is HTML - if (!data[0].key) data[0].key = options.noResults || "Nothing to show"; + if (!data[0].key) data[0].key = noResultsText; var nodes = tree.nodes(data[0]); @@ -41,12 +46,8 @@ nv.models.indentedTree = function() { var table = wrap.select('table').attr('width', '100%').attr('class', tableClass); - //clear the container, start from scratch - //d3.select("#" + container + " .indentedtree").remove(); - - - if (options.header) { + if (header) { var thead = tableEnter.append('thead'); var theadRow1 = thead.append('tr'); @@ -61,7 +62,6 @@ nv.models.indentedTree = function() { }); } - //tableEnter.append('tbody'); var tbody = table.selectAll('tbody') .data(function(d) {return d }); @@ -69,59 +69,57 @@ nv.models.indentedTree = function() { - - //compute max generations depth = d3.max(nodes, function(node) { return node.depth }); - tree.size([height, depth * childIndent]); + tree.size([height, depth * childIndent]); //TODO: see if this is necessary at all // Update the nodes… - var node = tbody.selectAll("tr") - //.data(nodes, function(d) { return d.id || (d.id = ++i); }) - .data(function(d) { return d }, function(d) { return d.id || (d.id == ++i)}) - .style('display', 'table-row'); + var node = tbody.selectAll('tr') + .data(function(d) { return d }, function(d) { return d.id || (d.id == ++i)}); + //.style('display', 'table-row'); //TODO: see if this does anything node.exit().remove(); - +/* if (options.click) node.on('click', options.click); +*/ - node.select("img.treeicon") - .attr("src", icon) + node.select('img.treeicon') + .attr('src', icon) .classed('folded', folded); - var nodeEnter = node.enter().append("tr"); + var nodeEnter = node.enter().append('tr'); options.columns.forEach(function(column, index) { - var nodeName = nodeEnter.append("td") - .style("padding-left", function(d) { return (index ? 0 : d.depth * childIndent + 12 + (icon(d) ? 0 : 16)) + 'px' }, 'important') //TODO: check why I did the ternary here + var nodeName = nodeEnter.append('td') + .style('padding-left', function(d) { return (index ? 0 : d.depth * childIndent + 12 + (icon(d) ? 0 : 16)) + 'px' }, 'important') //TODO: check why I did the ternary here .style('text-align', column.type == 'numeric' ? 'right' : 'left'); if (index == 0) { - nodeName.append("img") + nodeName.append('img') .classed('treeicon', true) .classed('folded', folded) .attr('src', icon) - .style("width", '14px') - .style("height", '14px') + .style('width', '14px') + .style('height', '14px') .style('padding', '0 1px') .style('display', function(d) { return icon(d) ? 'inline-block' : 'none'; }) - .on("click", click); + .on('click', click); } - nodeName.append("span") + nodeName.append('span') .attr('class', d3.functor(column.classes) ) .text(function(d) { return column.format ? column.format(d) : (d[column.key] || '-') }); if (column.showCount) - nodeName.append("span") + nodeName.append('span') .attr('class', 'childrenCount') .text(function(d) { return ((d.values && d.values.length) || (d._values && d._values.length)) ? @@ -130,17 +128,45 @@ nv.models.indentedTree = function() { }); - tbody.selectAll("tr") - //.data(nodes, function(d) { return d.id || (d.id = ++i); }) - .order() - //.classed('highlight', function(_,i) { return i % 2;}); //used to alternate background color - if (column.click) nodeName.select('span').on('click', column.click); }); + node + .order() + .on('click', function(d) { + dispatch.elementClick({ + row: this, //TODO: decide whether or not this should be consistent with scatter/line events + data: d, + pos: [d.x, d.y] + }); + }) + .on('dblclick', function(d) { + dispatch.elementDblclick({ + row: this, + data: d, + pos: [d.x, d.y] + }); + }) + .on('mouseover', function(d) { + dispatch.elementMouseover({ + row: this, + data: d, + pos: [d.x, d.y] + }); + }) + .on('mouseout', function(d) { + dispatch.elementMouseout({ + row: this, + data: d, + pos: [d.x, d.y] + }); + }); + + + // Toggle children on click. function click(d, _, unshift) { @@ -173,7 +199,7 @@ nv.models.indentedTree = function() { function icon(d) { - return (d._values && d._values.length) ? "images/grey-plus.png" : (d.values && d.values.length) ? "images/grey-minus.png" : ""; + return (d._values && d._values.length) ? iconOpen : (d.values && d.values.length) ? iconClose : ''; } function folded(d) { @@ -225,6 +251,18 @@ nv.models.indentedTree = function() { return chart; }; + chart.header = function(_) { + if (!arguments.length) return header; + header = _; + return chart; + }; + + chart.noResultsText = function(_) { + if (!arguments.length) return noResultsText; + noResultsText = _; + return chart; + }; + chart.options = function(_) { if (!arguments.length) return options; options = _;