How to show only integers (no decimals) in chart API x/y-axis labels

前端 未结 11 1335
予麋鹿
予麋鹿 2021-02-03 23:10

I\'m using a column chart with the Google Chart JS Api. I\'m displaying some values (total orders by day) that can only be represented as integers.

Everything is work

11条回答
  •  醉话见心
    2021-02-03 23:55

    In my case I have values ranging from 0 to 600.

    • if $value is smaller than 12: use the max value for vAxis.gridlines.count
    • if $value is greater than 12: use the default nr of gridlines (4)

    In this way you only display 'whole numbers'.

    You'll need to dynamically generate the javascript off course.

    Code Example:

    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Fase', 'Closed', 'Open'],
             $pe){
                $totals = countOpenIssuesInProjectExecution($pe);
                $open = $totals['open'];
                $closed = $totals['closed'];
                $data[] = "['". $FASE[$pe['fase_id']] . "', " . $closed . ", ". $open . "]\r\n";
                // What are the Max values vor Open & Closed Defects
                if($open > $max ){ $max = $open; }
                if($closed > $max ){ $max = $closed; }
            }
            $nrOfGridLines = ($max >= 12 ? 4 : $max+1); // Calculate the number of gridlines
            echo implode(",",$data);
            ?>
        ]);
        var options = {
            legend: { position: 'bottom' }
            ,title: "Evolution Defects: Open -VS- Closed"
            ,colors:['#00a160','red']
            ,isStacked: true
            ,vAxis: {textPosition: 'in', minValue:0,viewWindow: { min: 0 }, gridlines: {count: } }
    
        };
        var chart = new google.visualization.AreaChart(document.getElementById('chart_defects'));
        chart.draw(data, options);
    }
    

提交回复
热议问题