15

I have created a line chart that is able to update its data successfully in chartjs and I am trying to change the option values dynamically as well. More specifically I want to be able to change the scale. I would like to avoid re-creating the chart everytime updates are needed. Is there a way to update the options dynamically such as :

myLiveChart.options.scaleStepWidth = 10;
myLiveChart.update();

Note: I have tried the following as well :

myLiveChart.scaleStepWidth = 10;

Also tried myLiveChart.render(); instead of update, but nothing seems to really work.

here is a fiddle illustrating the problem: http://jsbin.com/yaxafehixe/1/edit?html,js,output

5
  • 1
    did you check the chartjs docs? this of any use - chartjs.org/docs/#scales-update-default-scale-config Commented Jul 28, 2016 at 19:16
  • 1
    @ Craicerjack :yes i did, but I couldn't find info on specifically want i wanted to do.. which is to update the options, and not the dataset. What i've tried so far (using the render or update function) is based on the docs . Commented Jul 28, 2016 at 19:20
  • problem is : i am not sure exactly where to check options such as: scaleOverride, scaleSteps ,scaleStepWidth: , scaleStartValue are not in scales section, they are directly under options section Commented Jul 28, 2016 at 19:33
  • yeah looking at the docs I dont know, it seems the options are added to the chart upon its creation and to update them you need to re-render the chart or re-create it... Commented Jul 28, 2016 at 19:43
  • @Craicerjack : that would suck a little. I don't think that's the case though, because in my fiddle I have tried myLiveChart.options.animation = true; and to my surprise it worked... which is weird because any other options i've tried, such as the ones i am trying to change, are not working... Commented Jul 28, 2016 at 20:00

1 Answer 1

25

I found a solution that works in case anybody googles this. Here is the thing : I was using chartjs version 1, in which the update() function doesn't work in all cases, especially when you are trying to change the options of a chart. Therefore, switching to version 2, allowed me to use update() properly with changing the options such as :

myChart.options.scales.yAxes[0].ticks.min = someValue;

Here is a fiddle to demonstrate the solution to my problem in case anybody faces the same issue: http://jsbin.com/bamemuliyu/3/edit?html,js,output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.