How does one go about creating an Histogram using the google chart api?

白昼怎懂夜的黑 提交于 2021-02-07 05:33:29

问题


Other than using the Column chart and naming appropriately is it possible to create histograms in google chart api?


回答1:


Google Charts does not have a histogram chart, since it is just a visualization library you will have to modify the Column Chart to suit your needs. However, I suspect the reason you are not satisfied with column chart is because of the column spacing, which doesn't look very histogram-like. So I will answer this question first:

Can you control the spacing between columns in a Column Chart?

No, not at this time. See this quote from the Google Charts Community

There's no support in the API for controlling the spacing between bars. You might be able to hack it if you're willing to dig into the chart's SVG.

So it is do-able but will take some extra work from you. You can also play around with the chartArea configuration option which will have some influence on the column spacing.

However, the original question may have a different answer actually.

Can you create a histogram-like chart using a Column Chart?

While you cannot control the spacing between sets of columns in a Column Chart, you can get the columns pressed up almost to one another by specifying them as different columns, and then setting each column's color to the same color in the configuration options.

Here is a simple 3-column histogram:

var data = google.visualization.arrayToDataTable([
    ['x', '1-10', '11-20', '21-30'],
    ['', 3, 5, 4]
  ]);

  // Create and draw the visualization.
  new google.visualization.ColumnChart(document.getElementById('visualization')).
      draw(data,
           {title:"My Histogram",
            width:600, height:400,
            hAxis: {title: null},
            colors: ['red','red','red'],
            legend: {position: 'none'}
           }
      );

Notice you have 1 row with 3 columns that are each colored 'red'. The downside to this is that you lose out on the labels along the x-axis telling you which column represents what. Again, you will have to have some sort of logic to construct this histogram and populate the data the way you want as well.

So the long story short is Google Charts doesn't have a Histogram and while it is possible with a Column Chart, you might consider looking into a different library.




回答2:


To add to mattedgod's answer, The column chart can now be created with the bars spaced tightly together, use the option:

bar:  {groupWidth:"100%"};



回答3:


Google introduced a couple of days ago an histogram chart : link



来源:https://stackoverflow.com/questions/9827485/how-does-one-go-about-creating-an-histogram-using-the-google-chart-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!