score:0
Accepted answer
You can combine the answers from How to modify chartjs tooltip to add customized attribute and How to make tool tip contents display on multiple lines
Script
function Label(short, long) {
this.short = short;
this.long = long
}
Label.prototype.toString = function() {
return this.short;
}
var ctx = $("#myChart").get(0).getContext("2d");
var data = {
labels: [
new Label("J", "S JAN JAN JAN JAN JAN JAN JAN E"),
new Label("F", "S FEB E"),
new Label("M", "S MAR E"),
new Label("A", "S APR APR APR APR APR APR APR E"),
new Label("M", "S MAY E"),
new Label("J", "S JUN E"),
new Label("J", "S JUL JUL JUL JUL JUL JUL JUL JUL JUL JUL JUL JUL JUL JUL E")
],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}]
};
var myLineChart = new Chart(ctx).Bar(data, {
tooltipTemplate: "<%if (label){%><%=label.long%>: <%}%><%= value %>",
customTooltips: function (tooltip) {
var tooltipEl = $('#chartjs-tooltip');
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
// split out the label and value and make your own tooltip here
var parts = tooltip.text.split(":");
var re = new RegExp('\b', 'g');
var innerHtml = '<span>' + parts[0].trim().replace(re, '<br/>') + '</span> : <span><b>' + parts[1].trim() + '</b></span>';
tooltipEl.html(innerHtml);
tooltipEl.css({
opacity: 1,
// the minimum amount is half the maximum width of the tooltip that we set in CSS ...
// ... + the x scale padding so that it's not right at the edge
left: Math.max(75 + 10, tooltip.chart.canvas.offsetLeft + tooltip.x) + 'px',
top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle,
});
}
});
CSS
#chartjs-tooltip {
opacity: 0;
position: absolute;
background: rgba(0, 0, 0, .7);
color: white;
padding: 3px;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, -120%);
transform: translate(-50%, -120%);
max-width: 150px;
}
HTML
<canvas id="myChart" width="400" height="200"></canvas>
<div id="chartjs-tooltip"></div>
Note that I've adjusted for the left side edge. If you don't have enough space on the top or the right, you'll need to do the same for those edges as well (maximum limit for tooltip.x and limit for tooltip.y)
Fiddle - http://jsfiddle.net/69vt0091/
Source: stackoverflow.com
Related Query
- Customize different tooltips of bar chart
- Chart.js Mixed Bar and Line chart with different scales
- Different color for each column in angular-chartjs bar chart
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- Remove the label and show only value in tooltips of a bar chart
- Vue ChartKick - different colours for bar chart
- how to customize tool tip while mouse go over bars on Chart js bar chart
- How to show different product name for every bar in chart js
- How to customize chart js Bar chart shape?
- Charts.js - Bar chart different colors for value intervals not working
- How do I get a different label for each bar in a bar chart in ChartJS?
- Grouped bar chart having each group with different data using chart.js
- Chart js bar with the highest value is a different color among the rest - React
- Chart.js manipulate each bar in chart with different labels
- chart js showing different width of bar
- different colors for bar chart with chart.js
- How to assign values to different lables(legends) of my dataset of stacked bar chart
- Bar Chart With 2 Different Attribute Of The Same Variable [Chart.Js]
- Angular 4: Different color for bar in bar chart dynamically using ChartJS
- Tooltips displaying "rgb(0,0,0)" instead of label value on bar chart
- How to set different colors in each stacked bar chart cell?
- Different color for each bar in a bar chart; ChartJS
- chart js 2 how to set bar width
- Chartjs Bar Chart showing old data when hovering
- Chart.js: Bar Chart Click Events
- Chartjs v2.0: stacked bar chart
- Horizontal stacked bar chart with chart.js
- Chart.js Bar Chart - how to chart bars from 0
- Horizontal bar chart in chart.js
- Vertical stacked bar chart with chart.js
More Query from same tag
- Show only nth tick LINE on x-axis for Chart.js diagram
- Embed unique identifier in Chart.js segments?
- ChartJs bar chart bar value displaying lower then Y axis value in pdf?
- Group Chart with multiple layers in ChartJS
- I want to change color of individual bar of bar graph
- Chart.js how to rewrite x for webAPP
- angular-chart.js custom color based on chart label values
- How can I add new data points to an existing chart with chart.js?
- Type error in using chartjs and tc-angular-chartjs
- How to remove the Legend of chart from angular Chart.js
- Chart JS can't access data from array within an object
- chart.js - user add the final point of the line
- .destroy() generating error 'window.myChart.destroy()' is not a function
- Chart.js title is not showing
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- ChartJS 2 How to globally remove xAxis gridLines?
- Last 24 Hours Data Report Show In Chart.Js
- Chart.js - Define custom y-axis scale
- How to know the length of the horizontal bar in Chart.js
- Responsive ChartJS in Bootstrap Table Cell
- How to remove or color line/grid in polar chart area using Chart.js?
- ChartJS Polar Area Chart Scale Removing
- chart.js onAnimationProgress progress percentage
- PieChart disappears on mouseout
- Chart.js timescale: set min and max on xAxes
- Chartjs doughnut chart for conditional data
- ng2 scatter chart how to put labels in x axis
- chart.js responsive bar chart label sizing
- How to show values on top of bars in a PDF using chart.js and chartjs-node-canvas?
- Chart.js v3: how to allow tick label overflow?