Google Charts如何隐藏值= 0

时间:2018-01-26 00:05:49

标签: javascript charts google-visualization option

是否可以隐藏null上的hAxis值? 或者当点位于pointSize: 0

时更好地设置hAxis

如果是,哪个选项?

我尝试了ComboChart

enter image description here

        function drawChart() {

        var jsonData = $.ajax({
            url: "getData.php",
            dataType: "json",
            async: false
            }).responseText;

        var options = {
            width: 2500,
            height: 1080,
            legend: { position: 'top', maxLines: 10 },
            bar: { groupWidth: '75%' },
            isStacked: true,
            title: '15-tägiger Wettbewerb, 17.01.2018 - 06.02.2018', 
            titlePosition: 'out',
            colors: ['green', '#e6693e', 'blue'],
            backgroundColor: 'white',
            tooltip: { trigger: 'selection' },
            legend: { position: 'none' },
            vAxis: { viewWindowMode:'explicit', viewWindow: { max: 130}},
            seriesType: 'bars',
            series: {   3: {type: 'scatter', pointSize: 30, color: 'black'},
                        4: {type: 'scatter', pointSize: 30, color: 'black'},
                        5: {type: 'scatter', pointSize: 30, color: 'black'},
                        6: {type: 'scatter', pointSize: 30, color: 'black'},

             },
             pointShape: 'star',
        };

        var data = new google.visualization.DataTable(jsonData);

        var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
        chart.draw(data, options);

        }

1 个答案:

答案 0 :(得分:1)

只是有点困惑,标题说隐藏0,但问题是隐藏null ......

但如果您使用null代替0,那么明星将不会出现...

['', 2, 4, null, null, null, null, null],  // <-- star will NOT appear
['', 2, 4, 0, 0, 0, 0, 0],                 // <-- star will appear at the bottom

请参阅以下工作代码段...

&#13;
&#13;
google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var jsonData = [
    ['', 2, 4, null, null, null, null, null],
    ['', 2, 4, 0, 0, 0, 0, 0],
  ];

  var options = {
    //width: 2500,
    //height: 1080,
    legend: { position: 'top', maxLines: 10 },
    bar: { groupWidth: '75%' },
    isStacked: true,
    title: '15-tägiger Wettbewerb, 17.01.2018 - 06.02.2018',
    titlePosition: 'out',
    colors: ['green', '#e6693e', 'blue'],
    backgroundColor: 'white',
    tooltip: { trigger: 'selection' },
    legend: { position: 'none' },
    vAxis: { viewWindowMode:'explicit', viewWindow: { max: 130}},
    seriesType: 'bars',
    series: {
      3: {type: 'scatter', pointSize: 30, color: 'black'},
      4: {type: 'scatter', pointSize: 30, color: 'black'},
      5: {type: 'scatter', pointSize: 30, color: 'black'},
      6: {type: 'scatter', pointSize: 30, color: 'black'},
     },
     pointShape: 'star',
  };

  var data = google.visualization.arrayToDataTable(jsonData, true);
  var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;

编辑

您可以使用带有计算列的数据视图将零更改为空,
请参阅以下工作代码段...

&#13;
&#13;
google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var jsonData = [
    ['', 2, 4, null, null, null, null, null],
    ['', 2, 4, 0, 0, 0, 0, 0],
  ];

  var options = {
    //width: 2500,
    //height: 1080,
    legend: { position: 'top', maxLines: 10 },
    bar: { groupWidth: '75%' },
    isStacked: true,
    title: '15-tägiger Wettbewerb, 17.01.2018 - 06.02.2018',
    titlePosition: 'out',
    colors: ['green', '#e6693e', 'blue'],
    backgroundColor: 'white',
    tooltip: { trigger: 'selection' },
    legend: { position: 'none' },
    vAxis: { viewWindowMode:'explicit', viewWindow: { max: 130}},
    seriesType: 'bars',
    series: {
      3: {type: 'scatter', pointSize: 30, color: 'black'},
      4: {type: 'scatter', pointSize: 30, color: 'black'},
      5: {type: 'scatter', pointSize: 30, color: 'black'},
      6: {type: 'scatter', pointSize: 30, color: 'black'},
     },
     pointShape: 'star',
  };

  var data = google.visualization.arrayToDataTable(jsonData, true);

  // build view
  var view = new google.visualization.DataView(data);
  var viewColumns = [];
  $.each(new Array(data.getNumberOfColumns()), function (col) {
    viewColumns.push({
      calc: function (dt, row) {
        var value = dt.getValue(row, col);
        if (value === 0) {
          value = null;
        }
        return value;
      },
      label: data.getColumnLabel(col),
      type: data.getColumnType(col)
    });
  });
  view.setColumns(viewColumns);

  var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));

  // draw view
  chart.draw(view, options);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;