Guest User

Untitled

a guest
Dec 10th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. </head>
  6. <body>
  7. <h2>World Happiness Report</h2>
  8. <textarea id="textarea_in_1" rows="30" cols="200" value=""></textarea><br>
  9. <textarea id="textarea_in_2" rows="30" cols="200" value=""></textarea><br>
  10. <button type="button" id="btnStart" onclick="AddPercentageBar()">Start</button><br>
  11. Output:<br>
  12. <textarea id="textarea_out" rows="30" cols="200" value=""></textarea><br>
  13.  
  14. <script type="text/javascript">
  15.  
  16. // Inputs:
  17. //      item : any value
  18. //      line : a token string (eg: "house*dog*cat")
  19. //      index: index where to insert the new item
  20. // Return value: array containing the new item
  21. function InsertItemAtLineOld(line, new_item, dest_index, sep)
  22. {
  23.     var line_bkp = line.split(sep);
  24.     var a = line.split(sep);
  25.     var b = [];
  26.    
  27.     for(i = 0; i < a.length; i++)
  28.     {
  29.         if( i < dest_index )
  30.         {
  31.             b.push(a[i]);
  32.         }
  33.         else if (i > dest_index)
  34.         {
  35.             b.push(a[i]);
  36.         }
  37.         else
  38.         {
  39.            
  40.             b.push(new_item);
  41.             b.push(a[i]);
  42.         }
  43.    
  44.     }
  45.     if (dest_index > (a.length-1))
  46.         b.push(new_item);
  47.  
  48.     return b;  
  49. }
  50.  
  51. function InsertItemAtLine(line, new_item, dest_index, sep)
  52. {
  53.     var a = line.split(sep);
  54.     a.splice(dest_index, 0, new_item);
  55.     return a.join(sep);
  56. }
  57.  
  58.  
  59. var MAX_GDP_PERCAPITA = 2.0000000000;
  60. var MAX_SOCIAL_SUPPORT = 2.0000000000;
  61. var MAX_HLEXP = 1.0000000000;
  62. var MAX_FTMLC = 1.0000000000;
  63. var MAX_GENEROSITY = 1.0000000000;
  64. var MAX_POC = 1.0000000000;
  65.  
  66. function AddPercentageBar()
  67. {
  68.     var MAX_COL_COUNT = 9;
  69.     var COL_WIDTH = 100;
  70.    
  71.     var intable = document.getElementById("textarea_in_1").value;
  72.     var outtable = "";
  73.     var i = 0, j = 0, n = 0;
  74.     var line = "";
  75.     var colcount = 0;
  76.     var colindex = 0;
  77.     var rowcount = 0;
  78.     var country_rank = 0;
  79.    
  80.     i = 0;
  81.     while(i < intable.length)
  82.     {
  83.         if (intable[i] == "\n")
  84.         {  
  85.        
  86.    
  87.             if (line != "|-")
  88.             {
  89.                 colcount++;
  90.                 colindex = colcount-1;
  91.                 n = NumberWithDot(line.replace("|",""));
  92.                
  93.            
  94.                         switch(colindex)
  95.                         {
  96.                             case 0:
  97.                                 break;
  98.                             case 1:
  99.                                 break;
  100.                             case 2:                            
  101.                                 line = "|data-sort-value=\"" + n + "\"|" + GetIncDecString(n) + " " + n;
  102.  
  103.                                
  104.                                 break;
  105.                             case 3:
  106.                            
  107.                                 line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_GDP_PERCAPITA) + "||" + "E4D00A" + "|width=" + COL_WIDTH + "}}";
  108.                                
  109.                                 break;
  110.                             case 4:
  111.                                 line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_SOCIAL_SUPPORT) + "||" + "C23B22" + "|width=" + COL_WIDTH + "}}";
  112.                                
  113.                            
  114.                                 break;
  115.                             case 5:
  116.                             line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_HLEXP) + "||" + "08457E" + "|width=" + COL_WIDTH + "}}";
  117.                                
  118.                                 break;
  119.                             case 6:
  120.                             line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_FTMLC) + "||" + "A4C639" + "|width=" + COL_WIDTH + "}}";
  121.                                
  122.                                 break;
  123.                             case 7:
  124.                             line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_GENEROSITY) + "||" + "8A2BE2" + "|width=" + COL_WIDTH + "}}";
  125.                                
  126.                                 break;
  127.                             case 8:
  128.                             line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_POC) + "||" + "91A3B0" + "|width=" + COL_WIDTH + "}}";
  129.                                
  130.                                 break;
  131.                             case 9:
  132.                            
  133.                                 break;
  134.                            
  135.                             default:
  136.                                 alert("error");
  137.                                
  138.                            
  139.                         }
  140.                 outtable += line + "\n";                       
  141.                
  142.             }
  143.             else
  144.             {
  145.                 colcount = 0;
  146.                 rowcount++;
  147.                
  148.                 country_rank = rowcount;
  149.                 outtable += "|-\n";
  150.                 outtable += "|" + country_rank + "\n";
  151.             }
  152.            
  153.                    
  154.             line = "";
  155.             i++;
  156.         }
  157.         else
  158.         {
  159.             line += intable[i];
  160.             i++;
  161.         }
  162.     }
  163.    
  164.    
  165.    
  166.    
  167.     document.getElementById("textarea_out").value = outtable;
  168. }
  169.  
  170.  
  171. function NumberWithDot(s)
  172. {
  173.     return s.replace(',', '.');
  174. }
  175.  
  176. function CalcPercentage(a, c)
  177. {
  178.     var result = -1.0;
  179.     if (a.indexOf(','))
  180.         a = NumberWithDot(a);
  181.     result = ((100 * a) / c);
  182.     return result;
  183. }
  184.  
  185. function GetIncDecString(value)
  186. {
  187.     var a = NumberWithDot(value);
  188.    
  189.     if (a > 0.000)
  190.         return "{{Increase}}";
  191.     else if (a < 0.000)
  192.         return "{{Decrease}}";
  193.     else
  194.         return "{{Steady}}";
  195. }
  196.  
  197. // Input 1: XLS copy-paste
  198. // Input 2: XLS copy-paste
  199. // Output: XLS table ready to be converted into wikitable
  200. var changelist = [];
  201.  
  202.  
  203. function MergeXlsTables_AddChangesColumn()
  204. {
  205.     var in1 = document.getElementById("textarea_in_1").value;
  206.     var in2 = document.getElementById("textarea_in_2").value; // table with changes
  207.     var outtext;
  208.     var change_value;
  209.    
  210.     var cells = [];
  211.     var line;
  212.    
  213.     // get info from the changes table
  214.     line = "";
  215.     for(i = 0; i < in2.length; i++)
  216.     {
  217.         if (in2[i] == "\n")
  218.         {
  219.             changelist.push(line);
  220.            
  221.            
  222.             line = "";
  223.         }
  224.         else
  225.         {
  226.             line += in2[i];
  227.         }
  228.     }
  229.  
  230.    
  231.     //set info to the second table
  232.     outtext = "";
  233.     line = "";
  234.     for(i = 0; i < in1.length; i++)
  235.     {
  236.         if (in1[i] == "\n")
  237.         {
  238.             cells = line.split("\t");
  239.  
  240.             change_value = GetChangeForCountry(cells[0]);
  241.             if (change_value != "")
  242.             {
  243.             }
  244.             else
  245.             {
  246.                
  247.             }
  248.  
  249.             line = InsertItemAtLine(line, change_value, 4, "\t");
  250.            
  251.        
  252.             outtext += line + "\n";
  253.            
  254.             line = "";
  255.         }
  256.         else
  257.         {
  258.             line += in1[i];
  259.         }
  260.     }
  261.    
  262.     document.getElementById("textarea_out").value = outtext;
  263.  
  264. }
  265.  
  266.  
  267. function GetChangeForCountry(countryname)
  268. {
  269.     var i;
  270.     var info = [];
  271.  
  272.     for(i = 0; i < changelist.length; i++)
  273.     {
  274.         info = changelist[i].split("\t");
  275.        
  276.         if (info[0].toLowerCase() == countryname.toLowerCase())
  277.         {
  278.            
  279.             return info[1];
  280.         }
  281.     }
  282.     return "0.000";
  283. }
  284.  
  285. </script>
  286.  
  287. </body>
  288. </html>
Add Comment
Please, Sign In to add comment