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

后端 未结 2 1851
失恋的感觉
失恋的感觉 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' }

提交回复
热议问题