Google Scatter Chart多个工具提示相互重叠

时间:2018-04-27 11:22:21

标签: javascript html css charts google-visualization

我正在使用谷歌图表并默认显示3个工具提示,当我加载图表时使用

tooltip: { trigger: 'selection' },
aggregationTarget: 'none',
selectionMode: 'multiple'

每件事情都很好但是当点数彼此非常接近时,如(1,1)和(1,1.5),工具提示相互交叉please see this image。有没有选择或方法可以解决这个问题。



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

function drawScatterChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'brand');
  data.addColumn('number', 'store');
  data.addColumn({
    type: 'string',
    role: 'tooltip'
  });
  data.addColumn({
    type: 'string',
    role: 'style'
  });
  var datarows = "";
  var json = JSON.parse('[{"store":4.73977695167286,"brand":4.95353159851301,"empty":false,"tooltip":true,"tooltiptext":"Alpha Bravo"},{"store":0.362526138475839,"brand":0.835487337360595,"empty":false,"tooltip":true,"tooltiptext":"Avg. For Age"},{"store":0.598803222730479,"brand":1.14256989262171,"empty":false,"tooltip":true,"tooltiptext":"Franchisee Avg."},{"store":0.0929368029739777,"brand":0.288104089219331,"empty":false,"tooltip":true,"tooltiptext":"Andrew Richardson"},{"store":4.64684014869888,"brand":7.00743494423792,"empty":false,"tooltip":true,"tooltiptext":"April Singer"},{"store":4.64684014869888,"brand":7.00743494423792,"empty":false,"tooltip":true,"tooltiptext":"April Singer"}]');
  $.each(json, function(ind, ele) {
    //Every row given must be either null or an array.
    var arr = [];
    //'point {stroke-color: #A3A3A3; stroke-width: 0; fill-color: none;');/
    if (ele.empty == true) {
      arr.push(ele.brand, ele.store, "", 'point {stroke-color: #A3A3A3; stroke-width: 0; fill-color: none;'); //" Franchise Avg. ");fill-color: #a52714;
    } else {
      if (ele.tooltip == true) {
        datarows += ind + ",";
      }
      arr.push(ele.brand, ele.store, ele.tooltiptext, 'point {stroke-color: #A3A3A3; stroke-width: 1'); //" Franchise Avg. ");fill-color: #a52714;
    }
    data.addRows([arr]);
  });

  var container = document.getElementById('chart_div');
  var chart = new google.visualization.ScatterChart(container);
  var options = {
    width: 500,
    height: 440,
    legend: 'none',
    hAxis: {
      title: 'Brand Engagement',
      gridlines: {
        count: 11
      }
    }, //, minValue: 0 
    vAxis: {
      title: 'Performance',
      gridlines: {
        count: 11
      }
    }, //, minValue: 0 
    backgroundColor: 'none',
    colors: ['#fff'],
    chartArea: {
      left: 50,
      top: 20,
      width: "80%",
      height: "87%"
    },
    tooltip: {
      isHtml: true,
      trigger: 'selection'
    },
    aggregationTarget: 'none',
    pointSize: 5,
    selectionMode: 'multiple',
  };
  google.visualization.events.addListener(chart, 'ready', setChartSelection);

  function setChartSelection() {
    var arrRows = datarows.split(",");
    chart.setSelection([{
        row: arrRows[0],
        column: 1
      },
      {
        row: arrRows[1],
        column: 1
      },
      {
        row: arrRows[2],
        column: 1
      }
    ]);
  }

  
  chart.draw(data, options);
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chart_div" style="float: left;">
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以设置任何选项以避免重叠......

一种可能的解决方案是使用资源管理器选项进行平移和缩放

explorer: {
  actions: ['dragToZoom', 'rightClickToReset']
}

在图表加载时,请参阅以下工作代码段 用鼠标在左下角的两个点周围画一个正方形,
一旦放大,两个工具提示都会完全显示......

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

function drawScatterChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'brand');
  data.addColumn('number', 'store');
  data.addColumn({
    type: 'string',
    role: 'tooltip'
  });
  data.addColumn({
    type: 'string',
    role: 'style'
  });
  var datarows = "";
  var json = JSON.parse('[{"store":4.73977695167286,"brand":4.95353159851301,"empty":false,"tooltip":true,"tooltiptext":"Alpha Bravo"},{"store":0.362526138475839,"brand":0.835487337360595,"empty":false,"tooltip":true,"tooltiptext":"Avg. For Age"},{"store":0.598803222730479,"brand":1.14256989262171,"empty":false,"tooltip":true,"tooltiptext":"Franchisee Avg."},{"store":0.0929368029739777,"brand":0.288104089219331,"empty":false,"tooltip":true,"tooltiptext":"Andrew Richardson"},{"store":4.64684014869888,"brand":7.00743494423792,"empty":false,"tooltip":true,"tooltiptext":"April Singer"},{"store":4.64684014869888,"brand":7.00743494423792,"empty":false,"tooltip":true,"tooltiptext":"April Singer"}]');
  $.each(json, function(ind, ele) {
    //Every row given must be either null or an array.
    var arr = [];
    //'point {stroke-color: #A3A3A3; stroke-width: 0; fill-color: none;');/
    if (ele.empty == true) {
      arr.push(ele.brand, ele.store, "", 'point {stroke-color: #A3A3A3; stroke-width: 0; fill-color: none;'); //" Franchise Avg. ");fill-color: #a52714;
    } else {
      if (ele.tooltip == true) {
        datarows += ind + ",";
      }
      arr.push(ele.brand, ele.store, ele.tooltiptext, 'point {stroke-color: #A3A3A3; stroke-width: 1'); //" Franchise Avg. ");fill-color: #a52714;
    }
    data.addRows([arr]);
  });

  var container = document.getElementById('chart_div');
  var chart = new google.visualization.ScatterChart(container);
  var options = {
    width: 500,
    height: 440,
    legend: 'none',
    hAxis: {
      title: 'Brand Engagement',
      gridlines: {
        count: 11
      }
    }, //, minValue: 0
    vAxis: {
      title: 'Performance',
      gridlines: {
        count: 11
      }
    }, //, minValue: 0
    backgroundColor: 'none',
    colors: ['#fff'],
    chartArea: {
      left: 50,
      top: 20,
      width: "80%",
      height: "87%"
    },
    tooltip: {
      isHtml: true,
      trigger: 'selection'
    },
    aggregationTarget: 'none',
    pointSize: 5,
    selectionMode: 'multiple',
    explorer: {
      actions: ['dragToZoom', 'rightClickToReset']
    }
  };
  google.visualization.events.addListener(chart, 'ready', setChartSelection);

  function setChartSelection() {
    var arrRows = datarows.split(",");
    chart.setSelection([{
        row: arrRows[0],
        column: 1
      },
      {
        row: arrRows[1],
        column: 1
      },
      {
        row: arrRows[2],
        column: 1
      }
    ]);
  }


  chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chart_div" style="float: left;">
</div>