From 0bd7c79f82e88cd07ae95cc47b1f4b7b710ac398 Mon Sep 17 00:00:00 2001 From: Akshay Verma Date: Tue, 28 Nov 2017 10:56:55 +0530 Subject: [PATCH 1/2] Fix - Failure to load visualization for RBI Datasets --- .../theme/public/timeseries_rbi-main.js | 73 ++++++++++++------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js b/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js index 15379f3..1e31238 100644 --- a/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js +++ b/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js @@ -12,10 +12,10 @@ ckan.module('groupbarchart-view', function($, _) { key.shift(); var state = new Object(); state.key = data[i].States - state.disabled = data[i].States == "All States" ? false : true + state.disabled = data[i].States == "All-States" ? false : true var values_arr = [] for (var j = 0; j < key.length; j++) { - if (data[i][key[j]] == "...") { + if (data[i][key[j]] == "..." || data[i][key[j]] == "-") { continue; } else { var temp = new Object(); @@ -35,33 +35,53 @@ ckan.module('groupbarchart-view', function($, _) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } - + var addMiscElements = function() { d3.select("#viz-header").text(toTitleCase(resource.name)); } + function remove_notes(data) { + var regex = /[a-zA-Z]/i; + var data_length = data.length + for (var i = 0, j = 0; j < data_length; j++) { + if (!(regex.test(data[i]["States"][0]))) { + data.splice(i, 1) + } else { + i++; + } + } + for (var i = 0; i < data.length; i++) { + } + return data + } + + function prepareData(data) { + return mungeData(remove_notes(data)) + } + function drawChart(data) { nv.addGraph(function() { var chart = nv.models.lineWithFocusChart(); - var xScale = d3.time.scale(); var mini, max; var minmax; chart.xScale; chart.x(function(d) { - return d.x}) - .y(function(d) { - return parseFloat(d.y) }) - .margin({ "left": 90, "right": 40, "top": 0, "bottom": 50 }) - .focusHeight(120) - .focusMargin({ "top": 30 }) - .pointSize(10) - .showLegend(true) - .legendPosition("top") - .focusMargin({ "top": 20 }) - .clipEdge(false); - + return d.x + }) + .y(function(d) { + return parseFloat(d.y) + }) + .margin({ "left": 90, "right": 40, "top": 0, "bottom": 50 }) + .focusHeight(120) + .focusMargin({ "top": 30 }) + .pointSize(10) + .showLegend(true) + .legendPosition("top") + .focusMargin({ "top": 20 }) + .clipEdge(false); + chart.xAxis .tickFormat(function(d) { var c = parseInt(d) + 1; @@ -75,18 +95,18 @@ ckan.module('groupbarchart-view', function($, _) { }); chart.legend.margin({ top: 10, right: 0, left: -20, bottom: 40 }) - .align("center"); + .align("center"); chart.yAxis.axisLabel(resource.name + "(Rs. Crore)") - .axisLabelDistance(20); + .axisLabelDistance(20); chart.tooltip.valueFormatter(function(d) { - return d3.format(",.f")(d) ; - }) - .headerFormatter(function(d) { - var c = parseInt(d) + 1; - return String(d) + " - " + String(c) - }) + return d3.format(",.f")(d); + }) + .headerFormatter(function(d) { + var c = parseInt(d) + 1; + return String(d) + " - " + String(c) + }) var chartdata = d3.select('#chart svg') .datum(data) @@ -98,11 +118,10 @@ ckan.module('groupbarchart-view', function($, _) { return chart; }); } - d3.csv(resource_url, function(error, data) { addMiscElements(); - drawChart(mungeData(data)); + drawChart(prepareData(data)); }); }() }; -}); +}); \ No newline at end of file From 12dc79b87083468a09c2ca534a2575b26d80f4b5 Mon Sep 17 00:00:00 2001 From: Akshay Verma Date: Tue, 28 Nov 2017 11:27:58 +0530 Subject: [PATCH 2/2] Add Metadata Notes generator --- .../theme/public/timeseries_rbi-main.js | 43 +++++++++++++++++-- .../theme/templates/timeseries_rbi_view.html | 1 + 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js b/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js index 1e31238..bccba1c 100644 --- a/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js +++ b/ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js @@ -40,7 +40,7 @@ ckan.module('groupbarchart-view', function($, _) { d3.select("#viz-header").text(toTitleCase(resource.name)); } - function remove_notes(data) { + function remove_record_notes(data) { var regex = /[a-zA-Z]/i; var data_length = data.length for (var i = 0, j = 0; j < data_length; j++) { @@ -55,8 +55,43 @@ ckan.module('groupbarchart-view', function($, _) { return data } + function add_notes() { + try { + var extra_fields = package_details.extras + var unit, note; + for (var i in extra_fields) { + if (extra_fields[i].key.substr(0, 4).toLowerCase() == "unit" ) { + unit = extra_fields[i].value; + } + if (extra_fields[i].key.substr(0, 4).toLowerCase() == "note") { + note = extra_fields[i].value; + } + } + if (unit) { + d3.select(".notes-content") + .text(function(d) { + return unit; + }) + d3.select(".notes-heading") + .text(function(d) { + return "Unit :"; + }) + } + if (note) { + d3.select(".unit-note-content") + .text(function(d) { + return note; + }) + d3.select(".unit-note-heading") + .text(function(d) { + return "Note :"; + }) + } + } catch (err) {} + } + function prepareData(data) { - return mungeData(remove_notes(data)) + return mungeData(remove_record_notes(data)) } function drawChart(data) { @@ -71,7 +106,8 @@ ckan.module('groupbarchart-view', function($, _) { return d.x }) .y(function(d) { - return parseFloat(d.y) + var value = parseFloat(d.y) + return parseFloat(value.toFixed(2)) }) .margin({ "left": 90, "right": 40, "top": 0, "bottom": 50 }) .focusHeight(120) @@ -121,6 +157,7 @@ ckan.module('groupbarchart-view', function($, _) { d3.csv(resource_url, function(error, data) { addMiscElements(); drawChart(prepareData(data)); + add_notes() }); }() }; diff --git a/ckanext/budgetviz/timeseries_rbi/theme/templates/timeseries_rbi_view.html b/ckanext/budgetviz/timeseries_rbi/theme/templates/timeseries_rbi_view.html index bb19d44..61fbe6a 100644 --- a/ckanext/budgetviz/timeseries_rbi/theme/templates/timeseries_rbi_view.html +++ b/ckanext/budgetviz/timeseries_rbi/theme/templates/timeseries_rbi_view.html @@ -33,6 +33,7 @@