Marco ChaméleaMMMMMMMMMMMMMMMHMMMMMMMMMMMMMMMMMMMMMMMMHMMMMMMMMMMM

MarcodddddiceAdélem TraeaddddiceAdéldiceAademetddicediceadettediceeadiceeadiceeadiceeaeadiceddiceAeaeaddddiceAeaeaeadicediceddice

DdddGddMMMMLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Ce tableau.js a une option par défaut pour tooltip, je veux faire une option tooltip personnalisée. Tram DaMenMélélélélélélélélieachélieélemettehaélieélemettehaélieélemetteachélieélieélemetteachélieélieélieélieélemetteachachach

Voici mon code

var chart = null; barChart: function (data1, data2, data3, label) { var data = { labels: label, datasets: [ { fillColor: "rgba(220,220,220,0.5)", strokeColor: "rgba(220,220,220,0.8)", highlightFill: "rgba(220,220,220,0.75)", highlightStroke: "rgba(220,220,220,1)", data: data1 }, { fillColor: "rgba(151,187,205,0.5)", strokeColor: "rgba(151,187,205,0.8)", highlightFill: "rgba(151,187,205,0.75)", highlightStroke: "rgba(151,187,205,1)", data: data2 }, { fillColor: "rgba(0,255,0,0.3)", strokeColor: "rgba(220,220,220,0.8)", highlightFill: "rgba(220,220,220,0.75)", highlightStroke: "rgba(0,255,0,0.3)", data: data3 }, ] } var cht = document.getElementById('exampleCanvas'); var ctx = cht.getContext('2d'); if (chart) chart.destroy(); chart = new Chart(ctx).Bar(data); } 

Essaye ça:

