You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. require 'SVG/Graph/Graph'
  2. module SVG
  3. module Graph
  4. # === Create presentation quality SVG line graphs easily
  5. #
  6. # = Synopsis
  7. #
  8. # require 'SVG/Graph/Line'
  9. #
  10. # fields = %w(Jan Feb Mar);
  11. # data_sales_02 = [12, 45, 21]
  12. # data_sales_03 = [15, 30, 40]
  13. #
  14. # graph = SVG::Graph::Line.new({
  15. # :height => 500,
  16. # :width => 300,
  17. # :fields => fields,
  18. # })
  19. #
  20. # graph.add_data({
  21. # :data => data_sales_02,
  22. # :title => 'Sales 2002',
  23. # })
  24. #
  25. # graph.add_data({
  26. # :data => data_sales_03,
  27. # :title => 'Sales 2003',
  28. # })
  29. #
  30. # print "Content-type: image/svg+xml\r\n\r\n";
  31. # print graph.burn();
  32. #
  33. # = Description
  34. #
  35. # This object aims to allow you to easily create high quality
  36. # SVG line graphs. You can either use the default style sheet
  37. # or supply your own. Either way there are many options which can
  38. # be configured to give you control over how the graph is
  39. # generated - with or without a key, data elements at each point,
  40. # title, subtitle etc.
  41. #
  42. # = Examples
  43. #
  44. # http://www.germane-software/repositories/public/SVG/test/single.rb
  45. #
  46. # = Notes
  47. #
  48. # The default stylesheet handles upto 10 data sets, if you
  49. # use more you must create your own stylesheet and add the
  50. # additional settings for the extra data sets. You will know
  51. # if you go over 10 data sets as they will have no style and
  52. # be in black.
  53. #
  54. # = See also
  55. #
  56. # * SVG::Graph::Graph
  57. # * SVG::Graph::BarHorizontal
  58. # * SVG::Graph::Bar
  59. # * SVG::Graph::Pie
  60. # * SVG::Graph::Plot
  61. # * SVG::Graph::TimeSeries
  62. #
  63. # == Author
  64. #
  65. # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
  66. #
  67. # Copyright 2004 Sean E. Russell
  68. # This software is available under the Ruby license[LICENSE.txt]
  69. #
  70. class Line < SVG::Graph::Graph
  71. # Show a small circle on the graph where the line
  72. # goes from one point to the next.
  73. attr_accessor :show_data_points
  74. # Accumulates each data set. (i.e. Each point increased by sum of
  75. # all previous series at same point). Default is 0, set to '1' to show.
  76. attr_accessor :stacked
  77. # Fill in the area under the plot if true
  78. attr_accessor :area_fill
  79. # The constructor takes a hash reference, fields (the names for each
  80. # field on the X axis) MUST be set, all other values are defaulted to
  81. # those shown above - with the exception of style_sheet which defaults
  82. # to using the internal style sheet.
  83. def initialize config
  84. raise "fields was not supplied or is empty" unless config[:fields] &&
  85. config[:fields].kind_of?(Array) &&
  86. config[:fields].length > 0
  87. super
  88. end
  89. # In addition to the defaults set in Graph::initialize, sets
  90. # [show_data_points] true
  91. # [show_data_values] true
  92. # [stacked] false
  93. # [area_fill] false
  94. def set_defaults
  95. init_with(
  96. :show_data_points => true,
  97. :show_data_values => true,
  98. :stacked => false,
  99. :area_fill => false
  100. )
  101. self.top_align = self.top_font = self.right_align = self.right_font = 1
  102. end
  103. protected
  104. def max_value
  105. max = 0
  106. if (stacked == true) then
  107. sums = Array.new(@config[:fields].length).fill(0)
  108. @data.each do |data|
  109. sums.each_index do |i|
  110. sums[i] += data[:data][i].to_f
  111. end
  112. end
  113. max = sums.max
  114. else
  115. max = @data.collect{|x| x[:data].max}.max
  116. end
  117. return max
  118. end
  119. def min_value
  120. min = 0
  121. if (min_scale_value.nil? == false) then
  122. min = min_scale_value
  123. elsif (stacked == true) then
  124. min = @data[-1][:data].min
  125. else
  126. min = @data.collect{|x| x[:data].min}.min
  127. end
  128. return min
  129. end
  130. def get_x_labels
  131. @config[:fields]
  132. end
  133. def calculate_left_margin
  134. super
  135. label_left = @config[:fields][0].length / 2 * font_size * 0.6
  136. @border_left = label_left if label_left > @border_left
  137. end
  138. def get_y_labels
  139. maxvalue = max_value
  140. minvalue = min_value
  141. range = maxvalue - minvalue
  142. top_pad = range == 0 ? 10 : range / 20.0
  143. scale_range = (maxvalue + top_pad) - minvalue
  144. scale_division = scale_divisions || (scale_range / 10.0)
  145. if scale_integers
  146. scale_division = scale_division < 1 ? 1 : scale_division.round
  147. end
  148. rv = []
  149. maxvalue = maxvalue%scale_division == 0 ?
  150. maxvalue : maxvalue + scale_division
  151. minvalue.step( maxvalue, scale_division ) {|v| rv << v}
  152. return rv
  153. end
  154. def calc_coords(field, value, width = field_width, height = field_height)
  155. coords = {:x => 0, :y => 0}
  156. coords[:x] = width * field
  157. coords[:y] = @graph_height - value * height
  158. return coords
  159. end
  160. def draw_data
  161. minvalue = min_value
  162. fieldheight = (@graph_height.to_f - font_size*2*top_font) /
  163. (get_y_labels.max - get_y_labels.min)
  164. fieldwidth = field_width
  165. line = @data.length
  166. prev_sum = Array.new(@config[:fields].length).fill(0)
  167. cum_sum = Array.new(@config[:fields].length).fill(-minvalue)
  168. for data in @data.reverse
  169. lpath = ""
  170. apath = ""
  171. if not stacked then cum_sum.fill(-minvalue) end
  172. data[:data].each_index do |i|
  173. cum_sum[i] += data[:data][i]
  174. c = calc_coords(i, cum_sum[i], fieldwidth, fieldheight)
  175. lpath << "#{c[:x]} #{c[:y]} "
  176. end
  177. if area_fill
  178. if stacked then
  179. (prev_sum.length - 1).downto 0 do |i|
  180. c = calc_coords(i, prev_sum[i], fieldwidth, fieldheight)
  181. apath << "#{c[:x]} #{c[:y]} "
  182. end
  183. c = calc_coords(0, prev_sum[0], fieldwidth, fieldheight)
  184. else
  185. apath = "V#@graph_height"
  186. c = calc_coords(0, 0, fieldwidth, fieldheight)
  187. end
  188. @graph.add_element("path", {
  189. "d" => "M#{c[:x]} #{c[:y]} L" + lpath + apath + "Z",
  190. "class" => "fill#{line}"
  191. })
  192. end
  193. @graph.add_element("path", {
  194. "d" => "M0 #@graph_height L" + lpath,
  195. "class" => "line#{line}"
  196. })
  197. if show_data_points || show_data_values
  198. cum_sum.each_index do |i|
  199. if show_data_points
  200. @graph.add_element( "circle", {
  201. "cx" => (fieldwidth * i).to_s,
  202. "cy" => (@graph_height - cum_sum[i] * fieldheight).to_s,
  203. "r" => "2.5",
  204. "class" => "dataPoint#{line}"
  205. })
  206. end
  207. make_datapoint_text(
  208. fieldwidth * i,
  209. @graph_height - cum_sum[i] * fieldheight - 6,
  210. cum_sum[i] + minvalue
  211. )
  212. end
  213. end
  214. prev_sum = cum_sum.dup
  215. line -= 1
  216. end
  217. end
  218. def get_css
  219. return <<EOL
  220. /* default line styles */
  221. .line1{
  222. fill: none;
  223. stroke: #ff0000;
  224. stroke-width: 1px;
  225. }
  226. .line2{
  227. fill: none;
  228. stroke: #0000ff;
  229. stroke-width: 1px;
  230. }
  231. .line3{
  232. fill: none;
  233. stroke: #00ff00;
  234. stroke-width: 1px;
  235. }
  236. .line4{
  237. fill: none;
  238. stroke: #ffcc00;
  239. stroke-width: 1px;
  240. }
  241. .line5{
  242. fill: none;
  243. stroke: #00ccff;
  244. stroke-width: 1px;
  245. }
  246. .line6{
  247. fill: none;
  248. stroke: #ff00ff;
  249. stroke-width: 1px;
  250. }
  251. .line7{
  252. fill: none;
  253. stroke: #00ffff;
  254. stroke-width: 1px;
  255. }
  256. .line8{
  257. fill: none;
  258. stroke: #ffff00;
  259. stroke-width: 1px;
  260. }
  261. .line9{
  262. fill: none;
  263. stroke: #ccc6666;
  264. stroke-width: 1px;
  265. }
  266. .line10{
  267. fill: none;
  268. stroke: #663399;
  269. stroke-width: 1px;
  270. }
  271. .line11{
  272. fill: none;
  273. stroke: #339900;
  274. stroke-width: 1px;
  275. }
  276. .line12{
  277. fill: none;
  278. stroke: #9966FF;
  279. stroke-width: 1px;
  280. }
  281. /* default fill styles */
  282. .fill1{
  283. fill: #cc0000;
  284. fill-opacity: 0.2;
  285. stroke: none;
  286. }
  287. .fill2{
  288. fill: #0000cc;
  289. fill-opacity: 0.2;
  290. stroke: none;
  291. }
  292. .fill3{
  293. fill: #00cc00;
  294. fill-opacity: 0.2;
  295. stroke: none;
  296. }
  297. .fill4{
  298. fill: #ffcc00;
  299. fill-opacity: 0.2;
  300. stroke: none;
  301. }
  302. .fill5{
  303. fill: #00ccff;
  304. fill-opacity: 0.2;
  305. stroke: none;
  306. }
  307. .fill6{
  308. fill: #ff00ff;
  309. fill-opacity: 0.2;
  310. stroke: none;
  311. }
  312. .fill7{
  313. fill: #00ffff;
  314. fill-opacity: 0.2;
  315. stroke: none;
  316. }
  317. .fill8{
  318. fill: #ffff00;
  319. fill-opacity: 0.2;
  320. stroke: none;
  321. }
  322. .fill9{
  323. fill: #cc6666;
  324. fill-opacity: 0.2;
  325. stroke: none;
  326. }
  327. .fill10{
  328. fill: #663399;
  329. fill-opacity: 0.2;
  330. stroke: none;
  331. }
  332. .fill11{
  333. fill: #339900;
  334. fill-opacity: 0.2;
  335. stroke: none;
  336. }
  337. .fill12{
  338. fill: #9966FF;
  339. fill-opacity: 0.2;
  340. stroke: none;
  341. }
  342. /* default line styles */
  343. .key1,.dataPoint1{
  344. fill: #ff0000;
  345. stroke: none;
  346. stroke-width: 1px;
  347. }
  348. .key2,.dataPoint2{
  349. fill: #0000ff;
  350. stroke: none;
  351. stroke-width: 1px;
  352. }
  353. .key3,.dataPoint3{
  354. fill: #00ff00;
  355. stroke: none;
  356. stroke-width: 1px;
  357. }
  358. .key4,.dataPoint4{
  359. fill: #ffcc00;
  360. stroke: none;
  361. stroke-width: 1px;
  362. }
  363. .key5,.dataPoint5{
  364. fill: #00ccff;
  365. stroke: none;
  366. stroke-width: 1px;
  367. }
  368. .key6,.dataPoint6{
  369. fill: #ff00ff;
  370. stroke: none;
  371. stroke-width: 1px;
  372. }
  373. .key7,.dataPoint7{
  374. fill: #00ffff;
  375. stroke: none;
  376. stroke-width: 1px;
  377. }
  378. .key8,.dataPoint8{
  379. fill: #ffff00;
  380. stroke: none;
  381. stroke-width: 1px;
  382. }
  383. .key9,.dataPoint9{
  384. fill: #cc6666;
  385. stroke: none;
  386. stroke-width: 1px;
  387. }
  388. .key10,.dataPoint10{
  389. fill: #663399;
  390. stroke: none;
  391. stroke-width: 1px;
  392. }
  393. .key11,.dataPoint11{
  394. fill: #339900;
  395. stroke: none;
  396. stroke-width: 1px;
  397. }
  398. .key12,.dataPoint12{
  399. fill: #9966FF;
  400. stroke: none;
  401. stroke-width: 1px;
  402. }
  403. EOL
  404. end
  405. end
  406. end
  407. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...