Google Charts Bubble Charts categorical x and y axes instead of numeric

后端 未结 2 1766
失恋的感觉
失恋的感觉 2021-01-15 03:09

I have created a beautiful bubble chart using Google Charts. Here is a shot of the chart:

\"enter

相关标签:
2条回答
  • 2021-01-15 03:47

    Judging from all the searching I did, and also the answer given here by jmac, I decided the only way to go was a Javascript hack to replace the axes numbers with words. The code I implemented is here:

        /*
         *
         * The following 2 functions are a little hacky, they have to be done after calling the "draw" function
         * The bubble chart originally displays only numbers along the x and y axes instead of customer or product names
         * These 2 functions replace those numbers with the words for the customers and products
         *
         */
        for ( var i = -2; i < products.length + 1; i ++ ){
            $('#customer_product_grid svg text[text-anchor="start"]:contains("'+i+'")').text(function(j,t){
                if (t == i){
                    if (i >= products.length || i < 0){
                        return " ";
                    }
                    return products[i];
                }
            });
        }
    
        for ( var i = -2; i <= customers.length + 3; i ++ ){
            $('#customer_product_grid svg text[text-anchor="end"]:contains("'+i+'")').text(function(j,t){
                if (i >= customers.length + 1 || i <= 0){
                    return " ";
                }else if (t == i){                    
                    return customers[i-1];
                }
            });
        }
    

    Basically, you just make a for loop that iterates through all the integers that you are showing on the x and y axes. Do some if...else stuff to either replace the integer with an element from the array, or just make it blank.

    Keep in mind for the above code to work properly, you need to have the following property in the chart options -> vAxis: { textPosition: 'in' }

    0 讨论(0)
  • 2021-01-15 04:04

    Unfortunately, there is no easy way to do this as bubble charts (or anything that uses numerical series for an axis value). You can work around it as explained here.

    The general concept is to eliminate your axis labels on the 'main chart' and adjust the gridlines to match the amount of labels you want. Then create an additional dummy chart which shows only the categories, and use that to show the labels.

    Unfortunately, this is how it has to be until Google decides to implement the entire ICU pattern set for chart axes...

    0 讨论(0)
提交回复
热议问题