admin管理员组

文章数量:1300006

So I tried messing around with my charts and noticed that integers/floats/dates work perfectly fine for the x axis, but the moment that you put in a string, it blows up.

I'm trying to graph a: Word/Count graph, but unfortunately I can't seem to be able to put strings in the x axis. Is this allowed in Google Charts, or is it strictly int/floats/dates?

So I tried messing around with my charts and noticed that integers/floats/dates work perfectly fine for the x axis, but the moment that you put in a string, it blows up.

I'm trying to graph a: Word/Count graph, but unfortunately I can't seem to be able to put strings in the x axis. Is this allowed in Google Charts, or is it strictly int/floats/dates?

Share Improve this question edited Nov 25, 2014 at 5:55 Pratik Butani 62.4k63 gold badges286 silver badges449 bronze badges asked Nov 25, 2014 at 0:51 Stupid.Fat.CatStupid.Fat.Cat 11.3k28 gold badges94 silver badges163 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

use vAxis and hAxis property as below

google.visualization.ColumnChart(document.getElementById('visualization')).
  draw(data,
       {title:"Yearly Coffee Consumption by Country",
        width:600, height:400,
        vAxis: {title: "Year",ticks: [{v:100, f:"100$"},{v:150, f:"150$"},{v:200, f:"200$"},{v:300, f:"300$"}]},
        hAxis: {title: "Cups",ticks: [{v:4, f:"3-4"},{v:8, f:"5-9"},{v:10, f:"9-13"},{v:14, f:"13-14"},{v:20, f:"15-20"}]} }
  );

for more detail check this How to get the vaxis line in column chart of google api chart?

Just define your column to be of type string and there is no issue. Do note that with a string major column, all rows will be evenly spaced among the chart.

var data = new google.visualization.DataTable();
        data.addColumn('string', 'Month');
        data.addColumn('number', 'Sales');
        data.addRows([
        ['Jan', 1],
        ['Feb', 5],
        ['Mar', 2],
        ['Apr', 3]
    ]);

To also use vertical lines in your chart you can not use column type string but you have to use either number, float or date. To still use strings as x-axis labels you can use ticks like in the jsfiddle here: http://jsfiddle/j29Pt/417/

本文标签: javascriptGoogle Chart is it possible to use Strings for the x axisStack Overflow