Last active
December 10, 2015 07:48
-
-
Save roundrobin/4403776 to your computer and use it in GitHub Desktop.
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
function Tooltip(config){ | |
this.x = config["x"] || 0; | |
this.y = config["y"] || 0; | |
this.width = config["width"] || 0; | |
this.height = config["height"] || 0; | |
this.padding = config["padding"] || {"top": 0, "bottom": 0, "left": 0, "right": 0}; | |
this.onclickCallback = config["onclickCall"]; | |
this.container = config["appendTo"]; | |
this.style = config["style"] || {}; | |
this.contentFill = config["tooltip-content"]; | |
this.container = this.container.append("g") | |
.attr("class","tooltip-container") | |
.attr("transform","translate("+[this.x,this.y]+")") | |
.on("click",config["onclick"]); | |
this.bgRect = this.container.append("svg:rect") | |
.attr("width", this.width) | |
.attr("height", this.height) | |
.attr("rx",10) | |
.attr("ry",10) | |
.attr("x",0) | |
.attr("y",0) | |
.attr("fill","#201F24") | |
.classed("bgRect",true) | |
.style(this.style); | |
this.arrow = this.container.append('svg:path') | |
.attr("fill","#201F24") | |
.attr("d","m0,0 l35,0 l-17.5,20Z") | |
.attr("fill","#201F24") | |
.attr("transform","translate("+[(this.width/2)-8,this.height]+")") | |
.style(this.style) | |
this.content = this.container.append("g") | |
.attr("class","tooltip-content") | |
this.contentFill(this.content, config)() | |
this.render(); | |
this.hide(); | |
} | |
Tooltip.prototype.render = function(){ | |
var currentScale = 1 | |
var bb = this.content.node().getBBox(); | |
var width = bb.width; | |
var height = bb.height; | |
var boxHeight = this.width - this.padding["left"] - this.padding["right"]; | |
var boxWidth = this.height - this.padding["top"] - this.padding["bottom"]; | |
var currentSF = 1; | |
var maxWidth = (boxWidth / width ); | |
var maxHeight = (boxHeight /height); | |
var max = Math.min(maxWidth, maxHeight); | |
var scaling = (currentSF * max); | |
var old_transform = ""; | |
if(this.content.attr("transform") != null){ | |
old_transform = this.content.attr("transform"); | |
} | |
this.content.attr("transform","scale("+scaling+")"); | |
var bb = this.content.node().getBoundingClientRect(); | |
this.content.attr("transform","translate("+(this.width/2 - bb.width/2)+","+(this.height/2 - bb.height/2)+")scale("+scaling+")"); | |
}; | |
Tooltip.prototype.show = function(){ | |
this.container | |
.attr("transform","translate("+[this.x+this.width/2,this.y=this.height]+")rotate(0)scale(1)") | |
.attr("fill-opacity",0) | |
.transition() | |
.duration(1000) | |
.attr("transform","translate("+[this.x,this.y]+")rotate(0)scale(1)") | |
.attr("fill-opacity",1) | |
}; | |
Tooltip.prototype.hide = function(){ | |
this.container | |
.attr("transform","translate("+[this.x,this.y]+")rotate(0)scale(1)") | |
.attr("fill-opacity",1) | |
.transition() | |
.duration(1000) | |
.attr("transform","translate("+[this.x+this.width/2,this.y=this.height]+")rotate(0)scale(1)") | |
.attr("fill-opacity",0) | |
}; | |
Tooltip.prototype.destroy = function(){ | |
this.container.remove(); | |
}; | |
function createTooltip(svg,x,y){ | |
var onclickCall = function(){ | |
console.log("click"); | |
} | |
var content = function(appendTo, config){ | |
return function(d,i){ | |
var text = appendTo.append('svg:text') | |
.attr("fill", "#FFFFFF") | |
.attr("x", 0) | |
.attr("y", 40) | |
.attr("font-size", 29) | |
.attr("font-family", "Arial") | |
.attr("text-anchor", "start") | |
; | |
text.append("tspan") | |
.attr("dy",0) | |
.attr("x",0) | |
.text("whitney houston"); | |
text.append("tspan") | |
.attr("dy",40) | |
.attr("x",0) | |
.text("Wikipedia link:"); | |
text.append("tspan") | |
.attr("dy",40) | |
.attr("x",0) | |
.attr("fill","#2632EE") | |
.attr("text-decoration","underline") | |
.text("here") | |
.on("click",function(){ | |
var href = "http://www.spiegel.de" | |
window.open(''+href+'', '_blank', 'width=600, height=300'); | |
}) | |
} | |
} | |
var config = {"width": 277, "height": 243, | |
"x" : x, "y": y, "appendTo": svg, | |
"tooltip-content" : content, "onclick": onclickCall, | |
"padding": {"top": 0, "bottom": 0, "left": 10, "right": 0}, | |
"style" : {"fill":"#A3A3A3"} | |
} | |
var tooltip = new Tooltip(config); | |
return tooltip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment