Created
September 5, 2011 18:07
-
-
Save philogb/1195578 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* posible implementation en la creacion del treemap... | |
onPlaceLabel: function(domElement, node) { | |
var width = node.getData('width'), | |
height = tm.leaf(node) ? node.getData('height') : tm.config.titleHeight, | |
fontStyle = 'Arial', | |
fontSize = calculateFontSize(node.name, fontStyle, width, height); | |
domElement.style.font = fontSize + 'px ' + fontStyle; | |
} | |
*/ | |
/* La function calcula el tamano correcto de la fuente basado en las lineas ocupadas y en el ancho maximo de una palabra en el texto */ | |
function calculateFontSize(html, fontStyle, width, height) { | |
var startSize = 50, | |
wordsAreThinnerThanWidth = false, | |
testDiv, wordTestDiv, style, wordStyle, offsetHeight, words, word, i, l; | |
if (!calculateFontSize.testDiv) { | |
testDiv = calculateFontSize.testDiv = document.createElement('div'); | |
style = testDiv.style; | |
style.visibility = 'hidden'; | |
style.position = 'absolute'; | |
document.body.appendChild(testDiv); | |
wordTestDiv = calculateFontSize.wordTestDiv = document.createElement('div'); | |
wordStyle = wordTestDiv.style; | |
wordStyle.visibility = 'hidden'; | |
wordStyle.position = 'absolute'; | |
wordStyle.width = 'auto'; | |
document.body.appendChild(wordTestDiv); | |
} | |
wordTestDiv = calculateFontSize.wordTestDiv; | |
wordStyle = wordTestDiv.style; | |
wordStyle.fontSize = startSize + 'px'; | |
wordStyle.fontStyle = fontStyle; | |
testDiv = calculateFontSize.testDiv; | |
testDiv.innerHTML = html; | |
words = html.match(/[^\s]+/g); | |
style = testDiv.style; | |
style.width = width + 'px'; | |
style.fontStyle = fontStyle; | |
style.fontSize = startSize + 'px'; | |
while (startSize > 0 && (testDiv.offsetHeight > height || !wordsAreThinnerThanWidth)) { | |
startSize -= 5; | |
style.fontSize = startSize + 'px'; | |
wordStyle.fontSize = startSize + 'px'; | |
if (!wordsAreThinnerThanWidth) { | |
for (i = 0, l = words.length; i < l; i++) { | |
word = words[i]; | |
wordTestDiv.innerHTML = word; | |
if (wordTestDiv.offsetWidth > width) { | |
break; | |
} | |
} | |
wordsAreThinnerThanWidth = (i == l); | |
} | |
} | |
testDiv.innerHTML = ''; | |
return Math.max(0, startSize); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment