我有一个线性函数competitorCost(time)
,我想在谷歌图表上绘制。我让它使用表格接收一些数据并使用这些数据来创建线性函数。但是它不会绘制图表。
var upfront;
var monthlyrate;
var wifi;
var firewall;
var backup;
var vpn;
var install;
var result;
var competitorCost = function(time){
return upfront + (time * monthlyrate);
};
var ourCost = function(time){
return 1000 + (time * 50);
};
$(document).ready(function(){
$('#submit').click(function(){
wifi = $('input[name=wifiPrice]').val();
firewall = $('input[name=firewallPrice]').val();
backup = $('input[name=backupPrice]').val();
vpn = $('input[name=vpnPrice]').val();
install = $('input[name=installPrice]').val();
cloudbackup = $('input[name=cloudbackuprate]').val();
support = $('input[name=supportrate]').val();
upfront = parseInt(wifi) + parseInt(firewall) + parseInt(backup) + parseInt(vpn) + parseInt(install);
monthlyrate = parseInt(cloudbackup) + parseInt(support);
});
});
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Months', 'Entreda', 'Competitors'],
['0', ourCost(0), competitorCost(0)],
['6', ourCost(6), competitorCost(6)],
['12', ourCost(12), competitorCost(12)],
['18', ourCost(18), competitorCost(18)],
['24', ourCost(24), competitorCost(24)]
]);
var options = {
title: 'Entreda vs Competitor Costs Over Time',
width: 480,
height: 270,
pointSize: 5
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
competitorCost(time)
不作图,但ourCost(time)
作图。如果我在实例化它们时给出upfront
和monthlyrate
值,则函数图就好了,所以函数中使用upfront
和monthlyrate
使用存在问题。有人看到我的错误吗?
更新:我刚刚意识到谷歌图表在页面加载后立即绘制,因此upfront
在monthlyrate
绘制图表时未定义。有人对我如何解决这个问题有建议吗?我想在按下提交按钮后让图表自己绘制,但我该怎么做呢?