简体   繁体   English

如何将高图甜甜圈图中心的HTML跨度与响应式设计对齐?

[英]How to align HTML span at center of highchart donut graph with responsive design?

I am using Highcharts.js for drawing a do-nut graph. 我正在使用Highcharts.js绘制一个坚果图。

Here i need legends just right to graph and a 'span' outside the container(svg) on the center of graph. 在这里,我需要图形正确的图例和图形中心的容器外部的“跨度”(svg)。 Legends work properly, while 'span' doesn't align at the center of graph on responsive view. 图例可以正常工作,而“span”在响应视图的图表中心没有对齐。

JSFIDDLE 的jsfiddle

var chartDiv = document.getElementById('container');
var textX = $(chartDiv).width()/2;
var textY = $(chartDiv).height()/2 ;
var span = '<span id="pieChartInfoText" style="position:absolute;text-align:center;">';
span += '<span class="centerText">'+Math.round((response.engagement / response.traffic || 0) * 100) + '%</span><br></span>';

$("#addText").append(span);
span = $('#pieChartInfoText');
span.css('left', textX + (span.width() * -0.5));
span.css('top', textY + (span.height() * -0.5));

there are a few things going wrong here but the main thing ist this: the circle is actually not in the center of the screen, so the text is but the circle is not you need to adjust the text position to that of the circle offest. 这里有一些问题,但主要的是这个:圆圈实际上不在屏幕的中心,所以文字是圆形的,你不需要将文字位置调整到圆圈的位置。 plus body has a margin and is not 100% height 加上身体有一个边缘,不是100%的高度

also this part here sets the height and the width so we can then calculate the percentage offset 此处此部分还设置了高度和宽度,以便我们可以计算百分比偏移量

var $addText =  $("#addText");
                $addText.append($span)
                .height($addText.height())
                .width($addText.width());

see this solution http://jsfiddle.net/zgLm44q5/1/ 看到这个解决方案http://jsfiddle.net/zgLm44q5/1/

also I optimised you code a bit, but there are still thing that could be written more elegantly 我也对你的代码进行了优化,但仍有一些东西可以写得更优雅

I have fixed the issue by using chart 'load' callback. 我已经通过使用图表'加载'回调来解决了这个问题。 Refer here 请参考这里

events: {
    load: function() {
        var chart = this,
        rend = chart.renderer,
        pie = chart.series[0],
        left = chart.plotLeft + pie.center[0],
        top =  chart.plotTop + pie.center[1],
        text = rend.text(Math.round((response.engagement / response.traffic || 0) * 100) + '%', left,  top).attr({'text-anchor': 'middle','class': 'middleText'}).add();
    }
}

JSFIDDLE 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM