【问题标题】:How can I use chart tooltip formatter in react-highcharts?如何在 react-highcharts 中使用图表工具提示格式化程序?
【发布时间】:2021-02-19 00:12:00
【问题描述】:

如何使用图表工具提示格式化程序? 我正在为 highcharts 使用反应包装器。
我有这样的配置:

const CHART_CONFIG = {
    ...
    tooltip:  
    {
        formatter: (tooltip) => {
            var s = '<b>' + this.x + '</b>';
            _.each(this.points, () => {
                s += '<br/>' + this.series.name + ': ' + this.y + 'm';
            });
            return s;
        },
        shared: true
    },
    ...
}    

但我无法使用此关键字访问图表范围,也无法从工具提示参数中获取点。 谢谢

【问题讨论】:

    标签: javascript reactjs highcharts react-redux react-highcharts


    【解决方案1】:

    OP 无法弄清楚如何使用 this 关键字访问图表的范围。简单的答案是因为 OP 使用的是粗箭头函数。相反,请尝试按照 OP 代码的此修改版本使用普通函数:

    const CHART_CONFIG = {
        ...
        tooltip:  
        {
            formatter() {   // Use a normal fn, not a fat arrow fn
                // Access properties using `this`
                // Return HTML string
            },
            shared: true
        },
        ...
    }
    

    【讨论】:

    • 这是因为箭头函数失去了对“this”的跟踪,我们必须使用普通函数:) 谢谢
    • 你是对的。谢谢。
    【解决方案2】:

    我已经遇到过这个问题。我已经通过创建一个函数来格式化工具提示,并将默认值应用于我想要的数据来解决它。

    这里是a live example,代码如下:

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import ReactHighcharts from 'react-highcharts';
    import './style.css';
    
    class App extends Component {
      static formatTooltip(tooltip, x = this.x, points = this.points) {
        let s = `<b>${x}</b>`;
        points.forEach((point) =>
          s += `<br/>${point.series.name}: ${point.y}m`
        );
    
        return s;
      }
    
      static getConfig = () => ({
        xAxis: {
          categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        tooltip: {
          formatter: App.formatTooltip,
          shared: true,
        },
        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }, {
            data: [194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]
        }],
      })
    
      render() {
        return (
          <div>
            <ReactHighcharts config={App.getConfig())} />
          </div>
        );
      }
    }
    
    render(<App />, document.getElementById('root'));
    

    【讨论】:

    • 你传递this.props.navs的目的是什么?我在渲染函数之前将它记录到控制台并且它是未定义的?
    【解决方案3】:

    这里还有另一种方法可以帮助您将 React 组件用作工具提示本身的一部分。

    const Item = ({name, value}) => <div> {name}: {value}</div>
    
    const config = { 
      formatter(this) {
        const container = document.createElement("div");
        return this.points.map(point => {
          ReactDOM.render(
            <Item name={point.series.name} value={point.y}/>
          )
          return container.innerHTML
        }
      }
    }
    

    【讨论】:

      【解决方案4】:

      Highcharts 预计 tootlip fromatter 将返回一个 HTML 字符串。当对 Highcharts 使用 react wrapper 时,可以使用格式化数据编写自定义工具提示组件,然后在设置格式化程序功能时的选项中,您可以使用 ReactDOMServer's renderToString 方法,该方法将从给定的元素。 虽然 renderToString 主要用于服务器端渲染,但它可以在浏览器环境中使用,没有任何问题。

      import ReactDOMServer from 'react-dom/server';
      import CustomTooltip from './CustomTooltip';
      
      const options = {
          tooltip: {
              formatter() {
                  return ReactDOMServer.renderToString(<CustomTooltip {...this} />);
              },
          },
      };
      

      【讨论】:

        【解决方案5】:

        我创建了一个工具提示切换器,可以用作常规工具提示或共享。

        isTooltipShared 是一个布尔属性,指示是否共享工具提示。

        const options = {
            tooltip: {
                      formatter: props.isTooltipShared ?
                        function () {
                          let s = `<b> ${this.x} </b>`;
                          this.points.forEach((point) => {
                            s += `<br/> ${point.series.name} : ${point.y}`;
                          });
                          return s;
                        } : function () {
                          return `${this.series.name} : <b> ${this.x} </b> - <b> ${this.y} </b>`;
                        },
                      shared: props.isTooltipShared,
                    },
        };
        

        并用于,

        <HighchartsReact
           highcharts={Highcharts}
           options={options}
        />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 2014-03-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多