Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

How to display pie slice data and tooltip together using chart.js

enter image description here

Can we display data in pie chart slice and also tolltip like the above image using chart.js?

Updated:

Here is my code in php page.

            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});
    });

    Chart.types.Pie.extend({
        name: "PieAlt",
        draw: function(){
            Chart.types.Pie.prototype.draw.apply(this, arguments);
            drawSegmentValues(this)
        }
    });
    var myPieChart = new Chart(ctx).PieAlt(piedata, {
        showTooltips: true,
        tooltipTemplate: "<%= Math.round(circumference / 6.283 * 100) %>%"

    });

    var radius = myPieChart.outerRadius;

    function drawSegmentValues(myPieChart)
    {
    //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();
}
<?

enter image description here

When mouse over the data in pie slice moved from its position. How to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Extend the chart and move your drawSegmentValues to inside the draw override, like so

Chart.types.Pie.extend({
  name: "PieAlt",
  draw: function(){
    Chart.types.Pie.prototype.draw.apply(this, arguments);
    drawSegmentValues(this)
  }
});

then use PieAlt

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

and modify the drawSegmentValues function slightly

function drawSegmentValues(myPieChart)
{
  var radius = myPieChart.outerRadius
  ...

Update

If you have a problem with the labels moving set the context textAlign and textBaseline properties, like so

...
ctx.font = textSize + "px Verdana";
ctx.textAlign = "start";
ctx.textBaseline = "bottom";
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...