Guest User

Untitled

a guest
Aug 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <title>Flot Examples With SharePoint</title>
  5.  
  6.  
  7. <!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
  8. <script language="javascript" type="text/javascript" src="../jquery.js"></script>
  9. <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
  10. <script language="javascript" type="text/javascript" src="../jquery.tmpl.min.js"></script>
  11. <script id="whatifTemplate" type="text/x-jquery-tmpl">
  12. {{each months}} <li>${$value}: <input class='whatifinput' type='text' value='' /> </li> {{/each}}
  13. </script>
  14.  
  15. </head>
  16. <body>
  17.  
  18. <h1> What-If Analysis </h1>
  19.  
  20. <p><h2>Donut Sales</h2></p>
  21. <table >
  22. <tr>
  23. <td valign="top">
  24. <div id="columnchart" style="width:600px;height:300px" >
  25. </div>
  26. </td>
  27. <td width="300px" valign="top">
  28. <div id="columnchartlabels"></div>
  29. </td>
  30. </tr>
  31. </table>
  32.  
  33. <ul id="whatif" style="list-style: none;">
  34.  
  35. </ul>
  36. <input id='calculate' type='button' value="Calculate" />
  37.  
  38.  
  39. <script>
  40.  
  41.  
  42. function PlotGraph( d , id, labelId)
  43. {
  44.  
  45. $.plot($(id), d,
  46. {
  47. xaxis: {
  48. ticks: 11,
  49. tickFormatter: function(x) {
  50. var Months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  51. return Months[x] ; }
  52. },
  53. legend: {
  54. show: true,
  55. backgroundOpacity: 0.8,
  56. container: labelId,
  57. labelFormatter: function(label, series) {
  58. return '<span class="legendLabel">'+ label + '</span>';
  59. }
  60. }
  61. });
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. function roundNumber(num, dec) {
  69. var result = String(Math.round(num*Math.pow(10,dec))/Math.pow(10,dec));
  70. if(result.indexOf('.')<0) {
  71. result+= '.';
  72. }
  73. while(result.length- result.indexOf('.')<=dec) {result+= '0';}
  74. if (dec==0)
  75. result = result.replace('.','');
  76. return result;
  77. }
  78.  
  79. function getFloatValue(n)
  80. {
  81. x = parseFloat(n);
  82. if (isNaN(x))
  83. {
  84. x = 0;
  85. }
  86. return parseFloat(roundNumber(x,2));
  87. }
  88.  
  89.  
  90.  
  91. function getSales()
  92. {
  93.  
  94. /*** This method is supposed to get data from SharePoint, but for the sake of simplicity, we are going to
  95. skip that step and hardcode the data.
  96. ***/
  97.  
  98.  
  99. var donutSales = [ [0,7000],[1,8200],[2,8300],[3,8200],[4,8000],[5,8300]];
  100. var donutSalesMinTargetData = [[0,8000],[11,8000]];
  101. var donutSalesMaxTargetData = [[0,10000],[11,10000]];
  102. var donutSalesMaxTrend = [];
  103.  
  104. var donutSalesData = new Array();
  105.  
  106. var sales = {label: "Donut Sales", data:donutSales,lines: { show: true ,lineWidth:3},color:"black"} ;
  107. var maxLimit = {label: "High Range", data: donutSalesMaxTargetData, lines: { show: true, lineWidth: 3, fill: 0.3 },color:"green" };
  108. var minLimit = {label: "Low range", data: donutSalesMinTargetData, lines: { show: true, lineWidth: 3, fill: 0.9},color:"red" };
  109.  
  110. var trendData = LinearReg(donutSales,6 );
  111.  
  112. var trend = {label: "Sales Trend", data:trendData,lines: { show: true ,lineWidth:3},color:"blue"} ;
  113.  
  114. donutSalesData.push(minLimit);
  115. donutSalesData.push(maxLimit);
  116. donutSalesData.push(sales);
  117. donutSalesData.push(trend);
  118.  
  119. PlotGraph( donutSalesData,"#columnchart", "#columnchartlabels");
  120.  
  121. var remainingMonths = {months:["Jul","Aug","Sep","Oct","Nov","Dec"]};
  122.  
  123. $( "#whatifTemplate" ).tmpl( remainingMonths ).appendTo( "#whatif" );
  124.  
  125.  
  126. //Calculate Trend based on data input for remaining months
  127. $("#calculate").click(function() {
  128. var newsales= $.extend([], donutSales);
  129. var newsalesData = new Array();
  130. $(".whatifinput").each(function(i,el){
  131. var n = i + donutSales.length ;
  132. newsales.push([n,parseFloat($(el).val())]);
  133.  
  134. });
  135. trendData = LinearReg(newsales,6 );
  136. trend = {label: "Sales Trend", data:trendData,lines: { show: true ,lineWidth:3},color:"blue"} ;
  137. newsalesData.push(minLimit);
  138. newsalesData.push(maxLimit);
  139. newsalesData.push(newsales);
  140. newsalesData.push(trend);
  141.  
  142. PlotGraph( newsalesData,"#columnchart", "#columnchartlabels");
  143. });
  144.  
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. function LinearReg(lndata )
  155. {
  156. XintoY = [];
  157. XintoX = [];
  158. SumofX = 0;
  159. SumofY = 0;
  160. SumofXY = 0;
  161. SumofXX = 0;
  162. for (var i=0; i < lndata.length; i++)
  163. {
  164. x = lndata[i][0];
  165. y = lndata[i][1];
  166. XintoY[i] = x * y;
  167. XintoX[i] = x * x;
  168. SumofX += x;
  169. SumofY += y;
  170. SumofXX += XintoX[i];
  171. SumofXY += XintoY[i];
  172. }
  173. var n = lndata.length;
  174. b = ((n * SumofXY) - (SumofX * SumofY))/(n*SumofXX - SumofX*SumofX );
  175. a = (SumofY - (b*SumofX)) / n ;
  176. retData = [];
  177. for (var i=0; i < 12; i++)
  178. {
  179. y1 = a + b * i;
  180. retData.push([i,y1]);
  181. }
  182. return retData;
  183. }
  184.  
  185.  
  186. getSales();
  187.  
  188.  
  189.  
  190.  
  191. </script>
  192.  
  193. </body>
  194. </html>
Add Comment
Please, Sign In to add comment