SmartLabel

SmartLabel is a component which can be used to measure and restrict text in svg / canvas where the layout engine does not automatically takes care of the text behaviours.
Concept
Live demo: fusioncharts.github.io/fusioncharts-smartlabel
To build the examples locally, run:
npm install
npm start
Then open localhost:8000
in a browser.
Installation
The easiest way to use fusioncharts-smartlabel is to install it from NPM and include it in your own build process (using Browserify, Webpack, etc).
You can also use the standalone build by including dist/fusioncharts-smartlabel.js
npm install fusioncharts-smartlabel --save
API
Please see the concept above before proceeding
SmartLabel is immensely useful when text needs to be drawn in SVG or Canvas. Since SVG / Canvas does not manage text, it is necessary to have pre procressing on the text before getting it rendered.
SmartLabel provides out of the box features to
- If a bound box is provided it wraps / truncates the text
- Calculate the metrics (height and width) of text for any style
- Add ellipses if the text is truncated
- Wraps a label in the bound box
To Create a new instance
import SmartLabel from 'fusioncharts-smartlabel'
var sl = new SmartLabel(container, useEllipses, options)
To apply style before calculating text metrics
sl.setStyle(style);
Decide whether the text would have trailing ellipses if truncated
sl.useEllipsesOnOverflow(useEllipses);
To get the text bounded by a bound box
smartlabel = sl.getSmartText(text, maxWidth, maxHeight, noWrap);
To get the lines for a truncated text
smartlabel = sl.getSmartText(text, maxWidth, maxHeight, noWrap);
generalizedSmartlabel = SmartLabel.textToLines(smartlabel);
To get the size of a given text
size = sl.getSize(text, detailedCalculationFlag);
To dispose the components
sl.dispose();
Usage
A visualization library requires to restrict a text in a bound box to eliminate over lapping. The use case of the application of SmartLabel is huge, however we are taking a small subset of the universal set.
For a typical column chart the labels in X axis conveys category information. It's important to display the labels properly in the visualization. But there are many parameters which affect the label placement.
If the chart is displayed in a space which is large enough to accommodate all the labels, then it is desired to place all the labels in one line

If the space is not enough to place all the labels side by side, it's required to wrap or truncate it.

It's some time desired to skip the labels as well if the category is continuous (Like month of the years)

All this can be eaisly done using SmartLabel.
Following is the example of how a text is constrained by a bound box.
text
.attr('dy', ".35em");
smarttext = this.smartlabel
.useEllipsesOnOverflow(true)
.getSmartText('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididun', 100, 200);
normalizedSL = SmartLabel.textToLines(smarttext);
tspan = text
.selectAll('tspan')
.data(smarttext.lines);
tspan
.enter()
.append('tspan')
.merge(tspan)
.attr("dy", normalizedSL.oriTextHeight)
.attr("x", 0)
.text(d => d);
License
MIT
Copyright (c) 2016 InfoSoft Global Pvt. Ltd. <support@fusioncharts.com>.