forked from chartjs/Chart.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom tooltip: add data points infos (chartjs#3201)
Expose tooltip items from tooltip model and added `x` and `y` properties to `TooltipItemInterface`
- Loading branch information
1 parent
35ff1a7
commit 0f30b5c
Showing
5 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<!doctype html> | ||
<html> | ||
|
||
<head> | ||
<title>Custom Tooltips using Data Points</title> | ||
<script src="../dist/Chart.bundle.js"></script> | ||
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | ||
<style> | ||
canvas{ | ||
-moz-user-select: none; | ||
-webkit-user-select: none; | ||
-ms-user-select: none; | ||
} | ||
.chartjs-tooltip { | ||
opacity: 1; | ||
position: absolute; | ||
background: rgba(0, 0, 0, .7); | ||
color: white; | ||
border-radius: 3px; | ||
-webkit-transition: all .1s ease; | ||
transition: all .1s ease; | ||
pointer-events: none; | ||
-webkit-transform: translate(-50%, 0); | ||
transform: translate(-50%, 0); | ||
padding: 4px; | ||
} | ||
|
||
.chartjs-tooltip-key { | ||
display: inline-block; | ||
width: 10px; | ||
height: 10px; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="canvas-holder1" style="width:75%;"> | ||
<canvas id="chart1"></canvas> | ||
</div> | ||
<div class="chartjs-tooltip" id="tooltip-0"></div> | ||
<div class="chartjs-tooltip" id="tooltip-1"></div> | ||
<script> | ||
var customTooltips = function (tooltip) { | ||
$(this._chart.canvas).css("cursor", "pointer"); | ||
|
||
$(".chartjs-tooltip").css({ | ||
opacity: 0, | ||
}); | ||
|
||
if (!tooltip || !tooltip.opacity) { | ||
return; | ||
} | ||
|
||
if (tooltip.dataPoints.length > 0) { | ||
tooltip.dataPoints.forEach(function (dataPoint) { | ||
var content = [dataPoint.xLabel, dataPoint.yLabel].join(": "); | ||
var $tooltip = $("#tooltip-" + dataPoint.datasetIndex); | ||
|
||
$tooltip.html(content); | ||
$tooltip.css({ | ||
opacity: 1, | ||
top: dataPoint.y + "px", | ||
left: dataPoint.x + "px", | ||
}); | ||
}); | ||
} | ||
}; | ||
var randomScalingFactor = function() { | ||
return Math.round(Math.random() * 100); | ||
}; | ||
var lineChartData = { | ||
labels: ["January", "February", "March", "April", "May", "June", "July"], | ||
datasets: [{ | ||
pointHitRadius: 100, | ||
label: "My First dataset", | ||
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()] | ||
}, { | ||
pointHitRadius: 100, | ||
label: "My Second dataset", | ||
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()] | ||
}] | ||
}; | ||
|
||
window.onload = function() { | ||
var chartEl = document.getElementById("chart1"); | ||
var chart = new Chart(chartEl, { | ||
type: "line", | ||
data: lineChartData, | ||
options: { | ||
title:{ | ||
display: true, | ||
text: "Chart.js - Custom Tooltips using Data Points" | ||
}, | ||
tooltips: { | ||
enabled: false, | ||
custom: customTooltips | ||
} | ||
} | ||
}); | ||
}; | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters