chartkick: `createChart` is not fired after Turbo Frame renders the view
Describe the bug
The Chartkick graph is not rendered when the Turbo Frame is replaced using Turbo Frames with GET requests. Others have reported the same issue before.
To reproduce Steps to reproduce.
<div class="flex justify-end mb-1">
<%= form_with url: [:reports, :sales], method: :get, data: { turbo_frame: "sales", turbo_action: "advance" } do |form| %>
<%= form.date_field :start_date, value: params[:start_date], class: "form-input" %>
<%= form.date_field :end_date, value: params[:end_date], class: "form-input" %>
<% end %>
</div>
<%= turbo_frame_tag "sales" do %>
<%= column_chart @sales %>
<!-- Sales table -->
<% end %>
The form above uses a data-turbo-frame to target the sales frame and tells Turbo to use the response from the form submission to replace the content of the sales frame. The page URL is also updated by adding a turbo-action data attribute to the form that triggers the frame navigation.
After submitting the form, the chart body appears but the <script> function is not executed so the chart is not displayed.
Checking the window.Chartkick.charts object returns {}.
HTML generated by Chartkick
<turbo-frame id="sales" src="http://lvh.me:5000/reports/sales?start_date=2022-11-01&end_date=2022-11-04" complete="">
<div id="chart-1" style="height: 280px; width: 100%; text-align: center; color: #999; line-height: 280px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;"><canvas style="box-sizing: border-box;" width="300"></canvas></div>
<script>
(function() {
if (document.documentElement.hasAttribute("data-turbolinks-preview")) return;
if (document.documentElement.hasAttribute("data-turbo-preview")) return;
var createChart = function() { new Chartkick["ColumnChart"]("chart-1", [["2022-11-01","0"],["2022-11-02","0"],["2022-11-03","0"],["2022-11-04","0"]], {"legend":false,"colors":["#000","#000"],"prefix":"$","round":2,"zeros":true,"thousands":","}); };
if ("Chartkick" in window) {
createChart();
} else {
window.addEventListener("chartkick:load", createChart, true);
}
})();
</script>
</turbo-frame>
As part of the PR from hotwired/turbo that triggers the frame navigation and allows for this type of form submission to occur, Turbo will dispatch a turbo:load event.
A simple fix that results in the expected behaviour was to add an event listener for the turbo:load event, and call createChart within that event listener.
--- a/chartkick/lib/helper.rb
+++ b/chartkick/lib/helper.rb
js = <<~JS
<script#{nonce_html}>
(function() {
if (document.documentElement.hasAttribute("data-turbolinks-preview")) return;
if (document.documentElement.hasAttribute("data-turbo-preview")) return;
var createChart = function() { #{createjs} };
if ("Chartkick" in window) {
createChart();
+
+ window.addEventListener("turbo:load", createChart);
} else {
window.addEventListener("chartkick:load", createChart);
}
})();
</script>
JS
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 16 (2 by maintainers)
Commits related to this issue
- Fix for Turbo [Closes https://github.com/ankane/chartkick/issues/608] — committed to wilg/chartkick by wilg 3 years ago
- Fix for Turbo [Closes https://github.com/ankane/chartkick/issues/608] — committed to OilCan/chartkick by wilg 3 years ago
Just hit the same issue - the problem seems to be caused by chartkick destroying all charts on turbo:before-render. It seems that inline script runs before that hooks, so new charts are destroyed just after they are rendered.
I fixed it using the following:
Thanks!
As far as I can tell I then need to override the whole function, which is quite long? (New to Rails, so asking stupid questions left and right!)
I did it using the following file content, but it definitely is very fragile, as any changes in any of the other 90 lines of code that I didn’t want to change will introduce weird bugs.
I’m experiencing what seems like same issue. The charts are not loading when navigating to a page for the first time with turbo. The proposed fix makes the charts load as expected. However, subsequent navigation in the app then results in error messages in the console, since the listeners on
turbo:loadare still active but turbo has loaded a new page that does not contain those charts. Adding theonce: trueoption prevents these error messages in my case while still solving the original problem of the charts failing to load.Hey all, i found the fix and hopefully will work for you too.
I wasn’t able to repro the issue on the fresh Rails 8 app from above so i was like 🤔.
Bit of context:
So just update the chart on each submit.
The same code works in a new Rails app but not in mine, so I compared the requests/responses between a full browser refresh and the form submission and realized that the response from the form (which has
Turbo-Framein the headers as the target for theturbo-frame) contained the full layout rather than just the view.On
ApplicationControlleri had:so Rails was loading the full layout even for turbo-frame requests.
The fix:
Thanks @ankane for the great job on this gem 🙌
Thank you @ankane - For what it’s worth, I was not able to reproduce using the fresh Rails 8 app either. Tried all kinds of combinations with frames, streams etc.
Hi all, I’m having trouble reproducing the issue (using the latest version of Turbo).
I’ve created a fresh Rails 8 app (with a minimal setup of Chartkick). If someone is able to create a minimal PR to reproduce, happy to look into it more.
hi folks, I run in the same issue and I don’t like to say that, but I have no idea how it works, but this solve the issue for me.
I add javascript include tag in app/views/layouts/application.html.erb
to be fair got this from https://stackoverflow.com/questions/73452250/chartkick-not-loading-in-ruby-on-rails-7