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

前端 未结 11 1329
予麋鹿
予麋鹿 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:52

    I'm using the Guava Multiset as my data. Cagy79's trick is the only one that I was able to get working. I also tried generating my own lines using an IntStream, but still had fractional numbers for some of the graphs. As mentioned above, just setting the format will create a graph where the hover over value and the gridline differ.

    Multiset items = TreeMultiset.create();

    Items added using .add and .addAll

    Make sure the items are sorted:

    occurrences = Multisets.copyHighestCountFirst(items);

    String option = "hAxis : { title : 'Count', gridlines : { count:" + ((max >=12) ? 4 : max+1) + } }")

    0 讨论(0)
  • 2021-02-03 23:53

    For anyone that are also searching for the answer, you can see the answer to this question here, which use options format. Can't get Google Charts Axis to format in decimal

    0 讨论(0)
  • 2021-02-03 23:55

    This issue will be resolved if you use vAxis: {gridlines: { count: 4}}

    0 讨论(0)
  • 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'],
            <?php
            $max = 0; // needed to determine the number of gridlines?
            $data = array();
            foreach($projectExecutions as $key => $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: <?php echo $nrOfGridLines; ?>} }
    
        };
        var chart = new google.visualization.AreaChart(document.getElementById('chart_defects'));
        chart.draw(data, options);
    }
    
    0 讨论(0)
  • 2021-02-03 23:56

    I don't have enough rep to reply to Ian Hunter's post directly, but what that does is format the label, but doesn't set the grid lines to the same value as the label, which can have undesirable results. So let's say you have these grid lines:

    10

    7.5

    5

    2.5

    0

    When you format the label to only show integers it shows this: 10 8 5 3 0

    But then your data on the graph doesn't tie-in with the y-scale labels. E.g If you have a value of 3 it actually shows above the '3' labels because the '3' label is actually 2.5

    Unfortunately, the only solution I could think of was to monitor the min/max ranges of the data shown in the graph, and then apply some logic to set the min/max according to how many y-scale labels I have, such that they would divide with no remainders.

    So in the case above, I'd set it to

            vAxis: { 
              viewWindow:{
                max:12,
                min:0
              }
            }
    

    This would give grid lines of

    12

    8

    6

    4

    0

    No decimals, and no problems with the data not correlating with the labels on the graph.

    0 讨论(0)
提交回复
热议问题