angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

Show Tooltip Labels On angular-chart Bar Chart

Open ChewingPencilLines opened this issue 10 years ago • 15 comments

Adding the following works for pie charts and such, but NOT for bar charts.

$scope.options = {
    tooltipEvents: [],
    tooltipCaretSize: 0,
    showTooltips: true,
    onAnimationComplete: function() {
        this.showTooltip(this.segments, true);
    }
}

This is really odd and I am wondering if it is due to how the bar chart data is defined (as an array of array, e.g. [ [2,3,4], [5,6,7] ] with each inner array being the data for a single data series).

I am only using a single series bar chart, e.g. [ [2, 3, 4] ], and it renders nicely. However, I am not able to get the tooltip labels permanently (not just on hover).

Help please, :(

See fiddle: http://jsfiddle.net/g0vg14ww/2/

ChewingPencilLines avatar Mar 15 '16 03:03 ChewingPencilLines

I want to show the tooltip on barchart so can you help me. i have tried the same code but it's not working for barchart. please help me $scope.options = { tooltipEvents: [], tooltipCaretSize: 0, showTooltips: true, onAnimationComplete: function() { this.showTooltip(this.segments, true); } }

amitq7000 avatar Jul 13 '16 10:07 amitq7000

Hi all,

Did anyone got the tooltips on bar chart?

erikneto avatar Sep 13 '16 15:09 erikneto

For anyone is trying to handle the event: onAnimationComplete Notice the options structure should be:

"options" : {
      "animation" : {
          "onComplete" : function () {
              ...
          }
      }
}

comlaterra avatar Oct 02 '16 21:10 comlaterra

For anyone who still needs help, I am sorry for the lack of response till date.

I did manage to figure it out on my own previously, which was why I closed the issue.

Here, ...

If you wish for the tooltips to be shown permanently, and not just on mouse hover, in the barChart.controller.js, this is what you set as the options:

$scope.barChartOptions = {
   showToolTips: true,
   tooltipEvents: [],
   onAnimationComplete: function() {
      this.showTootip(this.datasets[0].bars,true);
   }
};

If you wish for the tooltips to be shown only on mouse hover, in the barChart.controller.js, this is what you set as the options:

$scope.barChartOptions = {
   showToolTips: false,
   tooltipEvents: ["mousemove", "touchstart", "touchmove"],
};

For both options, in the relevant barChart.view.html, this is what you have:

<canvas chart-data="$scope.barChartData"
        chart-labels="$scope.barChartLabels"
        chart-options="$scope.barChartOptions"
        class="chart chart-bar">
</canvas>

ChewingPencilLines avatar Oct 03 '16 04:10 ChewingPencilLines

Thanks

On Wed, Aug 24, 2016 at 1:48 AM, R4ZuL [email protected] wrote:

I tried to show the tooltips permanently for a pie chart but the following code did not work:

// pieChart.controller.js class PieChartController { constructor() { var self = this; self.labels = ['Download Sales', 'In-Store Sales', 'Mail-Order Sales']; self.data = [300, 500, 100]; self.options = { tooltipEvents: [], tooltipCaretSize: 0, showTooltips: true, onAnimationComplete: function() { self.showTooltip(self.segments, true); } } } }

export default PieChartController;

//pieChart.component.js import template from './pieChart.tmpl.html'; import controller from './pieChart.controller.js';

let pieChartComponent = { scope: {}, template, controller };

export default pieChartComponent;

// pieChart.tmpl.html

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jtblin/angular-chart.js/issues/337#issuecomment-241863408, or mute the thread https://github.com/notifications/unsubscribe-auth/ALdW_BY9LJ3f7UaU17KJHCVGkxDaVhTeks5qi1WvgaJpZM4Hwrfw .

amitq7000 avatar Oct 03 '16 10:10 amitq7000

I tried the code below: $scope.barChartOptions = { showToolTips: true, tooltipEvents: [], animation: { onComplete: function() { console.log('onComplete event'); this.showTooltip(this.segments, true); //this.showToolTips(this.datasets[0].bars,true); } } };

and the result is this: onComplete event app.js:41 Uncaught TypeError: this.showTooltip is not a functiononComplete @ app.js:41startDigest @ Chart.min.js:11(anonymous function) @ Chart.min.js:11

erikneto avatar Oct 03 '16 18:10 erikneto

Please take note that it is

onAnimationComplete: function() {

and not

animation: {
  onComplete: function() {

Also, if you could just kindly format your code nicely in the future with indentation, that would make it a lot easier for others to read your comment, thank you!

ChewingPencilLines avatar Oct 04 '16 03:10 ChewingPencilLines

I have switched to the code below, but it didn't work:

    $scope.options = {
      showToolTips: true,
      tooltipEvents: [],
            onAnimationComplete: function() {
                alert('animation');
              this.showTootip(this.datasets[0].bars,true);
           }
    };

I have created the following fiddle: https://jsfiddle.net/kekonen/ejg1jh9n/

erikneto avatar Oct 17 '16 18:10 erikneto

That's because you are using the latest versions.

I was not. I was using:

"angular-chart.js": "~0.8.8",
"chart.js": "^1.0.2",

You may wish to check out the latest configuration options at : http://www.chartjs.org/docs/#chart-configuration-tooltip-configuration

ChewingPencilLines avatar Oct 18 '16 06:10 ChewingPencilLines

Ok. Just for doubt avoidance... do you mean that it works in previous versions but not on latests?

erikneto avatar Oct 18 '16 11:10 erikneto

Well, it worked in my project, which I have not updated in some time; hence why the versions used are so old.

On Oct 18, 2016 7:37 PM, "erikneto" [email protected] wrote:

Ok. Just for doubt avoidance... do you mean that it works in previous versions but not on latests?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jtblin/angular-chart.js/issues/337#issuecomment-254482222, or mute the thread https://github.com/notifications/unsubscribe-auth/AFBrTrx4fHfCWEvpL9YH43XaWZC7y_jxks5q1K9_gaJpZM4Hwrfw .

ChewingPencilLines avatar Oct 18 '16 11:10 ChewingPencilLines

I see. I will give it a try later. Just confused if this functionality was intentionally removed or if it is a bug.

Thanks.

erikneto avatar Oct 18 '16 11:10 erikneto

Chart.js had a lot of breaking changes going to 2.x so it is possible that it was deliberate, you should check Chart.js docs.

jtblin avatar Oct 19 '16 13:10 jtblin

does anybody know a solution how to show values on a top of bar chart? since I'm using chartjs 2 code below doesn't work((( $scope.options = { showToolTips: true, tooltipEvents: [], onAnimationComplete: function() { alert('animation'); this.showTootip(this.datasets[0].bars,true); } };

jack100501 avatar Jul 20 '17 06:07 jack100501

That did hep me

$scope.options = {
    events: false,
    tooltips: {
        enabled: false
    },
    hover: {
        animationDuration: 0
    },
    animation: {
        duration: 1,
        onComplete: function () {
            var chartInstance = this.chart,
                ctx = chartInstance.ctx;
            ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
            ctx.textAlign = 'center';
            ctx.textBaseline = 'bottom';

            this.data.datasets.forEach(function (dataset, i) {
                var meta = chartInstance.controller.getDatasetMeta(i);
                meta.data.forEach(function (bar, index) {
                    var data = dataset.data[index];                            
                    ctx.fillText(data, bar._model.x, bar._model.y - 5);
                });
            });
        }
    }
};

jack100501 avatar Jul 20 '17 08:07 jack100501