jQuery BulletChart: Labels Formatting

Conditionally format the bullet chart labels and tooltip.

A live JavaScript/jQuery example of jqxBulletChart, the jQWidgets BulletChart component, with its complete source code below. It is one of 9 jqxBulletChart demos for jQuery.

jqxBulletChart API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxtooltip.js, jqxbulletchart.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {
    $("#bulletChart").jqxBulletChart({
        width: 150,
        height: 500,
        barSize: "35%",
        title: "Temperature in Boston, Mass.",
        description: "on 28 May",
        ranges: [
            { startValue: 0, endValue: 10, color: "#0099FF", opacity: 1 },
            { startValue: 10, endValue: 15, color: "#006600", opacity: 1 },
            { startValue: 15, endValue: 20, color: "#FF6600", opacity: 1 },
            { startValue: 20, endValue: 25, color: "#FF4526", opacity: 1 }
        ],
        pointer: { value: 11, size: "25%", color: "#181818" },
        target: { value: 15, size: 4, color: "#181818" },
        ticks: { position: "both", interval: 5, size: 10 },
        orientation: "vertical",
        labelsFormatFunction: function (value, position) {
            var labelsColor;
            if (value < 10) {
                labelsColor = "#0099FF";
            } else if (value < 15) {
                labelsColor = "#006600";
            } else if (value < 20) {
                labelsColor = "#FF6600";
            } else {
                labelsColor = "#FF4526";
            }

            if (position == "near") {
                return "<span style='color: " + labelsColor + ";'>" + (value * 1.8 + 32) + "°F</span>";
            } else {
                return value + "°C";
            }
        },
        showTooltip: true,
        tooltipFormatFunction: function (value, target) {
            return "<div>Temperature on May 28: <strong>" + (value * 1.8 + 32) + "°F</strong> (" + value + "°C)</div><div>Average temperature in May: <strong>" + (target * 1.8 + 32) + "°F</strong> (" + target + "°C)</div>"
        }
    });
});

body.html

<div id="bulletChart">
</div>

Learn more

More jQuery BulletChart demos