我正在寻找一种解决方案,以显示一些数据的基础上,您悬停在图表内.这是我的jsfiddle美元和我想要的东西的样带. gif image

我希望我已经把一切都说清楚了,提前谢谢你!

const chart = document.getElementById('chart');

new Chart(chart, {
  type: 'doughnut',
  data: {
    labels: ['Bitcoin', 'Ethereum', 'Litecoin', 'Tether'],
    datasets: [{
      data: [50, 30, 10, 10],
      backgroundColor: [
        'orange',
        'purple',
        'darkblue',
        'green'
      ],
      borderWidth: 2,
    }]
  },
  options: {
    responsive: false,
    cutout: '72%',
    plugins: {
      legend: {
        display: false,
      },
    },
  }
});
.chart-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-balance {
  position: absolute;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<div class="chart-wrap">
  <canvas id="chart" class="kz-relative kz-index-10" width="224" height="224"></canvas>
  <div class="chart-balance">
    <p class="chart-asset-type">Ethereum</p>
    <h5 class="chart-asset-value">$1,000</h5>
  </div>
</div>

推荐答案

使用Chart.js的问题在于它创建了一个画布,您不能使用外部脚本代码来操作这些元素.我真的不知道在Chart.js中是否有可能的配置来模拟您想要的东西,因为我对它知之甚少. 无论如何,我设法用普洛特利图书馆做到了这一点.使用它,我用循环图表完成了部分工作,然后,我们在HTML中有了SVG元素.有了这一点,我可以操作默认的工具提示值,使其显示在定制元素的中心.

完整的代码在那里,但我建议您查看Plotly文档,如果您不了解MutationObserver的话.

// https://cdn.jsdelivr.net/npm/plotly.js-dist@2.x.x/plotly.min.js

const data = {
    labels: ["Bitcoin", "Ethereum", "Litecoin", "Tether"],
    values: [50, 30, 10, 10],
    name: "",
    hoverinfo: "label+percent+name",
    hole: 0.65,
    type: "pie",
    marker: {
        colors: ["orange", "purple", "darkblue", "green"],
        line: {
            color: "white",
            width: 4
        }
    }
}

const layout = {
    paper_bgcolor: "transparent",
    height: 550,
    width: 550,
    showlegend: false
}

const chartElement = document.querySelector("#chart"),
    chartLabelElement = document.querySelector(".chart-label"),
    chartValueElement = document.querySelector(".chart-value")

chartLabelElement.textContent = data.labels[0]
chartValueElement.textContent = data.values[0]

Plotly.newPlot(chartElement, [data], layout).then(() => {
    observeTooltip()
})

function observeTooltip() {
    window.MutationObserver = window.MutationObserver ||
    window.WebKitMutationObserver || window.MozMutationObserver

    const observer = new MutationObserver(mutations => {
        const tooltipElement = chartElement.querySelector(".hovertext")

        if (tooltipElement) {
            tooltipElement.remove()

            const textElements = tooltipElement.querySelectorAll("tspan")

            const label = textElements[0].textContent.trim(),
                value = textElements[1].textContent.trim()

            chartLabelElement.textContent = label
            chartValueElement.textContent = value
        }
    })

    observer.observe(chartElement, {
        childList: true,
        subtree: true
    })
}
.modebar-container {
  display: none;
}

#chart {
  width: fit-content;
  margin: 0 auto;
}

#chart path.surface:hover {
  cursor: pointer;
  filter: opacity(0.7);
}

#chart .hovertext {
  display: none;
}

.chart-block {
  position: relative;
}

.chart-center {
  position: absolute;
  top: 0;
  width: 0;
  height: 100%;
  width: 100%;

  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  
  text-align: center;
  font-family: "Segoe UI", sans-serif;
}

.chart-label {
  color: #555;
  font-size: 18px;
  font-weight: 700;
}

.chart-value {
  color: #000;
  font-size: 48px;
  font-weight: 700;
}
<script src="https://cdn.jsdelivr.net/npm/plotly.js-dist@2.x.x/plotly.min.js"></script>
<div class="chart-block">
    <div class="chart-center">
        <div>
            <div class="chart-label"></div>
            <div class="chart-value"></div>
        </div>
    </div>
    <div id="chart"></div>
</div>

Javascript相关问答推荐

RxJS setTimeout操作符等效

如何访问Json返回的ASP.NET Core 6中的导航图像属性

使用useup时,React-Redux无法找到Redux上下文值

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

vscode扩展-webView Panel按钮不起任何作用

如何为我的astro页面中的相同组件自动创建不同的内容?

XSLT处理器未运行

在浏览器中触发插入事件时检索编码值的能力

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

Webpack在导入前混淆文件名

为什么JPG图像不能在VITE中导入以进行react ?

按下单键和多值

为列表中的项目设置动画

如何根据查询结果重新排列日期

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

为什么这个最小Angular 的Licial.dev设置不起作用?

需要刷新以查看Mern堆栈应用程序中的更改

观察子组件中的@Output事件emits 器?

在ReactJS上挂载组件时获得多个身份验证请求

如何使用Angular JS双击按钮