# Chart.js Dynamic Update - AJAX

**URL:** <https://community.modx.com/t/chart-js-dynamic-update-ajax/1421>\
**Category:** General\
**Created:** [August 28, 2019, 2:29pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421 "2019-08-28T14:29:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lacdav07](https://community.modx.com/letter_avatar_proxy/v4/letter/l/cab0a1/32.png) [@Lacdav07](https://community.modx.com/u/Lacdav07)\
**Post date:** [August 28, 2019, 2:29pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/1 "2019-08-28T14:29:07Z")

</div>

Hi everyone,

I am currently using a DateRangePicker in combination with some radio-buttons to display a chart. The problem is that I am using PHP and therefore the page has to be reloaded every time I change the dates.

I tried with AJAX but had some struggle to recalculate the entire canvas again and to overwrite the container with the ‘old’ canvas.

My idea with AJAX was to create a 2nd document which calls a snippet that is only used to calculate the entire canvas in PHP with the results of the SQL query. But how do I get **that** result to the initial document?

Here’s a gif of how it currently looks.

 ![](https://modx-community.s3.dualstack.us-east-1.amazonaws.com/original/1X/80a1a3748653f4c8134eb21bd37eeffcc8b0fde8.gif)

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [August 28, 2019, 6:02pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/2 "2019-08-28T18:02:18Z")

</div>

One way is to create a processor file (with a class that extends modProcessor or one of it’s descendants). Make your Ajax call to that processor and have the processor return the canvas.

---

<div class="post-metadata">

**Author:** ![dev\_willis](https://community.modx.com/user_avatar/community.modx.com/dev_willis/32/1721_2.png) [@dev\_willis](https://community.modx.com/u/dev_willis)\
**Post date:** [August 29, 2019, 7:07pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/3 "2019-08-29T19:07:04Z")

</div>

I think I need some more clarification. It sounds like you want to know how to update the dataset Chart.js is using without reloading the page. However, your GIF appears to be demonstrating this behavior already, which leads me to think I don’t understand what you’re asking. Would you mind explaining further?

---

<div class="post-metadata">

**Author:** ![Lacdav07](https://community.modx.com/letter_avatar_proxy/v4/letter/l/cab0a1/32.png) [@Lacdav07](https://community.modx.com/u/Lacdav07)\
**Post date:** [August 30, 2019, 6:08am UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/4 "2019-08-30T06:08:47Z")

</div>

The GIF is cut, that’s why it appears to be the desired behavior. The chart is at the very end of a page which means that every time I press the calendar button, I have to scroll down.

What I’d like to achieve is to change the datasets dynamically without reloading the page - in this case through a submitted form.  
The struggle I have is that the datasets have to be requested by a query, which means that I would have to get the values from the daterange with e.g. jQuery, then I’d have to POST the dates with AJAX.

From that point on it would be redundant since I would call the first snippet again to execute the query with the new date range and then display the chart. But even if I did that anyways, how would I get **that** result (updated canvas) back to AJAX/jQuery to overwrite the first canvas?

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [August 30, 2019, 8:34pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/5 "2019-08-30T20:34:00Z")

</div>

You can include your $\_POST data in a JavaScript AJAX call to a processor. The processor can return the new data and you can create the display with JS, or (probably easier) the processor can create and return the entire new chart. Either way the success method of the AJAX call call can modify the page using either one without a reload.

---

<div class="post-metadata">

**Author:** ![bestmed90](https://community.modx.com/letter_avatar_proxy/v4/letter/b/b77776/32.png) [@bestmed90](https://community.modx.com/u/bestmed90)\
**Post date:** [September 16, 2019, 12:51pm UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/6 "2019-09-16T12:51:30Z")

</div>

Thanks, it helped me a lot [🙂](https://best-med.pl/powiekszanie-penisa-kwasem-hialuronowym/)

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [September 17, 2019, 5:14am UTC](https://community.modx.com/t/chart-js-dynamic-update-ajax/1421/7 "2019-09-17T05:14:25Z")

</div>

I’m glad I could help. 🙂
