javascript - 使用 morris.js 从条形图中删除悬停图例

标签 javascript svg charts morris.js

我正在使用 morris.js library生成图表。

我想从图表中删除悬停图例。请查看下图以便更好地理解。我怎样才能删除它们?有什么办法解决吗?提前致谢。

example chart showing what to remove

new Morris.Bar({
  // ID of the element in which to draw the chart.
  element: 'myfirstchart',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [
    {year: '2008', value: 20},
    {year: '2009', value: 10},
    {year: '2010', value: 5},
    {year: '2011', value: 5},
    {year: '2012', value: 20}
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'year',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value']
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="myfirstchart" style="height: 250px;"></div>

最佳答案

要禁用悬停图例,请使用 hideHover 属性并将其设置为 'always'

hideHover 属性接受以下值:

  • false( bool 值)- 始终显示悬停图例
  • true( bool 值)或 'auto'(字符串)- 当鼠标光标悬停在图表上时,仅显示悬停图例
  • 'always'(字符串)- 从不显示悬停图例

new Morris.Bar({
  // ID of the element in which to draw the chart.
  element: 'myfirstchart',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [
    {year: '2008', value: 20},
    {year: '2009', value: 10},
    {year: '2010', value: 5},
    {year: '2011', value: 5},
    {year: '2012', value: 20}
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'year',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value'],
  // Remove hover legend
  hideHover: 'always'
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="myfirstchart" style="height: 250px;"></div>

您还可以通过将 callback 函数分配给 hoverCallback 属性来自定义悬停图例:

new Morris.Bar({
  // ID of the element in which to draw the chart.
  element: 'myfirstchart',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [
    {year: '2008', value: 20},
    {year: '2009', value: 10},
    {year: '2010', value: 5},
    {year: '2011', value: 5},
    {year: '2012', value: 20}
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'year',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value'],
  // Customized hover legend via a callback
  hoverCallback: function (index, options, content, row) {
    return 'Legend ' + index + '<br> on year ' + row.year + '<br> with value ' + row.value;
  }
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="myfirstchart" style="height: 250px;"></div>

Check out the morris.js API for line & area charts

关于javascript - 使用 morris.js 从条形图中删除悬停图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51133544/

相关文章:

javascript - 如何防止 jQuery 插件更改我的 DOM?

jquery - 如何使用 jQuery 在 Chrome 中拖动 SVG 元素而不产生偏移? (火狐浏览器没问题)

javascript - 如何为nvd3.js图表​​绘制上限

javascript - 每个循环中有多个 Google 图表

javascript - 如何将一个查询的值(value)添加到另一个查询

javascript - 为什么这个二维数组的索引返回-1

javascript - 是否可以通过按 enter 从 HTML 表单中的一个控件移动到另一个控件?

javascript - 将 svg 转换为 Highcharts map

javascript - 使用 javascript 更改 svg 内所有元素的填充

python - matplotlib 中超过 9 个子图