Skip to content

Commit

Permalink
First iteration showing charts.
Browse files Browse the repository at this point in the history
 - Move js functions to charts.js
 - Modify jQuery calls to use getJSON and remove host and port name from URLs
 - add restful services for showing charts; add charts.clj to send required JSON (for charts)
  • Loading branch information
mmwaikar committed Jan 24, 2012
1 parent 7fdc8f8 commit 66238e5
Show file tree
Hide file tree
Showing 10 changed files with 1,118 additions and 39 deletions.
35 changes: 10 additions & 25 deletions public/charts.html
Expand Up @@ -8,38 +8,23 @@
<body>
<script src="js/knockout-2.0.0.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(document).ready(function() {
$.ajax({
url: 'http://localhost:3000/qa/def',
timeout: 5000,
type: 'GET',
dataType: 'json',
success: function(data) {
$.each(data, function(i, row) {
if (i !== 0)
$('#columns').append('<option>'+row.description+' - '+i+row.name+'</option>');
});
},
error: function (xhr) {
$('#returnJson').html('Error: ' + xhr.status + ' ' + xhr.statusText);
alert('error');
}
});
});
});
</script>
<script src="js/FusionCharts.js" type="text/javascript"></script>
<script src="js/charts.js" type="text/javascript"></script>
<div id="wrap">
<div id="header"><h1>Fusion charts for Adult Care QA</h1></div>
<div id="nav">
<h4>Choose the columns for graphs</h4>
<select id="columns" multiple="yes" size="20">
<h4>Choose the columns for the graph</h4>
<select id="columnsSelect" multiple="yes" size="20">
</select>
<h4>Choose the year for the graph</h4>
<select id="yearSelect">
</select>
<p>
<button id="drawChartButton" type="button">Display Chart</button>
</div>
<div id="main">
<h4>Charts</h4>
<div id="chart">
<div id="chartContainer">

</div>
</div>
Expand Down
Binary file added public/js/Column3D.swf
Binary file not shown.
69 changes: 69 additions & 0 deletions public/js/FusionCharts.HC.Charts.js

Large diffs are not rendered by default.

544 changes: 544 additions & 0 deletions public/js/FusionCharts.HC.js

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions public/js/FusionCharts.js

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions public/js/charts.js
@@ -0,0 +1,94 @@
function createOptionTag (name, description) {
var nameDesc = description + ' - ' + name;
return '<option value="' + name + '">' + nameDesc + '</option>';
};

function showError (xhr) {
$('#returnJson').html('Error: ' + xhr.status + ' ' + xhr.statusText);
alert('error');
};

function populateColumns (data) {
$.each(data, function(i, row) {
if (i !== 0)
$('#columnsSelect').append(createOptionTag(row.name, row.description));
});
};

function populateYear () {
for (var year=2002; year<2012; year++) {
$('#yearSelect').append('<option>'+year+'</option>');
}
};

function renderChart (data) {
// FusionCharts.setCurrentRenderer('javascript');
var myChart = new FusionCharts("js/Column3D.swf",
"myChartId", "400", "300", "0", "1" );
myChart.setJSONData(data);
myChart.render("chartContainer");
};

$(function() {
// sufficient to wait for dom ready
// no need to use $(document).ready(function() {}
$.getJSON('/qa/def', populateColumns).error(showError);
populateYear();
});

$(function() {
$('#drawChartButton').click(
function() {
var baseUrl = '/qa/charts/period/';
var year = $('#yearSelect').val();
var cols = $('#columnsSelect').val() || [];
var url = baseUrl + year + '/column/' + cols.join('');

$.getJSON(url, renderChart).error(showError);
});
});

// $(function() {
// $(document).ready(function() {
// $.ajax({
// url: '/qa/def',
// timeout: 5000,
// type: 'GET',
// dataType: 'json',
// success: function(data) {
// populateColumns(data);
// },
// error: function (xhr) {
// showError(xhr);
// $('#returnJson').html('Error: ' + xhr.status + ' ' + xhr.statusText);
// alert('error');
// }
// });
// populateYear();
// });
// });

// $(function() {
// $('#drawChartButton').click(
// function() {
// var baseUrl = '/qa/charts/period/';
// var year = $('#yearSelect').val();
// var cols = $('#columnsSelect').val() || [];
// var url = baseUrl + year + '/column/' + cols.join('');
// var s = "";

// $.ajax({
// url: url,
// timeout: 5000,
// type: 'GET',
// dataType: 'json',
// success: function(data) {
// renderChart(data);
// },
// error: function (xhr) {
// $('#returnJson').html('Error: ' + xhr.status + ' ' + xhr.statusText);
// alert('error');
// }
// });
// });
// });
154 changes: 154 additions & 0 deletions public/js/jquery.min.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/adultcareqa/charts.clj
@@ -0,0 +1,22 @@
(ns adultcareqa.charts
(:refer-clojure :exclude [replace])
(:use [clojure.string :as str]))

(defn- get-label-value [row]
(let [month-year (:Date row)
month (join (take (if (= (count month-year) 6) 2 1) month-year))
val (:RP_N1 row)]
(hash-map :label (str "Month " month) :value val)))

