Skip to main content
added 6 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

ThatThat's it - that's all the counting done.

$.when(queryData).done(function () { var NOT_GIVEN = '(Ikke oppgitt)';

$.when(queryData).done(function () {
  var NOT_GIVEN = '(Ikke oppgitt)';

  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = Object.keys(employeeCounts).reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // you may want to sort the counts, since they'll otherwise be pretty much random
  // This one will sort the data by count in descending order
  chartData.sort(function (a, b) { return b[1] - a[1]; });

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});

That it - that's all the counting done.

$.when(queryData).done(function () { var NOT_GIVEN = '(Ikke oppgitt)';

  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = Object.keys(employeeCounts).reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // you may want to sort the counts, since they'll otherwise be pretty much random
  // This one will sort the data by count in descending order
  chartData.sort(function (a, b) { return b[1] - a[1]; });

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});

That's it - that's all the counting done.

$.when(queryData).done(function () {
  var NOT_GIVEN = '(Ikke oppgitt)';

  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = Object.keys(employeeCounts).reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // you may want to sort the counts, since they'll otherwise be pretty much random
  // This one will sort the data by count in descending order
  chartData.sort(function (a, b) { return b[1] - a[1]; });

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});
fixed code
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = schoolsObject.keys(employeeCounts).reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // you may want to sort the counts, since they'll otherwise be pretty much random
  // This one will sort the data by count in descending order
  chartData.sort(function (a, b) { return b[1] - a[1]; });

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});
  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = schools.reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});
  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = Object.keys(employeeCounts).reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // you may want to sort the counts, since they'll otherwise be pretty much random
  // This one will sort the data by count in descending order
  chartData.sort(function (a, b) { return b[1] - a[1]; });

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});
added 1181 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

Alternatively, you could skip loading the list of schools, and just group the data:

$.when(queryData).done(function () { var NOT_GIVEN = '(Ikke oppgitt)';

  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = schools.reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});

This won't include schools where there sum was zero, but that may be a good thing (zero doesn't show up on a pie chart anyway).

Alternatively, you could skip loading the list of schools, and just group the data:

$.when(queryData).done(function () { var NOT_GIVEN = '(Ikke oppgitt)';

  // group the data
  var employeeCounts = this.data.reduce(function (memo, datum) {
    var schoolName = datum.Master.lookupValue;
    if(schoolName === NOT_GIVEN) {
      schoolName = datum.Bachelor.lookupValue;
    }
    memo[schoolName] = memo[schoolName] || 0; // create the key if it doesn't exist
    memo[schoolName]++;
  }, {});

  var chartData = schools.reduce(function (memo, name) {
    if(name !== NOT_GIVEN) {
      memo.push([name, employeeCounts[name]]);
    }
    return memo;
  }, []);

  // skipping the separate function here, since the function was hard-coupled
  // to this code anyway
  $('#container_skoler').highcharts({
    series: [{
      type: 'pie',
      name: 'Skole',
      data: chartData
    }]
  });
});

This won't include schools where there sum was zero, but that may be a good thing (zero doesn't show up on a pie chart anyway).

added 34 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
deleted 28 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
added 253 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading