J4L-CHART for Ruby

Copyright 2011, J4L Components (http://www.java4less.com)
Go bak to contents


The Chart API

 

Chart creation process

If you need it you can create charts using the API the process is:

1. Import our files require 'chart_loader.rb'
require 'chart_image.rb'
require 'chart_graphics.rb'
require 'graphics_provider.rb'
2. Create the title title = J4LChart::Title.new("Sales (thousands $)")
3. Create the axis you need (depends on the chart, for example piecharts do not have axis) XAxis= J4LChart::Axis.new(J4LChart::HORIZONTAL, J4LChart::Scale.new())
YAxis= J4LChart::Axis.new(J4LChart::VERTICAL, J4LChart::Scale.new())
XAxis.scale.min=0
YAxis.scale.min=0
.....
4. Create the legend l= J4LChart::Legend.new()
l.addItem("Products", J4LChart::FillStyle.new ( J4LChart::GraphicsProvider.getcolor( J4LChart::BLUE)) ))
l.addItem("Services", J4LChart::FillStyle.new( J4LChart::GraphicsProvider.getcolor( J4LChart::GREEN)) ))
5. Create the axis labels

XLabel= J4LChart::HAxisLabel.new("", J4LChart::GraphicsProvider.getcolor( J4LChart::BLUE)), J4LChart::GraphicsProvider.getfont("Arial", J4LChart::BOLD,14))

YLabel=  J4LChart::VAxisLabel.new("Brutto", J4LChart::GraphicsProvider.getcolor( J4LChart::BLACK)), J4LChart::GraphicsProvider.getfont("Arial",J4LChart::BOLD,14))

6. create the plotter (or plotters if you combine lines and bars) plot= J4LChart::LinePlotter3D.new()
7. create the chart and set properties (labels,legend) chart= J4LChart::Chart.new()
chart.resetChart(title,plot,XAxis,YAxis)
chart.XLabel=XLabel
chart.YLabel=YLabel
chart.legend=l
8. create the data series d1=[1,1,3,3.5,5,4,2]
data1= J4LChart::LineDataSerie.new(d1, J4LChart::LineStyle.new(0.2, J4LChart::GraphicsProvider.getcolor(J4LChart::BLUE)) ,J4LChart::LINE_NORMAL))
9. add serie to chart chart.addSerie(data1)
   

 

 

The ChartLoader

You can use the chart loader to create the charts using the parameters:

require 'chart_loader.rb'
require 'chart_image.rb'
require 'chart_graphics.rb'
require 'graphics_provider.rb'
require 'chart_examples.rb'

require 'rubygems'
require 'RMagick'

cha=J4LChart::ChartLoader.new()
cha.clearParams()
cha.setParameter("TITLECHART","Sales 1999")
cha.setParameter("LEGEND","TRUE")
cha.setParameter("XLABEL","Month")
cha.setParameter("YLABEL","Million $")
cha.setParameter("XAXIS_LABELS","June|July|Aug.|Sept.|Oct.|Nov.|Dec.")
cha.setParameter("SERIE_1","Products")
cha.setParameter("SERIE_2","Services")
cha.setParameter("SERIE_STYLE_1","2|#ff0000|LINE")
cha.setParameter("SERIE_STYLE_2","2|#000055|LINE")
cha.setParameter("SERIE_FILL_2","#000099")
cha.setParameter("SERIE_FONT_1","DEFAULT-3")
cha.setParameter("SERIE_FONT_2","DEFAULT-3")
cha.setParameter("SERIE_POINT_1","TRUE")
cha.setParameter("SERIE_POINT_2","TRUE")
cha.setParameter("SERIE_DATA_2","12|80|200|450|300|320|420")
cha.setParameter("SERIE_DATA_1","32|140|700|1300|760|760|930")
cha.setParameter("SERIE_TYPE_1","LINE")
cha.setParameter("SERIE_TYPE_2","LINE")
cha.setParameter("CHART_BORDER","0.2|BLACK|LINE")
cha.setParameter("CHART_FILL","YELLOW")
cha.setParameter("BIG_TICK_INTERVALX","1")
cha.setParameter("BIG_TICK_INTERVALY","1")
cha.setParameter("TICK_INTERVALY","100")
cha.setParameter("LEGEND_BORDER","1|BLACK|LINE")
cha.setParameter("LEGEND_FILL","WHITE")
cha.setParameter("XAXIS_TICKATBASE","true")
cha.setParameter("YSCALE_LOG","FALSE")
cha.setParameter("YSCALE_MIN","0")
cha.setParameter("YSCALE_MAX","1000")
cha.setParameter("SAVE","1")
cha.setParameter("SERIE_SECONDYAXIS_1","FALSE")
cha.setParameter("SERIE_BORDER_TYPE_1","NORMAL")
cha.setParameter("SERIE_DRAW_LINE_1","TRUE")
cha.setParameter("SERIE_LINE_TYPE_1","NORMAL")
cha.setParameter("SERIE_NEEDLE_STYLE_1","3|#000000|NORMAL")
cha.setParameter("SERIE_NEEDLE_LENTGH_1","80")
cha.setParameter("SERIE_BUBBLE_1","FALSE")
cha.setParameter("SERIE_FILL_BUBBLE_1","FALSE")
cha.setParameter("SERIE_LINE_END_1","FALSE")
cha.setParameter("NODE","SERIE2")
cha.setParameter("YLABEL_VERTICAL","FALSE")
cha.setParameter("YAXIS","TRUE")
cha.setParameter("YAXIS_TICKATBASE","FALSE")
cha.setParameter("GRIDY","FALSE")
cha.setParameter("YAXIS_START_WITH_BIG_TICK","FALSE")
cha.setParameter("CERO_YAXIS","NO")
cha.setParameter("YAXIS_INTEGER","TRUE")
cha.setParameter("YAXIS_VERTICAL_LABELS","FALSE")
cha.setParameter("YAXIS_TICK_TEXT_LINE","0")
cha.setParameter("YAXIS_CLOSED","FALSE")
cha.setParameter("YSCALE_EXACT_MAX","FALSE")
cha.setParameter("YSCALE_EXACT_MIN","TRUE")
cha.setParameter("SERIE_SECONDYAXIS_2","FALSE")
cha.setParameter("SERIE_BORDER_TYPE_2","NORMAL")
cha.setParameter("SERIE_DRAW_LINE_2","TRUE")
cha.setParameter("SERIE_LINE_TYPE_2","NORMAL")
cha.setParameter("SERIE_NEEDLE_STYLE_2","3|#000000|NORMAL")
cha.setParameter("SERIE_NEEDLE_LENTGH_2","80")
cha.setParameter("SERIE_BUBBLE_2","FALSE")
cha.setParameter("SERIE_FILL_BUBBLE_2","FALSE")
cha.setParameter("SERIE_LINE_END_2","FALSE")
cha.setParameter("LINECHART_FIXED_LIMITS","TRUE")
cha.setParameter("LINECHART_POINT_SIZE","6")

c=cha.build(false,false)

c.saveToFile("ChartLineClipping.png")

 

The steps are:

  1. Create the chart loader
  2. Set the parameters or load them from file
  3. build the chart
  4. export the chart to an image file