xAxis settings valueAxis settings

Label, tick marks & grid lines visibility:

Label, tick marks & grid lines visibility:

Custom positions:

Custom positions:


Description


This is an example of JavaScript chart demonstrating how to add labels, tick marks and grid lines at custom offsets.

jQuery Chart: Axis Custom Offsets

JavaScript Chart custom offsets example.

A live JavaScript/jQuery example of jqxChart, the jQWidgets Chart component, with its complete source code below. It is one of 92 jqxChart demos for jQuery.

jqxChart 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 $. Getting started with jqxChart shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxdraw.js, jqxchart.core.js, jqxbuttons.js, jqxradiobutton.js, jqxcheckbox.js, jqxlistbox.js, jqxscrollbar.js, jqxdropdownlist.js, jqxinput.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 () {

    var sampleData = [
        { a: 0.1535, b: 0.5 },
        { a: 0.48, b: 20.5 },
        { a: 10, b: 60 },
        { a: 100, b: 80 },
        { a: 200, b: 90 },
        { a: 245.11, b: 100.13 },
        { a: 300.13, b: 150.13 },
        { a: 340, b: 200 }
       ];

    var settings = {
        title: "Custom labels, grid lines and tick marks offsets",
        description: " ",
        padding: { left: 5, top: 5, right: 15, bottom: 5 },
        titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
        source: sampleData,
        enableAnimations: false,
        xAxis:
        {
            dataField: "a",
            logarithmicScale: true,
            logarithmicScaleBase: 2,
            labels: {
                visible: true,
                custom: [{ value: 10 }, { value: 20}]
            },
            gridLines:
            {
                visible: true,
                custom: [{ value: 10 }, { value: 20}]
            },
            tickMarks:
            {
                visible: true,
                custom: [{ value: 10 }, { value: 20}]
            },
            flip: false
        },
        valueAxis: {
            logarithmicScale: true,
            logarithmicScaleBase: 2,
            title: { text: "" },
            labels: {
                horizontalAlignment: "right",
                custom: [{ value: 10 }, { value: 20}]
            },
            gridLines: {
                custom: [{ value: 10 }, { value: 20}]
            },
            tickMarks: {
                custom: [{ value: 10 }, { value: 20}]
            },
            flip: false
        },
        seriesGroups:
                [
                    {
                        type: "scatter",
                        series: [
                                { dataField: "a", displayText: "A", symbolType: "diamond", symbolSize: 10 },
                                { dataField: "b", displayText: "B", symbolType: "triangle_up", symbolSize: 10 }
                            ]
                    }
                ]
    };

    // setup chart settings
    $("#chartContainer").jqxChart(settings);

    var chartInstance = $("#chartContainer").jqxChart("getInstance");

    function addAxisSettingsHandlers(axis) {
        var visibilityOptions = [true, false, "custom"];
        $("#dropDownLabelsVisibility_" + axis).jqxDropDownList({ source: visibilityOptions, selectedIndex: 0, width: "235", height: "25", dropDownHeight: 80 });

        $("#dropDownLabelsVisibility_" + axis).on("change", function (event) {
            var value = event.args.item.value;

            chartInstance[axis].labels.visible = value == "false" ? false : value;
            chartInstance[axis].gridLines.visible = value == "false" ? false : value;
            chartInstance[axis].tickMarks.visible = value == "false" ? false : value;

            chartInstance.refresh();
        });

        $("#listboxCustomPositions_" + axis).jqxListBox({ source: [10, 20], height: 80, width: 235 });

        $("#inputCustomPosition_" + axis).jqxInput({ height: 22 });

        $("#btnAddCustomPosition_" + axis).jqxButton().on("click", function () {
            var value = $("#inputCustomPosition_" + axis).val();
            if (!isNaN(parseFloat(value))) {
                $("#listboxCustomPositions_" + axis).jqxListBox("addItem", parseFloat(value));
                var items = $("#listboxCustomPositions_" + axis).jqxListBox("getItems");
                var customOffsets = [];
                for (var i = 0; i < items.length; i++)
                    customOffsets.push({ value: parseFloat(items[i].value) });

                chartInstance[axis].labels.custom = customOffsets;
                chartInstance[axis].gridLines.custom = customOffsets;
                chartInstance[axis].tickMarks.custom = customOffsets;

                chartInstance.refresh();
            }
        });

        $("#btnRemoveCustomPosition_" + axis).jqxButton().on("click", function () {
            var idx = $("#listboxCustomPositions_" + axis).jqxListBox("selectedIndex");
            if (idx == -1)
                return;

            $("#listboxCustomPositions_" + axis).jqxListBox("removeAt", idx);
            var items = $("#listboxCustomPositions_" + axis).jqxListBox("getItems");
            var customOffsets = [];
            for (var i = 0; i < items.length; i++)
                customOffsets.push({ value: parseFloat(items[i].value) });

            chartInstance[axis].labels.custom = customOffsets;
            chartInstance[axis].gridLines.custom = customOffsets;
            chartInstance[axis].tickMarks.custom = customOffsets;

            chartInstance.refresh();
        });
    }

    addAxisSettingsHandlers('xAxis');
    addAxisSettingsHandlers('valueAxis');

});

body.html

<div id="chartContainer" style="width:800px; height: 500px;"></div>
<table>
    <tbody><tr>
        <td style="width:300px;"><b>xAxis settings</b></td>
        <td style="width:300px;"><b>valueAxis settings</b></td>
    </tr>
    <tr>
        <td>
            <p style="font-family: Verdana; font-size: 12px;">Label, tick marks &amp; grid lines visibility:</p>
            <div id="dropDownLabelsVisibility_xAxis">
            </div>
        </td>       
        <td>
            <p style="font-family: Verdana; font-size: 12px;">Label, tick marks &amp; grid lines visibility:</p>
            <div id="dropDownLabelsVisibility_valueAxis">
            </div>
        </td>       
    </tr>
    <tr>
        <td>
            <p style="font-family: Verdana; font-size: 12px;">Custom positions:</p>
            <input type="text" id="inputCustomPosition_xAxis" style="width: 50px">
            <input type="button" value="Add" id="btnAddCustomPosition_xAxis">
            <input type="button" value="Remove" id="btnRemoveCustomPosition_xAxis">
            <div style="margin-top:10px" id="listboxCustomPositions_xAxis">
            </div>
        </td>       
        <td>
            <p style="font-family: Verdana; font-size: 12px;">Custom positions:</p>
            <input type="text" id="inputCustomPosition_valueAxis" style="width: 50px">
            <input type="button" value="Add" id="btnAddCustomPosition_valueAxis">
            <input type="button" value="Remove" id="btnRemoveCustomPosition_valueAxis">
            <div style="margin-top:10px" id="listboxCustomPositions_valueAxis">
            </div>
        </td>       
    </tr>
</tbody></table>

Learn more

More jQuery Chart demos