この回答は、ここで見つけたソリューションに基づいています: http://jsfiddle.net/asgallant/6Y8jF/2/
要点は、Google の伝説を隠して独自のものを構築することです。legend: {position: 'none'}
最初に、オプションの 1 つとしてを渡して、組み込みの凡例を無効にしますchart.draw
。
addDiv
チャートのホルダー div を作成する関数で、凡例を保持する 2 番目の要素を追加します。
function addDiv(parent_id, div_id) {
$("#" + parent_id).append('<div id="' + div_id + '" class="chart-chart"></div><ul id="legend_' + div_id + '" class="chart-legend"></ul>');
}
次に、drawChart
関数で凡例を作成します。
function drawChart(chart, original_table,
x_attr, y_attr, x_axis_lbl, y_axis_lbl, x_min_max,
div_id) { //pass div_id to this function to be able to get legend element
var google_table = allSeriesToGoogleTable(original_table,
x_attr, y_attr, ranking_titles);
var colors = ["#3366cc","#dc3912","#ff9900"]; //use whatever colors you like
var options = {'chartArea':{width:"60%"},
vAxis: {'title': y_axis_lbl}, 'hAxis': {'title': x_axis_lbl},
'interpolateNulls':true,
colors: colors, //use the same colors for the chart and the legend
legend: {position: 'none'} //hide default legend
};
var legend = $('#legend_' + div_id);
for (var i = 1; i < ranking_titles.length; i++) {
var li = $('<li></li>'),
marker = $('<div class="legendMarker"></div>'),
explanation = $('<span></span>');
explanation.text(ranking_titles[i]); // legend marker text
marker.css( {backgroundColor: colors[i-1]} ); //marker color
li.append(marker).append(explanation);
legend.append(li);
}
if ( ! x_min_max === undefined )
//do something
chart.draw( google_table, options );
// add the data table to the chart
chart.google_table = google_table;
}
もちろん、フィドルからの CSS も必ず含めてください。
.chart-chart {
float: left;
}
.chart-legend {
margin: 30px 0 0 0;
float: left;
list-style: none;
}
div.legendMarker {
height: 1em;
width: 1em;
display: inline-block;
margin: 0 5px 0 0;
}
コードをフィドルに入れることができなかったので、これはテストされていませんが、99% はそこに到達するはずです。