谷歌图表趋势线未显示

Google charts trendline not showing

本文关键字:显示 趋势线 谷歌      更新时间:2023-09-26

我有一个谷歌图表折线图,我想在上面显示趋势线,但它没有显示。

数据是从数据库中获取的,javascript由PHP生成,但生成的javascript如下所示:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table, 
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
    var dataS = new google.visualization.DataTable();
    dataS.addColumn('string', 'Dag');
    dataS.addColumn('number', 'Poäng');
    dataS.addRows([
        ['1', 32],
        ['2', 37],
        ['3', 37],
        ['4', 40],
        ['5', 31],
        ['6', 38],
        ['7', 28],
        ['8', 34],
        ['9', 41],
        ['10', 41],
    ]);
    var optionsS = {
      title: '',
      legend: 'none',
      hAxis: {title: 'Serie'},
      vAxis: {title: 'Poäng'},
      pointSize: 4,
      trendlines: { 0: {} }
    };
    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.LineChart(document.getElementById('chart_div_series'));
    chart.draw(dataS, optionsS);
}
</script>

该脚本主要是从谷歌图表示例中复制粘贴的。图表工作正常,除了趋势线没有显示。知道为什么吗?

您必须具有连续的域轴(键入"数字"、"日期"、"日期时间"或"一天中的时间")才能使用趋势线。 通过将第一列设置为"字符串"类型(并用字符串填充它),您将禁用趋势线。 切换到"数字"类型列,趋势线将起作用:

var dataS = new google.visualization.DataTable();
dataS.addColumn('number', 'Dag');
dataS.addColumn('number', 'Poäng');
dataS.addRows([
    [1, 32],
    [2, 37],
    [3, 37],
    [4, 40],
    [5, 31],
    [6, 38],
    [7, 28],
    [8, 34],
    [9, 41],
    [10, 41]
]);