Change text size calculation algorithm

parent 1a704760
......@@ -159,29 +159,6 @@ function c3_axis(d3, params, $$) {
var tickPosition = scale(d) + (tickCentered ? 0 : tickOffset);
return range[0] < tickPosition && tickPosition < range[1] ? innerTickSize : 0;
}
// Seek a specific value in the stylesheets of a document
function getStyleValue(selector, property) {
var css, rule, sheet, value;
for (var styleId = 0; styleId < document.styleSheets.length; styleId++) {
sheet = document.styleSheets[styleId];
for (var ruleId = 0; ruleId < sheet.cssRules.length; ruleId++) {
rule = sheet.cssRules[ruleId];
css = rule.cssText;
if (css.indexOf(selector) !== -1) {
if (css.indexOf(property) !== -1) {
try {
value = css.split(property)[1].split(':')[1].split(';')[0];
return value.trim();
} catch (e) {
// Error will be caught in case of incorrect CSS
return undefined;
}
}
}
}
}
}
text = tick.select("text");
tspan = text.selectAll('tspan')
......@@ -195,14 +172,13 @@ function c3_axis(d3, params, $$) {
tspan.enter().append('tspan');
tspan.exit().remove();
var fontSize;
var fontSize = parseFloat(getStyleValue('.c3-axis-y .tick', 'font-size'));
tspan.text(function (d) {
fontSize = parseFloat(getStyleValue('.c3-axis-y .tick', 'font-size'));
if (d.splitted.length) {
$$.config.maxTickTextWidth = Math.max(
$$.config.maxTickTextWidth || 0,
d.splitted.length * fontSize / $$.config.tick_text_h_to_w
d.splitted.length * fontSize / $$.config.text_h_to_w
);
}
return d.splitted;
......
......@@ -196,7 +196,8 @@ c3_chart_internal_fn.getDefaultConfig = function () {
tooltip_init_position: {top: '0px', left: '50px'},
// default additional tick padding
tick_text_padding: 0,
tick_text_h_to_w: 1.76,
// change this when you change font-family!
text_h_to_w: 1.76,
// used when size is not available yet
default_tick_width: 30,
// caching
......
......@@ -43,6 +43,30 @@ c3_chart_fn = c3.chart.fn;
c3_chart_internal_fn = c3.chart.internal.fn;
// Seek a specific value in the stylesheets of a document
function getStyleValue(selector, property) {
var css, rule, sheet, value;
for (var styleId = 0; styleId < document.styleSheets.length; styleId++) {
sheet = document.styleSheets[styleId];
for (var ruleId = 0; ruleId < sheet.cssRules.length; ruleId++) {
rule = sheet.cssRules[ruleId];
css = rule.cssText;
if (css.indexOf(selector) !== -1) {
if (css.indexOf(property) !== -1) {
try {
value = css.split(property)[1].split(':')[1].split(';')[0];
return value.trim();
} catch (e) {
// Error will be caught in case of incorrect CSS
return undefined;
}
}
}
}
}
}
c3_chart_internal_fn.init = function () {
var $$ = this, config = $$.config;
......
......@@ -137,6 +137,8 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
options = options || {};
withTransition = getOption(options, "withTransition", true);
withTransitionForTransform = getOption(options, "withTransitionForTransform", true);
$$.clearLegendItemTextBoxCache();
function legendText(id){
return isDefined(config.data_names[id]) ? config.data_names[id] : isDefined(config.data_names.id) ? config.data_names.id : id;
......@@ -144,7 +146,7 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
function getTextBox(textElement, id) {
if (!legendItemTextBox[id]) {
legendItemTextBox[id] = $$.getTextRect(textElement.textContent, CLASS.legendItem);
legendItemTextBox[id] = $$.getTextRect(textElement.textContent, '.c3-legend-item');
}
return legendItemTextBox[id];
}
......
......@@ -48,25 +48,12 @@ c3_chart_internal_fn.redrawText = function (xForText, yForText, forFlow, withTra
];
};
c3_chart_internal_fn.getTextRect = function (text, cls) {
var body = this.d3.select('body').classed('c3', true),
svg = body.append("svg").style('visibility', 'hidden'), rect;
svg.selectAll('.dummy')
.data([text])
.enter().append('text')
.classed(cls ? cls : "", true)
.text(text)
.each(function () { rect = this.getBoundingClientRect(); });
svg.remove();
body.classed('c3', false);
// jsdom can't compute real size of text element
// getBoundingClientRect() returns zero-sized rect in jsdom
// This is a hack to estimate size depending on text length and average glyph size
if(isNode()){
// 4 is average glyph size. Warning! This may break on font change.
// 8 is size of square near the text.
rect.width = text.length * 4 - 8;
}
return rect;
var fontSize = parseFloat(getStyleValue(cls, 'font-size'));
return {
height: fontSize,
width: text.length * fontSize / this.config.text_h_to_w
};
};
c3_chart_internal_fn.generateXYForText = function (areaIndices, barIndices, lineIndices, forX) {
var $$ = this,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment