Rebase repair-charts
onto master
This commit is contained in:
@ -1,18 +1,16 @@
|
|||||||
$(document).on('turbolinks:load', function() {
|
$(document).on('turbolinks:load', function() {
|
||||||
// http://localhost:3333/exercises/38/statistics good for testing
|
// /38/statistics good for testing
|
||||||
// originally at--> localhost:3333/exercises/69/statistics
|
|
||||||
|
|
||||||
if ($.isController('exercises') && $('.graph-functions').isPresent()) {
|
if ($.isController('exercises') && $('.working-time-graphs').isPresent()) {
|
||||||
var working_times = $('#data').data('working-time');
|
var working_times = $('#data').data('working-time');
|
||||||
|
|
||||||
function get_minutes (time_stamp){
|
function get_minutes (timestamp){
|
||||||
try{
|
try{
|
||||||
hours = time_stamp.split(":")[0];
|
hours = timestamp.split(":")[0];
|
||||||
minutes = time_stamp.split(":")[1];
|
minutes = timestamp.split(":")[1];
|
||||||
seconds = time_stamp.split(":")[2];
|
seconds = timestamp.split(":")[2];
|
||||||
|
|
||||||
minutes = parseFloat(hours * 60) + parseInt(minutes);
|
return parseFloat(hours * 60) + parseInt(minutes);
|
||||||
return minutes
|
|
||||||
} catch (err){
|
} catch (err){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -39,7 +37,7 @@ $(document).on('turbolinks:load', function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getWidth() {
|
function getWidth() {
|
||||||
if (self.innerHeight) {
|
if (self.innerWidth) {
|
||||||
return self.innerWidth;
|
return self.innerWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,15 +51,12 @@ $(document).on('turbolinks:load', function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DRAW THE LINE GRAPH ------------------------------------------------------------------------------
|
// DRAW THE LINE GRAPH ------------------------------------------------------------------------------
|
||||||
function draw_line_graph() {
|
function drawLineGraph() {
|
||||||
var width_ratio = .8;
|
var width_ratio = .8;
|
||||||
if (getWidth() * width_ratio > 1000){
|
if (getWidth() * width_ratio > 1000){
|
||||||
width_ratio = 1000 / getWidth();
|
width_ratio = 1000 / getWidth();
|
||||||
}
|
}
|
||||||
var height_ratio = .7; // percent of height
|
var height_ratio = .7;
|
||||||
|
|
||||||
// currently sets as percentage of window width, however, unfortunately
|
|
||||||
// is not yet responsive
|
|
||||||
|
|
||||||
var margin = {top: 100, right: 20, bottom: 70, left: 70},//30,50
|
var margin = {top: 100, right: 20, bottom: 70, left: 70},//30,50
|
||||||
width = (getWidth() * width_ratio) - margin.left - margin.right,
|
width = (getWidth() * width_ratio) - margin.left - margin.right,
|
||||||
@ -139,91 +134,61 @@ $(document).on('turbolinks:load', function() {
|
|||||||
svg.append("path")
|
svg.append("path")
|
||||||
.datum(minutes_count)
|
.datum(minutes_count)
|
||||||
.attr("class", "line")
|
.attr("class", "line")
|
||||||
.attr('id', 'myPath')// new
|
.attr('id', 'myPath')
|
||||||
.attr("stroke", "orange")
|
.attr("stroke", "orange")
|
||||||
.attr("stroke-width", 5)
|
.attr("stroke-width", 5)
|
||||||
.attr("fill", "none")// end new
|
.attr("fill", "none")
|
||||||
.attr("d", line);//---
|
.attr("d", line);
|
||||||
//.on("mousemove", mMove)//new again
|
|
||||||
//.append("title");
|
|
||||||
|
|
||||||
// function type(d) {
|
|
||||||
// d.frequency = +d.frequency;
|
|
||||||
// return d;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_line_graph();
|
drawLineGraph();
|
||||||
|
|
||||||
// THIS SHOULD DISPLAY THE X AND Y VALUES BUT
|
|
||||||
// THE RESULTS ARE WRONG AT THE END FOR SOME REASON
|
|
||||||
|
|
||||||
//function mMove() {
|
|
||||||
// var x_width = getWidth() * width_ratio;
|
|
||||||
// //var x_value = m[0]*(minutes_count.length/x_width);
|
|
||||||
//
|
|
||||||
// var y_height = x_width * height_ratio;
|
|
||||||
// //var y_value = (((y_height - m[1])/y_height)*100);
|
|
||||||
//
|
|
||||||
// //console.log('y is: ' + y_value);
|
|
||||||
// var m = d3.mouse(this);
|
|
||||||
// d3.select("#myPath").select("title")
|
|
||||||
// .text((y_height-m[1])/(y_height) * 100 + "% of Students" +"\n"+
|
|
||||||
// (m[0]*(minutes_count.length/x_width)) +" Minutes");//text(m[1]);
|
|
||||||
//}
|
|
||||||
|
|
||||||
// DRAW THE SECOND GRAPH ------------------------------------------------------------------------------
|
// DRAW THE SECOND GRAPH ------------------------------------------------------------------------------
|
||||||
function draw_bar_graph() {
|
function drawBarGraph() {
|
||||||
var number_of_bars = 40;
|
var groupWidth = 5;
|
||||||
var group_increment = Math.ceil(maximum_minutes / number_of_bars); // range in minutes
|
var groupRanges = 0;
|
||||||
var group_ranges = group_increment; // just for the start
|
var workingTimeGroups = [];
|
||||||
var minutes_array_for_bar = [];
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
var section_value = 0;
|
var clusterCount = 0;
|
||||||
for (var i = 0; i < minutes_array.length; i++) {
|
for (var i = 0; i < minutes_array.length; i++) {
|
||||||
if ((minutes_array[i] < group_ranges) && (minutes_array[i] >= (group_ranges - group_increment))) {
|
if ((minutes_array[i] > groupRanges) && (minutes_array[i] < (groupRanges + groupWidth))) {
|
||||||
section_value++;
|
clusterCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
minutes_array_for_bar.push(section_value);
|
workingTimeGroups.push(clusterCount);
|
||||||
group_ranges += group_increment;
|
groupRanges += groupWidth;
|
||||||
}
|
}
|
||||||
while (group_ranges < maximum_minutes + group_increment);
|
while (groupRanges < maximum_minutes);
|
||||||
|
|
||||||
//console.log(minutes_array_for_bar); // this var used as the bars
|
|
||||||
//minutes_array_for_bar = [39, 20, 28, 20, 39, 34, 26, 23, 16, 8];
|
|
||||||
|
|
||||||
var max_of_array = Math.max.apply(Math, minutes_array_for_bar);
|
|
||||||
var min_of_array = Math.min.apply(Math, minutes_array_for_bar);
|
|
||||||
|
|
||||||
|
var maxStudentsInGroup = Math.max.apply(Math, workingTimeGroups);
|
||||||
|
|
||||||
var width_ratio = .8;
|
var width_ratio = .8;
|
||||||
if (getWidth()*width_ratio > 1000){
|
var height_ratio = .7;
|
||||||
width_ratio = 1000/getWidth();
|
|
||||||
}
|
|
||||||
var height_ratio = .7; // percent of height
|
|
||||||
|
|
||||||
var margin = {top: 100, right: 20, bottom: 70, left: 70},//30,50
|
var margin = {top: 100, right: 20, bottom: 70, left: 70},
|
||||||
width = (getWidth() * width_ratio) - margin.left - margin.right,
|
width = (getWidth() * width_ratio) - margin.left - margin.right,
|
||||||
height = (width * height_ratio) - margin.top - margin.bottom;
|
height = (width * height_ratio) - margin.top - margin.bottom;
|
||||||
|
|
||||||
var x = d3.scaleBand()
|
var x = d3.scaleBand()
|
||||||
.range([0, width], .1);
|
.rangeRound([0, width])
|
||||||
|
.paddingInner(0.1)
|
||||||
var y = d3.scaleLinear()
|
.domain(workingTimeGroups.map(function (d, i) {
|
||||||
.range([0,height-(margin.top + margin.bottom)]);
|
return i * groupWidth;
|
||||||
|
}));
|
||||||
|
|
||||||
var xAxis = d3.axisBottom(x)
|
var xAxis = d3.axisBottom(x)
|
||||||
|
.tickValues(x.domain().filter(function(d, i){
|
||||||
|
return (d % 50) === 0
|
||||||
|
}));
|
||||||
|
|
||||||
|
var y = d3.scaleLinear()
|
||||||
|
.domain([0, maxStudentsInGroup])
|
||||||
|
.range([height, 0]);
|
||||||
|
|
||||||
|
var yAxis = d3.axisLeft(y)
|
||||||
.ticks(10);
|
.ticks(10);
|
||||||
|
|
||||||
|
|
||||||
var yAxis = d3
|
|
||||||
.axisLeft(d3.scaleLinear().domain([0,max_of_array]).range([height,0]))//y
|
|
||||||
.ticks(10)
|
|
||||||
.tickSizeInner(-width);
|
|
||||||
|
|
||||||
var tip = d3.tip()
|
var tip = d3.tip()
|
||||||
.attr('class', 'd3-tip')
|
.attr('class', 'd3-tip')
|
||||||
.offset([-10, 0])
|
.offset([-10, 0])
|
||||||
@ -239,40 +204,16 @@ $(document).on('turbolinks:load', function() {
|
|||||||
|
|
||||||
svg.call(tip);
|
svg.call(tip);
|
||||||
|
|
||||||
x.domain(minutes_array_for_bar.map(function (d, i) {
|
|
||||||
i++;
|
|
||||||
var high_side = i * group_increment;
|
|
||||||
var low_side = high_side - group_increment;
|
|
||||||
return (low_side+"-"+high_side);
|
|
||||||
}));
|
|
||||||
|
|
||||||
y.domain(minutes_array_for_bar.map(function (d) {
|
|
||||||
return (d);
|
|
||||||
}));
|
|
||||||
|
|
||||||
svg.append("g")
|
svg.append("g")
|
||||||
.attr("class", "x axis")
|
.attr("class", "x axis")
|
||||||
.attr("transform", "translate(0," + height + ")")
|
.attr("transform", "translate(0," + height + ")")
|
||||||
.call(xAxis)
|
.call(xAxis);
|
||||||
.selectAll("text")
|
|
||||||
.style("text-anchor", "end")
|
|
||||||
.attr("dx", "-.8em")
|
|
||||||
.attr("dy", ".15em")
|
|
||||||
.attr("transform", function(d) {
|
|
||||||
return "rotate(-45)"
|
|
||||||
});
|
|
||||||
|
|
||||||
svg.append("g")
|
svg.append("g")
|
||||||
.attr("class", "y axis")
|
.attr("class", "y axis")
|
||||||
.call(yAxis)
|
.call(yAxis);
|
||||||
.append("text")
|
|
||||||
.attr("transform", "rotate(-90)")
|
|
||||||
.attr("y", 6)
|
|
||||||
.attr("dy", ".71em");
|
|
||||||
//.style("text-anchor", "end")
|
|
||||||
//.text("Students");
|
|
||||||
|
|
||||||
svg.append("text") // y axis label
|
svg.append("text")
|
||||||
.attr("transform", "rotate(-90)")
|
.attr("transform", "rotate(-90)")
|
||||||
.attr("x", -height / 2)
|
.attr("x", -height / 2)
|
||||||
.attr("dy", "-3em")
|
.attr("dy", "-3em")
|
||||||
@ -280,45 +221,40 @@ $(document).on('turbolinks:load', function() {
|
|||||||
.text("Students")
|
.text("Students")
|
||||||
.style('font-size', 14);
|
.style('font-size', 14);
|
||||||
|
|
||||||
svg.append("text")// x axis label
|
svg.append("text")
|
||||||
.attr("class", "x axis")
|
.attr("class", "x axis")
|
||||||
.attr("text-anchor", "middle")
|
.attr("text-anchor", "middle")
|
||||||
.attr("x", width / 2)
|
.attr("x", width / 2)
|
||||||
.attr("y", height)
|
.attr("y", height)
|
||||||
.attr("dy", ((height / 20) + 40) + 'px')
|
.attr("dy", ((height / 20) + 20) + 'px')
|
||||||
.text("Working Time (Minutes)")
|
.text("Working Time (Minutes)")
|
||||||
.style('font-size', 14);
|
.style('font-size', 14);
|
||||||
|
|
||||||
y = d3.scaleLinear()
|
|
||||||
.domain([(0),max_of_array])
|
|
||||||
.range([0,height]);
|
|
||||||
|
|
||||||
|
|
||||||
svg.selectAll(".bar")
|
svg.selectAll(".bar")
|
||||||
.data(minutes_array_for_bar)
|
.data(workingTimeGroups)
|
||||||
.enter().append("rect")
|
.enter().append("rect")
|
||||||
.attr("class", "bar")
|
.attr("class", "bar")
|
||||||
.attr("x", function(d,i) { var bar_increment = width / minutes_array_for_bar.length;
|
.attr("x", function(d, i) {
|
||||||
var bar_x = i * bar_increment;
|
return x(i * groupWidth);
|
||||||
return (bar_x)})
|
})
|
||||||
.attr("width", x.bandwidth())
|
.attr("width", x.bandwidth())
|
||||||
.attr("y", function(d) { return height - y(d); })
|
.attr("y", function(d) { return y(d); })
|
||||||
.attr("height", function(d) { return y(d); })
|
.attr("height", function(d) { return height - y(d); })
|
||||||
.on('mouseover', tip.show)
|
.on('mouseenter', tip.show)
|
||||||
.on('mouseout', tip.hide);
|
.on('mouseout', tip.hide);
|
||||||
|
|
||||||
svg.append("text")// Title
|
svg.append("text")
|
||||||
.attr("class", "x axis")
|
.attr("class", "x axis")
|
||||||
.attr("text-anchor", "middle")
|
.attr("text-anchor", "middle")
|
||||||
.attr("x", (width / 2))//+300)
|
.attr("x", (width / 2))
|
||||||
.attr("y", 0)
|
.attr("y", 0)
|
||||||
.attr("dy", '-1.5em')
|
.attr("dy", '-1.5em')
|
||||||
.text("Distribution of Time Spent by Students")
|
.text("Distribution of Time Spent by Students")
|
||||||
.style('font-size', 20)
|
.style('font-size', 20)
|
||||||
.style('text-decoration', 'underline');
|
.style('text-decoration', 'underline');
|
||||||
|
|
||||||
}
|
}
|
||||||
draw_bar_graph();
|
|
||||||
|
drawBarGraph();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -33,7 +33,7 @@ h1 = @exercise
|
|||||||
-working_time_array.push working_time
|
-working_time_array.push working_time
|
||||||
hr
|
hr
|
||||||
.d-none#data data-working-time=ActiveSupport::JSON.encode(working_time_array)
|
.d-none#data data-working-time=ActiveSupport::JSON.encode(working_time_array)
|
||||||
.graph-functions
|
.working-time-graphs
|
||||||
div#chart_1
|
div#chart_1
|
||||||
hr
|
hr
|
||||||
div#chart_2
|
div#chart_2
|
||||||
|
Reference in New Issue
Block a user