<commit_before>/**
 * FCViz 
 * Payload script for loan auction pages
 */
 
window.fcViz = function (e) {
  targetUrl = $(e.target).attr('href');
  switch(targetUrl) {
    case "#bids-summary":
      if ($("#bids_summary_chart_control").length == 0) {
        renderBidSummaryCharts();
      }
      break;
    default:
      break;
  }
} 

$('.tabs').tabs().bind('change', window.fcViz);

//Observe mutations made to #bids-summary, so that we can reapply window.fcViz
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if(mutation.addedNodes.length > 0 && mutation.addedNodes[0].id == "bids_summary" && $("#bids_summary_chart_control").length == 0) {
      renderBidSummaryCharts();
    }
  })
});

observer.observe(document, { childList: true, subtree: true });

var bidSummaryTab = $("#bids-summary")[0];

if (bidSummaryTab.attributes["class"].value.indexOf("active") != -1) {  
  renderBidSummaryCharts();
}

function renderBidSummaryCharts() {
  var table = $("#bids_summary").find("table");
  var chartControlDiv = document.createElement("div");
  chartControlDiv.id = "bids_summary_chart_control";
  table.before(chartControlDiv);
    
  var data = makeDataFrom(table);
  var cumData = makeCumulative(data);
    
  makeBidSummaryChart(data, cumData);
}

function makeCumulative(data) {
  var cumData = [];

  for (i=0;i<data.length;++i) {
    if (i==0) {
      cumData.push({ name: data[i].name, value: data[i].name === "Rej"? 0 : data[i].value});
    } else {
      cumData.push({ name: data[i].name, value: data[i].value + cumData[i-1].value});
    }
  }
  
  var total = +document.getElementById("amount").innerHTML.replace("£","").replace(",","");
  cumData.forEach(function(d){ d.value = d.value/total * 100; });
  return cumData;  
}

function makeDataFrom(table) {
  var rows = table.find("tbody");

  //TODO: iterate through the bid-groups and note information about the user's bid.
  //Interpolate that on the chart.

  var data = [];
  
  rows.children().filter(":not(.sub-accepted, .sub-group)").each(function(){
    data.push({ 
      name: +$(this).attr("data-annualised_rate"),
      value: +$(this).attr("data-amount")
    });
  });
  
  if (data.length > 1 && data[0].name === data[1].name) {
    data[0].name = "Rej";
  }
  return data;
}

function makeBidSummaryChart(data, cumData) {
  var margin = {top: 20, right: 30, bottom: 30, left: 30},
  width = $("#bids_summary").width() * 0.95 - margin.left - margin.right,
  height = 250 - margin.top - margin.bottom;

  var x = d3.scale.ordinal()
      .rangeRoundBands([0, width], .1);

  var y = d3.scale.linear()
      .range([height, 0]);
      
  var cumY = d3.scale.linear()
      .range([height, 0]);
      
  var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");

  if (data.length > 30) {
    //Hide every other tick on the x-axis.
    //Do this by inspecting the 3rd element,
    //inferring if it is even or odd, and 
    //hiding the tick if it is the other.
    var isOdd = data[2].name * 10 % 2;
    xAxis.tickFormat(function(d){ return d === "Rej" || isOdd == d * 10 % 2? d : ""; });
  }
      
  var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickFormat(function(d){ return d3.formatPrefix(d, 3).scale(d); });

  var cumYAxis = d3.svg.axis()
      .scale(cumY)
      .orient("right");
      
  var chart = d3.select("#bids_summary_chart_control").append("svg")
      .attr("id", "bids_summary_viz")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
  x.domain(data.map(function(d) { return d.name; }));<commit_msg>Drop alt labels better, rework makeCumulative<commit_after>/**
 * FCViz 
 * Payload script for loan auction pages
 */
 
window.fcViz = function (e) {
  targetUrl = $(e.target).attr('href');
  switch(targetUrl) {
    case "#bids-summary":
      if ($("#bids_summary_chart_control").length == 0) {
        renderBidSummaryCharts();
      }
      break;
    default:
      break;
  }
} 

$('.tabs').tabs().bind('change', window.fcViz);

//Observe mutations made to #bids-summary, so that we can reapply window.fcViz
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if(mutation.addedNodes.length > 0 && mutation.addedNodes[0].id == "bids_summary" && $("#bids_summary_chart_control").length == 0) {
      renderBidSummaryCharts();
    }
  })
});

observer.observe(document, { childList: true, subtree: true });

var bidSummaryTab = $("#bids-summary")[0];

if (bidSummaryTab.attributes["class"].value.indexOf("active") != -1) {  
  renderBidSummaryCharts();
}

function renderBidSummaryCharts() {
  var table = $("#bids_summary").find("table");
  var chartControlDiv = document.createElement("div");
  chartControlDiv.id = "bids_summary_chart_control";
  table.before(chartControlDiv);
    
  var data = makeDataFrom(table);
  var cumData = makeCumulative(data);
    
  makeBidSummaryChart(data, cumData);
}

function makeCumulative(data) {
  var cumData = [];
  cumData.push({ name: data[0].name, value: data[0].name === "Rej"? 0 : data[0].value});
  for (i=1;i<data.length;++i) {
    cumData.push({ name: data[i].name, value: data[i].value + cumData[i-1].value});
  }
  
  var total = +document.getElementById("amount").innerHTML.replace("£","").replace(",","");
  cumData.forEach(function(d){ d.value = d.value/total * 100; });
  return cumData;  
}

function makeDataFrom(table) {
  var rows = table.find("tbody");

  //TODO: iterate through the bid-groups and note information about the user's bid.
  //Interpolate that on the chart.

  var data = [];
  
  rows.children().filter(":not(.sub-accepted, .sub-group)").each(function(){
    data.push({ 
      name: +$(this).attr("data-annualised_rate"),
      value: +$(this).attr("data-amount")
    });
  });
  
  if (data.length > 1 && data[0].name === data[1].name) {
    data[0].name = "Rej";
  }
  return data;
}

function makeBidSummaryChart(data, cumData) {
  var margin = {top: 20, right: 30, bottom: 30, left: 30},
  width = $("#bids_summary").width() * 0.95 - margin.left - margin.right,
  height = 250 - margin.top - margin.bottom;

  var x = d3.scale.ordinal()
      .rangeRoundBands([0, width], .1);

  var y = d3.scale.linear()
      .range([height, 0]);
      
  var cumY = d3.scale.linear()
      .range([height, 0]);
      
  var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");

  if (data.length > 30) {
    //Hide every other tick on the x-axis.
    //Do this by inspecting the 3rd element,
    //inferring if it is even or odd, and 
    //hiding the tick if it is the other.
    var labels = [];
    data.forEach(function(d){ labels.push(d.name)});
    xAxis.tickFormat(function(d){ return labels.indexOf(d) % 2? "" : d; });
  }
      
  var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickFormat(function(d){ return d3.formatPrefix(d, 3).scale(d); });

  var cumYAxis = d3.svg.axis()
      .scale(cumY)
      .orient("right");
      
  var chart = d3.select("#bids_summary_chart_control").append("svg")
      .attr("id", "bids_summary_viz")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
  x.domain(data.map(function(d) { return d.name; }));
{'fraction_non_alphanumeric': 0.18041103017689905, 'fraction_numerical': 0.013527575442247659, 'mean_word_length': 3.2883435582822087, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '63842', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2632, 'n_words': 557}