Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset not working (when align: 'center') #26

Closed
alzearafat opened this issue Jan 18, 2018 · 2 comments
Closed

Offset not working (when align: 'center') #26

alzearafat opened this issue Jan 18, 2018 · 2 comments

Comments

@alzearafat
Copy link

Hi, cool plugin

But I have an issue with offset options: https://chartjs-plugin-datalabels.netlify.com/positioning#alignment-and-offset

The label is not pulled out from the anchor point. It just stays there.

This is my code:

var datatables_plugin = [
    Chart.defaults.global.plugins.datalabels.anchor = 'end',
    Chart.defaults.global.plugins.datalabels.font.size = 11,
    Chart.defaults.global.plugins.datalabels.font.weight = 700,
    Chart.defaults.global.plugins.datalabels.padding.bottom = 81,
    Chart.defaults.global.plugins.datalabels.offset = 8,
    Chart.defaults.global.defaultFontColor = 'black'
];

function EAF() {
    var ctx = document.getElementById("chart_EAF");
    datatables_plugin;

    var chart_EAF = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["16-Okt", "17-Okt", "18-Okt", "19-Okt", "20-Okt", "21-Okt", "22-Okt"],
            datasets: [{
                label: "% (Persen)",
                backgroundColor: ["#1fb6ff", "#1fb6ff", "#1fb6ff", "#1fb6ff", "#1fb6ff", "#1fb6ff", "#1fb6ff"],
                data: [82.99, 82.99, 82.99, 82.99, 82.99, 82.99, 82.99]
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            },
            legend: {
                display: true,
                fontColor: 'black'
            },
            title: {
                display: true,
                text: 'EAF'
            }
        }
    });
}

Any help would be greatly appreciated!
Thanks!

@simonbrunel
Copy link
Member

simonbrunel commented Jan 18, 2018

The offset represents the distance (in pixels) to pull the label away from the anchor point. This option is not applicable when align is center

By default, align === 'center'

You should try align: 'end' or 'start' or ... but not 'center':

Chart.defaults.global.plugins.datalabels.align = 'end'; 

You could also use Chart.helpers.merge

Chart.helpers.merge(Chart.defaults.global.plugins.datalabels, {
  align: 'end',
  anchor: 'end',
  font: {
    size: 11,
    weight: 700
  },
  offset: 8
  // ...
});

@alzearafat
Copy link
Author

Hi @simonbrunel

Wow, thanks! It works now. I just add :

Chart.defaults.global.plugins.datalabels.align = 'end'; 

And works like a charm. Thanks!

@alzearafat alzearafat changed the title Offset not working [SOLVED] Offset not working Jan 20, 2018
@simonbrunel simonbrunel changed the title [SOLVED] Offset not working Offset not working (when align: 'center') Feb 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants