谷歌图表中的某些点是否有可能没有价值?

问题描述 投票:1回答:2

假设我想添加3的移动平均值,这意味着我没有1和2值的值。 Google Charts绘制的emty字符串好像是0,这在绘制图表时很难看,是否有可能阻止它?

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Day', 'Candlestick', 'Begin', 'End', 'High'],
    ['Mon', 20, 28, 38, 45],
    ['Tue', 31, 38, 55, 66],
    ['Wed', 50, 55, 77, 80],
    ['Thu', 77, 77, 66, 50],
    ['Fri', 68, 66, 22, 15]
  ]);

  // add column for line, add data to rows...
  var colIndex = data.addColumn('number', 'Line');
  for (var i = 0; i < 2; i++) {
    data.setValue(i, colIndex, "");
  }
  for (var i = 2; i < data.getNumberOfRows(); i++) {
    data.setValue(i, colIndex, data.getValue(i, 1));
  }

  var options = {
    height: 400,
    series: {
      1: {
        type: 'line'
      }
    }
  };

  var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
charts google-visualization
2个回答
1
投票

使用值 - > null

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

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Day', 'Candlestick', 'Begin', 'End', 'High'],
    ['Mon', 20, 28, 38, 45],
    ['Tue', 31, 38, 55, 66],
    ['Wed', 50, 55, 77, 80],
    ['Thu', 77, 77, 66, 50],
    ['Fri', 68, 66, 22, 15]
  ]);

  // add column for line, add data to rows...
  var colIndex = data.addColumn('number', 'Line');
  for (var i = 0; i < 2; i++) {
    data.setValue(i, colIndex, null);
  }
  for (var i = 2; i < data.getNumberOfRows(); i++) {
    data.setValue(i, colIndex, data.getValue(i, 1));
  }

  var options = {
    height: 400,
    series: {
      1: {
        type: 'line'
      }
    }
  };

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

但是,当然,你不必设置任何价值......

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Day', 'Candlestick', 'Begin', 'End', 'High'],
    ['Mon', 20, 28, 38, 45],
    ['Tue', 31, 38, 55, 66],
    ['Wed', 50, 55, 77, 80],
    ['Thu', 77, 77, 66, 50],
    ['Fri', 68, 66, 22, 15]
  ]);

  // add column for line, add data to rows...
  var colIndex = data.addColumn('number', 'Line');
  for (var i = 2; i < data.getNumberOfRows(); i++) {
    data.setValue(i, colIndex, data.getValue(i, 1));
  }

  var options = {
    height: 400,
    series: {
      1: {
        type: 'line'
      }
    }
  };

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

0
投票

很奇怪,它不适用于字面值

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Day', 'Candlestick', 'Begin', 'End', 'High', 'MA'],
    ['Mon', 20, 28, 38, 45, null],
    ['Tue', 31, 38, 55, 66, null],
    ['Wed', 50, 55, 77, 80,40],
    ['Thu', 77, 77, 66, 50,40],
    ['Fri', 68, 66, 22, 15,40]
  ]);

  var options = {
    height: 400,
    series: {
      1: {
        type: 'line'
      }
    }
  };

  var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
© www.soinside.com 2019 - 2024. All rights reserved.