需要帮助在切片顶部居中放置饼图数据标签. 我正在使用Reaction HighChart.

目前图表看起来是这样的.

Current PIE chart

标签居中的示例代码:

function redrawDatalabels() {
  var chart = this,
    cX = chart.series[0].center[0],
    cY = chart.series[0].center[1],
    shapeArgs,
    ang,
    posX,
    posY,
    bBox;

  Highchart.each(chart.series[0].data, function (point, i) {
    if (point.dataLabel) {
      bBox = point.dataLabel.getBBox();
      shapeArgs = point.shapeArgs;
      ang = (shapeArgs.end - shapeArgs.start) / 2 + shapeArgs.start;
      const labelPos = point.labelPosition.alignment === "right" ? 1 : -1;
      posX = cX + (shapeArgs.r / 2) * Math.cos(ang) + (labelPos * bBox.width) / 2;
      posY = cY + (shapeArgs.r / 2) * Math.sin(ang) - bBox.height / 2;
      point.dataLabel._pos.x = posX;
      point.dataLabel._pos.y = posY;
    }
  });
  chart.series[0].placeDataLabels();
}

CodeSandBox:https://codesandbox.io/s/hc-animate-9j72hn?file=/src/App.js

推荐答案

您可以将标签放在图表切片的顶部居中,主要更改将在options.plotOptions.pie.dataLabels.distance属性上进行,以控制标签的距离.

因此,固定代码将如下所示:

import Highcharts from "highcharts";
import { each } from "lodash";
import { PureComponent } from "react";
import "./styles.css";

function redrawDatalabels() {
  var chart = this,
    cX = chart.plotWidth / 2,
    cY = chart.plotHeight / 2,
    shapeArgs,
    ang,
    posX,
    posY,
    bBox;

  each(chart.series[0].data, function (point, i) {
    if (point.dataLabel) {
      bBox = point.dataLabel.getBBox();
      shapeArgs = point.shapeArgs;
      ang = (shapeArgs.end - shapeArgs.start) / 2 + shapeArgs.start;
      let labelPos = point.labelPosition.alignment === "right" ? 1 : -1;
      labelPos = (labelPos * bBox.width) / 2;
      posX = cX + (shapeArgs.r / 2) * Math.cos(ang) + labelPos;
      posY = cY + (shapeArgs.r / 2) * Math.sin(ang) - bBox.height / 2;
      point.dataLabel._pos.x = posX;
      point.dataLabel._pos.y = posY;
    }
  });
  chart.series[0].placeDataLabels();
}


const options = {
  chart: {
    animation: false,
    renderTo: "chart1",
    type: "pie",
  },
  legend: {
    enabled: true,
  },
  plotOptions: {
    chart: {
      animation: false,
      renderTo: "chart1",
      type: "pie",
      events: {
        load: redrawDatalabels,
        redraw: redrawDatalabels
      }
    },
    legend: {
      enabled: true
    },
    pie: {
      allowPointSelect: true,
      cursor: "pointer",
      dataLabels: {
        enabled: true,
        connectorWidth: 0,
        format: "{point.percentage:.1f}%",
        distance: -25, // Adjust the distance of labels from the cente
        style: {
          color:
            (Highcharts.theme && Highcharts.theme.contrastTextColor) || "black",
          textOutline: "none", 
        },
      },
      size: "80%",
      innerSize: "65%",
    },
  },
  series: [
    {
      type: "pie",
      name: "Browser share",
      data: [
        ["Firefox", 50],
        ["IE", 10],
        ["Chrome", 30],
        ["Safari", 5],
        ["Opera", 5],
      ],
    },
  ],
};

class App extends PureComponent {
  componentDidMount() {
    this.chart1 = Highcharts.chart(options);
  }

  render() {
    return (
      <div className="App">
        <div id="chart1" />
      </div>
    );
  }
}

export default App;

我在您的Sandbox 上编辑了这段代码,它看起来是这样的:

enter image description here

Javascript相关问答推荐

React中的表格中 Select Radio按钮

使用expressjs加载Nuxt版本=3.9.0(自定义服务器)

CSS背景过滤器忽略转换持续时间(ReactJS)

如何指定1条记录1个表?

创建私有JS出口

使用print This时, map 容器已在LeafletJS中初始化

GrapeJS -如何保存和加载自定义页面

格式值未保存在redux持久切片中

使用复选框在d3.js多折线图中添加或删除线条

切换时排序对象数组,切换不起作用

为什么!逗号和空格不会作为输出返回,如果它在参数上?

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

使用Promise.All并发解决时,每个promise 的线性时间增加?

在Java中寻找三次Bezier曲线上的点及其Angular

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

按什么顺序接收`storage`事件?

每次重新呈现时调用useState initialValue函数

在验证和提交表单后使用useNavigate()进行react 重定向,使用带有加载器和操作的路由

有没有办法通过使用不同数组中的值进行排序

为什么我不能使用其同级元素调用和更新子元素?