Vous pouvez effectuer des modifications à l'échelle mondiale en utilisant ce code:

 Chart.defaults.global = { // Boolean - Determines whether to draw tooltips on the canvas or not showTooltips: true, // Array - Array of string names to attach tooltip events tooltipEvents: ["mousemove", "touchstart", "touchmove"], // String - Tooltip background colour tooltipFillColor: "rgba(0,0,0,0.8)", // String - Tooltip label font declaration for the scale label tooltipFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", // Number - Tooltip label font size in pixels tooltipFontSize: 14, // String - Tooltip font weight style tooltipFontStyle: "normal", // String - Tooltip label font colour tooltipFontColor: "#fff", // String - Tooltip title font declaration for the scale label tooltipTitleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", // Number - Tooltip title font size in pixels tooltipTitleFontSize: 14, // String - Tooltip title font weight style tooltipTitleFontStyle: "bold", // String - Tooltip title font colour tooltipTitleFontColor: "#fff", // Number - pixel width of padding around tooltip text tooltipYPadding: 6, // Number - pixel width of padding around tooltip text tooltipXPadding: 6, // Number - Size of the caret on the tooltip tooltipCaretSize: 8, // Number - Pixel radius of the tooltip border tooltipCornerRadius: 6, // Number - Pixel offset from point x to tooltip edge tooltipXOffset: 10, // String - Template string for single tooltips tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>", // String - Template string for single tooltips multiTooltipTemplate: "<%= value %>", // Function - Will fire on animation progression. onAnimationProgress: function(){}, // Function - Will fire on animation completion. onAnimationComplete: function(){} } 

Utilisez ce lien pour référence

C'est très simple lorsque vous savez où placer l'option.

La réponse est d'ajouter l'option personnalisée lorsque vous créez le graphique:

 chart = new Chart(ctx).Bar(data, {"options goes here"} ); 

Après avoir passé la variable de données avec les informations de données, vous pouvez ajouter des options personnalisées, par exemple, vous souhaitez modifier la taille du titre de la info-bulle et vous souhaitez également ajouter une couleur gris clair dans le titre de la info-bulle que vous feriez quelque chose comme ca:

 chart = new Chart(ctx).Bar(data, { //Option for title font size in pixels tooltipTitleFontSize: 14, //Option for tooltip title color tooltipTitleFontColor: "#eee", 

});

Une autre façon que vous pouvez faire est de créer l'ensemble d'options comme une variable à des fins d'organisation et de pouvoir la réutiliser.

  //Create a set of relevant options for you chart var myoptions = { scaleShowGridLines : false, responsive : true, scaleFontSize: 12, pointDotRadius : 4, scaleFontStyle: 14, scaleLabel: "<%= ' ' + value%> %", } //Create the Chart chart = new Chart(ctx).Bar(data, myoptions); 

J'espère qu'il est clair maintenant

Cordialement

J'ai utilisé cela, je l'ai trouvé sur stackoverflow, mais j'essaie presque de le retrouver

 <div id="chartjs-tooltip"></div> 

 var chartoptions = { customTooltips: function ( tooltip ) { var tooltipEl = $( "#chartjs-tooltip" ); if ( !tooltip ) { tooltipEl.css( { opacity: 0 } ); return; } tooltipEl.removeClass( 'above below' ); tooltipEl.addClass( tooltip.yAlign ); // split out the label and value and make your own tooltip here var parts = tooltip.text.split( ":" ); var innerHtml = '<span>' + parts[0].trim() + '</span> : <span><b>' + parts[1].trim() + '</b></span>'; tooltipEl.html( innerHtml ); tooltipEl.css( { opacity: 1, left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px', top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px', fontFamily: tooltip.fontFamily, fontSize: tooltip.fontSize, fontStyle: tooltip.fontStyle } ); } } 

Lien vers où je l'ai compris: Chart.js: modification du modèle d'info-bulle

Vous pouvez vérifier l'info-bulle css – http://www.chartjs.org/docs/#chart-configuration-tooltip-configuration

 tooltips: { bodyFontColor: "#000000", //#000000 bodyFontSize: 50, bodyFontStyle: "bold", bodyFontColor: '#FFFFFF', bodyFontFamily: "'Helvetica', 'Arial', sans-serif", footerFontSize: 50, callbacks: { label: function(tooltipItem, data) { var value = data.datasets[0].data[tooltipItem.index]; if(tooltipItem.index == 0) { return "<?php echo $data1;?>"; } else if(tooltipItem.index == 1) { return "<?php echo $data2;?>"; } else if(tooltipItem.index == 2) { return "<?php echo $data3;?>"; } else { return "<?php echo $data4; ?>"; } }, }, }, 

La nouvelle version de chart.js, version 2, se trouve ici:

https://github.com/nnnick/Chart.js/releases

La version 2 ajoute des tooltip callbacks :

Chaque rappel d'info-bulle (beforeTitle, title, afterTitle, etc.) accepte une chaîne ou un tableau. Si un tableau est utilisé, il produira plusieurs lignes. Les astuces proposent maintenant beaucoup plus d'options pour la personnalisation visuelle.


Cependant, il existe une fourchette de chart.js appelée chartNew.js, trouvée ici:

https://github.com/FVANCOP/ChartNew.js/

Il ajoute plusieurs excellentes améliorations au vénérable chart.js, y compris:

  • Fonctions d'info-bulle (lors du téléchargement / décompression, regardez dans le dossier Samples et regardez annotateFunction.html . Lorsque survolez n'importe quel point, vous pouvez faire n'importe quoi).

  • En passant un tableau de couleurs à un diagramme à barres (au lieu de chaque barre en série ayant la même couleur)

  • Mettre le texte sur le tableau où vous le souhaitez

Beaucoup etc.


Notez que chart.js a été grandement amélioré dans la version 2, mais la nouvelle version n'est pas entièrement compatible avec l'arrière (il suffit de changer pour le plugin v2). ChartNew.js fonctionnera avec l'ancien code tout en étendant les capacités.

J'ai trouvé cette page utile:

https://github.com/nnnick/Chart.js/blob/master/samples/pie-customTooltips.html

Il montre où et comment définir la fonction pour votre info-bulle personnalisée, ainsi qu'un exemple de style. J'ai dû modifier le code pour correspondre à mes besoins, mais c'est un excellent exemple de la mise en œuvre de la fonctionnalité d'info-bulle personnalisée.

Quelques choses à noter qui m'ont jeté au début:

1) L'id dans les règles de style doit être modifié pour correspondre à votre division d'info-bulle. (C'est évident, mais je n'ai pas réussi à l'aborder)

2) tooltip.text suivra le format que vous définissez pour 'tooltipTemplate' dans vos options ou la valeur par défaut tooltipTemplate définie dans chart.js

Peut-être que cela peut vous aider

  Chart.types.Line.extend({ name: "LineAlt", initialize: function (data) { Chart.types.Line.prototype.initialize.apply(this, arguments); var xLabels = this.scale.xLabels xLabels.forEach(function (label, i) { if (i % 2 == 1) xLabels[i] = label.substring(1, 4); }) } }); var data = { labels: ["1/jan/08", "15/fab/08", "1/mar/08", "1/apr/08", "10/apr/08", "10/may/2008", "1/jun/2008"], datasets: [{ label: "First dataset", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,20,20,1)", pointColor: "rgba(220,20,20,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: [65, 59, 80, 81, 56, 55, 90] }, { label: "Third dataset", fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(15,187,25,1)", pointColor: "rgba(15,187,25,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(151,187,205,1)", data: [38, 55, 50, 65, 35, 67, 54] }] 

};

 var ctx = document.getElementById("myChart").getContext("2d"); var myChart = new Chart(ctx).LineAlt(data); // Chart.js replaces the base inRange function for Line points with a function that checks only the x coordinate // we replace it with the original inRange fucntion (one that checks both x and y coordinates) myChart.datasets.forEach(function(dataset) { dataset.points.forEach(function(point) { point.inRange = Chart.Point.prototype.inRange; }); }); // Chart.js shows a multiTooltip based on the index if it detects that there are more than one datasets // we override it to show a single tooltip for the inRange element myChart.showTooltip = function(ChartElements, forceRedraw) { // this clears out the existing canvas - the actual Chart.js library code is a bit more optimized with checks for whether active points have changed, etc. this.draw(); // draw tooltip for active elements (if there is one) Chart.helpers.each(ChartElements, function(Element) { var tooltipPosition = Element.tooltipPosition(); new Chart.Tooltip({ x: Math.round(tooltipPosition.x), y: Math.round(tooltipPosition.y), xPadding: this.options.tooltipXPadding, yPadding: this.options.tooltipYPadding, fillColor: this.options.tooltipFillColor, textColor: this.options.tooltipFontColor, fontFamily: this.options.tooltipFontFamily, fontStyle: this.options.tooltipFontStyle, fontSize: this.options.tooltipFontSize, caretHeight: this.options.tooltipCaretSize, cornerRadius: this.options.tooltipCornerRadius, text: Chart.helpers.template(this.options.tooltipTemplate, Element), chart: this.chart, custom: this.options.customTooltips }).draw(); }, this); }; 

http://jsfiddle.net/6cgo4opg/747/