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.

Graph.rb 30 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. begin
  2. require 'zlib'
  3. @@__have_zlib = true
  4. rescue
  5. @@__have_zlib = false
  6. end
  7. require 'rexml/document'
  8. module SVG
  9. module Graph
  10. VERSION = '@ANT_VERSION@'
  11. # === Base object for generating SVG Graphs
  12. #
  13. # == Synopsis
  14. #
  15. # This class is only used as a superclass of specialized charts. Do not
  16. # attempt to use this class directly, unless creating a new chart type.
  17. #
  18. # For examples of how to subclass this class, see the existing specific
  19. # subclasses, such as SVG::Graph::Pie.
  20. #
  21. # == Examples
  22. #
  23. # For examples of how to use this package, see either the test files, or
  24. # the documentation for the specific class you want to use.
  25. #
  26. # * file:test/plot.rb
  27. # * file:test/single.rb
  28. # * file:test/test.rb
  29. # * file:test/timeseries.rb
  30. #
  31. # == Description
  32. #
  33. # This package should be used as a base for creating SVG graphs.
  34. #
  35. # == Acknowledgements
  36. #
  37. # Leo Lapworth for creating the SVG::TT::Graph package which this Ruby
  38. # port is based on.
  39. #
  40. # Stephen Morgan for creating the TT template and SVG.
  41. #
  42. # == See
  43. #
  44. # * SVG::Graph::BarHorizontal
  45. # * SVG::Graph::Bar
  46. # * SVG::Graph::Line
  47. # * SVG::Graph::Pie
  48. # * SVG::Graph::Plot
  49. # * SVG::Graph::TimeSeries
  50. #
  51. # == Author
  52. #
  53. # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
  54. #
  55. # Copyright 2004 Sean E. Russell
  56. # This software is available under the Ruby license[LICENSE.txt]
  57. #
  58. class Graph
  59. include REXML
  60. # Initialize the graph object with the graph settings. You won't
  61. # instantiate this class directly; see the subclass for options.
  62. # [width] 500
  63. # [height] 300
  64. # [show_x_guidelines] false
  65. # [show_y_guidelines] true
  66. # [show_data_values] true
  67. # [min_scale_value] 0
  68. # [show_x_labels] true
  69. # [stagger_x_labels] false
  70. # [rotate_x_labels] false
  71. # [step_x_labels] 1
  72. # [step_include_first_x_label] true
  73. # [show_y_labels] true
  74. # [rotate_y_labels] false
  75. # [scale_integers] false
  76. # [show_x_title] false
  77. # [x_title] 'X Field names'
  78. # [show_y_title] false
  79. # [y_title_text_direction] :bt
  80. # [y_title] 'Y Scale'
  81. # [show_graph_title] false
  82. # [graph_title] 'Graph Title'
  83. # [show_graph_subtitle] false
  84. # [graph_subtitle] 'Graph Sub Title'
  85. # [key] true,
  86. # [key_position] :right, # bottom or righ
  87. # [font_size] 12
  88. # [title_font_size] 16
  89. # [subtitle_font_size] 14
  90. # [x_label_font_size] 12
  91. # [x_title_font_size] 14
  92. # [y_label_font_size] 12
  93. # [y_title_font_size] 14
  94. # [key_font_size] 10
  95. # [no_css] false
  96. # [add_popups] false
  97. def initialize( config )
  98. @config = config
  99. self.top_align = self.top_font = self.right_align = self.right_font = 0
  100. init_with({
  101. :width => 500,
  102. :height => 300,
  103. :show_x_guidelines => false,
  104. :show_y_guidelines => true,
  105. :show_data_values => true,
  106. # :min_scale_value => 0,
  107. :show_x_labels => true,
  108. :stagger_x_labels => false,
  109. :rotate_x_labels => false,
  110. :step_x_labels => 1,
  111. :step_include_first_x_label => true,
  112. :show_y_labels => true,
  113. :rotate_y_labels => false,
  114. :stagger_y_labels => false,
  115. :scale_integers => false,
  116. :show_x_title => false,
  117. :x_title => 'X Field names',
  118. :show_y_title => false,
  119. :y_title_text_direction => :bt,
  120. :y_title => 'Y Scale',
  121. :show_graph_title => false,
  122. :graph_title => 'Graph Title',
  123. :show_graph_subtitle => false,
  124. :graph_subtitle => 'Graph Sub Title',
  125. :key => true,
  126. :key_position => :right, # bottom or right
  127. :font_size =>12,
  128. :title_font_size =>16,
  129. :subtitle_font_size =>14,
  130. :x_label_font_size =>12,
  131. :x_title_font_size =>14,
  132. :y_label_font_size =>12,
  133. :y_title_font_size =>14,
  134. :key_font_size =>10,
  135. :no_css =>false,
  136. :add_popups =>false,
  137. })
  138. set_defaults if respond_to? :set_defaults
  139. init_with config
  140. end
  141. # This method allows you do add data to the graph object.
  142. # It can be called several times to add more data sets in.
  143. #
  144. # data_sales_02 = [12, 45, 21];
  145. #
  146. # graph.add_data({
  147. # :data => data_sales_02,
  148. # :title => 'Sales 2002'
  149. # })
  150. def add_data conf
  151. @data = [] unless defined? @data
  152. if conf[:data] and conf[:data].kind_of? Array
  153. @data << conf
  154. else
  155. raise "No data provided by #{conf.inspect}"
  156. end
  157. end
  158. # This method removes all data from the object so that you can
  159. # reuse it to create a new graph but with the same config options.
  160. #
  161. # graph.clear_data
  162. def clear_data
  163. @data = []
  164. end
  165. # This method processes the template with the data and
  166. # config which has been set and returns the resulting SVG.
  167. #
  168. # This method will croak unless at least one data set has
  169. # been added to the graph object.
  170. #
  171. # print graph.burn
  172. def burn
  173. raise "No data available" unless @data.size > 0
  174. calculations if respond_to? :calculations
  175. start_svg
  176. calculate_graph_dimensions
  177. @foreground = Element.new( "g" )
  178. draw_graph
  179. draw_titles
  180. draw_legend
  181. draw_data
  182. @graph.add_element( @foreground )
  183. style
  184. data = ""
  185. @doc.write( data, 0 )
  186. if @config[:compress]
  187. if @@__have_zlib
  188. inp, out = IO.pipe
  189. gz = Zlib::GzipWriter.new( out )
  190. gz.write data
  191. gz.close
  192. data = inp.read
  193. else
  194. data << "<!-- Ruby Zlib not available for SVGZ -->";
  195. end
  196. end
  197. return data
  198. end
  199. # Set the height of the graph box, this is the total height
  200. # of the SVG box created - not the graph it self which auto
  201. # scales to fix the space.
  202. attr_accessor :height
  203. # Set the width of the graph box, this is the total width
  204. # of the SVG box created - not the graph it self which auto
  205. # scales to fix the space.
  206. attr_accessor :width
  207. # Set the path to an external stylesheet, set to '' if
  208. # you want to revert back to using the defaut internal version.
  209. #
  210. # To create an external stylesheet create a graph using the
  211. # default internal version and copy the stylesheet section to
  212. # an external file and edit from there.
  213. attr_accessor :style_sheet
  214. # (Bool) Show the value of each element of data on the graph
  215. attr_accessor :show_data_values
  216. # The point at which the Y axis starts, defaults to '0',
  217. # if set to nil it will default to the minimum data value.
  218. attr_accessor :min_scale_value
  219. # Whether to show labels on the X axis or not, defaults
  220. # to true, set to false if you want to turn them off.
  221. attr_accessor :show_x_labels
  222. # This puts the X labels at alternative levels so if they
  223. # are long field names they will not overlap so easily.
  224. # Default it false, to turn on set to true.
  225. attr_accessor :stagger_x_labels
  226. # This puts the Y labels at alternative levels so if they
  227. # are long field names they will not overlap so easily.
  228. # Default it false, to turn on set to true.
  229. attr_accessor :stagger_y_labels
  230. # This turns the X axis labels by 90 degrees.
  231. # Default it false, to turn on set to true.
  232. attr_accessor :rotate_x_labels
  233. # This turns the Y axis labels by 90 degrees.
  234. # Default it false, to turn on set to true.
  235. attr_accessor :rotate_y_labels
  236. # How many "steps" to use between displayed X axis labels,
  237. # a step of one means display every label, a step of two results
  238. # in every other label being displayed (label <gap> label <gap> label),
  239. # a step of three results in every third label being displayed
  240. # (label <gap> <gap> label <gap> <gap> label) and so on.
  241. attr_accessor :step_x_labels
  242. # Whether to (when taking "steps" between X axis labels) step from
  243. # the first label (i.e. always include the first label) or step from
  244. # the X axis origin (i.e. start with a gap if step_x_labels is greater
  245. # than one).
  246. attr_accessor :step_include_first_x_label
  247. # Whether to show labels on the Y axis or not, defaults
  248. # to true, set to false if you want to turn them off.
  249. attr_accessor :show_y_labels
  250. # Ensures only whole numbers are used as the scale divisions.
  251. # Default it false, to turn on set to true. This has no effect if
  252. # scale divisions are less than 1.
  253. attr_accessor :scale_integers
  254. # This defines the gap between markers on the Y axis,
  255. # default is a 10th of the max_value, e.g. you will have
  256. # 10 markers on the Y axis. NOTE: do not set this too
  257. # low - you are limited to 999 markers, after that the
  258. # graph won't generate.
  259. attr_accessor :scale_divisions
  260. # Whether to show the title under the X axis labels,
  261. # default is false, set to true to show.
  262. attr_accessor :show_x_title
  263. # What the title under X axis should be, e.g. 'Months'.
  264. attr_accessor :x_title
  265. # Whether to show the title under the Y axis labels,
  266. # default is false, set to true to show.
  267. attr_accessor :show_y_title
  268. # Aligns writing mode for Y axis label.
  269. # Defaults to :bt (Bottom to Top).
  270. # Change to :tb (Top to Bottom) to reverse.
  271. attr_accessor :y_title_text_direction
  272. # What the title under Y axis should be, e.g. 'Sales in thousands'.
  273. attr_accessor :y_title
  274. # Whether to show a title on the graph, defaults
  275. # to false, set to true to show.
  276. attr_accessor :show_graph_title
  277. # What the title on the graph should be.
  278. attr_accessor :graph_title
  279. # Whether to show a subtitle on the graph, defaults
  280. # to false, set to true to show.
  281. attr_accessor :show_graph_subtitle
  282. # What the subtitle on the graph should be.
  283. attr_accessor :graph_subtitle
  284. # Whether to show a key, defaults to false, set to
  285. # true if you want to show it.
  286. attr_accessor :key
  287. # Where the key should be positioned, defaults to
  288. # :right, set to :bottom if you want to move it.
  289. attr_accessor :key_position
  290. # Set the font size (in points) of the data point labels
  291. attr_accessor :font_size
  292. # Set the font size of the X axis labels
  293. attr_accessor :x_label_font_size
  294. # Set the font size of the X axis title
  295. attr_accessor :x_title_font_size
  296. # Set the font size of the Y axis labels
  297. attr_accessor :y_label_font_size
  298. # Set the font size of the Y axis title
  299. attr_accessor :y_title_font_size
  300. # Set the title font size
  301. attr_accessor :title_font_size
  302. # Set the subtitle font size
  303. attr_accessor :subtitle_font_size
  304. # Set the key font size
  305. attr_accessor :key_font_size
  306. # Show guidelines for the X axis
  307. attr_accessor :show_x_guidelines
  308. # Show guidelines for the Y axis
  309. attr_accessor :show_y_guidelines
  310. # Do not use CSS if set to true. Many SVG viewers do not support CSS, but
  311. # not using CSS can result in larger SVGs as well as making it impossible to
  312. # change colors after the chart is generated. Defaults to false.
  313. attr_accessor :no_css
  314. # Add popups for the data points on some graphs
  315. attr_accessor :add_popups
  316. protected
  317. def sort( *arrys )
  318. sort_multiple( arrys )
  319. end
  320. # Overwrite configuration options with supplied options. Used
  321. # by subclasses.
  322. def init_with config
  323. config.each { |key, value|
  324. self.send((key.to_s+"=").to_sym, value ) if respond_to? key.to_sym
  325. }
  326. end
  327. attr_accessor :top_align, :top_font, :right_align, :right_font
  328. KEY_BOX_SIZE = 12
  329. # Override this (and call super) to change the margin to the left
  330. # of the plot area. Results in @border_left being set.
  331. def calculate_left_margin
  332. @border_left = 7
  333. # Check for Y labels
  334. max_y_label_height_px = rotate_y_labels ?
  335. y_label_font_size :
  336. get_y_labels.max{|a,b|
  337. a.to_s.length<=>b.to_s.length
  338. }.to_s.length * y_label_font_size * 0.6
  339. @border_left += max_y_label_height_px if show_y_labels
  340. @border_left += max_y_label_height_px + 10 if stagger_y_labels
  341. @border_left += y_title_font_size + 5 if show_y_title
  342. end
  343. # Calculates the width of the widest Y label. This will be the
  344. # character height if the Y labels are rotated
  345. def max_y_label_width_px
  346. return font_size if rotate_y_labels
  347. end
  348. # Override this (and call super) to change the margin to the right
  349. # of the plot area. Results in @border_right being set.
  350. def calculate_right_margin
  351. @border_right = 7
  352. if key and key_position == :right
  353. val = keys.max { |a,b| a.length <=> b.length }
  354. @border_right += val.length * key_font_size * 0.6
  355. @border_right += KEY_BOX_SIZE
  356. @border_right += 10 # Some padding around the box
  357. end
  358. end
  359. # Override this (and call super) to change the margin to the top
  360. # of the plot area. Results in @border_top being set.
  361. def calculate_top_margin
  362. @border_top = 5
  363. @border_top += title_font_size if show_graph_title
  364. @border_top += 5
  365. @border_top += subtitle_font_size if show_graph_subtitle
  366. end
  367. # Adds pop-up point information to a graph.
  368. def add_popup( x, y, label )
  369. txt_width = label.length * font_size * 0.6 + 10
  370. tx = (x+txt_width > width ? x-5 : x+5)
  371. t = @foreground.add_element( "text", {
  372. "x" => tx.to_s,
  373. "y" => (y - font_size).to_s,
  374. "visibility" => "hidden",
  375. })
  376. t.attributes["style"] = "fill: #000; "+
  377. (x+txt_width > width ? "text-anchor: end;" : "text-anchor: start;")
  378. t.text = label.to_s
  379. t.attributes["id"] = t.object_id.to_s
  380. @foreground.add_element( "circle", {
  381. "cx" => x.to_s,
  382. "cy" => y.to_s,
  383. "r" => "10",
  384. "style" => "opacity: 0",
  385. "onmouseover" =>
  386. "document.getElementById(#{t.object_id}).setAttribute('visibility', 'visible' )",
  387. "onmouseout" =>
  388. "document.getElementById(#{t.object_id}).setAttribute('visibility', 'hidden' )",
  389. })
  390. end
  391. # Override this (and call super) to change the margin to the bottom
  392. # of the plot area. Results in @border_bottom being set.
  393. def calculate_bottom_margin
  394. @border_bottom = 7
  395. if key and key_position == :bottom
  396. @border_bottom += @data.size * (font_size + 5)
  397. @border_bottom += 10
  398. end
  399. if show_x_labels
  400. max_x_label_height_px = (not rotate_x_labels) ?
  401. x_label_font_size :
  402. get_x_labels.max{|a,b|
  403. a.to_s.length<=>b.to_s.length
  404. }.to_s.length * x_label_font_size * 0.6
  405. @border_bottom += max_x_label_height_px
  406. @border_bottom += max_x_label_height_px + 10 if stagger_x_labels
  407. end
  408. @border_bottom += x_title_font_size + 5 if show_x_title
  409. end
  410. # Draws the background, axis, and labels.
  411. def draw_graph
  412. @graph = @root.add_element( "g", {
  413. "transform" => "translate( #@border_left #@border_top )"
  414. })
  415. # Background
  416. @graph.add_element( "rect", {
  417. "x" => "0",
  418. "y" => "0",
  419. "width" => @graph_width.to_s,
  420. "height" => @graph_height.to_s,
  421. "class" => "graphBackground"
  422. })
  423. # Axis
  424. @graph.add_element( "path", {
  425. "d" => "M 0 0 v#@graph_height",
  426. "class" => "axis",
  427. "id" => "xAxis"
  428. })
  429. @graph.add_element( "path", {
  430. "d" => "M 0 #@graph_height h#@graph_width",
  431. "class" => "axis",
  432. "id" => "yAxis"
  433. })
  434. draw_x_labels
  435. draw_y_labels
  436. end
  437. # Where in the X area the label is drawn
  438. # Centered in the field, should be width/2. Start, 0.
  439. def x_label_offset( width )
  440. 0
  441. end
  442. def make_datapoint_text( x, y, value, style="" )
  443. if show_data_values
  444. @foreground.add_element( "text", {
  445. "x" => x.to_s,
  446. "y" => y.to_s,
  447. "class" => "dataPointLabel",
  448. "style" => "#{style} stroke: #fff; stroke-width: 2;"
  449. }).text = value.to_s
  450. text = @foreground.add_element( "text", {
  451. "x" => x.to_s,
  452. "y" => y.to_s,
  453. "class" => "dataPointLabel"
  454. })
  455. text.text = value.to_s
  456. text.attributes["style"] = style if style.length > 0
  457. end
  458. end
  459. # Draws the X axis labels
  460. def draw_x_labels
  461. stagger = x_label_font_size + 5
  462. if show_x_labels
  463. label_width = field_width
  464. count = 0
  465. for label in get_x_labels
  466. if step_include_first_x_label == true then
  467. step = count % step_x_labels
  468. else
  469. step = (count + 1) % step_x_labels
  470. end
  471. if step == 0 then
  472. text = @graph.add_element( "text" )
  473. text.attributes["class"] = "xAxisLabels"
  474. text.text = label.to_s
  475. x = count * label_width + x_label_offset( label_width )
  476. y = @graph_height + x_label_font_size + 3
  477. t = 0 - (font_size / 2)
  478. if stagger_x_labels and count % 2 == 1
  479. y += stagger
  480. @graph.add_element( "path", {
  481. "d" => "M#{x} #@graph_height v#{stagger}",
  482. "class" => "staggerGuideLine"
  483. })
  484. end
  485. text.attributes["x"] = x.to_s
  486. text.attributes["y"] = y.to_s
  487. if rotate_x_labels
  488. text.attributes["transform"] =
  489. "rotate( 90 #{x} #{y-x_label_font_size} )"+
  490. " translate( 0 -#{x_label_font_size/4} )"
  491. text.attributes["style"] = "text-anchor: start"
  492. else
  493. text.attributes["style"] = "text-anchor: middle"
  494. end
  495. end
  496. draw_x_guidelines( label_width, count ) if show_x_guidelines
  497. count += 1
  498. end
  499. end
  500. end
  501. # Where in the Y area the label is drawn
  502. # Centered in the field, should be width/2. Start, 0.
  503. def y_label_offset( height )
  504. 0
  505. end
  506. def field_width
  507. (@graph_width.to_f - font_size*2*right_font) /
  508. (get_x_labels.length - right_align)
  509. end
  510. def field_height
  511. (@graph_height.to_f - font_size*2*top_font) /
  512. (get_y_labels.length - top_align)
  513. end
  514. # Draws the Y axis labels
  515. def draw_y_labels
  516. stagger = y_label_font_size + 5
  517. if show_y_labels
  518. label_height = field_height
  519. count = 0
  520. y_offset = @graph_height + y_label_offset( label_height )
  521. y_offset += font_size/1.2 unless rotate_y_labels
  522. for label in get_y_labels
  523. y = y_offset - (label_height * count)
  524. x = rotate_y_labels ? 0 : -3
  525. if stagger_y_labels and count % 2 == 1
  526. x -= stagger
  527. @graph.add_element( "path", {
  528. "d" => "M#{x} #{y} h#{stagger}",
  529. "class" => "staggerGuideLine"
  530. })
  531. end
  532. text = @graph.add_element( "text", {
  533. "x" => x.to_s,
  534. "y" => y.to_s,
  535. "class" => "yAxisLabels"
  536. })
  537. text.text = label.to_s
  538. if rotate_y_labels
  539. text.attributes["transform"] = "translate( -#{font_size} 0 ) "+
  540. "rotate( 90 #{x} #{y} ) "
  541. text.attributes["style"] = "text-anchor: middle"
  542. else
  543. text.attributes["y"] = (y - (y_label_font_size/2)).to_s
  544. text.attributes["style"] = "text-anchor: end"
  545. end
  546. draw_y_guidelines( label_height, count ) if show_y_guidelines
  547. count += 1
  548. end
  549. end
  550. end
  551. # Draws the X axis guidelines
  552. def draw_x_guidelines( label_height, count )
  553. if count != 0
  554. @graph.add_element( "path", {
  555. "d" => "M#{label_height*count} 0 v#@graph_height",
  556. "class" => "guideLines"
  557. })
  558. end
  559. end
  560. # Draws the Y axis guidelines
  561. def draw_y_guidelines( label_height, count )
  562. if count != 0
  563. @graph.add_element( "path", {
  564. "d" => "M0 #{@graph_height-(label_height*count)} h#@graph_width",
  565. "class" => "guideLines"
  566. })
  567. end
  568. end
  569. # Draws the graph title and subtitle
  570. def draw_titles
  571. if show_graph_title
  572. @root.add_element( "text", {
  573. "x" => (width / 2).to_s,
  574. "y" => (title_font_size).to_s,
  575. "class" => "mainTitle"
  576. }).text = graph_title.to_s
  577. end
  578. if show_graph_subtitle
  579. y_subtitle = show_graph_title ?
  580. title_font_size + 10 :
  581. subtitle_font_size
  582. @root.add_element("text", {
  583. "x" => (width / 2).to_s,
  584. "y" => (y_subtitle).to_s,
  585. "class" => "subTitle"
  586. }).text = graph_subtitle.to_s
  587. end
  588. if show_x_title
  589. y = @graph_height + @border_top + x_title_font_size
  590. if show_x_labels
  591. y += x_label_font_size + 5 if stagger_x_labels
  592. y += x_label_font_size + 5
  593. end
  594. x = width / 2
  595. @root.add_element("text", {
  596. "x" => x.to_s,
  597. "y" => y.to_s,
  598. "class" => "xAxisTitle",
  599. }).text = x_title.to_s
  600. end
  601. if show_y_title
  602. x = y_title_font_size + (y_title_text_direction==:bt ? 3 : -3)
  603. y = height / 2
  604. text = @root.add_element("text", {
  605. "x" => x.to_s,
  606. "y" => y.to_s,
  607. "class" => "yAxisTitle",
  608. })
  609. text.text = y_title.to_s
  610. if y_title_text_direction == :bt
  611. text.attributes["transform"] = "rotate( -90, #{x}, #{y} )"
  612. else
  613. text.attributes["transform"] = "rotate( 90, #{x}, #{y} )"
  614. end
  615. end
  616. end
  617. def keys
  618. return @data.collect{ |d| d[:title] }
  619. end
  620. # Draws the legend on the graph
  621. def draw_legend
  622. if key
  623. group = @root.add_element( "g" )
  624. key_count = 0
  625. for key_name in keys
  626. y_offset = (KEY_BOX_SIZE * key_count) + (key_count * 5)
  627. group.add_element( "rect", {
  628. "x" => 0.to_s,
  629. "y" => y_offset.to_s,
  630. "width" => KEY_BOX_SIZE.to_s,
  631. "height" => KEY_BOX_SIZE.to_s,
  632. "class" => "key#{key_count+1}"
  633. })
  634. group.add_element( "text", {
  635. "x" => (KEY_BOX_SIZE + 5).to_s,
  636. "y" => (y_offset + KEY_BOX_SIZE).to_s,
  637. "class" => "keyText"
  638. }).text = key_name.to_s
  639. key_count += 1
  640. end
  641. case key_position
  642. when :right
  643. x_offset = @graph_width + @border_left + 10
  644. y_offset = @border_top + 20
  645. when :bottom
  646. x_offset = @border_left + 20
  647. y_offset = @border_top + @graph_height + 5
  648. if show_x_labels
  649. max_x_label_height_px = (not rotate_x_labels) ?
  650. x_label_font_size :
  651. get_x_labels.max{|a,b|
  652. a.to_s.length<=>b.to_s.length
  653. }.to_s.length * x_label_font_size * 0.6
  654. x_label_font_size
  655. y_offset += max_x_label_height_px
  656. y_offset += max_x_label_height_px + 5 if stagger_x_labels
  657. end
  658. y_offset += x_title_font_size + 5 if show_x_title
  659. end
  660. group.attributes["transform"] = "translate(#{x_offset} #{y_offset})"
  661. end
  662. end
  663. private
  664. def sort_multiple( arrys, lo=0, hi=arrys[0].length-1 )
  665. if lo < hi
  666. p = partition(arrys,lo,hi)
  667. sort_multiple(arrys, lo, p-1)
  668. sort_multiple(arrys, p+1, hi)
  669. end
  670. arrys
  671. end
  672. def partition( arrys, lo, hi )
  673. p = arrys[0][lo]
  674. l = lo
  675. z = lo+1
  676. while z <= hi
  677. if arrys[0][z] < p
  678. l += 1
  679. arrys.each { |arry| arry[z], arry[l] = arry[l], arry[z] }
  680. end
  681. z += 1
  682. end
  683. arrys.each { |arry| arry[lo], arry[l] = arry[l], arry[lo] }
  684. l
  685. end
  686. def style
  687. if no_css
  688. styles = parse_css
  689. @root.elements.each("//*[@class]") { |el|
  690. cl = el.attributes["class"]
  691. style = styles[cl]
  692. style += el.attributes["style"] if el.attributes["style"]
  693. el.attributes["style"] = style
  694. }
  695. end
  696. end
  697. def parse_css
  698. css = get_style
  699. rv = {}
  700. while css =~ /^(\.(\w+)(?:\s*,\s*\.\w+)*)\s*\{/m
  701. names_orig = names = $1
  702. css = $'
  703. css =~ /([^}]+)\}/m
  704. content = $1
  705. css = $'
  706. nms = []
  707. while names =~ /^\s*,?\s*\.(\w+)/
  708. nms << $1
  709. names = $'
  710. end
  711. content = content.tr( "\n\t", " ")
  712. for name in nms
  713. current = rv[name]
  714. current = current ? current+"; "+content : content
  715. rv[name] = current.strip.squeeze(" ")
  716. end
  717. end
  718. return rv
  719. end
  720. # Override and place code to add defs here
  721. def add_defs defs
  722. end
  723. def start_svg
  724. # Base document
  725. @doc = Document.new
  726. @doc << XMLDecl.new
  727. @doc << DocType.new( %q{svg PUBLIC "-//W3C//DTD SVG 1.0//EN" } +
  728. %q{"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"} )
  729. if style_sheet && style_sheet != ''
  730. @doc << Instruction.new( "xml-stylesheet",
  731. %Q{href="#{style_sheet}" type="text/css"} )
  732. end
  733. @root = @doc.add_element( "svg", {
  734. "width" => width.to_s,
  735. "height" => height.to_s,
  736. "viewBox" => "0 0 #{width} #{height}",
  737. "xmlns" => "http://www.w3.org/2000/svg",
  738. "xmlns:xlink" => "http://www.w3.org/1999/xlink",
  739. "xmlns:a3" => "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/",
  740. "a3:scriptImplementation" => "Adobe"
  741. })
  742. @root << Comment.new( " "+"\\"*66 )
  743. @root << Comment.new( " Created with SVG::Graph " )
  744. @root << Comment.new( " SVG::Graph by Sean E. Russell " )
  745. @root << Comment.new( " Losely based on SVG::TT::Graph for Perl by"+
  746. " Leo Lapworth & Stephan Morgan " )
  747. @root << Comment.new( " "+"/"*66 )
  748. defs = @root.add_element( "defs" )
  749. add_defs defs
  750. if not(style_sheet && style_sheet != '') and !no_css
  751. @root << Comment.new(" include default stylesheet if none specified ")
  752. style = defs.add_element( "style", {"type"=>"text/css"} )
  753. style << CData.new( get_style )
  754. end
  755. @root << Comment.new( "SVG Background" )
  756. @root.add_element( "rect", {
  757. "width" => width.to_s,
  758. "height" => height.to_s,
  759. "x" => "0",
  760. "y" => "0",
  761. "class" => "svgBackground"
  762. })
  763. end
  764. def calculate_graph_dimensions
  765. calculate_left_margin
  766. calculate_right_margin
  767. calculate_bottom_margin
  768. calculate_top_margin
  769. @graph_width = width - @border_left - @border_right
  770. @graph_height = height - @border_top - @border_bottom
  771. end
  772. def get_style
  773. return <<EOL
  774. /* Copy from here for external style sheet */
  775. .svgBackground{
  776. fill:#ffffff;
  777. }
  778. .graphBackground{
  779. fill:#f0f0f0;
  780. }
  781. /* graphs titles */
  782. .mainTitle{
  783. text-anchor: middle;
  784. fill: #000000;
  785. font-size: #{title_font_size}px;
  786. font-family: "Arial", sans-serif;
  787. font-weight: normal;
  788. }
  789. .subTitle{
  790. text-anchor: middle;
  791. fill: #999999;
  792. font-size: #{subtitle_font_size}px;
  793. font-family: "Arial", sans-serif;
  794. font-weight: normal;
  795. }
  796. .axis{
  797. stroke: #000000;
  798. stroke-width: 1px;
  799. }
  800. .guideLines{
  801. stroke: #666666;
  802. stroke-width: 1px;
  803. stroke-dasharray: 5 5;
  804. }
  805. .xAxisLabels{
  806. text-anchor: middle;
  807. fill: #000000;
  808. font-size: #{x_label_font_size}px;
  809. font-family: "Arial", sans-serif;
  810. font-weight: normal;
  811. }
  812. .yAxisLabels{
  813. text-anchor: end;
  814. fill: #000000;
  815. font-size: #{y_label_font_size}px;
  816. font-family: "Arial", sans-serif;
  817. font-weight: normal;
  818. }
  819. .xAxisTitle{
  820. text-anchor: middle;
  821. fill: #ff0000;
  822. font-size: #{x_title_font_size}px;
  823. font-family: "Arial", sans-serif;
  824. font-weight: normal;
  825. }
  826. .yAxisTitle{
  827. fill: #ff0000;
  828. text-anchor: middle;
  829. font-size: #{y_title_font_size}px;
  830. font-family: "Arial", sans-serif;
  831. font-weight: normal;
  832. }
  833. .dataPointLabel{
  834. fill: #000000;
  835. text-anchor:middle;
  836. font-size: 10px;
  837. font-family: "Arial", sans-serif;
  838. font-weight: normal;
  839. }
  840. .staggerGuideLine{
  841. fill: none;
  842. stroke: #000000;
  843. stroke-width: 0.5px;
  844. }
  845. #{get_css}
  846. .keyText{
  847. fill: #000000;
  848. text-anchor:start;
  849. font-size: #{key_font_size}px;
  850. font-family: "Arial", sans-serif;
  851. font-weight: normal;
  852. }
  853. /* End copy for external style sheet */
  854. EOL
  855. end
  856. end
  857. end
  858. end

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