gchart.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import math
  2. def jsfile(dp, fout, title):
  3. htmltext = """
  4. <html>
  5. <head>
  6. <!--Load the AJAX API-->
  7. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  8. <script type="text/javascript">
  9. // Load the Visualization API and the piechart package.
  10. google.load('visualization', '1.0', {'packages':['corechart']});
  11. // Set a callback to run when the Google Visualization API is loaded.
  12. google.setOnLoadCallback(drawChart);
  13. // Callback that creates and populates a data table,
  14. // instantiates the pie chart, passes in the data and
  15. // draws it.
  16. function drawChart() {
  17. var data = google.visualization.arrayToDataTable(
  18. %s
  19. );
  20. var options = {
  21. title: '%s',
  22. legend: { position: 'top', maxLines: 2 },
  23. colors: ['#5C3292', '#1A8763', '#871B47', '#999999', '#5CFF92'],
  24. interpolateNulls: false,
  25. };
  26. var chart = new google.visualization.Histogram(document.getElementById('chart_div'));
  27. chart.draw(data, options);
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <!--Div that will hold the pie chart-->
  33. <div id="chart_div"></div>
  34. </body>
  35. </html>
  36. """
  37. htmltext = htmltext % (dp, title)
  38. f = open(fout, 'w')
  39. f.write(htmltext)
  40. f.close()
  41. #print htmltext
  42. def jsfile2(dp, fout, title):
  43. htmltext = """
  44. <html>
  45. <head>
  46. <!--Load the AJAX API-->
  47. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  48. <script type="text/javascript">
  49. // Load the Visualization API and the piechart package.
  50. google.load('visualization', '1.0', {'packages':['corechart']});
  51. // Set a callback to run when the Google Visualization API is loaded.
  52. google.setOnLoadCallback(drawChart);
  53. // Callback that creates and populates a data table,
  54. // instantiates the pie chart, passes in the data and
  55. // draws it.
  56. function drawChart() {
  57. var data = google.visualization.arrayToDataTable(
  58. %s
  59. );
  60. var options = {
  61. title: '%s',
  62. legend: { position: 'top', maxLines: 2 },
  63. colors: ['#5C3292', '#1A8763', '#871B47', '#999999', '#5CFF92'],
  64. interpolateNulls: false,
  65. };
  66. var chart = new google.visualization.Histogram(document.getElementById('chart_div'));
  67. chart.draw(data, options);
  68. }
  69. </script>
  70. </head>
  71. <body>
  72. <!--Div that will hold the pie chart-->
  73. <div id="chart_div"></div>
  74. </body>
  75. </html>
  76. """
  77. htmltext = htmltext % (dp, title)
  78. f = open(fout, 'w')
  79. f.write(htmltext)
  80. f.close()
  81. #print htmltext
  82. def jsfile3(fout, jschunk, htmldiv):
  83. htmltext = """
  84. <html>
  85. <head>
  86. <!--Load the AJAX API-->
  87. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  88. <script type="text/javascript">
  89. // Load the Visualization API and the piechart package.
  90. google.load('visualization', '1.0', {'packages':['corechart']});
  91. // Set a callback to run when the Google Visualization API is loaded.
  92. google.setOnLoadCallback(drawChart);
  93. // Callback that creates and populates a data table,
  94. // instantiates the pie chart, passes in the data and
  95. // draws it.
  96. function drawChart() {
  97. %s
  98. }
  99. </script>
  100. </head>
  101. <body>
  102. <!--Div that will hold the pie chart-->
  103. %s
  104. </body>
  105. </html>
  106. """
  107. htmltext = htmltext % (jschunk, htmldiv)
  108. f = open(fout, 'w')
  109. f.write(htmltext)
  110. f.close()
  111. #print htmltext
  112. def format_weekly_data_points3(infile, outfile, title):
  113. jstext = """
  114. var data%s = google.visualization.arrayToDataTable(
  115. %s
  116. );
  117. var options = {
  118. title: '%s',
  119. legend: { position: 'top', maxLines: 2 },
  120. colors: ['#5C3292', '#1A8763', '#871B47', '#999999', '#5CFF92'],
  121. interpolateNulls: false,
  122. };
  123. var chart%s = new google.visualization.Histogram(document.getElementById('chart_div%s'));
  124. chart%s.draw(data%s, options);
  125. """
  126. f = open(infile)
  127. points_series = {}
  128. cnt = 0
  129. for l in f.readlines():
  130. l = l.strip("\n").split(',')
  131. # to limit the data points to a partcular year range, add the condition
  132. # l[2][0:4] not in ['2014', '2015', '2013']:
  133. if math.fabs(float(l[1])) > 0.035 or l[2][0:4] not in ['2014', '2015', '2013']:
  134. continue
  135. else:
  136. if l[2][5:7] not in points_series:
  137. points_series[l[2][5:7]]= []
  138. if l[0] == '1':
  139. points_series[l[2][5:7]].append( "[%s, null,null,null,null]," % l[1])
  140. elif l[0] == '2':
  141. points_series[l[2][5:7]].append( "[null, %s, null,null,null]," % l[1])
  142. elif l[0] == '3':
  143. points_series[l[2][5:7]].append( "[null,null,%s, null,null]," % l[1])
  144. elif l[0] == '4':
  145. points_series[l[2][5:7]].append( "[null,null,null,%s, null]," % l[1])
  146. elif l[0] == '5':
  147. points_series[l[2][5:7]].append( "[null,null,null,null,%s ]," % l[1])
  148. # cnt = cnt + 1
  149. # if cnt > 50:
  150. # break
  151. s = '[["M", "T", "W", "R", "F"],'
  152. jstexts = {}
  153. jschunk = ''
  154. htmldiv = ''
  155. for month, magnitudes in points_series.iteritems():
  156. s = '[["M", "T", "W", "R", "F"],'
  157. s = s + ''.join(e for e in magnitudes)
  158. s = s[:len(s)-1] + ']'
  159. jstexts[month] = jstext
  160. jstexts[month] = jstexts[month] % (month, s, "HKCE Open/Previous day fluctuation by month:" + month, month, month, month, month)
  161. print month, len(magnitudes) -1
  162. jschunk = jschunk + jstexts[month]
  163. for month in sorted(points_series.keys()):
  164. htmldiv = htmldiv + '<div id="chart_div%s"></div>' % month
  165. #print jschunk
  166. print htmldiv
  167. jsfile3(outfile, jschunk, htmldiv)
  168. def format_weekly_data_points2(infile, outfile, title):
  169. # this routine formats data into a tabular format of 5 columns
  170. # each column represents a series of a weekday with monday starts at 1
  171. # and tuesday starts at 2
  172. # the value at each column is the % change in HSI on the open vs previous close
  173. # the objective is to see whether there exists a pattern with mondays
  174. # experience more fluctations when market opens
  175. f = open(infile)
  176. points = []
  177. for l in f.readlines():
  178. l = l.strip("\n").split(',')
  179. if math.fabs(float(l[1])) > 0.035 or l[2][5:7] <> '10':
  180. continue
  181. else:
  182. if l[0] == '1':
  183. points.append( "[%s, null,null,null,null]," % l[1])
  184. elif l[0] == '2':
  185. points.append( "[null, %s, null,null,null]," % l[1])
  186. elif l[0] == '3':
  187. points.append( "[null,null,%s, null,null]," % l[1])
  188. elif l[0] == '4':
  189. points.append( "[null,null,null,%s, null]," % l[1])
  190. elif l[0] == '5':
  191. points.append( "[null,null,null,null,%s ]," % l[1])
  192. s = '[["M", "T", "W", "R", "F"],'
  193. s = s + ''.join(e for e in points)
  194. s = s[:len(s)-1] + ']'
  195. print len(points) -1
  196. jsfile(s, outfile, title)
  197. def format_data_points(infile, outfile, title):
  198. f = open(infile)
  199. points = []
  200. for l in f.readlines():
  201. l = l.strip("\n").split(',')
  202. points.append( "['%s', %s]," % (l[0], l[1]))
  203. s = '[["daily change", "Num Days"],'
  204. s = s + ''.join(e for e in points)
  205. s = s[:len(s)-1] + ']'
  206. print s
  207. print len(points) -1
  208. jsfile(s, outfile, title)
  209. if __name__ == '__main__':
  210. #format_data_points("data/hsi-monday-change.csv", "data/hsi-monday-change.html", "monday open change magnitude")
  211. #format_weekly_data_points2("data/hsi-weekday-change.csv", "data/hsi-weekday-change.html", "weekday open change magnitude")
  212. #format_weekly_data_points2("data/hsi-weekday-change-over-5-years.csv", "data/hsi-weekday-change-over-5-years.html", "weekday open change magnitude")
  213. #format_weekly_data_points3("data/hsi-weekday-change-over-5-years.csv", "data/hsi-weekday-change-over-5-years.html", "weekday open change magnitude")
  214. format_weekly_data_points3("data/hkce-weekday-change-over-5-years.csv", "data/hkce-weekday-change-over-5-years.html", "weekday open change magnitude")