Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. doc.ready(() => {
  3.  
  4.     ////////////////////////////////////////////////////////////
  5.     // Globals
  6.     ////////////////////////////////////////////////////////////
  7.  
  8.     const pies = $(".-jsPieValue");
  9.     const charts = $(".o-pieCharts__chart");
  10.  
  11.     let pie_values = [ // this is used to keep track of the current pie state
  12.  
  13.         Array(charts.length).fill(0),
  14.         Array(charts.length).fill(0)
  15.  
  16.     ];
  17.  
  18.  
  19.     ////////////////////////////////////////////////////////////
  20.     // Helpers
  21.     ////////////////////////////////////////////////////////////
  22.  
  23.     // Temp data generation:
  24.  
  25.     const generateDummyPieValues = (sets_of_data = 1) => { // 1 by default, pass in 2 for comparison
  26.  
  27.         /*
  28.             The real data needs to always be a 2D array, even if
  29.             there isn't a comparison being made.
  30.  
  31.             // to show regular full blue circles:
  32.  
  33.             pie_values = [
  34.  
  35.                 [10, 20, 30, 40, 50], // regular data
  36.  
  37.             ];
  38.  
  39.             // to show comparison with blue / orange half circles:
  40.  
  41.             pie_values = [
  42.  
  43.                 [10, 20, 30, 40, 50], // regular data
  44.                 [50, 40, 30, 20, 10], // comparison data
  45.             ];
  46.  
  47.         */
  48.  
  49.         let data = [];
  50.  
  51.         for(let i = 0; i < sets_of_data; i++)
  52.             data.push([...Array(charts.length)].map(e => rand(1, 230)));
  53.  
  54.         return data;
  55.     }
  56.  
  57.     // Numbers animation:
  58.  
  59.     const tweenPieValues = (pie, current, next, speed) => {
  60.  
  61.         if(current != next) {
  62.  
  63.             pie.text(current);
  64.  
  65.             setTimeout(() => {
  66.  
  67.                 tweenPieValues(pie, (current > next ? --current : ++current), next, speed);
  68.  
  69.             }, speed);
  70.         }
  71.     }
  72.  
  73.  
  74.     ////////////////////////////////////////////////////////////
  75.     // Update
  76.     ////////////////////////////////////////////////////////////
  77.  
  78.     // Numerical Values:
  79.  
  80.     const updatePieValues = new_pie_values => {
  81.  
  82.         new_pie_values.map((e, pie_set) => {
  83.  
  84.             charts.each(function(p) {
  85.  
  86.                 const chart = $(this);
  87.                 const pie = chart.find(".m-pie").eq(pie_set).find(".a-text");
  88.                 const current = pie_values[pie_set][p];
  89.                 const next = new_pie_values[pie_set][p];
  90.                 const difference = current > next ? (current - next) : (next - current);
  91.                 const speed = 450 / difference;
  92.  
  93.                 pie_values[pie_set][p] = new_pie_values[pie_set][p];
  94.                
  95.                 tweenPieValues(pie, current, next, speed);
  96.  
  97.             });
  98.  
  99.         });
  100.  
  101.         updatePieSizes();
  102.     }
  103.  
  104.     // Sizes:
  105.  
  106.     const updatePieSizes = () => {
  107.  
  108.         const is_comparison = pie_values.length == 2;
  109.         const max_height = is_comparison ? 250 : 300;
  110.  
  111.         pie_values.map((e, pie_set) => {
  112.  
  113.             const largest = pie_values[pie_set].indexOf(Math.max.apply(Math, pie_values[pie_set]));
  114.  
  115.             charts.each(function(p) {
  116.  
  117.                 const chart = $(this);
  118.                 const pie = chart.find(".m-pie").eq(pie_set);
  119.                 const circle = pie.find(".a-circle");
  120.                 const percentage = pie_values[pie_set][p] / pie_values[pie_set][largest];
  121.                 const size = max_height * percentage;
  122.  
  123.                 if(is_comparison) { // comparison
  124.  
  125.                     $(".o-pieCharts").addClass("-compare");
  126.  
  127.                     circle.css({
  128.  
  129.                         width : (size / 2),
  130.                         height : size,
  131.                         "border-radius" : (pie_set == 0 ? `${ size / 2 }px 0 0 ${ size / 2 }px` : `0 ${ size / 2 }px ${ size / 2 }px 0`)
  132.  
  133.                     });
  134.  
  135.                 } else {
  136.  
  137.                     $(".o-pieCharts").removeClass("-compare");
  138.  
  139.                     circle.css({ width : size, height : size });
  140.                 }
  141.  
  142.                 size >= 100 ? pie.addClass("-large") : pie.removeClass("-large");
  143.  
  144.             });
  145.            
  146.         });
  147.     }
  148.  
  149.  
  150.     ////////////////////////////////////////////////////////////
  151.     // Init and updates
  152.     ////////////////////////////////////////////////////////////
  153.  
  154.     // Update on data change:
  155.  
  156.     doc.on("click", () => { // This will likely be an input onchange event instead of a doc click
  157.  
  158.         updatePieValues(generateDummyPieValues(2)); // replace generateDummyPieValues() call with data array
  159.  
  160.     });
  161.  
  162.     // Initialisation:
  163.  
  164.     updatePieValues(generateDummyPieValues(2)); // replace generateDummyPieValues() call with data array
  165.  
  166. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement