【问题标题】:How to use a react component as the label in Highcharts?如何使用 React 组件作为 Highcharts 中的标签?
【发布时间】:2021-03-13 23:54:07
【问题描述】:

我有一个bubble map chart,它在地图上显示城市的位置。该地图具有默认标签,但我想在地图上使用custom react component 作为label。这是我的源代码,但它有错误并且不起作用:

import React, { Component, Fragment } from "react";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import HighchartsMap from "highcharts/modules/map";
import mapData from "@highcharts/map-collection/countries/gb/gb-all.geo.json";
import proj4 from "proj4";
import CustomLabel from "./CustomLabel";

HighchartsMap(Highcharts);

class BubbleMapChart extends Component {
  render() {
    const options = {
      chart: {
        map: "countries/gb/gb-all",
        proj4
      },
      series: [
        {
          name: "countries",
          nullColor: "#fff",
          showInLegend: false,
          mapData: mapData
        },
        {
          // Specify points using lat/lon
          type: "mapbubble",
          // PAY ATTENTION TO THIS SECTION - USE A CUSTOM LABEL COMPONENT
          dataLabels: {
            enabled: true,
            format: <CustomLabel name={"point.name"} />
          },
          minSize: "5%",
          maxSize: "15%",
          showInLegend: true,
          data: [
            {
              name: "London",
              lat: 51.507222,
              lon: -0.1275
            },
            {
              name: "Birmingham",
              lat: 52.483056,
              lon: -1.893611
            }
          ]
        }
      ]
    };

    return (
        <HighchartsReact
          highcharts={Highcharts}
          options={options}
          constructorType={"mapChart"}
        />
    );
  }
}

customLabel 组件为例:

 import React, { Component } from "react";
    class CustomLabel extends Component {
  render() {
    return (
      <div>
        {/* Doesn't show this Division (actually doesn't apply the style ...) */}
        <div
          style={{ BackgroundColor: "red", width: "10px", height: "10px" }}
        ></div>
        <span>{this.props.name}</span>

        <br />

        {/* Doesn't show the red bullet inside the text */}
        <Badge color="#f50" text={this.props.name} />
      </div>
    );
  }
}
    export default CustomLabel;

如何自定义 highcharts 中的数据标签?其实我想使用自定义组件作为标签。

【问题讨论】:

标签: javascript reactjs highcharts label react-highcharts


【解决方案1】:

在数据标签的格式化函数中使用ReactDOMServerrenderToStaticMarkuprenderToString 方法:

    dataLabels: {
      enabled: true,
      formatter: function () {
        return renderToStaticMarkup(<CustomLabel name={this.point.name} />);
      }
    }

现场演示: https://codesandbox.io/s/highcharts-react-demo-forked-40icn?file=/demo.jsx

文档: https://reactjs.org/docs/react-dom-server.html

API 参考: https://api.highcharts.com/highmaps/series.mapbubble.dataLabels.formatter


或者,如果您需要在 CustomLabel 中使用一些响应式逻辑,请利用 React 中的 Portal。


文档: https://reactjs.org/docs/portals.html

示例: https://www.npmjs.com/package/highcharts-react-official#how-to-add-react-component-to-a-charts-element

【讨论】:

  • 感谢您的回复,但不幸的是,在您的解决方案中,我们无法使用第三方组件。例如,如果我将antd 库中的Badge 组件放在customLabel 中,它就不起作用。这是徽章组件:&lt;Badge status="error" /&gt;
  • 嗨@Zahra Talebi,请在codesandbox中重现问题。
  • 我已经在代码沙箱中重现了这个问题。另外,我对上面的问题做了一些修改。谢谢codesandbox.io/s/highcharts-react-demo-forked-wkc5v?file=/…
  • 感谢您的示例。 Badge 组件在状态上传播,renderToStaticMarkup 方法没有完全转换它。你需要使用我上面提到的传送门。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2023-02-26
  • 1970-01-01
相关资源
最近更新 更多