谷歌图表趋势线没有显示 [英] Google charts trendline not showing
问题描述
我有一个Google图表折线图,我想展示一个趋势线,但它没有显示。
I have a Google chart line chart that I want to show a trendline on, but it doesn't show up.
数据从数据库中提取, javascript是由PHP生成的,但生成的JavaScript看起来像这样:
The data is fetched from a database and the javascript is generated by PHP but the resulting javascript looks like this:
<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>
该脚本主要是来自Google图表示例的复制品。除趋势线没有显示外,图表工作正常。任何想法为什么?
The script is mainly copypaste from the Google charts example. The chart works fine, except for the trend line not showing up. Any ideas why?
推荐答案
你必须有一个连续的域轴(类型数字,日期 ,或timeofday)以使用趋势线。通过将第一列设置为字符串类型(并用字符串填充),您将禁用趋势线。切换到数字类型列,趋势线将起作用:
You have to have a continuous domain axis (type "number", "date", "datetime", or "timeofday") in order to use a trendline. By setting your first column to a "string" type (and populating it with strings), you are disabling the trendline. Switch to a "number" type column, and the trendline will work:
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]
]);
这篇关于谷歌图表趋势线没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!