On a recent project I've been working on, Markolio (market portfolio), we wanted to chart historical pricing data. After a quick Google search, Highcharts / Highstocks became the obvious option. I came across a few roadblocks while getting my graph functioning properly, so I figured I would share my solution to hopefully help others.
To begin, you will need an array of arrays, which is the data that you want graphed. I have my dates along the X axis, and prices along the Y axis. My first hiccup was that Javascript expects a date to be in milliseconds. Rails will convert a date to an integer by simply doing .to_i but you will need to multiple this by 1000 to make Javascript happy. My resulting code is as follows:
Easy enough, right? Graphing the data is pretty straightforward, you just set a javascript variable to your Rails object, then feed Highstocks the Javascript variable in the series configuration. My next struggle came with calculating the percentage change between the first value and the last value. Highstocks has the min and max values as part of their API, but no obvious way to get the first Y value on the X-axis and the last Y value on the X-axis. I posed the question on the Highstocks forums, and the response works great:
My full Highstocks configuration is as follows:
The above code will calculate the difference between the first and last points when the graph is loaded, as well as when the user zooms in on any point of the graph. The calculation happens if the user clicks one of the pre-defined ranges via the range selector, or if they manually zoom in on a portion of the graph.
The last little feature here is the year to date value in the range selector. I didn't come across this in the documentation, but rather on the Highcharts Github page. This is pretty straight forward as well.
That's it!
Showing posts with label rails 5. Show all posts
Showing posts with label rails 5. Show all posts
Thursday, November 9, 2017
Graphing data in Rails 5 with Highcharts / Highstocks
Wednesday, July 12, 2017
Nginx virtualhost example for Passenger / Rails
For those of us coming from the Apache world, getting a working Nginx virtualhost configured for Rails can be quite the headache. It's actually pretty simple once you know what's going on. After installing nginx and passenger-phusion for nginx, all you need is the following (Note: the location portion of the config is for setting resource expiration to 1 month. You may need to adjust this based on your needs):
Sunday, October 9, 2016
Uninitialized constant error with Rails 5, Paperclip and Amazon S3
I haven't used Heroku to host anything in quite a while, but had a personal project that I wanted to put up and show someone. Unfortunately, every time you push an update to Heroku or the app reboots, it clears any files you've uploaded to it. I went ahead with setting up Amazon S3 for file storage and thought everything would be gravy to my dismay. Heroku gave me a 500 error when I tried uploading an image: NameError (uninitialized constant Paperclip::Storage::S3::AWS)
I wondered if it was an issue specific to Heroku, so I setup the Paperclip AWS config to test locally to no avail; I was still getting the same error. I immediately went to Google and tried all of the solutions I came across on Github and Stack Overflow to no avail; my problem persisted. I finally came across a post on Stack Overflow that had a similar error, and in the comments, someone suggested using the 5.0.0.beta2 version of the Paperclip gem. I updated my Gemfile to use that version: gem 'paperclip', '5.0.0.beta2' and after a bundle install - file uploads were working! I did have to specify the s3_host_name in my paperclip config to get images to display, but it worked. Since I haven't come across much information on this on the web, I thought I'd make a post about it. So if anyone else is having this issue, update your Gemfile to use a version of the Paperclip gem > 5.0. I updated mine to use Paperclip 5.1.0 and everything is working great.
Cheers.
I wondered if it was an issue specific to Heroku, so I setup the Paperclip AWS config to test locally to no avail; I was still getting the same error. I immediately went to Google and tried all of the solutions I came across on Github and Stack Overflow to no avail; my problem persisted. I finally came across a post on Stack Overflow that had a similar error, and in the comments, someone suggested using the 5.0.0.beta2 version of the Paperclip gem. I updated my Gemfile to use that version: gem 'paperclip', '5.0.0.beta2' and after a bundle install - file uploads were working! I did have to specify the s3_host_name in my paperclip config to get images to display, but it worked. Since I haven't come across much information on this on the web, I thought I'd make a post about it. So if anyone else is having this issue, update your Gemfile to use a version of the Paperclip gem > 5.0. I updated mine to use Paperclip 5.1.0 and everything is working great.
gem 'paperclip', '5.1.0'With the paperclip config as follows:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
s3_host_name: ENV.fetch('S3_HOST_NAME'),
}
}
Cheers.
Wednesday, October 5, 2016
Rails 5 resque error 'cannot load such file -- rack/showexceptions'
I decided to setup some background jobs for an app I've been working on, and decided to go with Resque for handling the jobs. I wanted to be able to run the resque jobs as rake tasks, so I added the appropriate lines to my Rakefile, but when starting my Rails server - I got an error:
Cheers
After a little Googling, I came across this issue posted on the Sidekiq Github account. The recommendation was to add the Sinatra gem, which worked for Sidekiq users. I gave it a try, and that fixed the error for me as well:! Unable to load application: LoadError: cannot load such file -- rack/showexceptions
gem 'sinatra', github: 'sinatra'Just thought I'd throw this up in case anyone else came across this issue.
Cheers
Subscribe to:
Posts (Atom)