reactjs - Chartjs- 2 圆环图自定义工具提示

标签 reactjs charts tooltip donut-chart

我想使用 Chartjs-2 在 reactjs 中显示一些数据。
为了显示我的数据,我想要一个 donut 饼图。当我使用特定的饼图时,它显示我如下:
enter image description here

但所需的输出应该是这样的:
enter image description here

我从文档中读到我应该从选项中编辑工具提示,但我找不到这样做的方法。你知道如何?

最佳答案

您需要在图表选项中实现您的自定义工具提示。您必须用您想要的样式和大小替换 html 元素样式和大小。

var myPieChart = new Chart(ctx, {
    type: 'pie',
    data: data,
    options: {
        tooltips: {
            custom: function(tooltipModel) {
                // Tooltip Element
                var tooltipEl = document.getElementById('chartjs-tooltip');

                // Create element on first render
                if (!tooltipEl) {
                    tooltipEl = document.createElement('div');
                    tooltipEl.id = 'chartjs-tooltip';
                    tooltipEl.innerHTML = "<table></table>"
                    document.body.appendChild(tooltipEl);
                }

                // Hide if no tooltip
                if (tooltipModel.opacity === 0) {
                    tooltipEl.style.opacity = 0;
                    return;
                }

                // Set caret Position
                tooltipEl.classList.remove('above', 'below', 'no-transform');
                if (tooltipModel.yAlign) {
                    tooltipEl.classList.add(tooltipModel.yAlign);
                } else {
                    tooltipEl.classList.add('no-transform');
                }

                function getBody(bodyItem) {
                    return bodyItem.lines;
                }

                // Set Text
                if (tooltipModel.body) {
                    var titleLines = tooltipModel.title || [];
                    var bodyLines = tooltipModel.body.map(getBody);

                    var innerHtml = '<thead>';

                    titleLines.forEach(function(title) {
                        innerHtml += '<tr><th>' + title + '</th></tr>';
                    });
                    innerHtml += '</thead><tbody>';

                    bodyLines.forEach(function(body, i) {
                        var colors = tooltipModel.labelColors[i];
                        var style = 'background:' + colors.backgroundColor;
                        style += '; border-color:' + colors.borderColor;
                        style += '; border-width: 2px';
                        var span = '<span class="chartjs-tooltip-key" style="' + style + '"></span>';
                        innerHtml += '<tr><td>' + span + body + '</td></tr>';
                    });
                    innerHtml += '</tbody>';

                    var tableRoot = tooltipEl.querySelector('table');
                    tableRoot.innerHTML = innerHtml;
                }

                // `this` will be the overall tooltip
                var position = this._chart.canvas.getBoundingClientRect();

                // Display, position, and set styles for font
                tooltipEl.style.opacity = 1;
                tooltipEl.style.left = position.left + tooltipModel.caretX + 'px';
                tooltipEl.style.top = position.top + tooltipModel.caretY + 'px';
                tooltipEl.style.fontFamily = tooltipModel._fontFamily;
                tooltipEl.style.fontSize = tooltipModel.fontSize;
                tooltipEl.style.fontStyle = tooltipModel._fontStyle;
                tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
            }
        }
    }
});

关于reactjs - Chartjs- 2 圆环图自定义工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42696822/

相关文章:

reactjs - babel-import-plugin 无法解决有关使用整个 antd 包的警告。 Antd bug 或者 babel import 插件的不正确使用?

css - 在 React Native 中显示像 Instagram 一样的图像部分?

charts - 删除 JavaFX 2 LineChart 图例项

ms-access - 使用 Access 2007 VBA 创建数据透视图

java - 强制显示 Java 工具提示

javascript - d3 工具提示未显示

javascript - ReactJs - 获取后调用函数

reactjs - 如何让reactjs/flux/react-router spa同构?

java - 在 Java 中创建时尚的图表,例如使用 JFreeChart

android 跨度点击事件