如何在Google图表中的图例上添加工具提示 [英] How to add a tooltip to the legend in google charts

查看:80
本文介绍了如何在Google图表中的图例上添加工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新版本的Google Charts API。

Using the newest version of the Google Charts API.

我有一个简单的条形图,当我将鼠标悬停在图例中的元素上时,我想显示一个工具提示解释图例中的每个项目是什么。我仍然希望条形图上的工具提示保持不变,并显示其标签和值。

I have a simple bar chart and I would like to show a tooltip when hovering over the elements in the legend which explain what each item in the legend is. I would still like the tooltip on the bars to stay the same and display its labels and values.

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">

google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawBasic);

function drawBasic() {

      var data = google.visualization.arrayToDataTable([
        ['City', '2010 Population',],
        ['New York City, NY', 8175000],
        ['Los Angeles, CA', 3792000],
        ['Chicago, IL', 2695000],
        ['Houston, TX', 2099000],
        ['Philadelphia, PA', 1526000]
      ]);

      var options = {
        title: 'Population of Largest U.S. Cities',
        chartArea: {width: '50%'},
        hAxis: {
          title: 'Total Population',
          minValue: 0
        },
        vAxis: {
          title: 'City'
        }
      };

      var chart = new google.visualization.BarChart(document.getElementById('chart_div'));

      chart.draw(data, options);
    }

     </script>
    </head> 
  <body>
 <div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>

在上图中,当您将鼠标悬停在上面时,会在 2010年美国人口普查数据上添加提示信息吗?

In the above chart, how would you add a tooltip to the 2010 population legend element when you hover over it that says "Data taken from 2010 US Census"?

更新

弄清楚答案后,我制作了一个代码笔以显示如何使图例中的每个元素显示不同的描述。我希望这对以后的人有所帮助。

After figuring out the answer I've made a codepen to show how you can make each element in the legend show different descriptions. I hope this helps someone in the future.

https://codepen.io/jonnyske7ch/pen/bWzmxW

google.charts.load('current', {
  callback: drawBasic,
  packages: ['corechart']
});

function drawBasic() {
  var data = google.visualization.arrayToDataTable([
    ['City', '2010 Population', '2011 Population'],
    ['New York City, NY', 8175000, 3792000],
    ['Los Angeles, CA', 3792000, 1526000],
    ['Chicago, IL', 2695000, 8175000],
    ['Houston, TX', 2099000, 2695000],
    ['Philadelphia, PA', 1526000, 2099000]
  ]);

  var options = {
    title: 'Population of Largest U.S. Cities',
    chartArea: {width: '50%'},
    hAxis: {
      title: 'Total Population',
      minValue: 0
    },
    vAxis: {
      title: 'City'
    }
  };

  var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
  var legendTooltip = document.getElementById('legend-tooltip');

  // set legend tooltip position
  google.visualization.events.addListener(chart, 'ready', function (gglEvent) {
    var chartLayout = chart.getChartLayoutInterface();
    var legendBounds = chartLayout.getBoundingBox('legend');
    legendTooltip.style.top = (legendBounds.top + (legendBounds.height * 2)) + 'px';
    legendTooltip.style.left = legendBounds.left + 'px';
  });

  // show legend tooltip
  google.visualization.events.addListener(chart, 'onmouseover', function (gglEvent) {
    if (gglEvent.row === null) {
			if (data.getColumnLabel(gglEvent.column) === '2010 Population') {
				$('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2010 Census');
			} else if (data.getColumnLabel(gglEvent.column) === '2011 Population') {
				$('#series-name').html(data.getColumnLabel(gglEvent.column) + ' - Data from 2011 Census');
			}
      $(legendTooltip).removeClass('hidden');
    }
  });

  // hide legend tooltip
  google.visualization.events.addListener(chart, 'onmouseout', function (gglEvent) {
    if (gglEvent.row === null) {
      $(legendTooltip).addClass('hidden');
    }
  });

  chart.draw(data, options);
}

.hidden {
  display: none;
  visibility: hidden;
}

.ggl-tooltip {
  background-color: #ffffff;
  border: 1px solid #E0E0E0;
  display: inline-block;
  font-size: 10pt;
  padding: 8px 8px 8px 8px;
  position: absolute;
}

.ggl-tooltip div {
  margin-top: 4px;
}

.ggl-tooltip 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 id="chart_div"></div>
<div id="legend-tooltip" class="ggl-tooltip hidden">
  <div id="series-name"></div>
</div>

推荐答案

图例条目没有标准的工具提示,但是您可以添加自己的...

no standard tooltip for legend entries, but you can add your own...

参见以下工作片段,

在这里,我使用图表事件 onmouseover onmouseout

知道何时传说已悬停

here, i use chart events onmouseover and onmouseout,
to know when the legend has been "hovered"

事件发生时,如果 row 事件的属性为 null

,则说明将悬停在图例

when the events fire, if the row property of the event is null,
then the legend is being hovered

我还使用 getChartLayoutInterface 将工具提示放置在图例附近

i also use getChartLayoutInterface to position the tooltip near the legend

google.charts.load('current', {
  callback: drawBasic,
  packages: ['corechart']
});

function drawBasic() {
  var data = google.visualization.arrayToDataTable([
    ['City', '2010 Population',],
    ['New York City, NY', 8175000],
    ['Los Angeles, CA', 3792000],
    ['Chicago, IL', 2695000],
    ['Houston, TX', 2099000],
    ['Philadelphia, PA', 1526000]
  ]);

  var options = {
    title: 'Population of Largest U.S. Cities',
    chartArea: {width: '50%'},
    hAxis: {
      title: 'Total Population',
      minValue: 0
    },
    vAxis: {
      title: 'City'
    }
  };

  var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
  var legendTooltip = document.getElementById('legend-tooltip');

  // set legend tooltip position
  google.visualization.events.addListener(chart, 'ready', function (gglEvent) {
    var chartLayout = chart.getChartLayoutInterface();
    var legendBounds = chartLayout.getBoundingBox('legend');
    legendTooltip.style.top = (legendBounds.top + (legendBounds.height * 2)) + 'px';
    legendTooltip.style.left = legendBounds.left + 'px';
  });

  // show legend tooltip
  google.visualization.events.addListener(chart, 'onmouseover', function (gglEvent) {
    if (gglEvent.row === null) {
      $('#series-name').html(data.getColumnLabel(gglEvent.column));
      $(legendTooltip).removeClass('hidden');
    }
  });

  // hide legend tooltip
  google.visualization.events.addListener(chart, 'onmouseout', function (gglEvent) {
    if (gglEvent.row === null) {
      $(legendTooltip).addClass('hidden');
    }
  });

  chart.draw(data, options);
}

.hidden {
  display: none;
  visibility: hidden;
}

.ggl-tooltip {
  background-color: #ffffff;
  border: 1px solid #E0E0E0;
  display: inline-block;
  font-size: 10pt;
  padding: 8px 8px 8px 8px;
  position: absolute;
}

.ggl-tooltip div {
  margin-top: 4px;
}

.ggl-tooltip 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 id="chart_div"></div>
<div id="legend-tooltip" class="ggl-tooltip hidden">
  <div><span>Series Info</span></div>
  <div id="series-name"></div>
</div>

这篇关于如何在Google图表中的图例上添加工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