Data from db into google charts? - ruby-on-rails

New to Rails. How can I export data from my database( I'm using Sqlite3) into a chart in rails? I have 2 columns I would like to visualizes, Timestamps and Decimal values.
I have seen plenty of examples on how to display data with Google Charts in rails, but never data from a db.
Any instruction or examples would be appreciated.

Well, I guest you are using googlecharts gem. In the page there are a lot of really basic examples. like this one:
Gchart.line(:data => [0, 40, 10, 70, 20])
So, instead of put the numbers, yo can put your data like this
Gchart.line(:data => MyModel.pluck(:my_attribute))
where my_atributte is a column in the MyModel Object
this will create a link like this:
http://chart.apis.google.com/chart?chd=s:AjI9R&cht=lc&chs=300x200&chxr=0,0,70
and you can use it as you want... maybe put in a img tag or whatever

write a webservice which will return json data, use that to populate charts
look here http://pivotallabs.com/building-a-fast-lightweight-rest-service-with-rails-3/

Related

How do I use importXML/importHTML to retrieve a stock options chain that is hidden?

I am trying to use importXML or importHTML function to retrieve a stock options chain, specifically using this function to get a table. So far, this is what I have:
=importhtml("https://bigcharts.marketwatch.com/quickchart/options.asp?symb=TSLA","table",3)
The problem I'm getting is that I can't retrieve "hidden" tables. For example, if you go on the website: https://bigcharts.marketwatch.com/quickchart/options.asp?symb=TSLA
If you scroll down on this page the hidden tables would only be revealed if you click on ,"show april 2022", "show may 2022", etc. I am trying to retrieve all of this information.
The end result is that I would like to create a table that looks like this:
https://www.barchart.com/stocks/quotes/tsla/put-call-ratios
And a table that looks like this:
https://www.barchart.com/stocks/quotes/TSLA/options?moneyness=10&view=stacked&expiration=2022-04-14-m
As a result, there are two things that I am trying to create, the above tables shown on barchart.
I have tried to use importHTML or importXML on barchart, but it looks like it's not allowed. If there is a way to directly retrieve the information from barchart, that would be a much better solution rather than having to import all of the data separately using a different website.
Please note, that I do have a beginner knowledge, so a step-by-step solution on what to do would be very helpful. Thank you

Access all fields in Zapier Code Step

Is it possible to access all the fields from a previous step as a collection like json rather than having to explicitly setting each one in the input data?
Hope the screenshot illustrates the idea:
https://www.screencast.com/t/TTSmUqz2auq
The idea is I have a step that lookup responses in a google form and I wish to parse the result to display all the Questions and Answer into an email.
Hope this is possible
Thanks
David here, from the Zapier Platform team. Unfortunately, what I believe you're describing right now isn't possible. Usually this works fine since users only map a few values. The worst case is when you want every value, which it sounds like you're facing. It would be cool to map all of them. I can pass that along to the team! In the meantime, you'll have to click everything you're going use in the code step.
If you really don't want to create a bunch of variables, but you could map them all into a single input and separate them with a separator like |, which (as long as it doesn't show up in the data), it's easy to split in the code step.
Hope that helps!
The simplest solution would be to create an additional field in the output object that is a JSON string of the output. In a Python code step, it would look like
import json
output = {'id': 123, 'hello': 'world'}
output['allfields'] = json.dumps(output)
or for returning a list
import json
output = [{'id': 123, 'hello': 'world'},{'id': 456, 'bye': 'world'}]
for x in output:
x['allfields'] = json.dumps(output[output.index(x)])
Now you have the individual value to use in steps as well as ALL the values to use in a code step (simply convert them from JSON). The same strategy holds for Javascript as well (I simply work in Python).
Zapier Result
Fields are accessible in an object called input_data by default. So a very simplistic way of grabbing a value (in Python) would be like:
my_variable = input_data['actual_field_name_from_previous_step']
This differs from explicitly naming the the field with Input Data (optional). Which as you know, is accessed like so:
my_variable = input['your_label_for_field_from_previous_step']
Here's the process description in Zapier docs.
Hope this helps.

Split datetime value received from external API in Rails app

I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.
The way I have set up the app means that the value is "stored" like this in my view: #search.dining_date_and_time
I have tried two approaches to solving this problem:
Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"
Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized>
This is when instead I put #search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.
Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as #search.dining_date_and_time from the Search model for any of this to work?
Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.
Happy to provide further details, code snippets if required.
As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.
The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:
$(function() {
var rawDateTime = $('#searchDiningDateTime').html();
// console.log(rawDateTime);
var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
// console.log(cleanDate);
$('#searchDiningDateTime').html(cleanDate);
var cleanTime = $.format.date(rawDateTime, "HH:mm");
// console.log(cleanTime);
$('#searchTime').html(cleanTime);
});
Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).

Can I link to other URL's using chartkick's charts?

I've got a rails 4 app and I'm using chartkick. I'm currently using pie charts, and I'm wondering if there is a way I can get each section of my pie chart to navigate to an endpoint or a url or anything really.
How would someone tackle that? Would it be done in the ERB file, or the controller? And how would it be done?
I've spent a lot of time trying to figure this one out myself. Unfortunately, there does not seem to be a simple 'add the link here and go' kind of option.
Chartkick allows you to specify whether you want to use the Google Charts Library or the Highcharts library. Both of these libraries allow you to draw charts where you can specify the 'click' action of a particular data point / data series. To utilize this, you can draw your chart using straight JQuery/JS -- it's much easier than it seems if you're only used to the standard chartkick stuff.
Here is the HighCharts documentation for this:
http://api.highcharts.com/highcharts#plotOptions.pie.events.click
Here is an awesome blog post showing how to setup the JS/Jquery (you can convert this to Coffescript really easy using http://js2.coffee)
http://birdchan.com/home/2012/09/07/highcharts-pie-charts-can-have-url-links/
His approach there works great, and, you can make it easier by creating an endpoint on your resource's controller that serves up the data hash you'll need for the chart, replacing:
data: [
{name: 'Not Tested', color: '#FFA850', y: 87, url: 'http://my_site1.com'},
{name: 'Fail', color: '#FF2929', y: 2, url: 'http://my_site2.com'},
{name: 'Pass', color: '#31FF4F', y: 32, url: 'http://my_site3.com'}
]
With something like:
$.ajax {
url: "/place/resources/resource_id/top_members.json"
success: (data) ->
draw_some_pie_chart(data)
}
Or you can also use the GON gem to make that hash available as a variable in your Coffeescript/JS file.
I'm sure there is a more specific/thorough way to answer your question. I can update my answer if you'd like to post samples of the code you're trying to use

How to pass data to google charts from sqlite3 database on ROR web app

I am a beginner ROR web developer, I have google charts running on my web app . But charts are drawn based on the data hard coded.
my code:
Gchart.line(:data => [300, 100, 30, 200, 100, 200, 300, 10], :axis_with_labels =>'x',
:axis_labels => ['Jan|July|Jan|July|Jan'])
I need the chart to be drawn based on the data drawn from my database (Sqlite3).
Can I do it with googlecharts ? If yes, how do I write code to draw data from db.
Surely, you can do it. Google Chart API is a JavaScript library, so it just some data to pass in. If you are using Rails 3.2, the common way to do is to render the data within the html dom with data attribute, then use jQuery to access the data.
You can read more about this from railscast episode: http://railscasts.com/episodes/324-passing-data-to-javascript

Resources