2017-09-25 58 views
0

我使用谷歌的柱形圖和條形圖https://developers.google.com/chart/interactive/docs/gallery/添加小,自定義圖標,彈出窗口,讓谷歌圖表的標題

一切都很好,除了一兩件事:我要新增一個小的「i」圖標圖表的標題。當我點擊將鼠標移到它上面時,會出現一個自定義彈出窗口並顯示一些信息。

讓我們這個例子中https://developers.google.com/chart/interactive/docs/gallery/columnchart

var options = { 
    title: 'Motivation and Energy Level Throughout the Day', 

我想,我圖標和彈出式將出現標題爲「...日」

我結束之後要正確放置已經通過文檔,但沒有發現任何東西。有沒有辦法來實現這個?

P.S. 或者也許有一種方法可以得到類似的結果,也就是說,將i圖標放在圖表的其他位置?但它應該與整個圖表有關。

+0

@WhiteHat,那對我來說不起作用,疊加是爲了一些其他的目標 - >它總是可見的,就像一個水印 - >不是我所需要的。 – Jori

+0

@WhiteHat,並彈出? – Jori

+0

@WhiteHat,好的,但問題是:如何在鼠標懸停在i圖標上時彈出窗口,無論它是何種類型的彈出窗口? – Jori

回答

0

您可以使用overlay放置圖表

上的「信息」圖標,然後使用CSS來顯示一個「工具提示」當圖標懸停

見下工作片斷,
的疊加層放置圖表的'ready'事件內

google.charts.load('current', { 
 
    packages: ['controls', 'corechart', 'table'] 
 
}).then(function() { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('timeofday', 'Time of Day'); 
 
    data.addColumn('number', 'Motivation Level'); 
 
    data.addRows([ 
 
    [{v: [8, 0, 0], f: '8 am'}, 1], 
 
    [{v: [9, 0, 0], f: '9 am'}, 2], 
 
    [{v: [10, 0, 0], f:'10 am'}, 3], 
 
    [{v: [11, 0, 0], f: '11 am'}, 4], 
 
    [{v: [12, 0, 0], f: '12 pm'}, 5], 
 
    [{v: [13, 0, 0], f: '1 pm'}, 6], 
 
    [{v: [14, 0, 0], f: '2 pm'}, 7], 
 
    [{v: [15, 0, 0], f: '3 pm'}, 8], 
 
    [{v: [16, 0, 0], f: '4 pm'}, 9], 
 
    [{v: [17, 0, 0], f: '5 pm'}, 10], 
 
    ]); 
 

 
    var options = { 
 
    hAxis: { 
 
     title: 'Time of Day', 
 
     format: 'h:mm a', 
 
     viewWindow: { 
 
     min: [7, 30, 0], 
 
     max: [17, 30, 0] 
 
     } 
 
    }, 
 
    legend: { 
 
     alignment: 'end', 
 
     position: 'bottom' 
 
    }, 
 
    title: 'Motivation and Energy Level Throughout the Day', 
 
    vAxis: { 
 
     title: 'Rating (scale of 1-10)', 
 
     ticks: [ 
 
     {v: 8.5, f: 'A'}, 
 
     {v: 4.5, f: 'B'}, 
 
     {v: 2, f: 'C'} 
 
     ] 
 
    } 
 
    }; 
 

 
    var container = document.getElementById('chart'); 
 
    var chart = new google.visualization.ColumnChart(container); 
 

 
    google.visualization.events.addListener(chart, 'ready', function() { 
 
    var chartLayout = chart.getChartLayoutInterface(); 
 
    var chartPosition = $('#chart').position(); 
 
    var titleLeft = 24; 
 
    var titlePosition = chartLayout.getBoundingBox('title'); 
 
    $('#chart-overlay').css({ 
 
     left: (chartPosition.left + titlePosition.left - titleLeft) + 'px', 
 
     top: (chartPosition.top + titlePosition.top) + 'px' 
 
    }).removeClass('hidden'); 
 
    }); 
 

 
    chart.draw(data, options); 
 
});
.hidden { 
 
    display: none; 
 
    visibility: hidden; 
 
} 
 

 
.overlay { 
 
    position: absolute; 
 
    z-index: 1; 
 
} 
 

 
.overlay:hover .popup { 
 
    display: inline-block; 
 
} 
 

 
.popup { 
 
    background-color: #fff9c4; 
 
    border: 1px solid #ffd54f; 
 
    display: none; 
 
    padding: 6px; 
 
} 
 

 
.popup span { 
 
    font-weight: bold; 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 

 
<div class="hidden overlay" id="chart-overlay"> 
 
    <div> 
 
    <img alt="Information" src="http://findicons.com/files/icons/2166/oxygen/16/dialog_information.png" /> 
 
    </div> 
 
    <div class="popup"> 
 
    <div> 
 
     <span>Information:</span> 
 
    </div> 
 
    <div> 
 
     This is the information for the tooltip... 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<div id="chart"></div>