2013-07-04 13 views
5

Google Chartの軸ラベルに数値の代わりにテキストラベルを付けることができます。数値の代わりにGoogle Chartsの軸ラベルを文字列/テキスト/名義/カテゴリに設定する方法

example bubble chart with text on both axis

が、私はstackoverflowの上のどこかに、ここでこの例を見つけましたが、今はもうそれを見つけることができません。結果は、この(例えばバブルチャート)のようになります。とにかくその答えは不完全であり、働いていませんでした。それはHTML全体を含んでおらず、微妙な変数を持っていました。軸ステッピングを設定することも重要です。そうしないと、2番目のラベルまたは10番目ごとに表示されます。

答えて

4

上記のチャートを作成する完全なHTML + JSソースです。

両方の軸のラベルを文字列(配列で定義)に変換します。また、実際にすべてのラベルが表示されるように、軸ステップの設定も含まれています。

私はそれは代わりに、あなたのスクリーンショットに指定されているあなたは、文字列値を持つことができ、ラベルに数値を有していると誰か:)

<html> 
    <head> 
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script> 
    <script type="text/javascript"> 
    google.load("visualization", "1", {packages:["corechart"]}); 
    google.setOnLoadCallback(drawChart); 
    function drawChart() { 

    //these arrays hold the label strings 
    var products = new Array(); 
    for (var i = 1; i < 10; i ++) 
     products[i]='product'+i; 

    var customers = new Array(); 
    for (var i = 1; i < 8; i ++) 
     customers[i]='customer'+i; 


    var options = { 
    'title':'Customer/Product Grid', 
    // 'vAxis': { textPosition: 'in' }, 
    vAxis: { 
    viewWindow: { 
     max: products.length, 
     min: 0, 
     }, 
    gridlines: { 
     count: products.length, 
     color : 'white', 
     } 
    }, 
    hAxis: { 
    viewWindow: { 
     max: customers.length, 
     min: 0, 
     }, 
    gridlines: { 
     count: customers.length, 
     color : 'white', 
     } 
    }, 
    'width': 1000, 
    'height':500 
    }; 

    //dtd 
    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'); 

    // add some random numbers to show off 
    for (var i = 1; i < products.length; i ++) 
    for (var j = 1; j < customers.length; j ++) 
     { 
     customer_product_grid_data_table.addRow([ 
      '',j,i,50*Math.cos(i+j),20*Math.sin(i) 
     ]); 
     } 

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

    /* 
    * This can also be 
    * text[text-anchor="start"] 
    * or 
    * text[text-anchor="middle"] 
    * depending on the axis settings. If it doesnt work, try another one, or check the generated SVG source code in your HTML doc. 
    */ 
    for (var i = 0; i < products.length ; i ++){ 
    $('#chart_div svg text[text-anchor="end"]:contains("'+i+'")').text(function(j,t){ 
     if (t == i){ 
     if (i >= products.length || i < 1){ 
      return " "; 
     } 
     return products[i]; 
     } 
    }); 
    } 

    for (var i = 0; i < customers.length ; i ++){ 
    $('#chart_div svg text[text-anchor="middle"]:contains("'+i+'")').text(function(j,t){ 
     if (t == i){ 
     if (i >= customers.length || i < 1){ 
      return " "; 
     } 
     return customers[i]; 
     } 
    }); 


    } // end function 

    } 
    </script> 
    </head> 
    <body> 
    <div id="chart_div" style="width: 900px; height: 500px;"></div> 
    </body> 
</html> 
0

のために有用であると思います。これを行うには、haxisオプションで

format:"product" 
vaxisオプションで

format:"customer" 

を挿入する必要があります。

これはあなたにとって便利です。

+0

こんにちはようなタイプを追加します。あなたの貢献に感謝します。あなたは同じ出力を生み出す権利があります。しかし、私は実際には、名前は自明ではないと仮定していました。私はいくつかの例の名前しか示していません。実際には、名前はcustomer [1] = 'Microsoft'、customer [2] = 'Apple'、customer [3] = 'Google'などのようなもっと現実的な名前を持つ必要があります... –

+0

いいえ、それはありません。あなたが投稿したリンクはまったく異なる問題のため、ここではこの質問とは関係ありません:) –

0

文字列として、X軸のラベルを渡すと、文字列、

data.addColumn('string', 'product'); 
関連する問題