仅凭一组数据是无法实现的。让两条线对齐的简单方法是将收入水平轴的最大值设置为用于百分比的收入目标(因此它对应于 100%),并将最小值设置为等于您想要显示的收入水平(通常,0% 是最容易工作的)。像这样的东西:
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Year');
data.addColumn('number', 'Income');
data.addRows([
[2000, 35000],
[2001, 38000],
[2002, 42000],
[2003, 44000],
[2004, 47000],
[2005, 51000],
[2006, 55000],
[2007, 60000],
[2008, 61000],
[2009, 65000],
[2010, 59000],
[2011, 62000],
[2012, 63000]
]);
var targetIncome = 80000;
var minIncomePercent = 0;
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'Percent of Target',
calc: function (dt, row) {
return dt.getValue(row, 1) / targetIncome;
}
}, 1]);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, {
height: 400,
width: 600,
hAxis: {
format: '####'
},
vAxes: {
0: {
title: 'Income Level',
format: '$#,###',
minValue: targetIncome * minIncomePercent,
maxValue: targetIncome
},
1: {
title: 'Income Percentage of Target',
format: '#%',
minValue: minIncomePercent,
maxValue: 1
}
},
series: {
0: {
targetAxisIndex: 1,
enableInteractivity: false,
pointSize: 0,
lineWidth: 0,
visibleInLegend: false
},
1: {
targetAxisIndex: 0
}
}
});
}
见http://jsfiddle.net/asgallant/6N5mZ/