如果只有零和正数,谷歌折线图会变为负值

时间:2017-09-26 09:04:23

标签: javascript google-visualization

我使用Google图表来构建图表。这是我的代码:

var array = [["Day", "Value"], ["15", 0], ["16", 0], ["17", 0], ["18", 0], ["19", 0], ["20", 0], ["21", 7], ["22", 0], ["23", 0], ["24", 0], ["25", 0]];

值数组只有零和正值

这只显示正值

var data = google.visualization.arrayToDataTable(array);
var options = {
    curveType: 'function',
    legend: {position: 'bottom'},
    vAxis: {viewWindowMode: "explicit", viewWindow: {min: 0}, minValue: 0, maxValue: 4},
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);

这显示负值和正值

var data = google.visualization.arrayToDataTable(array);
var options = {
    curveType: 'function',
    legend: {position: 'bottom'},
    vAxis: {viewWindowMode: "explicit", minValue: 0, maxValue: 4},
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);

That is how graph looks like. Line goes to negative values

If only positive values shown the line breaks

如何在图表上仅显示正值?没有换行

1 个答案:

答案 0 :(得分:0)

如果你使用curveType:' function'它是近似值。只需评论或删除此行:

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);



      function drawChart() {
        var data = google.visualization.arrayToDataTable([["Day", "Value"], ["15", 0], ["16", 0], ["17", 0], ["18", 0], ["19", 0], ["20", 0], ["21", 7], ["22", 0], ["23", 0], ["24", 0], ["25", 0]]);

        var options = {
          title: 'Company Performance',
          //curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="curve_chart" style="width: 900px; height: 500px"></div>
  </body>
</html>