user3059325
user3059325

Reputation: 85

Javascript - How to insert if and else statement inside option specification area of chart.js

I am using chart.js chart on my web page and I need to specify the min and max value of the chart axis. I can easily use the code shown below to specify the minimum and maximum value of the x axis of the chart but...

options: {
    scales: {
        xAxes: [{
            ticks: {
                max: 100,
                min: 0,

But, I need to have different minimum and maximum value depending on the value of the javascript variable 'Subject'.

So, I need to have the code such as:

options: {
    scales: {
        xAxes: [{
            ticks: {

                if(Subject == 'English'){
                    return "max: 100,";
                } else {
                    return "max: 50,";
                }        
                min: 0

But this code shows "token not valid" error message. How can I correctly insert conditional statement within the javascript option specification area?

Upvotes: 1

Views: 2322

Answers (2)

Frank Villarreal
Frank Villarreal

Reputation: 1

You can write the if statement outside the option, this worked for me:

if(Subject == 'English'){
  max1= 100;
} else {
  max1= 50;
}

And then:

options: {
    scales: {
        xAxes: [{
            ticks: {
                max: max1,
                min: 0,   

 

Upvotes: 0

Mocca
Mocca

Reputation: 499

Have you tried ternary ?

options: {
    scales: {
        xAxes: [{
            ticks: {
                max: (Subject == 'English') ? 100 : 50,
                min: 0,

Upvotes: 3

Related Questions