Method: Scale#draw_xy_scale
- Defined in:
- lib/scale.rb
#draw_xy_scale(data, data_description, y_serie_name, x_serie_name, r, g, b, with_margin = 0, angle = 0, decimals = 1) ⇒ Object
This function is used by scatter charts. It will compute everything needed to draw the associated line and plot charts. You must specify the name of the two series that will be used as X and Y data. By default this function will compute the min & max values of both series, anyway you can override the automatic scaling by calling first the setFixedScale function.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/scale.rb', line 279 def draw_xy_scale(data,data_description,y_serie_name,x_serie_name,r,g,b,with_margin=0,angle=0,decimals=1) self.validate_data("draw_xy_scale",data) c_text_color = allocate_color(@picture,r,g,b) self.draw_line(@g_area_x1,@g_area_y1,@g_area_x1,@g_area_y2,r,g,b) self.draw_line(@g_area_x1,@g_area_y2,@g_area_x2,@g_area_y2,r,g,b) # Process Y scale */ if(@vmin.nil? && @vmax.nil?) @vmin = data[0][y_serie_name] @vmax = data[0][y_serie_name] data.each do |key| if !key[y_serie_name].nil? value = key[y_serie_name] if (value.is_a?(Numeric)) @vmax = value if ( @vmax < value) @vmin = value if ( @vmin > value) end end end if(@vmax.is_a?(String)) @vmax = @vmax.gsub(/\.[0-9]+/,'')+1 if (@vmax > @vmax.gsub(/\.[0-9]+/,'') ) end data_range = @vmax - @vmin data_range = 0.1 if (data_range.to_f == 0.0 ) #Compute automatic scaling scale_ok = false factor = 1 min_div_height = 25 max_divs = (@g_area_y2 - @g_area_y1)*1.0 / min_div_height if (@vmin == 0 && @vmax == 0 ) @vmin = 0 @vmax = 2 scale = 1 divisions = 2 elsif (max_divs > 1) while(!scale_ok) scale1 = ( @vmax - @vmin )*1.0 / factor scale2 = ( @vmax - @vmin )*1.0 /factor / 2 scale4 = ( @vmax - @vmin )*1.0 / factor / 4 if ( scale1 > 1 && scale1 <= max_divs && !scale_ok) scale_ok = true divisions = (scale1).floor scale = 1 end if ( scale2 > 1 && scale2 <= max_divs && !scale_ok) scale_ok = true divisions = (scale2).floor scale = 2 end if (!scale_ok) factor = factor * 10.0 if ( scale2 > 1 ) factor = factor / 10.0 if ( scale2 < 1 ) end end if ((((@vmax*1.0 / scale) / factor)).floor != ((@vmax*1.0 / scale) / factor)) grid_id = ( @vmax*1.0 / scale / factor).floor + 1 @vmax = grid_id * scale * factor divisions = divisions+1 end if (((@vmin*1.0 / scale) / factor).floor != ((@vmin*1.0 / scale) / factor)) grid_id = ( @vmin*1.0 / scale / factor).floor @vmin = grid_id * scale * factor*1.0 divisions = divisions+1 end else #/* Can occurs for small graphs */ scale = 1 end divisions = 2 if ( divisions.nil? ) if ( is_real_int((@vmax-@vmin)/(divisions-1))) divisions-=1 elsif ( is_real_int((@vmax-@vmin)/(divisions+1))) divisions+=1 end else divisions = @divisions end @division_count = divisions data_range = @vmax - @vmin data_range = 0.1 if (data_range.to_f == 0.0 ) @division_height = ( @g_area_y2 - @g_area_y1 )*1.0 / divisions @division_ratio = ( @g_area_y2 - @g_area_y1 )*1.0 /data_range ypos = @g_area_y2 xmin = nil i =1 while(i<= divisions+1) self.draw_line(@g_area_x1,ypos,@g_area_x1-5,ypos,r,g,b) value = @vmin*1.0 + (i-1) * (( @vmax - @vmin ) / divisions) value = value.round(decimals) value = value.round if value.floor == value.ceil value = "#{value} #{data_description['unit']['y']}" if ( data_description["format"]["y"]== "number") value = self.to_time(value) if ( data_description["format"]["y"] == "time" ) value = self.to_date(value) if ( data_description["format"]["y"] == "date" ) value = self.to_metric(value) if ( data_description["format"]["Y"] == "metric" ) value = self.to_currency(value) if ( data_description["format"]["Y"] == "currency" ) position = image_ftb_box(@font_size,0,@font_name,value) text_width =position[2]-position[0] image_ttf_text(@picture,@font_size,0,@g_area_x1-10-text_width,ypos+(@font_size/2),c_text_color,@font_name,value) xmin = @g_area_x1-10-text_width if ( xmin.nil? || xmin > @g_area_x1-10-text_width) ypos = ypos - @division_height i = i+1 end # Process X scale */ if(@v_x_min.nil? && @v_x_max.nil?) @v_x_min =data[0][x_serie_name] @v_x_max =data[0][x_serie_name] data.each do |key| if !key[x_serie_name].nil? value = key[x_serie_name] if (value.is_a?(Numeric)) @v_x_max = value if ( @v_x_max < value) @v_x_min = value if ( @v_x_min > value) end end end if (@v_x_max.is_a?(String)) @v_x_max = @v_x_max.gsub(/\.[0-9]+/,'')+1 if (@v_x_max > @v_x_max.gsub(/\.[0-9]+/,'') ) end data_range = @vmax - @vmin data_range = 0.1 if (data_range.to_f == 0.0 ) # Compute automatic scaling scale_ok = false factor = 1 min_div_width = 25 max_divs = (@g_area_x2 - @g_area_x1) / min_div_width if ( @v_x_min == 0 && @v_x_max == 0 ) @v_x_min = 0 @v_x_max = 2 scale = 1 x_divisions = 2 elsif (max_divs > 1) while(!scale_ok) scale1 = ( @v_x_max - @v_x_min ) / factor scale2 = ( @v_x_max - @v_x_min ) / factor / 2 scale4 = ( @v_x_max - @v_x_min ) / factor / 4 if ( scale1 > 1 && scale1 <= max_divs && !scale_ok) scale_ok = true x_divisions = (scale1).floor scale = 1 end if ( scale2 > 1 && scale2 <= max_divs && !scale_ok) scale_ok = true x_divisions = (scale2).floor scale = 2 end if (!scale_ok) factor = factor * 10 if ( scale2 > 1 ) factor = factor / 10 if ( scale2 < 1 ) end end if ( (@v_x_max*1.0 / scale / factor).floor != @v_x_max / scale / factor) grid_id = ( @v_x_max*1.0 / scale / factor).floor + 1 @v_x_max = grid_id * scale * factor x_divisions+=1 end if ( (@v_x_min*1.0 / scale / factor).floor != @v_x_min / scale / factor) grid_id = ( @v_x_min / scale / factor).floor @v_x_min = grid_id * scale * factor x_divisions+=1 end else #/* Can occurs for small graphs */ scale = 1 end x_divisions = 2 if ( x_divisions.nil? ) if ( is_real_int((@v_x_max-@v_x_min)/(x_divisions-1))) x_divisions-=1 elsif ( is_real_int((@v_x_max-@v_x_min)/(x_divisions+1))) x_divisions+=1 end else x_divisions = @x_divisions end @x_division_count = divisions @data_count = divisions + 2 x_data_range = @v_x_max - @v_x_min x_data_range = 0.1 if ( x_data_range == 0 ) @division_width = ( @g_area_x2 - @g_area_x1 ) / x_divisions @x_division_ratio = ( @g_area_x2 - @g_area_x1 ) / x_data_range xpos = @g_area_x1 ymax =nil i=1 while(i<= x_divisions+1) self.draw_line(xpos,@g_area_y2,xpos,@g_area_y2+5,r,g,b) value = @v_x_min + (i-1) * (( @v_x_max - @v_x_min ) / x_divisions) value = value.round(decimals) value = value.round if value.floor == value.ceil value = "#{value}#{data_description['unit']['y']}" if ( data_description["format"]["y"] == "number") value = self.to_time(value) if ( data_description["format"]["y"] == "time" ) value = self.to_date(value) if ( data_description["format"]["y"] == "date" ) value = self.to_metric(value) if ( data_description["format"]["Y"] == "metric" ) value = self.to_currency(value) if ( data_description["format"]["Y"] == "currency" ) position = image_ftb_box(@font_size,angle,@font_name,value) text_width =position[2].abs+position[0].abs text_height = position[1].abs+position[3].abs if ( angle == 0 ) ypos = @g_area_y2+18 image_ttf_text(@picture,@font_size,angle,(xpos).floor-(text_width/2).floor,ypos,c_text_color,@font_name,value) else ypos = @g_area_y2+10+text_height if ( angle <= 90 ) image_ttf_text(@picture,@font_size,angle,(xpos).floor-text_width+5,ypos,c_text_color,@font_name,value) else image_ttf_text(@picture,@font_size,angle,(xpos).floor+text_width+5,ypos,c_text_color,@font_name,value) end end ymax = ypos if (ymax.nil? || ymax < ypos) i=i+1 xpos = xpos + @division_width end #Write the Y Axis caption if set if ((!data_description["axis"].nil? && !data_description["axis"]["y"].nil?) ) position = image_ftb_box(@font_size,90,@font_name,data_description["axis"]["y"]) text_height = (position[1]).abs+(position[3]).abs text_top = ((@g_area_y2 - @g_area_y1) / 2) + @g_area_y1 + (text_width/2) image_ttf_text(@picture,@font_size,90,xmin-@font_size,text_top,c_text_color,@font_name,data_description["axis"]["y"].to_s) end if ((!data_description["axis"].nil? && !data_description["axis"]["x"].nil?) ) position = image_ftb_box(@font_size,90,@font_name,data_description["axis"]["x"]) text_width = (position[2]).abs+(position[0]).abs text_left = ((@g_area_x2 - @g_area_x1) / 2) + @g_area_x1 + (text_width/2) image_ttf_text(@picture,@font_size,0,text_left,ymax+@font_size+5,c_text_color,@font_name,data_description["axis"]["x"].to_s) end end |