简体   繁体   English

如何使用画布在饼图的每个切片的鼠标悬停时添加工具提示

[英]How to add a tooltip on mouseover of each slice of a pie chart using canvas

  var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Green", "Red", "Blue"]; var myData = [30, 15, 38, 22, 30, 20, 10]; var cx = 150; var cy = 150; var radius = 100; ctx.globalAlpha = 0.50; pieChart(myData, myColor); ctx.globalAlpha = 1.00; function pieChart(data, colors) { // calc data total var total = 0; for (var i = 0; i < data.length; i++) { total += data[i]; } // calc sweep angles for each piece of pie var sweeps = [] for (var i = 0; i < data.length; i++) { sweeps.push(data[i] / total * PI2); } // draw outer pie var accumAngle = 0; for (var i = 0; i < sweeps.length; i++) { var f = randomColor(); drawWedge(radius, accumAngle, accumAngle + sweeps[i], f, data[i]); accumAngle += sweeps[i]; } } function drawWedge(radius, startAngle, endAngle, fill, label) { // draw the wedge ctx.beginPath(); ctx.moveTo(cx, cy); ctx.arc(cx, cy, radius, startAngle, endAngle, false); ctx.closePath(); ctx.fillStyle = fill; ctx.strokeStyle = 'white'; ctx.fill(); ctx.stroke(); } function randomColor() { return ('#' + (Math.floor(Math.random() * 0x1000000) + 0x1000000).toString(16).substr(1)); } 
 <canvas id="canvas" width=512 height=512></canvas> 

I have a simply question - is there any option to add a tooltip on mouseover event on canvas pie arc? 我有一个简单的问题-是否可以在画布饼形图上的mouseover事件上添加工具提示? Can anyone help me how could i do that. 谁能帮助我,我该怎么做。 I am able to draw a pie chart using canvas please fine the below snippet: 我可以使用画布绘制饼图,请对以下代码段进行罚款:

Canvas.title = "Tool tip text" Canvas.title =“工具提示文字”

At the most basic level you can use the canvas.title property to set the tooltip. 在最基本的级别上,您可以使用canvas.title属性设置工具提示。 Just get the mouse move events and record the mouse position. 只需获取鼠标移动事件并记录鼠标位置即可。 Then when you detect that the mouse has changed position, check which slice the mouse is over, if any and set the tooltip to match. 然后,当您检测到鼠标已更改位置时,请检查鼠标在哪个切片上(如果有),并将工具提示设置为匹配。

Note that the browsers tooltip is a little slow to respond to changes. 请注意,浏览器的工具提示对更改的响应有些慢。

For a better response. 以获得更好的响应。

You could also draw the chart every frame of the render loop and highlight the parts of the pie the mouse is over and create a more responsive tooltip than the browser default which can be a little slow to respond at times. 您还可以在渲染循环的每一帧绘制图表,并突出显示鼠标悬停的饼图部分,并创建比浏览器默认设置更敏感的工具提示,有时响应速度可能有些慢。

The example below is just your code with the modification needed to get the basic tooltip. 下面的示例只是您的代码,其中进行了修改以获得基本的工具提示。

Note that I keep the mouse event listener and the function that changes the tooltip separate, this is because on some systems the mouse can have a very high sample rate (my current gaming mouse can fire 700+ times a second) and doing too much in the mousemove event can cause laggy response to user input. 请注意,我将鼠标事件侦听器和更改工具提示的功能分开放置,这是因为在某些系统上,鼠标可能具有很高的采样率(我目前的游戏鼠标每秒可以触发700次以上),并且在mousemove事件可能导致对用户输入的响应迟钝。

  var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Green", "Red", "Blue"]; var myData = [30, 15, 38, 22, 30, 20, 10]; var cx = 150; var cy = 150; var radius = 100; var mouse = {x :0,y:0,oldx : 0,oldy:0}; ctx.globalAlpha = 0.50; pieChart(myData, myColor); ctx.globalAlpha = 1.00; function pieChart(data, colors) { // calc data total var total = 0; for (var i = 0; i < data.length; i++) { total += data[i]; } // calc sweep angles for each piece of pie var sweeps = [] for (var i = 0; i < data.length; i++) { sweeps.push(data[i] / total * PI2); data[i] = { value : data[i], angle : data[i] / total * PI2, text : "Data is "+((data[i] / total) * 100).toFixed(0) + "%", } } // draw outer pie var accumAngle = 0; for (var i = 0; i < sweeps.length; i++) { var f = randomColor(); drawWedge(radius, accumAngle, accumAngle + sweeps[i], f, data[i].value); accumAngle += sweeps[i]; } } function drawWedge(radius, startAngle, endAngle, fill, label) { // draw the wedge ctx.beginPath(); ctx.moveTo(cx, cy); ctx.arc(cx, cy, radius, startAngle, endAngle, false); ctx.closePath(); ctx.fillStyle = fill; ctx.strokeStyle = 'white'; ctx.fill(); ctx.stroke(); } function randomColor() { return ('#' + (Math.floor(Math.random() * 0x1000000) + 0x1000000).toString(16).substr(1)); } canvas.addEventListener("mousemove",function(event){ mouse.x = event.clientX; mouse.y = event.clientY; }) function update(){ // only on change in mouse position if(mouse.x !== mouse.oldx || mouse.y !== mouse.oldy){ var x = mouse.oldx = mouse.x; var y = mouse.oldy = mouse.y; x -= cx; // vector from pie center y -= cy; var newText = "My pie chart. Mouse over slices for more info."; var dist = Math.sqrt(x * x + y * y); // get distance from center if(dist < radius){ var ang = Math.atan2(y,x); // get angle note y is first ang += Math.PI * 2; // rotate 360 as atan2 starts at -Pi ang %= Math.PI * 2; // normalize to range 0 to 2Pi var i = 0; var tAng = 0 while(i < myData.length-1){ if(ang < tAng + myData[i].angle){ break; } tAng += myData[i].angle; i += 1; } newText = myData[i].text; } canvas.title = newText; } requestAnimationFrame(update); } update(); 
 <canvas id="canvas" width=512 height=512></canvas> 

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

相关问题 如何使用Zoomcharts设置每个切片高度的饼图 - How to set pie chart each slice height using Zoomcharts 如何在高级图表库中的饼图中设置每个切片的颜色? - How to set color of each slice in pie chart in high charts library? 如何使用chart.js在饼图中显示切片内的切片值 - How to show slice value inside of slice in pie chart using chart.js Highcharts-如何将工具提示悬停在每个饼图上的图标后面? - Highcharts - How do I position tooltip behind icon on each pie slice on hover? 如何显示chart.js中每个切片的饼图数据值 - How to display pie chart data values of each slice in chart.js 饼图上的jqPlot mouseOver - jqPlot mouseOver on pie chart 使用 Highcharts 的 3D 饼图在每个切片内放置百分比标签 - Place percentage labels inside each slice using Highcharts' 3D Pie Chart 如何使用html5 canvas绘制多级饼图? - How to draw a multi level pie chart using html5 canvas? 如何在动画Amcharts时间轴饼图中的鼠标悬停时删除PullInSlice / PullOutSlice onclick并添加PullInSlice / PullOutSlice - How to remove PullInSlice/PullOutSlice onclick and add PullInSlice/PullOutSlice on mouseover hover in Animated Amcharts Timeline Pie-chart 如何使用d3.js在饼图附近显示工具提示? - How to get a tooltip show up near the pie slice using d3.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM