Chartjs-Charts.js – Bubble chart with two word axis

2👍

I’ve recently had the same requirement for a dataset and utilised the callback function for each axis in the scale option. I populated the list of values for the labels into an array and then used the index of the point to perform a lookup to rename the tick label.

var colours = ["Red", "Blue", "Green", "Yellow"];
var carSizes = ["Small", "Medium", "Large"];

// Small Red = 10
// Small Green = 14
// Medium Yellow = 23
var dataPoints = [{x: 0, y: 0, r: 10}, {x: 2, y: 0, r: 14}, {x: 3, y: 1, r: 23}

var myBubbleChart = new Chart(bubbleCtx, {
            type: 'bubble',
            data: dataPoints,
            options: {
                title: {
                    display: true,
                    text: "Car Orders"
                },
                scales: {
                    yAxes: [{
                        ticks: {
                            stepSize: 1,
                            callback: function (value, index, values) {
                                if (index < carSizes.length) {
                                    return carSizes[carSizes.length - (1 + index)]; //this is to reverse the ordering
                                }
                            }
                        },
                        position: 'left'
                    }],
                    xAxes: [{
                        ticks: {
                            stepSize: 1,
                            callback: function (value, index, values) {
                                if (index < colours.length) {
                                    return colours[index];
                                }
                            }
                        },
                        position: 'bottom'
                    }]
                }
            }
        });

After much trial and error, I found it necessary to set the step size to 1 otherwise the chart would get skewed with data appearing outside the gridlines.

If you are not setting the data dynamically and know the minimum and maximum values for each axis, you can set the min and max attributes for the ticks and specify the axis type as ‘category’.

yAxes: [{
    type: 'category',
    ticks: {
        stepSize: 1,
        min: 'Small',
        max: 'Large'
    },
    position: 'left'
}]

0👍

You can use line type chart with bordercolour radius 0. it will act as line chart and avoid line. It will appeared like bubble chart.

Leave a comment