(defn get-charts-map
"Returns a map, which can be easily converted to the
JSON representation as expected by Fusion charts."
[caption x-axis-name y-axis-name data-array]
{
:chart {
:caption caption
:xAxisName x-axis-name
:yAxisName y-axis-name
}
:data (map #(get-label-value %) data-array)
})
60 changes: 50 additions & 10 deletions src/adultcareqa/mongo.clj
Expand Up @@ -2,6 +2,7 @@
(:refer-clojure :exclude [extend replace reverse])
(:use [somnium.congomongo]
[clojure.string :as str]
[clojure.set :only [intersection]]
[adultcareqa.utils :only [get-date]]))

(def ^:dynamic *db* "adultcare")
Expand Down Expand Up @@ -43,22 +44,61 @@
(def memoized-get-data-definitions
"Memoizes the get-data-definitions function."
(memoize get-data-definitions))


(defn columns-in-data
"Returns all the columns present in the data in the QA table."
([] (columns-in-data {:for-json false}))
([{for-json :for-json :or {for-json false}}]
(map #(name %) (keys (first (memoized-get-data {:for-json for-json}))))))

(defn columns-in-data-definitions
"Returns all the columns present in the data definitions table."
([] (columns-in-data-definitions {:for-json false}))
([{for-json :for-json :or {for-json false}}]
(map #(:name %) (memoized-get-data-definitions {:for-json for-json}))))

(defn common-columns-in-data-and-definitions
"Returns columns from data definitions table for which
there is some data in QA table."
([] (common-columns-in-data-and-definitions {:for-json false}))
([{for-json :for-json :or {for-json false}}]
(intersection (into #{} (columns-in-data {:for-json for-json}))
(into #{} (columns-in-data-definitions {:for-json for-json})))))

(defn get-data-definitions-for-columns-in-data
"Returns the data definitions for only those columns
which are actually present in the data."
([] (get-data-definitions-for-columns-in-data {:for-json false}))
([{for-json :for-json :or {for-json false}}]
(let [common-columns (common-columns-in-data-and-definitions {:for-json for-json})]
(filter #(contains? common-columns (:name %)) (memoized-get-data-definitions {:for-json for-json})))))

(defn mass-inserts
"Does mass inserts of values in a collection-name collection.
columns represents the various columns in collection-name."
columns represents the various columns in collection-name."
[collection-name columns values]
(with-mongo conn
(mass-insert! collection-name
(for [row values]
(zipmap columns row)))))

;; (defn filter-by [{period :period column :column
;; :or {period "" column ""}}]
;; (if-not (str/blank? column)
;; (list :only [column]))
;; (if-not (str/blank? period)
;; (list :where {:Date
;;
(defn get-filtered-data
"Returns data filtered by column for a particular period."
[{period :period column :column
:or {period "" column ""}}]
(let [data (memoized-get-data)
for-period (filter #(> (.indexOf (:Date %) period) -1) data)]
(map #(select-keys % (vector (keyword column) :Date)) for-period)))

;; (defmacro filter-by [collection {period :period column :column
;; :or {period "" column ""}}]
;; (let [col-list (if-not (str/blank? column)
;; (list :only [column]))
;; where-list (if-not (str/blank? period)
;; (list :where {:Date (re-pattern period)}))
;; y (concat '(somnium.congomongo/with-mongo conn) (list (concat '(somnium.congomongo/fetch) (list collection) col-list where-list)))]
;; (prn y)
;; (eval y)))

;; example of fetching some columns
;; (with-mongo conn (fetch *data-definition-collection* :only [:name :type]))
;; (with-mongo conn (fetch *data-definition-collection* :only [:name :type]))
19 changes: 15 additions & 4 deletions src/adultcareqa/services.clj
@@ -1,8 +1,9 @@
(ns adultcareqa.services
(:use [compojure.core]
[adultcareqa.mongo :as mongo]
[clojure.data.json :as json]
[ring.middleware.json-params])
[ring.middleware.json-params]
[adultcareqa.mongo :as mongo]
[adultcareqa.charts :as charts])
(:require [compojure.route :as route]))

(defn json-response [data & [status]]
Expand All @@ -19,15 +20,25 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defroutes app
;; restful URLs for data
(GET "/qa" []
(json-response (mongo/memoized-get-data {:for-json true})))

(GET "/qa/def" []
(json-response (mongo/memoized-get-data-definitions {:for-json true})))
(json-response (mongo/get-data-definitions-for-columns-in-data {:for-json true})))

;; (GET "/qa/period/:period" [period] (json-response (hash-map :returnVal (str "Hello " period))))
(GET "/qa/period/:period" [period] (json-response (str "Hello " period)))
(GET "/qa/period/:period/column/:column" [period column] (str "Hello " period ", column: " column))

(GET "/qa/period/:period/column/:column" [period column]
(json-response (mongo/get-filtered-data {:period period :column column})))

;; restful URLs for charts
(GET "/qa/charts/period/:period/column/:column" [period column]
(json-response (charts/get-charts-map "Monthly RP_N1 Summary"
"Month"
"Resident Count"
(mongo/get-filtered-data {:period period :column column}))))

(route/files "/" {:root "public"})
(route/not-found "Page not found")) ;;(mongo/filter-by {:period period})))
Expand Down

0 comments on commit 66238e5

Please sign in to comment.