避免 Google 圖表中 X 軸的小數點 (Avoid decimal points in X-axis in Google charts)


問題描述

避免 Google 圖表中 X 軸的小數點 (Avoid decimal points in X‑axis in Google charts)

我正在使用 Google 圖表,並創建了一個水平堆疊圖表。我繪製圖表的數組是

[['name','count',{ role: "style" }],
['abc', 1, '#4CAF50'],
['pqr', 2, '#4CAF50'],
['xyz', 1, '#4CAF50'],
['efg', 1, '#4CAF50'],
['klm', 2, '#4CAF50']]

這導致 X 軸0.5, 1.0, 1.5, 2.0.

但是我想避免X軸上的小數點。

為此我嘗試了:

hAxis: {
          format: '#'
        }

AND

hAxis: {
          format: '0'
        }

但是這些給出 X 軸 為:1, 1, 2, 2。 這是不可取的。

I想要 X 軸 點類似於:1,2,3,4

我該怎麼做?任何幫助都是非常可觀的......


參考解法

方法 1:

You could specify X‑axis explicitly via ticks option. But since your goal is set ticks based on the input data, you could specify it as demonstrated below:

google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Year', 'Sold Books (M)', { role: 'annotation' }],
        ['2010', 1.1, ''],
        ['2011', 3.0, ''],
        ['2012', 9.0, ''],
        ['2013', 7.3, ''],
        ['2014', 3.5, '']
    ]);




    var options = {
        width: 600,
        height: 400,
        legend: { position: "none" },
        isStacked: true,
        hAxis: {
            ticks: calcIntTicks(data,1)
        }
    };
    var chart = new google.visualization.BarChart(document.getElementById("barchart_values"));
    chart.draw(data, options);
}

function calcIntTicks(dataTable,step) {
    var min = Math.floor(dataTable.getColumnRange(1).min);
    var max = Math.ceil(dataTable.getColumnRange(1).max);
    var vals = [];
    for (var cur = min; cur <= max; cur+=step) {
        vals.push(cur);
    }
    return vals;
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="barchart_values" style="width: 900px; height: 300px;"></div>

方法 2:

Try the ticks option. Here is the fiddle for it.

ticks: [1, 2, 3] 

(by Deepu SasidharanVadim GremyachevRaman)

參考文件

  1. Avoid decimal points in X‑axis in Google charts (CC BY‑SA 2.5/3.0/4.0)

#javascript #decimal #google-visualization






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論