question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Write text into a bar element?

See original GitHub issue

I’m trying to customize a stacked bar chart, and having some trouble figuring out the right way to inject some text into each bar. This is what I currently have, and it does inject a span element, but its not actually getting visibly displayed. Not sure if its just that I’m not setting the height and width of the span or what. Anyone have any idea what I’m missing?

var chart = Chartist.Bar(this.getDOMNode(), chartData, chartOptions).on("draw", function (data) {
    if (data.type === "bar") {
        var label = new Chartist.Svg("span");
        label.addClass("ct-label ct-horizontal");
        label.text("Test");
        data.element.append(label);
        data.element.attr({
            style: "stroke-width: 35px"
        });
    }
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
ryexleycommented, Feb 24, 2015

Yeah, I came up with a solution for this…and it was to use the text element as you suggested. For anyone who might need to do something similar in the future, here’s what I came up with:

Handle the draw event of the chart with a function that looks something like this:

onDraw: function (data) {
    if (data.type === "bar") {
        var label, labelText, barLength, labelWidth, barClasses,
            barWidth = 40,
            barHorizontalCenter = (data.x1 + (data.element.width() * .5)),
            barVerticalCenter =  (data.y1 + (barWidth * .12)),
            classLabelMap = {
                cardinal: "Scared",
                flamingo: "Confused",
                sunglow: "Indifferent",
                pistachio: "Confident",
                grass: "Secure"
            };

        // set the width of the bar
        data.element.attr({
            style: "stroke-width: " + barWidth + "px"
        });

        // need to grab the CSS classes that were applied to the bar
        barClasses = data.element.parent().classes();
        // typically each bar gets two classes, ct-series and ct-series-<color>
        // (cardinal, flamingo, etc)
        barClasses.forEach(className => {
            // eliminate the basic series class...we can't do anything with that
            var color = className.replace("ct-series", "");
            // once that is stripped off, if there's anything left, then we have
            // the class that specifies the color of the bar...
            if (color.length) {
                // strip off the remaining leading dash
                color = color.substr(1, color.length);
                // use the color class to map to the text we want to display in
                // the custom label for the bar
                labelText = classLabelMap[color];
            }
        });

        // add the custom label text as an attribute to the bar for use by a tooltip
        data.element.attr({ label: labelText }, "ct:series");

        // create a custom label element to insert into the bar
        label = new Chartist.Svg("text");
        label.text(labelText);
        label.attr({
            x: barHorizontalCenter,
            y: barVerticalCenter,
            "text-anchor": "middle",
            style: "font-family: 'proxima-nova-alt', Helvetica, Arial, sans-serif; font-size: 12px; fill: white"
        });

        // add the new custom text label to the bar
        data.group.append(label);

        // only *now* that its been added to the bar and written to the DOM
        // can we measure it to see if it actually fits in the bar
        barLength = data.element.width(); // get the width of the bar itself
        labelWidth = label._node.clientWidth; // now get the width of the custom label
        // if the padded width of the label is larger than the bar...
        if ((labelWidth + 20) >= barLength) {
            // don't show it
            label.remove();
        }
    }
}

And the result ends up looking something like this:

stacked-horizontal-bar-chart-with-custom-labels

I’m open to suggestion if anyone has any ideas for how the above could be done better. Personally, key’ing off the class names of the parent feels a bit hackish, but it was the only thing I could find that uniquely identified each of the stacked bars. If there is something that would be better, please let me know.

1reaction
mmilitocommented, Nov 24, 2015

This is fantastic, it would be great if you were able to add this to Chartist main code! Great job!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write text into a bar element? (Chart.js) - Ionic Forum
Hi, i made a stacked bar chart and i want to write some info in every stack.How can i write? import { Component...
Read more >
Add a text box to a chart - Microsoft Support
Click the chart to which you want to add a text box. In Office 2013 and newer versions: On the Format tab, in...
Read more >
How to write text above the bars on a bar plot (Python)?
plt.bar() returns a list of rectangles that can be used to position suitable text above each of the bars as follows:
Read more >
Adding text to top of bar chart by appending text element
Hi all, I played around and change this original notebook, to this: Using join because I wanted to try to add some transitions...
Read more >
Adding texts to bars - MuseScore
I've found it easiest to use a generic text line attached to the bar. Then you can use the start/end text from it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found