SHARE
TWEET

Untitled

a guest Feb 16th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function combine(data) {
  2.             // await fetch('/dashboard/stats')
  3.             //     .then((response) => response.json())
  4.             //     .then((myJson) => {
  5.             //         data = myJson;
  6.             //         console.log({test: myJson});
  7.             //     })
  8.             var data = [{"label":"Amaranthaceae","data":150},{"label":"Cupressaceae","data":75},{"label":"Boraginaceae","data":62},{"label":"Compositae","data":10},{"label":"Lamiaceae","data":8},{"label":"Apiaceae","data":6},{"label":"Pinaceae","data":5},{"label":"Leguminosae","data":5},{"label":"Gentianaceae","data":4},{"label":"Papaveraceae","data":3},{"label":"Ranunculaceae","data":3},{"label":"Solanaceae","data":3},{"label":"Capparaceae","data":2},{"label":"Fagaceae","data":2},{"label":"Plantaginaceae","data":2},{"label":"Cucurbitaceae","data":2},{"label":"Oleaceae","data":2},{"label":"Poaceae","data":2},{"label":"Lauraceae","data":2},{"label":"Paeoniaceae","data":2},{"label":"Ericaceae","data":2},{"label":"Cannabaceae","data":2},{"label":"Acanthaceae","data":1},{"label":"Acoraceae","data":1},{"label":"Pteridaceae","data":1},{"label":"Amaryllidaceae","data":1},{"label":"Primulaceae","data":1},{"label":"Sapindaceae","data":1},{"label":"Urticaceae","data":1},{"label":"Brassicaceae","data":1}];
  9.             var total = 0,
  10.                 combined = 0,
  11.                 numCombined = 0,
  12.                 color = options.series.pie.combine.color,
  13.                 newdata = [];
  14.  
  15.             // Fix up the raw data from Flot, ensuring the data is numeric
  16.  
  17.             for (var i = 0; i < data.length; ++i) {
  18.  
  19.                 var value = data[i].data;
  20.  
  21.                 // If the data is an array, we'll assume that it's a standard
  22.                 // Flot x-y pair, and are concerned only with the second value.
  23.  
  24.                 // Note how we use the original array, rather than creating a
  25.                 // new one; this is more efficient and preserves any extra data
  26.                 // that the user may have stored in higher indexes.
  27.  
  28.                 if ($.isArray(value) && value.length == 1) {
  29.                     value = value[0];
  30.                 }
  31.  
  32.                 if ($.isArray(value)) {
  33.                     // Equivalent to $.isNumeric() but compatible with jQuery < 1.7
  34.                     if (!isNaN(parseFloat(value[1])) && isFinite(value[1])) {
  35.                         value[1] = +value[1];
  36.                     } else {
  37.                         value[1] = 0;
  38.                     }
  39.                 } else if (!isNaN(parseFloat(value)) && isFinite(value)) {
  40.                     value = [1, +value];
  41.                 } else {
  42.                     value = [1, 0];
  43.                 }
  44.  
  45.                 data[i].data = [value];
  46.             }
  47.  
  48.             // Sum up all the slices, so we can calculate percentages for each
  49.  
  50.             for (var i = 0; i < data.length; ++i) {
  51.                 total += data[i].data[0][1];
  52.             }
  53.  
  54.             // Count the number of slices with percentages below the combine
  55.             // threshold; if it turns out to be just one, we won't combine.
  56.  
  57.             for (var i = 0; i < data.length; ++i) {
  58.                 var value = data[i].data[0][1];
  59.                 if (value / total <= options.series.pie.combine.threshold) {
  60.                     combined += value;
  61.                     numCombined++;
  62.                     if (!color) {
  63.                         color = data[i].color;
  64.                     }
  65.                 }
  66.             }
  67.  
  68.             for (var i = 0; i < data.length; ++i) {
  69.                 var value = data[i].data[0][1];
  70.                 if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
  71.                     newdata.push(
  72.                         $.extend(data[i], {     /* extend to allow keeping all other original data values
  73.                                                    and using them e.g. in labelFormatter. */
  74.                             data: [[1, value]],
  75.                             color: data[i].color,
  76.                             label: data[i].label,
  77.                             angle: value * Math.PI * 2 / total,
  78.                             percent: value / (total / 100)
  79.                         })
  80.                     );
  81.                 }
  82.             }
  83.  
  84.             if (numCombined > 1) {
  85.                 newdata.push({
  86.                     data: [[1, combined]],
  87.                     color: color,
  88.                     label: options.series.pie.combine.label,
  89.                     angle: combined * Math.PI * 2 / total,
  90.                     percent: combined / (total / 100)
  91.                 });
  92.             }
  93.  
  94.             return newdata;
  95.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top