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

Add possibility to change style for each of the ticks #2442

Closed
moviesound opened this issue May 3, 2016 · 41 comments · Fixed by #6939
Closed

Add possibility to change style for each of the ticks #2442

moviesound opened this issue May 3, 2016 · 41 comments · Fixed by #6939

Comments

@moviesound
Copy link

It would be great if in future there is a possibility to change style (color, bgcolor, font-weight) for each of the ticks. For example if one needs to show in XAxes that 7 May 2016 and 8 May 2016 are holidays on the linear graph - changes the style for this ticks on the XAxes. Now style is set once for all ticks.

@etimberg
Copy link
Member

etimberg commented May 3, 2016

@moviesound this is a good idea. For anyone who comes across this, I'd be happy to merge a PR adding this.

@etimberg etimberg added this to the Version 2.x milestone May 3, 2016
@moviesound
Copy link
Author

moviesound commented May 3, 2016

This is an example. There may be placed array in fontColor (only for XAxes). The amount of color elements must be equal to the amount of the elements in data.labels.

<canvas id="graph-canvas" width="800" height="400"></canvas>

<script>
data = {
    labels: ["26 Apr","27 Apr","28 Apr","29 Apr","30 Apr","01 May","02 May","03 May"],
    datasets: [{
        label: "total",
        data: [2, 3, 4, 2, 3, 5, 6, 3]
        }]
};

var ctx = document.getElementById("graph-canvas");
var myLineChart = new Chart(ctx, {
    type: 'line',
    data,
    options: {
        scales: {
            xAxes: [{
                ticks: {
                    fontColor:['rgba(44,44,44,0.8)','rgba(44,44,44,0.8)','rgba(44,44,44,0.8)','rgba(178,31,31,1)','rgba(44,44,44,0.8)','rgba(44,44,44,0.8)','rgba(44,44,44,0.8)','rgba(44,44,44,0.8)'],
                    callback: function(tickValue, index, ticks) {
                        return tickValue;
                    }
                }
            }]
        }
    }
});
</script>

