Skip to content

Commit

Permalink
Merge pull request #13 from cbgaindia/fix-rbi_viz
Browse files Browse the repository at this point in the history
Fix - Failure to load visualization for RBI Datasets
  • Loading branch information
akshay2905 authored Nov 28, 2017
2 parents 54e7c9e + 12dc79b commit a0b1677
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
110 changes: 83 additions & 27 deletions ckanext/budgetviz/timeseries_rbi/theme/public/timeseries_rbi-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -35,33 +35,89 @@ 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_record_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 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_record_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) {
var value = parseFloat(d.y)
return parseFloat(value.toFixed(2))
})
.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;
Expand All @@ -75,18 +131,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)
Expand All @@ -98,11 +154,11 @@ ckan.module('groupbarchart-view', function($, _) {
return chart;
});
}

d3.csv(resource_url, function(error, data) {
addMiscElements();
drawChart(mungeData(data));
drawChart(prepareData(data));
add_notes()
});
}()
};
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<script>
var resource_url = "{{ h.view_resource_url(resource_view, resource, package) }}";
var resource ={{ h.literal(h.dump_json(resource)) }};
var package_details = {{ h.literal(h.dump_json(package)) }}
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js'></script>

Expand Down

0 comments on commit a0b1677

Please sign in to comment.