根据本文,我正在使用非常相似的代码使用画布创建饼图:
http://wickedlysmart.com/how-to-make-a-pie-chart-with-html5s-canvas/
从这张图片中可以看出,有些情况下标签是颠倒的:
以下是将标签写入图表的代码:
var drawSegmentLabel = function(canvas, context, i) {
context.save();
var x = Math.floor(canvas.width / 2);
var y = Math.floor(canvas.height / 2);
var degrees = sumTo(data, i);
var angle = degreesToRadians(degrees);
context.translate(x, y);
context.rotate(angle);
context.textAlign = 'right';
var fontSize = Math.floor(canvas.height / 32);
context.font = fontSize + 'pt Helvetica';
var dx = Math.floor(canvas.width * 0.3) - 20;
var dy = Math.floor(canvas.height * 0.05);
context.fillText(labels[i], dx, dy);
context.restore();
};
我正在尝试纠正这一点,因此文本始终是可读的,而不是颠倒的,但无法弄清楚如何做到这一点!