在Google饼图上禁用点击事件

问题描述 投票:1回答:2

我使用的是Google饼图,我想在饼图上禁用点击事件。这是我的代码:

google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['State Group', 'Names'],
      ['Andhra Pradesh', 9],
      ['Madhya Pradesh', 4.5],
      ['Tamilnadu / Pondicherry', 7],
      ['Karnataka', 2.5],
      ['Others', 1]
    ]);

    var options = {
      // title: 'My Daily Activities',
      pieSliceText: 'none',
      pieHole: 0.80,
          slices: {  0: {color: '#46B1C9'},
                1: {color: '#84C0C6'},
                2: {color: '#F37992'},
                3: {color: '#6A6478'},
                          4: {color: '#C9CCCA'},
      },
    };

    var chart = new google.visualization.PieChart(document.getElementById('donutchart'));

    chart.draw(data, options);
  }

我已经使用此代码禁用了点击事件,但是不知何故!

google.visualization.events.addListener(chart, 'ready', function(){
            $("#donutchart").unbind("click");
    });
javascript html5 pie-chart
2个回答
0
投票
 google.visualization.events.addListener(chart, 'ready', function(){
            $("#donutchart").unbind("click").click(onClick);

尝试一下...


0
投票

我几年前就知道这一点,但我一直在努力弄清楚这一点,并提出了一种解决方案。

我的问题是我正在使用HTML功能自定义工具提示,但是当我单击图形的一部分时,工具提示的css将被清除干净。

我能够使用Google图表为您提供的方法来隔离选择事件(getSelection(),请参阅文档here,这实际上令人困惑,因为他们将其直接放置在全局窗口中。

[一旦我发现我实际上将事件传递给了自定义处理程序,并做了一些陪审团的决定,以不禁用单击/选择事件(因为这似乎不太可能),而是保留我的自定义CSS(在我的用例之外,可以以多种方式使用此逻辑)

[您可以在下面看到我的代码,其中我使用返回的事件行,并将其与一些硬编码数据(名称,颜色)进行匹配,以更改这些工具提示CSS。然后,一旦我能够捕获并记录选择信息,就将其传递给处理程序以执行相同的操作。我还必须使用对象作为状态形式,因为由于某些原因,当您两次单击图表切片时,Google图表会返回一个空数组,而不是带有事件对象的数组。

希望这对以后的所有人有帮助!

google.charts.load("current", {packages: ["corechart"]});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
 var rows = [
  ['Savings Category', 'Savings'],
  ['Trade Savings', 11, ],
  ['Shipping Savings', 20],
  ['Bulk Savings', 12],
  ['Promo Savings', 12],
 ];

 var data = google.visualization.arrayToDataTable(rows);

 var colors = {
  "Trade Savings": '#0066a7',
  "Shipping Savings": '#54c0e9',
  "Bulk Savings": '#cccccc',
  "Promo Savings": '#818181',
 }

 var options = {
  click:'event',
  pieHole: 0.8,
  legend: 'none',
  height: '90%',
  width: '90%',
  pieSliceText: 'none',
  backgroundColor: '#f8f5f3',
  colors: ['#0066a7', '#54c0e9', '#cccccc', '#818181'],
  chartArea: {
   height: '70%',
   width: '70%',
  },
  slices: [{offset: 0.05}, {offset: 0.05}, {offset: 0.05}, {offset: 0.05}],
  tooltip: {
   isHtml: true,
   text: "value",
   textStyle: { fontSize: 11},
   trigger: 'focus',
  },
 };

 var chart = new google.visualization.PieChart(document.getElementById('doughnut-chart'));
 chart.draw(data, options);

 google.visualization.events.addListener(chart, 'onmouseover', toolTipHandler)

 google.visualization.events.addListener(chart, 'select', selectHandler);

 function selectHandler() {
  toolTipHandler(chart.getSelection()[0])
 }

 var chartState = {
  selectedSlice: null
 }

 function toolTipHandler(e) {

  var currSlice;

  if (!e) {
   currSlice = chartState.selectedSlice
   chartState.selectedSlice = null
  }else{
   currSlice = rows[e.row + 1]
   chartState.selectedSlice = rows[e.row + 1]
  }

  if (currSlice[0] === "Bulk Savings"){

   $(".google-visualization-tooltip").css("border-bottom", "2px solid " + colors[currSlice[0]])
   $(".google-visualization-tooltip").css({
    "position": "relative",
    "top": "-100px",
    "left": "0",
    "z-index": "1",
    "width": "160px",
    "color": "yellow"
  })
  }
  else if (currSlice[0] === "Trade Savings"){
   $(".google-visualization-tooltip").css("border-bottom", "2px solid " + colors[currSlice[0]])
   $(".google-visualization-tooltip").css({
    "position": "relative",
    "top": "-206px",
    "bottom": "0",
    "left": "260px",
    "width": "30%",
    "z-index": "1",
    "text-align": "center",
   })
  }
  else if (currSlice[0] === "Shipping Savings"){
   $(".google-visualization-tooltip").css("border-bottom", "2px solid " + colors[currSlice[0]])
   $(".google-visualization-tooltip").css({
    "position": "relative",
    "top": "-113px",
    "left": "42px",
    "z-index": "1",
    "padding": "0 40px 0 0",
    "margin": "0 0 0 20px",
    "text-align": "center",
   })
  }
  else{
   $(".google-visualization-tooltip").css("border-top", "2px solid " + colors[currSlice[0]])
   $(".google-visualization-tooltip").css({
    "position": "relative",
    "top": "-125px",
    "left": "-10px",
    "z-index": "1",
    "padding": "0 40px 0 0",
    "margin": "0 0 0 20px",
    "text-align": "center",
   })
  }
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.