Skip to content

Instantly share code, notes, and snippets.

@aldrienht
Created December 7, 2016 08:01
Show Gist options
  • Save aldrienht/f31967e6eba4088324088df55e03a271 to your computer and use it in GitHub Desktop.
Save aldrienht/f31967e6eba4088324088df55e03a271 to your computer and use it in GitHub Desktop.
Simple Dynamin Morris Bar Chart - Rails
// JS Code
// Assumed that you already declared libraries needed.
$.ajax({
type: "GET",
dataType: 'json',
url: "/en/project_chart", // This is the URL to the API
})
.done(function( data ) {
if ((typeof data === "undefined" || data === null) || data.length === 0) {
return;
}else{
var chart = Morris.Bar({
element: 'morris-bar-chart',
data: [0,0], // initial value
xkey: 'title', //horizontal title (key)
ykeys: ['total', 'registered'], //bar titles (keys)
labels: ['Needed Applicant', 'Registered Applicant'], // on hover tooltip contents
hideHover: 'auto',
resize: true
});
}
});
// Routes
get 'project_chart' => 'projects#project_chart'
// Project Controller
def project_chart
@projects = Project.not_failed.order('created_at DESC').limit(7)
result = []
@projects.each do |project|
result << {
title: project.title, //replace with yours
registered: project.count_associated_applicants, //replace with yours
total: project.no_of_needed_applicants //replace with yours
}
end
render json: result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment