简体   繁体   中英

Adding a small, custom icon and popup to a title of Google Charts

I'm using Google's Column and Bar charts https://developers.google.com/chart/interactive/docs/gallery/

All is fine except one thing: I want to add a small "i" icon next to a title of a chart. When I click or move a mouse over it, a custom popup should appear with some information.

Let's take this example https://developers.google.com/chart/interactive/docs/gallery/columnchart

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

I want that i-icon and popup which will appear to be placed right after the end of the title "...the Day"

I've gone through the documentation but haven't found anything. Is there a way to implement this?

PS Or perhaps there's a way to get a similar result, that is, place i-icon somewhere else on a chart? But it should be related to a whole chart.

you can use an overlay to place an "information" icon on the chart

then use css to show a "tooltip" when the icon is hovered

see following working snippet,
the overlay is placed within the chart's 'ready' event

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM