chart.js - Change style of hover and tooltip in chartjs or ng2-charts -
is there way change style of hover tooltip chartjs or ng2-charts? want hide hover points , display them whenever hover on them along indicator line. here exact chart model want build:
https://interactive-bitcoin-price-chart-foxkmkynmg.now.sh/
thank in advance tips.
edit: followed instructions of grunt apply chart option angular app, full chart shown tooltip whenever hover, line-trace indicator not. here codes:
plugin-hoverline.ts:
export class pluginhoverline { posx: null; ismouseout:boolean = false; drawline(chart, posx) { const ctx = chart.ctx, x_axis = chart.scales['x-axis-0'], y_axis = chart.scales['y-axis-0'], x = posx, topy = y_axis.top, bottomy = y_axis.bottom; if (posx < x_axis.left || posx > x_axis.right) return; // draw line ctx.save(); ctx.beginpath(); ctx.moveto(x, topy); ctx.lineto(x, bottomy); ctx.linewidth = chart.options.lineonhover.linewidth; ctx.strokestyle = chart.options.lineonhover.linecolor; ctx.stroke(); ctx.restore(); }; beforeinit(chart) { chart.options.events.push('mouseover'); }; afterevent(chart, event) { if (!chart.options.lineonhover || !chart.options.lineonhover.enabled) return; if (event.type !== 'mousemove' && event.type !== 'mouseover') { if (event.type === 'mouseout') this.ismouseout = true; chart.clear(); chart.draw(); return; } this.posx = event.x; this.ismouseout = false; chart.clear(); chart.draw(); this.drawline(chart, this.posx); var metadata = chart.getdatasetmeta(0).data, radius = chart.data.datasets[0].pointhoverradius, posx = metadata.map(e => e._model.x); posx.foreach(function(pos, posindex) { if (this.posx < pos + radius && this.posx > pos - radius) { chart.updatehoverstyle([metadata[posindex]], null, true); chart.tooltip._active = [metadata[posindex]]; } else chart.updatehoverstyle([metadata[posindex]], null, false); }.bind(this)); chart.tooltip.update(); }; afterdatasetsdraw(chart, ease) { if (!this.posx) return; if (!this.ismouseout) this.drawline(chart, this.posx); }; }
banner.component.ts: (chart component)
import { component, oninit } '@angular/core'; import { historicalbpiservice } '../../services/historical-bpi.service'; import { pluginhoverline } './plugin-hoverline'; @component({ selector: 'app-banner', templateurl: './banner.component.html', styleurls: ['./banner.component.scss'] }) export class bannercomponent implements oninit { private dataurl: string = 'historical/close.json'; constructor( private historicalbpiservice:historicalbpiservice ) {} // linechart public linechartdata:any = [ { data:[], label: 'bitcoin price' } ]; public linechartlabels:array<any> = []; public linechartoptions:any = { responsive: true, maintainaspectratio: false, layout: { padding: 0 }, lineonhover: { enabled: true, linecolor: '#bbb', linewidth: 1 }, scales: { yaxes: [{ display: true, scalelabel: { display: false, labelstring: 'usd' }, ticks: { display: false }, gridlines: { display: true, tickmarklength: 0 } }], xaxes: [{ ticks: { display: false }, gridlines: { display: false, tickmarklength: 0 } }] }, elements: { point: { radius: 3 }, line: { tension: 0.4, // 0 disables bezier curves } }, hover: { mode: 'nearest', intersect: false }, tooltips: { mode: 'nearest', intersect: false, backgroundcolor: 'rgb(95,22,21)', callbacks: { label: function (tooltipitems, data) { return data.datasets[tooltipitems.datasetindex].label + ' : ' + '$' + tooltipitems.ylabel.tolocalestring(); }, labelcolor: function(tooltipitem, chart) { var dataset = chart.config.data.datasets[tooltipitem.datasetindex]; return { backgroundcolor : dataset.backgroundcolor } } } } }; public linechartcolors:array<any> = [ { backgroundcolor: 'rgba(199,32,48,0.8', bordercolor: 'rgb(95,22,21);', pointbackgroundcolor: 'rgba(218,208,163,0.8)', pointhoverbackgroundcolor: 'rgba(218,208,163,0.8)', pointhoverbordercolor: 'rgb(218,208,163)', pointhoverradius: 6, steppedline: false } ]; public linechartlegend:boolean = false; public linecharttype:string = 'line'; // events public chartclicked(e:any):void { console.log(e); } public charthovered(e:any):void { console.log(e); } ngoninit(){ this.historicalbpiservice.getbpidata(this.dataurl) .subscribe( res => { //this.linechartdata = object.keys(res.bpi).map(function (key) { return res.bpi[key];}); this.linechartdata[0].data = object.values(res.bpi); this.linechartlabels = object.keys(res.bpi); //console.log(this.linechartdata,this.linechartlabels); } ) } }
template:
<div class="chart"> <canvas basechart height="360px" [datasets]="linechartdata" [labels]="linechartlabels" [options]="linechartoptions" [colors]="linechartcolors" [legend]="linechartlegend" [charttype]="linecharttype" (charthover)="charthovered($event)" (chartclick)="chartclicked($event)"></canvas> </div>
unfortunately there no built-in functionality yet. can use this chart plugin (once created own purpose) achieve goal.
to utilize plugin, set following option in chart's options config :
lineonhover: { enabled: true, linecolor: '#bbb', linewidth: 1 }
also, make sure set following properties dataset :
pointradius: 0, pointhoverradius: 5, pointhoverbackgroundcolor: 'white'
see - live demo
Comments
Post a Comment