【问题标题】:All tooltips always visible on react-google-charts on page load?在页面加载时,所有工具提示始终在 react-google-charts 上可见?
【发布时间】:2022-01-04 12:56:03
【问题描述】:

有没有一种方法可以显示所有工具提示,而无需悬停或选择 react-google-chart 上的某个部分?

设法使其与 Google 图表 (pureJS) 一起使用,但无法找到在 react-google-charts 中使用相同功能的方法。

google.load('visualization', '1.0', {
  'packages': ['corechart']
});
google.setOnLoadCallback(drawStuff);

function drawStuff() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Topping');
  data.addColumn('number', 'Slices');
  data.addRows([
    ['Mushrooms', 3],
    ['Onions', 1],
    ['Olives', 1],
    ['Zucchini', 1],
    ['Pepperoni', 2]
  ]);

  // Set chart options
  var options = {
    'title': 'How Much Pizza I Ate Last Night',
    'width': 400,
    'height': 300,
    tooltip: {
      trigger: 'selection',
      /* isHtml: true */
    }
  };

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  google.visualization.events.addListener(chart, 'ready', function(e) {
    chart.setSelection([{
      row: 0,
      column: null
    }, {
      row: 1,
      column: null
    }, {
      row: 2,
      column: null
    }, {
      row: 3,
      column: null
    }, {
      row: 4,
      column: null
    }]);
  });
  chart.draw(data, options);
};

<div id="chart_div"></div>


.chart {
  background-color: #eee;
}

https://jsfiddle.net/dahlin/cfpyu9m5/11/

【问题讨论】:

    标签: javascript reactjs react-google-charts


    【解决方案1】:

    试试这个,

    import React from "react";
    import { Chart } from "react-google-charts";
    
    export const data = [
      ["Task", "Hours per Day"],
      ["Work", 11],
      ["Eat", 2],
      ["Commute", 2],
      ["Watch TV", 2],
      ["Sleep", 7]
    ];
    
    export const options = {
      title: "My Daily Activities",
      tooltip: {
        trigger: "selection"
      }
    };
    
    export default function App() {
      return (
        <Chart
          chartType="PieChart"
          data={data}
          options={options}
          width={"100%"}
          height={"400px"}
          chartEvents={[
            {
              eventName: "ready",
              callback: ({ chartWrapper, google }) => {
                const chart = chartWrapper.getChart();
                chart.setSelection([
                  {
                    row: 0,
                    column: null
                  },
                  {
                    row: 1,
                    column: null
                  },
                  {
                    row: 2,
                    column: null
                  },
                  {
                    row: 3,
                    column: null
                  },
                  {
                    row: 4,
                    column: null
                  }
                ]);
              }
            }
          ]}
        />
      );
    }
    

    工作代码在这里 See the sandbox page

    【讨论】:

    • chart.setSelection 出错。
    • @DahlinCarneiro 我没有看到任何错误,它工作正常。我正在添加一个有效的沙盒链接,请查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多