Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- </head>
- <body>
- <h2>World Happiness Report</h2>
- <textarea id="textarea_in_1" rows="30" cols="200" value=""></textarea><br>
- <textarea id="textarea_in_2" rows="30" cols="200" value=""></textarea><br>
- <button type="button" id="btnStart" onclick="AddPercentageBar()">Start</button><br>
- Output:<br>
- <textarea id="textarea_out" rows="30" cols="200" value=""></textarea><br>
- <script type="text/javascript">
- // Inputs:
- // item : any value
- // line : a token string (eg: "house*dog*cat")
- // index: index where to insert the new item
- // Return value: array containing the new item
- function InsertItemAtLineOld(line, new_item, dest_index, sep)
- {
- var line_bkp = line.split(sep);
- var a = line.split(sep);
- var b = [];
- for(i = 0; i < a.length; i++)
- {
- if( i < dest_index )
- {
- b.push(a[i]);
- }
- else if (i > dest_index)
- {
- b.push(a[i]);
- }
- else
- {
- b.push(new_item);
- b.push(a[i]);
- }
- }
- if (dest_index > (a.length-1))
- b.push(new_item);
- return b;
- }
- function InsertItemAtLine(line, new_item, dest_index, sep)
- {
- var a = line.split(sep);
- a.splice(dest_index, 0, new_item);
- return a.join(sep);
- }
- var MAX_GDP_PERCAPITA = 2.0000000000;
- var MAX_SOCIAL_SUPPORT = 2.0000000000;
- var MAX_HLEXP = 1.0000000000;
- var MAX_FTMLC = 1.0000000000;
- var MAX_GENEROSITY = 1.0000000000;
- var MAX_POC = 1.0000000000;
- function AddPercentageBar()
- {
- var MAX_COL_COUNT = 9;
- var COL_WIDTH = 100;
- var intable = document.getElementById("textarea_in_1").value;
- var outtable = "";
- var i = 0, j = 0, n = 0;
- var line = "";
- var colcount = 0;
- var colindex = 0;
- var rowcount = 0;
- var country_rank = 0;
- i = 0;
- while(i < intable.length)
- {
- if (intable[i] == "\n")
- {
- if (line != "|-")
- {
- colcount++;
- colindex = colcount-1;
- n = NumberWithDot(line.replace("|",""));
- switch(colindex)
- {
- case 0:
- break;
- case 1:
- break;
- case 2:
- line = "|data-sort-value=\"" + n + "\"|" + GetIncDecString(n) + " " + n;
- break;
- case 3:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_GDP_PERCAPITA) + "||" + "E4D00A" + "|width=" + COL_WIDTH + "}}";
- break;
- case 4:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_SOCIAL_SUPPORT) + "||" + "C23B22" + "|width=" + COL_WIDTH + "}}";
- break;
- case 5:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_HLEXP) + "||" + "08457E" + "|width=" + COL_WIDTH + "}}";
- break;
- case 6:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_FTMLC) + "||" + "A4C639" + "|width=" + COL_WIDTH + "}}";
- break;
- case 7:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_GENEROSITY) + "||" + "8A2BE2" + "|width=" + COL_WIDTH + "}}";
- break;
- case 8:
- line = "|data-sort-value=\"" + n + "\"|" + "{{Percentage bar|" + CalcPercentage(n, MAX_POC) + "||" + "91A3B0" + "|width=" + COL_WIDTH + "}}";
- break;
- case 9:
- break;
- default:
- alert("error");
- }
- outtable += line + "\n";
- }
- else
- {
- colcount = 0;
- rowcount++;
- country_rank = rowcount;
- outtable += "|-\n";
- outtable += "|" + country_rank + "\n";
- }
- line = "";
- i++;
- }
- else
- {
- line += intable[i];
- i++;
- }
- }
- document.getElementById("textarea_out").value = outtable;
- }
- function NumberWithDot(s)
- {
- return s.replace(',', '.');
- }
- function CalcPercentage(a, c)
- {
- var result = -1.0;
- if (a.indexOf(','))
- a = NumberWithDot(a);
- result = ((100 * a) / c);
- return result;
- }
- function GetIncDecString(value)
- {
- var a = NumberWithDot(value);
- if (a > 0.000)
- return "{{Increase}}";
- else if (a < 0.000)
- return "{{Decrease}}";
- else
- return "{{Steady}}";
- }
- // Input 1: XLS copy-paste
- // Input 2: XLS copy-paste
- // Output: XLS table ready to be converted into wikitable
- var changelist = [];
- function MergeXlsTables_AddChangesColumn()
- {
- var in1 = document.getElementById("textarea_in_1").value;
- var in2 = document.getElementById("textarea_in_2").value; // table with changes
- var outtext;
- var change_value;
- var cells = [];
- var line;
- // get info from the changes table
- line = "";
- for(i = 0; i < in2.length; i++)
- {
- if (in2[i] == "\n")
- {
- changelist.push(line);
- line = "";
- }
- else
- {
- line += in2[i];
- }
- }
- //set info to the second table
- outtext = "";
- line = "";
- for(i = 0; i < in1.length; i++)
- {
- if (in1[i] == "\n")
- {
- cells = line.split("\t");
- change_value = GetChangeForCountry(cells[0]);
- if (change_value != "")
- {
- }
- else
- {
- }
- line = InsertItemAtLine(line, change_value, 4, "\t");
- outtext += line + "\n";
- line = "";
- }
- else
- {
- line += in1[i];
- }
- }
- document.getElementById("textarea_out").value = outtext;
- }
- function GetChangeForCountry(countryname)
- {
- var i;
- var info = [];
- for(i = 0; i < changelist.length; i++)
- {
- info = changelist[i].split("\t");
- if (info[0].toLowerCase() == countryname.toLowerCase())
- {
- return info[1];
- }
- }
- return "0.000";
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment