【问题标题】:Google Charts Bubble Charts categorical x and y axes instead of numericGoogle Charts Bubble Charts 分类 x 和 y 轴而不是数字
【发布时间】:2013-01-25 16:26:40
【问题描述】:

我使用Google Charts 创建了一个漂亮的气泡图。这是图表的截图:

x 轴上的数字代表单个客户。 y 轴上的数字代表单个产品。如您所见,大约有 23 个客户和 7 个产品。

问题是 X 和 Y 轴只能是数字(据我从文档中知道)。我希望能够沿轴显示客户和产品的字符串值,而不仅仅是代表整数。这将使我们更容易理解我们正在查看的内容。

有谁知道如何做到这一点?

我确实有包含客户和产品字符串的 JS 数组。它们的整数索引对应于图表上显示的数字。例如:

customers[6] = "Microsoft"
customers[7] = "Dell"
...

但现在只显示整数。

任何帮助将不胜感激!谢谢!

这是我用来定义图表的代码:

    var options = {
        'title':'Customer / Product Grid',
        'width': 1000,
        'height':500
    };

    //for customer product grid
    var customer_product_grid_data_table = new google.visualization.DataTable();
    customer_product_grid_data_table.addColumn('string', 'Customer and Product');
    customer_product_grid_data_table.addColumn('number', 'Customer');
    customer_product_grid_data_table.addColumn('number', 'Product');
    customer_product_grid_data_table.addColumn('number', 'Profit Margin');
    customer_product_grid_data_table.addColumn('number', 'Proportion of Sales');

    for (var i = 1; i < customer_product_grid_data.length; i ++){ 
        customer_product_grid_data_table.addRow([
            '',
            customer_product_grid_data[i][0],
            customer_product_grid_data[i][1],
            (customer_product_grid_data[i][3] - customer_product_grid_data[i][2]) / customer_product_grid_data[i][3] * 100,
            customer_product_grid_data[i][3] / qnty_sell_total
        ]); 
    }

    var chart = new google.visualization.BubbleChart(document.getElementById('customer_product_grid'));
    chart.draw(customer_product_grid_data_table, options);

【问题讨论】:

    标签: javascript charts google-visualization


    【解决方案1】:

    从我所做的所有搜索以及 jmac 给出的答案来看,我认为唯一的方法是使用 Javascript hack 将轴编号替换为单词。我实现的代码在这里:

        /*
         *
         * 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];
                }
            });
        }
    

    基本上,您只需创建一个 for 循环,遍历您在 x 和 y 轴上显示的所有整数。做一些if...else 的事情,要么用数组中的元素替换整数,要么把它留空。

    请记住,要使上述代码正常工作,您需要在图表选项中具有以下属性 -> vAxis: { textPosition: 'in' }

    【讨论】:

    • 这是一个很棒的 hack。感谢你的分享!这将使其他人在未来变得无比轻松。
    • 你能解释一下它是如何工作的吗?您是否正在阅读 SVG 并能以某种方式识别轴标签?
    • 是的,在 Google Chrome 中,我打开了开发控制台。在显示“元素”的选项卡中,您可以找到谷歌图表的 div,然后在里面我可以看到包含轴标签的元素。请记住,要使上述代码正常工作,您需要在图表选项中具有以下属性 -> vAxis: { textPosition: 'in' }
    【解决方案2】:

    不幸的是,没有像气泡图(或任何使用数字系列作为轴值的任何东西)的简单方法。您可以按照here 的说明解决它。

    一般概念是消除“主图表”上的轴标签并调整网格线以匹配您想要的标签数量。然后创建一个仅显示类别的附加虚拟图表,并使用它来显示标签。

    不幸的是,在 Google 决定为图表轴实现整个 ICU 模式集之前,它必须是这样......

    【讨论】:

    • 谢谢,我最终使用 jQuery 来解决这个问题,用单词替换轴号。相当 hack,但现在似乎可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多