Then in chart.js near the row 6349 after the
helpers.each(this.ticks, function(label, index) {
there can be placed

if(Object.prototype.toString.call( tickFontColor ) === '[object Array]'){
    this.ctx.fillStyle = tickFontColor[index];
}

@etimberg etimberg removed this from the Version 2.x milestone Sep 9, 2016
@etimberg etimberg changed the title Add possibility yo change style for each of the ticks Add possibility to change style for each of the ticks Oct 16, 2016
@ortichon
Copy link

for chart.js 2.6.0 you should do the change @moviesound suggested, but in:
chart.js/src/core/core.scale.js at line 830, change:

context.fillStyle = itemToDraw.major ? majorTickFontColor : tickFontColor;

to

if (Object.prototype.toString.call( tickFontColor ) === '[object Array]') {
    context.fillStyle = itemToDraw.major ? majorTickFontColor[index] : tickFontColor[index];
} else {
    context.fillStyle = itemToDraw.major ? majorTickFontColor : tickFontColor;
}

and in line 798 change:

helpers.each(itemsToDraw, function(itemToDraw) {

to:

helpers.each(itemsToDraw, function(itemToDraw, index) {

@kneeki
Copy link

kneeki commented Jan 26, 2018

@etimberg, could you provide an example on how to use 683e86e?

edit:
After noticing this commit I cloned @bevingtongroup's repo, built it, and used the new Chart.min.js file, I was able to achieve the following result:

image

Using the following in my Chart options:

 yAxes: [{
    ticks: {
        fontColor: ['white', '#89817b', 'white', '#89817b', 'white', '#89817b', 'white', '#89817b', 'white', '#89817b', 'white', '#89817b'],
        fontFamily: 'Gotham Book',
        fontFunction: function (tickIndex) {
            if (tickIndex % 2 !== 0) {
                return '14px "Gotham Book"';
            } else {
                return '30px "Gotham Book"';
            }
        },
        callback: function (value) {
            return '$' + abbreviate_number(value);
        }
    }
}],

@iursevla
Copy link

iursevla commented Feb 2, 2018

@kneeki how did you build the repo? What are the commands?

@kneeki
Copy link

kneeki commented Feb 3, 2018

@iursevla , I use Linux, here is the process:

cd ~/
git clone https://github.com/bevingtongroup/Chart.js.git
cd Chart.js
# Use apt to install npmjs if you haven't already
npm install
sudo npm install -g gulp
gulp build

Once it's built, you can use the code in my previous post to achieve the same effect.

@oscaruribe
Copy link

Can someone merge this into main pls?

smartkrio pushed a commit to smartkrio/Chart.js that referenced this issue Mar 27, 2018
@crabdul
Copy link

crabdul commented Jun 2, 2018

Is this now a default feature?
and if so, have the docs been updated?

@Lenndev
Copy link

Lenndev commented Aug 31, 2018

@crabdul it seems this is not a default feature, so for now i recommend to copy https://github.com/bevingtongroup/Chart.js or copy the code base of this main repo and modify code yourself

@Lenndev
Copy link

Lenndev commented Aug 31, 2018

oke.. i've tested the code of bevingtongroup, and it works like a charm... this really! should be pulled into this general repo

@quannt
Copy link

quannt commented Nov 5, 2018

Any chance of merging this? it's been more than a year.

amanjotSuccessive pushed a commit to amanjotSuccessive/Chart.js that referenced this issue Dec 24, 2018
amanjotSuccessive pushed a commit to amanjotSuccessive/Chart.js that referenced this issue Dec 24, 2018
@rpearce
Copy link

rpearce commented Jan 21, 2019

Are there any open PRs for this at the moment to make this change? If not, either somebody with the most knowledge of the issue can perhaps put one up, or I can try?

@nagix
Copy link
Contributor

nagix commented Jan 30, 2019

Scriptable options for tick labels are not yet supported even in the master.

@benmccann benmccann reopened this Jan 30, 2019
@wintersocram
Copy link

You could accomplish this with scriptable options...

I believe this is just for bubble, not for line (Note: scriptable options are only supported by a few bubble chart options.). Anyway, I tried to use scriptable to change the color of each element of XAxes, but it always get the last color of the array and apply that for all items. @benmccann , do you have an example? I am really blocked by that.

@wintersocram
Copy link

@crabdul it seems this is not a default feature, so for now i recommend to copy https://github.com/bevingtongroup/Chart.js or copy the code base of this main repo and modify code yourself

Guys, a little help? Maybe is not the place to ask that, however will be helpfull to have here the complete way to use this work around (and very cleaver) solution:
I have success with the solution @ortichon , and it is good enough to me to make my own custom copy of chartjs until it will be commit at Master. The problem is: how to make this copy to work? I created a new folder with this custom code (copy from chart.js - master), I runned the npm i, gulp build... But I am having problem to add that to my project... before it was inside my package.json, inside dependencies: "chart.js": "^2.7.2", now I need to use my customized folder inside my project, but how? inside the same package json, but pointing to my custom version, inside dependencies as well? Very newbie this question, but has a long time i dont touch in import files / dependencies... someone can help me with that?

@ortichon
Copy link

ortichon commented Feb 19, 2019

@Winnetou
I've just forked chart.js repo to my own account,
commited and pushed the changes I wanted,
and then added to my package.json:
"chart.js": "<your-account-name>/Chart.js"
then npm install and you're done.
so actually I'm installing the package directly from my Github repo

@benmccann
Copy link
Contributor

benmccann commented Feb 19, 2019

scriptable options are only supported by a few bubble chart options.

Yes, for 2.7.3. The latest code in master supports many more scriptable options.

@EggsProduction11
Copy link

EggsProduction11 commented Apr 11, 2019

is this feature available in master or do I have to use @benpdavison version of chart.js?

@sohskd
Copy link

sohskd commented Jul 8, 2019

Does chartjs 2.8.0 support the different color axis for different ticks?

@vishnup95
Copy link

@etimberg Can you please merge these changes to master.

@QiQiBiBi
Copy link

QiQiBiBi commented Jan 8, 2020

Wow 3 years no one does that.

@benmccann
Copy link
Contributor

The best way to implement this would be to use scriptable options, which are supported thoughout the library.

Right now, there's one set of options for major ticks and one set for minor ticks:

function parseTickFontOptions(options) {

Instead, we could accept a function which takes a context parameter and provides the user with the chart, scale, tick, tickIndex, etc. Then _computeLabelItems could resolve a new set of font options by calling the function for each label.

@etimberg etimberg added this to the Version 3.0 milestone Jan 14, 2020
@QiQiBiBi
Copy link

The best way to implement this would be to use scriptable options, which are supported thoughout the library.

Right now, there's one set of options for major ticks and one set for minor ticks:

function parseTickFontOptions(options) {

Instead, we could accept a function which takes a context parameter and provides the user with the chart, scale, tick, tickIndex, etc. Then _computeLabelItems could resolve a new set of font options by calling the function for each label.

I am using Vue-chartjs but strange that except for callbacks: {} other params such as fontColor or fontSize doesn't accept a function. And I am not sure which instance produces _computeLabelItems, oops.

@etimberg
Copy link
Member

@QiQiBiBi the scriptable tick options are only in the dev version. It's not yet released. If you build chart.js yourself you can get access to it

@Darkscanner
Copy link

Is there any documentation on these scriptable options and this parseTickFontOptions? I am having trouble understanding how it works

@benmccann
Copy link
Contributor

Yes, there are some docs for 3.0.0-alpha here: https://www.chartjs.org/docs/next/axes/styling/

@Darkscanner
Copy link

Darkscanner commented Jul 14, 2020

I'm a bit confused. I'm trying to get font colors to work in 3.0 and I can't get them to show at all. Even using the exact code from the migration guide, nothing seems to be happening: https://jsfiddle.net/egy8qcxu/

@kurkle
Copy link
Member

kurkle commented Jul 14, 2020

@Darkscanner
Copy link

I'm unclear on how that page relates to the issue. Like I said, the code in the JS fiddle is ripped straight from the migration guide, yet it doesn't change the font: https://jsfiddle.net/egy8qcxu/ but even if I try to replicate the syntax on the page provided, that doesn't seem to work either: https://jsfiddle.net/egy8qcxu/2/

Am I missing something fundamental? Do I need to include anything beyond the chart.min.js for 3.0 for fonts to work?

@benmccann
Copy link
Contributor

The code in the migration guide has changed a bit in master and reflects the upcoming 3.0.0-alpha.2 release: https://www.chartjs.org/docs/master/getting-started/v3-migration/

Here's a sample from master that shows some ticks as being bolded: https://www.chartjs.org/samples/master/scales/financial.html

@Darkscanner
Copy link

Figured out the problem: It seems I was using the wrong version. Originally I just googled Chart js 3.0 and downloaded the min from github. Replaced that min with the min included in the example pages and everything seems to be working now.

To anyone else having trouble, make sure you have the right version!

@01
Copy link

01 commented Jul 23, 2020

Seems that the 3.0.0-alpha.2 breaks the chartjs-plugin-datalabels plugin

@benmccann
Copy link
Contributor

Probably none of the plugins are compatible with 3.0 and will need to be updated when 3.0 final is released

@hichamglia
Copy link

Figured out the problem: It seems I was using the wrong version. Originally I just googled Chart js 3.0 and downloaded the min from github. Replaced that min with the min included in the example pages and everything seems to be working now.

To anyone else having trouble, make sure you have the right version!

Would please share the code that worked for you.

@needbill
Copy link

needbill commented Aug 4, 2022

Using the last 3.9, if anybody still stumbles upon this topic, here is a working jsfiddle https://jsfiddle.net/keag3ptL/1/ where the X Axis Label get conditionally styled based on their label value.

@luckyboy125
Copy link

I'm chartjs 2 module for react.
I need to change color of last item as axis line in line chart.
how do change color of last item?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging a pull request may close this issue.