Graphique.js-beignet montre tooltips toujours?

Lorsqu'on utilise la carte.bibliothèque js, je peux ajouter plusieurs beignets sur ma page sans problème.

http://www.chartjs.org/docs/#doughnut-pie-chart

mais je ne peux pas trouver un moyen de toujours montrer les conseils - pas seulement quand la souris plane sur le donut. Quelqu'un sait si cela est possible?

18
demandé sur Taapo 2014-09-04 12:55:14

5 réponses

j'ai eu le même problème aujourd'hui et je l'ai résolu assez facilement en ajoutant les options onAnimationComplte et tooltipevents.

onAnitmationComplete appelle la méthode pour montrer les tooltips comme un événement de vol stationnaire fait. Normalement, vous définissez les événements dans tooltipevents pour afficher les tooltips mais nous devons les supprimer et passer un tableau vide.

Note: ( http://www.chartjs.org/docs/#doughnut-pie-chart ).

Javascript:

var options = 
{
    tooltipTemplate: "<%= value %>",

    onAnimationComplete: function()
    {
        this.showTooltip(this.segments, true);

        //Show tooltips in bar chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
        //this.showTooltip(this.datasets[0].bars, true);

        //Show tooltips in line chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
        //this.showTooltip(this.datasets[0].points, true);  
    },

    tooltipEvents: [],

    showTooltips: true
}

var context = $('#chart').get(0).getContext('2d');
var chart = new Chart(context).Pie(data, options);  

HTML:

<div id="chartContainer">
    <canvas id="chart" width="200" height="200"></canvas>
</div>

Exemple De Données:

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1"
    },
    {
        value: 100,
        color: "#FDB45C",
        highlight: "#FFC870"
    }
]

jsfiddle PIE: http://jsfiddle.net/5gyfykka /

jsfiddle BAR / LINE: http://jsfiddle.net/5gyfykka/14 /

43
répondu Kapi 2014-11-24 12:31:33

j'ai étendu sur la méthode de Kapi donc quand vous survolez, vous pouvez toujours garder la fonctionnalité par défaut comme le changement de couleur, et quand vous survolez une section, il va cacher le reste. Je pense que c'est mieux.

var options =
{
    onAnimationComplete: function () {
        this.showTooltip(this.segments, true);
    },        
}

var ctx = document.getElementById("Chart").getContext("2d");
var myPieChart = new Chart(ctx).Pie(data, options);

$("#Chart").on('mouseleave', function (){
    myPieChart.showTooltip(myPieChart.segments, true);
});
6
répondu Indy411 2015-05-22 08:05:30

vous pouvez le faire en écrivant votre propre plugin qui supporte la version de ChartJS > 2.1.5.

incluez le code suivant dans votre script:

// Show tooltips always even the stats are zero

Chart.pluginService.register({
  beforeRender: function(chart) {
    if (chart.config.options.showAllTooltips) {
      // create an array of tooltips
      // we can't use the chart tooltip because there is only one tooltip per chart
      chart.pluginTooltips = [];
      chart.config.data.datasets.forEach(function(dataset, i) {
        chart.getDatasetMeta(i).data.forEach(function(sector, j) {
          chart.pluginTooltips.push(new Chart.Tooltip({
            _chart: chart.chart,
            _chartInstance: chart,
            _data: chart.data,
            _options: chart.options.tooltips,
            _active: [sector]
          }, chart));
        });
      });

      // turn off normal tooltips
      chart.options.tooltips.enabled = false;
    }
  },
  afterDraw: function(chart, easing) {
    if (chart.config.options.showAllTooltips) {
      // 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;
      }

      // turn on tooltips
      chart.options.tooltips.enabled = 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();
      });
      chart.options.tooltips.enabled = false;
    }
  }
});

// Show tooltips always even the stats are zero

et ensuite il suffit d'utiliser la ligne suivante dans les options de n'importe quel graphique sur lequel vous voulez montrer toutes les infobulles disponibles.

showAllTooltips: true

violon de travail est donné ci-dessous

// Show tooltips always even the stats are zero

Chart.pluginService.register({
  beforeRender: function(chart) {
    if (chart.config.options.showAllTooltips) {
      // create an array of tooltips
      // we can't use the chart tooltip because there is only one tooltip per chart
      chart.pluginTooltips = [];
      chart.config.data.datasets.forEach(function(dataset, i) {
        chart.getDatasetMeta(i).data.forEach(function(sector, j) {
          chart.pluginTooltips.push(new Chart.Tooltip({
            _chart: chart.chart,
            _chartInstance: chart,
            _data: chart.data,
            _options: chart.options.tooltips,
            _active: [sector]
          }, chart));
        });
      });

      // turn off normal tooltips
      chart.options.tooltips.enabled = false;
    }
  },
  afterDraw: function(chart, easing) {
    if (chart.config.options.showAllTooltips) {
      // 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;
      }

      // turn on tooltips
      chart.options.tooltips.enabled = 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();
      });
      chart.options.tooltips.enabled = false;
    }
  }
});

// Show tooltips always even the stats are zero


var canvas = $('#myCanvas2').get(0).getContext('2d');
var doughnutChart = new Chart(canvas, {
  type: 'doughnut',
  data: {
    labels: [
      "Success",
      "Failure"
    ],
    datasets: [{
      data: [45, 9],
      backgroundColor: [
        "#1ABC9C",
        "#566573"
      ],
      hoverBackgroundColor: [
        "#148F77",
        "#273746"
      ]
    }]
  },
  options: {
    // In options, just use the following line to show all the tooltips
    showAllTooltips: true
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
     <canvas id="myCanvas2" width="350" height="296"></canvas>
</div>

Working JSFIDDLE here.

6
répondu Suhaib Janjua 2017-02-16 11:13:59

dans le cas où quelqu'un essaie de cacher certains des segments tooltips; do it in the tooltiplate:

tooltipTemplate : "<%  var percentage = Math.round(circumference / 6.283 * 100); if (percentage >8)%><%= percentage %>%";

par exemple, ce code vérifie la valeur du %et affiche seulement des valeurs supérieures à 8% pour réduire l'encombrement

1
répondu ardavar 2015-12-04 22:36:49

si vous ne voulez afficher qu'une infobulle, vous devez utiliser ce code. Voici exaple pour le premier segment.

chart.showTooltip([chart.segments[0]], true);
La fonction

showTooltip n'accepte que les tableaux bidimensionnels pour le premier paramètre.

0
répondu Jan Šafránek 2016-02-08 08:32:26