score:0
If you are looking for a way to show a tooltip for a specific element by clicking on a button you may take a look to github or SO
I changed a bit the code I found there just to let you show a tooltip on button click.
I added a property to your options in Chartjs:
showMyTooltips: '',
This must contain the index of the tooltip to be shown. This value is used inside the registered plugin like in the example code whose links are reported in this answer.
Here is the snippet:
Chart.pluginService.register({
beforeRender: function (chart) {
if (!!chart.config.options.showMyTooltips && chart.config.options.showMyTooltips.trim().length != 0) {
chart.pluginTooltips = [];
chart.config.options.showMyTooltips.trim().split(',').forEach(function (ele, idx) {
var sector = chart.getDatasetMeta(0).data[ele];
chart.pluginTooltips.push(new Chart.Tooltip({
_chart: chart.chart,
_chartInstance: chart,
_data: chart.data,
_options: chart.options.tooltips,
_active: [sector]
}, chart));
});
}
},
afterDraw: function (chart, easing) {
if (chart.config.options.showMyTooltips) {
// we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
if (!chart.allTooltipsOnce) {
if (easing !== 1)
return;
chart.allTooltipsOnce = true;
}
Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
tooltip.initialize();
tooltip.update();
// we don't actually need this since we are not animating tooltips
tooltip.pivot();
tooltip.transition(easing).draw();
});
}
}
});
var ctx = document.getElementById('ctx').getContext('2d');
var ocacolor = '#EE8B54',
ocbcolor = '#C2B49B',
ocbacolor = '#AECF86',
ocgcolor = '#E97676',
ocpcolor = '#088DA5';
var backgroundcolor = [ocacolor, ocbcolor, ocbacolor, ocgcolor, ocpcolor]
data = {
datasets: [{
data: [5, 2, 3, 9, 2],
backgroundColor: backgroundcolor
}],
labels: [
'OCA',
'OCB',
'OCBA',
'OCG',
'OCP'
]
};
//CREATING CHART
var myDoughnutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
legend: {
display: false
},
showMyTooltips: '',
onClick: function (evt, dataset) {
//I GET THE LABEL TEXT OF THE CLICKED ITEM
console.log(dataset[0]._model.label);
}
}
});
$(":radio").on('change', function (e) {
myDoughnutChart.options.showMyTooltips = myDoughnutChart.data.labels.indexOf(this.id).toString();
myDoughnutChart.update(true);
})
.graph{
height: 500px;
width: 500px;
text-align: center;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<div class="graph">
<canvas id="ctx"></canvas>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary">
<input type="radio" name="options" id="OCA" autocomplete="off"> Test OCA
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="OCB" autocomplete="off"> Test OCB
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="OCBA" autocomplete="off"> Test OCBA
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="OCG" autocomplete="off"> Test OCG
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" id="OCP" autocomplete="off"> Test OCP
</label>
</div>
</div>
Source: stackoverflow.com
Related Query
- How can I trigger the hover mode from outside the chart with charts.js 2?
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- How can I load multiple Chartjs charts with different data on the same page?
- How can I prevent the hover data from displaying in Chart.JS?
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How can I fix the offset of the gradient to be aligned with the chart points(chart.js)?
- How can I display desired/particular chart in App.vue from Charts.vue which contains different charts in Vuejs
- How can I remove the white border from Chart.js pie chart when all legends are hidden?
- How do i have a single Chart.vue component instead of different chart components and then display charts from it by changing id of the API
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- How to prevent first/last bars from being cut off in a chart with time scale
- How to display data labels outside in pie chart with lines in ionic
- Chart js: how can I align the legend and the title
- Chart.js canvas, how can I swap data without the previous data affecting my hover events?
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Angular chart how to show the legend data value by default along with legend name
- How to achieve the best possible performance with mutable data and real-time charts in React?
- How to create chartjs chart with data from database C#
- Canvas is already in use. Chart with ID '0' must be destroyed before the canvas can be reused. chat.JS
- How can I divide this Object.values with the total?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- Chart is too big. How can I reduce size for the chart in vue js?
- Chart.js - Draw charts with opposite bars - How to set both the y-axis ends to positive number?
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
More Query from same tag
- Chart.js barchart custom on hover method
- Chart.js turn off all y axis and toggle them on / off
- How to display k as thousand for the values of chart.js - Javascript
- Chart JS - Disable lines and Uncheck legends by default
- How do I make many ChartJS charts on the same page to have the same height and also even column width?
- Update chart data in angular2-chartjs
- ChartJS getting an unwanted line between first data point and last data point
- JSON cyclic object value when posting data in ChartJS
- How to download chart js image as bmp
- How to make a chart with an associative array with chart.js?
- How to display value after the bar using chart.js
- Chart.js not working on my page?
- ChartJS – How to show border on empty pie chart?
- Calculate weighted sum over all points in chart.js
- Is there a way to change part of the title to another font style or set a custom title format in Chart.js?
- manipulate objects from inside a function
- Set Scope in Angular-Charts.js after rendering
- Draw two plots using chartjs over one another with transparency
- Why won't this html page run, Devexpress documentation ChartJS
- How to change the position of the tooltip for the Bar type chart in Chart.Js
- How to format the left legend on chartjs
- How to change the chart line or area colors according to the user need?
- Chart.js same Y axis on left and right
- Chart.js minimal width of Chart with scrollable overflow
- react-chartjs: difference between `scales.x`/`xAxes`/`xAxes[]`
- Chart JS - Bubble Chart is not getting rendered when loaded Dynamically
- Vue.js component for Chart.js - strange issue on AJAX reload
- Chart.js: passing objects instead of int values as data
- Chart.JS Time Axis Not Parsing
- Javascript highcharts code similar to image