
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This is an add-on to ankane's Chartkick to allow the user to specify chart data in a block and have it automatically sourced remotely to avoid making too many queries in the original page render. Check out the demo at http://chartkick-remote-demo.heroku.com.
For more on the fabulous Chartkick library, see http://ankane.github.io/chartkick/.
In your controller, add the following to tell the controller to respond to json requests for chart data:
include Chartkick::Remote
chartkick_remote
Then in your views, pass your data as a block:
<%= line_chart { Task.group_by_day(:completed_at).count } %>
To prevent remote requests for a particular chart, set remote
to false
in the chart options:
<%= line_chart(remote: false) { Task.group_by_day(:completed_at).count } %>
This will generate the data set immediately when the page is rendered.
You can also pass arguments to chartkick for all views on your controller as options for the chartkick_remote
call:
<%= chartkick_remote height: "500px" %>
Finally, if you need to, you can restrict chartkick_remote to particular views:
<%= chartkick_remote only: :show %>
This will prevent the remote requests for other views.
First, set up 'chartkick' as described at http://ankane.github.io/chartkick/.
Then, add this line to your application's Gemfile:
gem 'chartkick-remote'
This gem works by not executing "remote" blocks when initially rendering your html page or when making any json request other than the single request that actually needs the data generated in that block. When responding to a json request, the controller will actually render your template, but only to a string that it will discard, and only so that it can find and execute the one block that is generating the data for this json request. This means that:
Using this methodology, it's easy to write a page that makes many, many json requests, which may swamp your server and possibly even time out if you have a global timeout
value set for your ajax requests. chartkick-remote
provides a javascript script, available as a rails asset, that can be used to limit the number of simultaneous assets on your page.
You can include it by adding the following to your javascript manifest:
//= require jquery.ajax.queue-concurrent
If you include this javascript in this gist on your page, you can then specify the maximum number of allowable requests globally for your page as follows:
$.ajaxSetup({queue: true, queueMaxConcurrency: 2});
This script is based on @maccman's jquery.ajax.queue.coffee script provides a basic queueing transport layer for ajax requests which I've modified to provide an option to set the maximum number of requests that can be made in parallel. You can see the modifications with a link to the original at https://gist.github.com/ashanbrown/1ad9ab33971b64fe6fef.
Sometimes it's useful to be able to be able to debug a single chart at a time even though there may be several on the page. Use the standalone
option to add a link to each chart that opens the page in standalone mode, where only the single chart is shown.
chartkick_remote standalone: true
FAQs
Unknown package
We found that chartkick-remote demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.