如何在Google图表中为图例添加工具提示

3
使用最新版本的Google Charts API。
我有一个简单的条形图,希望在悬停在图例元素上时显示工具提示,以解释每个图例项的含义。 我仍希望在条形上保留相同的工具提示,并显示其标签和值。
<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年人口图例元素上时,您该如何添加一个工具提示,显示“数据来自2010年美国人口普查”? 更新 找到答案后,我已经制作了一个CodePen来展示如何使图例中的每个元素显示不同的描述。希望这能帮助未来的某个人。

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>

1个回答

4

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

请参见以下工作示例,

在这里,我使用图表事件onmouseoveronmouseout
以了解何时“悬停”在图例上

当事件触发时,如果事件的row属性为null
则正在悬停在图例上

我还使用getChartLayoutInterface将工具提示定位在图例附近

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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接