From 92ba97a62c3e73e4d48014524bacf8db9098e42f Mon Sep 17 00:00:00 2001 From: "tien.nguyenv" Date: Thu, 8 Sep 2016 20:54:51 +0700 Subject: [PATCH 1/2] Add tooltip #6 --- public/vis/components/elements/rect.js | 98 ++++++++++++++++++++------ 1 file changed, 76 insertions(+), 22 deletions(-) diff --git a/public/vis/components/elements/rect.js b/public/vis/components/elements/rect.js index 3f33168..4a7f53d 100644 --- a/public/vis/components/elements/rect.js +++ b/public/vis/components/elements/rect.js @@ -1,5 +1,4 @@ var d3 = require('d3'); - function rect() { var color = d3.scale.category10(); var x = function (d) { return d.x; }; @@ -14,30 +13,85 @@ function rect() { var strokeWidth = 0; var fillOpacity = 1; var strokeOpacity; + var yTitle = ''; + var xTitle = ''; + var legendTitle = ''; + var tip = d3.select('body') + .append('div').classed('verticesTooltip', true) + .attr('opacity', 0) + .style({ + 'position': 'absolute', + 'color': 'black', + 'font-size': '10px', + 'width': '100px', + 'height': 'auto', + 'padding': '5px', + 'border': '2px solid gray', + 'border-radius': '5px', + 'pointer-events': 'none', + 'opacity': '0', + 'background': '#f4f4f4' + }); + + var _verticaleTooltipShow = function (d) { + debugger; + var strHtml = ""; + if(d.data!= undefined && (d.row != null || d.row != undefined)){ + strHtml += ''+ yTitle +': ' + d.row + '
'; + } + if(d.data!= undefined && (d.col != null || d.col != undefined)){ + strHtml += ''+ xTitle +': ' + d.col + '
'; + } + if(d != undefined && (d.value != null || d.value != undefined)){ + strHtml += ''+ legendTitle +': ' + parseFloat(d.value).toFixed(2) + '
'; + } + tip.html(strHtml) + .style('left', (d3.event.pageX) + 'px') + .style('top', (d3.event.pageY) + 'px') + .style('z-index',3000); + } + + var _verticesTooltip = function (d) { + if(d.data != undefined){ + var title = d3.selectAll('text.title'); + xTitle = title[0][0].textContent || 'Column'; + yTitle = title[0][1].textContent || 'Row'; + legendTitle = d3.selectAll('text.text')[0][0].textContent || 'Metric'; + _verticaleTooltipShow(d); + tip.style('opacity', 0.9); + } + }; + + // hide tooltip of vertices + var _verticesTooltipHide = function () { + tip.style('opacity', 0); + }; function element(selection) { selection.each(function (data) { - var cells = d3.select(this).selectAll('rect.' + cssClass) - .data(data); - - cells.exit().remove(); - - cells.enter().append('rect') - .attr('class', cssClass); - - cells - .attr('x', x) - .attr('y', y) - .attr('rx', rx) - .attr('ry', ry) - .attr('width', width) - .attr('height', height) - .style('fill', fill) - .style('fill-opacity', fillOpacity) - .style('stroke', stroke) - .style('stroke-width', strokeWidth) - .style('stroke-opacity', strokeOpacity); - }); + var cells = d3.select(this).selectAll('rect.' + cssClass) + .data(data) + .on('mouseover', _verticesTooltip) + .on('mousemove',_verticaleTooltipShow) + .on('mouseout', _verticesTooltipHide); + + cells.exit().remove(); + + cells.enter().append('rect') + .attr('class', cssClass); + cells + .attr('x', x) + .attr('y', y) + .attr('rx', rx) + .attr('ry', ry) + .attr('width', width) + .attr('height', height) + .style('fill', fill) + .style('fill-opacity', fillOpacity) + .style('stroke', stroke) + .style('stroke-width', strokeWidth) + .style('stroke-opacity', strokeOpacity); + }); } function colorFill(d, i) { From f0099c98e5298fb647582991e1a4cf550285130f Mon Sep 17 00:00:00 2001 From: scorpiovn Date: Thu, 8 Sep 2016 21:00:16 +0700 Subject: [PATCH 2/2] Update rect.js auto for width --- public/vis/components/elements/rect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/vis/components/elements/rect.js b/public/vis/components/elements/rect.js index 4a7f53d..1e697cd 100644 --- a/public/vis/components/elements/rect.js +++ b/public/vis/components/elements/rect.js @@ -23,7 +23,7 @@ function rect() { 'position': 'absolute', 'color': 'black', 'font-size': '10px', - 'width': '100px', + 'width': 'auto', 'height': 'auto', 'padding': '5px', 'border': '2px solid gray',