我想使用Google Charts中的烛台或折线图。 有没有办法从图形的最右边(最后一个值)到Y轴绘制一条水平线,Y轴显示在轴上? 我只想在图表上清楚地显示“最后”点的Y值。
答案 0 :(得分:1)
由于Google图表使用HTML5 / SVG呈现,您可以考虑以下解决方案来呈现自定义水平轴:
google.load('visualization', '1.1', { packages: ['corechart'] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales'],
['2004', 1000],
['2005', 1170],
['2006', 660],
['2007', 1030]
]);
var options = {
title: 'Company Performance',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
drawAdditionalHAxis(chart, data); //render custom axis (line & label) for the last value
}
function drawAdditionalHAxis(chart,dataTable){
var layout = chart.getChartLayoutInterface();
var chartArea = layout.getChartAreaBoundingBox();
var svg = chart.getContainer().getElementsByTagName('svg')[0];
var lastVal = dataTable.getValue(dataTable.getNumberOfRows()-1,1);
var yLoc = layout.getYLocation(lastVal);
svg.appendChild(createLine(chartArea.left,yLoc,chartArea.width + chartArea.left,yLoc,'#cccccc',1)); // axis line
svg.appendChild(createText(chartArea.left - 40 ,yLoc + 5,'Arial','13',lastVal)); // axis label
}
function createLine(x1, y1, x2, y2, color, w) {
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', x1);
line.setAttribute('y1', y1);
line.setAttribute('x2', x2);
line.setAttribute('y2', y2);
line.setAttribute('stroke', color);
line.setAttribute('stroke-width', w);
return line;
}
function createText(x, y, fontFamily, fontSize,value) {
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', x);
text.setAttribute('y', y);
text.setAttribute('font-family', fontFamily);
text.setAttribute('font-size', fontSize);
text.innerHTML = value;
return text;
}

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart" style="width: 640px; height: 480px"></div>
&#13;