Chart.js pie chart slice data is disappearing when mouse over for tooltips

邮差的信 提交于 2019-12-12 03:32:34

问题


Pie chart is implemented using Chart.js in my php page.

I wish to display pie slice with data value and also tooltip when mouse over the slice.

I came up with the following code.

            printf( '<table>' );
            echo '<tr><td style="text-align: right;"><canvas id="pie-canvas-'
                 . $canvasId
                 . '" width=256px height=257px ></canvas></td><td style="text-align: left;width:360px;" id="legend" class="chart-legend"></td></tr>';

            echo '<script type="text/javascript">drawPie('
                . $canvasId
                . ', '
                . $data
                .', '
                . $legend
                . ');</script>';
            printf( '</table>' );

printf( '<script type="text/javascript" src="extlib/Chart.min.js"></script>' );
printf( '<script type="text/javascript" src="extlib/jquery-min.js"></script>' );
printf( '<script type="text/javascript">' );
?>
function drawPie( canvasId, data, legend )
{
//pie chart for machine status
    var canvas = document.getElementById( "pie-canvas-" + canvasId );
    var ctx = canvas.getContext( "2d" );
    var midX = canvas.width/2;
    var midY = canvas.height/2;
    var piedata = [];

    $.each(data,function(i,val){
        piedata.push({value:val.hostStatusCount,color:val.color,label:val.status});
    });

    var myPieChart = new Chart(ctx).Pie(piedata, {
        showTooltips: true,
        tooltipTemplate: "<%= Math.round(circumference / 6.283 * 100) %>%",
        onAnimationProgress: drawSegmentValues
    });

    var radius = myPieChart.outerRadius;

    function drawSegmentValues()
    {
    //displays segements(number of machines) for each slice of pie in percentage
        var length = myPieChart.segments.length;
        var totalValue = 0;
        for ( var i=0; i < length; i++ )
        {
            totalValue +=myPieChart.segments[i].value;
        }
        for( var i=0; i < length; i++ )
        {
            ctx.fillStyle="black";
            var textSize = canvas.width/15;
            ctx.font= textSize+"px Verdana";

            // Get needed variables
            var value = Math.round( ( ( myPieChart.segments[i].value ) / totalValue ) * 100 );
            var startAngle = myPieChart.segments[i].startAngle;
            var endAngle = myPieChart.segments[i].endAngle;
            var middleAngle = startAngle + ( ( endAngle - startAngle ) / 2 );

            // Compute text location
            var posX = ( radius /1.5 ) * Math.cos( middleAngle ) + midX;
            var posY = ( radius/1.5 ) * Math.sin( middleAngle ) + midY;

            // Text offside by middle
            var w_offset = ctx.measureText( value ).width / 2;
            var h_offset = textSize / 4;

            ctx.fillText( value+"%", posX - w_offset, posY + h_offset );
        }
    }

    //legend for status
    if( legend )
        document.getElementById("legend").innerHTML = myPieChart.generateLegend();
}
<?

After the animation the data is displaying but when mouse over tooltip is displaying for the slice and all the slice data disappeared.

I tried custom tooltip also and I got up with nothing :-(

How to display both?.That is all data for slices of pie always visible and mouse over only tooltip get displayed only onthe selected slice.

Thanks in advance.

来源:https://stackoverflow.com/questions/35772795/chart-js-pie-chart-slice-data-is-disappearing-when-mouse-over-for-tooltips

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!