Skip to content

Commit

Permalink
add some experimentation with jfreecharts in SWT.
Browse files Browse the repository at this point in the history
  • Loading branch information
sundbp committed Nov 27, 2010
1 parent 5093f49 commit f98aa9c
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 0 deletions.
133 changes: 133 additions & 0 deletions jfc_test/ts_chart_demo.rb
@@ -0,0 +1,133 @@
require 'java'
require 'swt/swt_wrapper'
require 'jfreechart/jfreechart_wrapper'

puts "Starting demo!"

class TimeSeriesExample

def initialize
# A Display is the connection between SWT and the native GUI. (jruby-swt-cookbook/apidocs/org/eclipse/swt/widgets/Display.html)
display = Swt::Widgets::Display.get_current

# A Shell is a window in SWT parlance. (jruby-swt-cookbook/apidocs/org/eclipse/swt/widgets/Shell.html)
@shell = Swt::Widgets::Shell.new

# A Shell must have a layout. FillLayout is the simplest.
layout = Swt::Layout::FillLayout.new
@shell.setLayout(layout)

# create a jfreechart!
chart = create_chart(create_dataset)
frame = Jfree::Experimental::Chart::Swt::ChartComposite.new(@shell, Swt::SWT::NONE, chart, true);
frame.setDisplayToolTips(true);
frame.setHorizontalAxisTrace(false);
frame.setVerticalAxisTrace(false);

@shell.setSize(1000, 500);
@shell.setText("Time series demo for jfreechart running with SWT");

# This lays out the widgets in the Shell
#@shell.pack

# And this displays the Shell
@shell.open
end

def create_chart(dataset)
chart = Jfree::Chart::ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", # title
"Date", # x-axis label
"Price Per Unit", # y-axis label
dataset, # data
true, # create legend?
true, # generate tooltips?
false # generate URLs?
);

chart.setBackgroundPaint(java.awt.Color.white);

plot = chart.getPlot();
plot.setBackgroundPaint(java.awt.Color.lightGray);
plot.setDomainGridlinePaint(java.awt.Color.white);
plot.setRangeGridlinePaint(java.awt.Color.white);
plot.setAxisOffset(Jfree::Ui::RectangleInsets.new(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);

renderer = plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);

axis = plot.getDomainAxis();
axis.setDateFormatOverride(java.text.SimpleDateFormat.new("MMM-yyyy"));

chart
end

def create_dataset
s1 = Jfree::Data::Time::TimeSeries.new("L&G European Index Trust")
s1.add(Jfree::Data::Time::Month.new(2, 2001), 181.8)
s1.add(Jfree::Data::Time::Month.new(3, 2001), 167.3)
s1.add(Jfree::Data::Time::Month.new(4, 2001), 153.8)
s1.add(Jfree::Data::Time::Month.new(5, 2001), 167.6)
s1.add(Jfree::Data::Time::Month.new(6, 2001), 158.8)
s1.add(Jfree::Data::Time::Month.new(7, 2001), 148.3)
s1.add(Jfree::Data::Time::Month.new(8, 2001), 153.9)
s1.add(Jfree::Data::Time::Month.new(9, 2001), 142.7)
s1.add(Jfree::Data::Time::Month.new(10, 2001), 123.2)
s1.add(Jfree::Data::Time::Month.new(11, 2001), 131.8)
s1.add(Jfree::Data::Time::Month.new(12, 2001), 139.6)
s1.add(Jfree::Data::Time::Month.new(1, 2002), 142.9)
s1.add(Jfree::Data::Time::Month.new(2, 2002), 138.7)
s1.add(Jfree::Data::Time::Month.new(3, 2002), 137.3)
s1.add(Jfree::Data::Time::Month.new(4, 2002), 143.9)
s1.add(Jfree::Data::Time::Month.new(5, 2002), 139.8)
s1.add(Jfree::Data::Time::Month.new(6, 2002), 137.0)
s1.add(Jfree::Data::Time::Month.new(7, 2002), 132.8)

s2 = Jfree::Data::Time::TimeSeries.new("L&G UK Index Trust")
s2.add(Jfree::Data::Time::Month.new(2, 2001), 129.6)
s2.add(Jfree::Data::Time::Month.new(3, 2001), 123.2)
s2.add(Jfree::Data::Time::Month.new(4, 2001), 117.2)
s2.add(Jfree::Data::Time::Month.new(5, 2001), 124.1)
s2.add(Jfree::Data::Time::Month.new(6, 2001), 122.6)
s2.add(Jfree::Data::Time::Month.new(7, 2001), 119.2)
s2.add(Jfree::Data::Time::Month.new(8, 2001), 116.5)
s2.add(Jfree::Data::Time::Month.new(9, 2001), 112.7)
s2.add(Jfree::Data::Time::Month.new(10, 2001), 101.5)
s2.add(Jfree::Data::Time::Month.new(11, 2001), 106.1)
s2.add(Jfree::Data::Time::Month.new(12, 2001), 110.3)
s2.add(Jfree::Data::Time::Month.new(1, 2002), 111.7)
s2.add(Jfree::Data::Time::Month.new(2, 2002), 111.0)
s2.add(Jfree::Data::Time::Month.new(3, 2002), 109.6)
s2.add(Jfree::Data::Time::Month.new(4, 2002), 113.2)
s2.add(Jfree::Data::Time::Month.new(5, 2002), 111.6)
s2.add(Jfree::Data::Time::Month.new(6, 2002), 108.8)
s2.add(Jfree::Data::Time::Month.new(7, 2002), 101.6)

dataset = Jfree::Data::Time::TimeSeriesCollection.new
dataset.addSeries(s1)
dataset.addSeries(s2)

dataset
end

# This is the main gui event loop
def start
display = Swt::Widgets::Display.get_current

# until the window (the Shell) has been closed
while !@shell.isDisposed
# check for and dispatch new gui events
display.sleep unless display.read_and_dispatch
end
display.dispose
end
end

Swt::Widgets::Display.set_app_name "Timeseries Example"

app = TimeSeriesExample.new
app.start
exit
Binary file added jfreechart/gnujaxp.jar
Binary file not shown.
Binary file added jfreechart/iText-2.1.5.jar
Binary file not shown.
Binary file added jfreechart/jcommon-1.0.16.jar
Binary file not shown.
Binary file added jfreechart/jfreechart-1.0.13-experimental.jar
Binary file not shown.
Binary file added jfreechart/jfreechart-1.0.13-swt.jar
Binary file not shown.
Binary file added jfreechart/jfreechart-1.0.13.jar
Binary file not shown.
66 changes: 66 additions & 0 deletions jfreechart/jfreechart_wrapper.rb
@@ -0,0 +1,66 @@
require 'java'
require 'swt/swt_wrapper'

module Jfree

def self.jars
[
'jcommon-1.0.16',
'swtgraphics2d',
'jfreechart-1.0.13',
'jfreechart-1.0.13-swt',
'jfreechart-1.0.13-experimental',
'iText-2.1.5'
]
end

Jfree.jars.each do |jar|
path = File.expand_path(File.dirname(__FILE__) + "/../jfreechart/" + jar)
if File.exist?(path + ".jar")
puts "loading #{jar}"
require path
else
raise "jar file required: #{path}.jar"
end
end

module Chart
import org.jfree.chart.ChartFactory
import org.jfree.chart.JFreeChart
module Axis
import org.jfree.chart.axis.DateAxis
end
module Plot
import org.jfree.chart.plot.XYPlot
end
module Renderer
module Xy
import org.jfree.chart.renderer.xy.XYItemRenderer
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer
end
end
end

module Data
module Time
import org.jfree.data.time.Month
import org.jfree.data.time.TimeSeries
import org.jfree.data.time.TimeSeriesCollection
end
module Xy
import org.jfree.data.xy.XYDataset
end
end

module Experimental
module Chart
module Swt
import org.jfree.experimental.chart.swt.ChartComposite
end
end
end

module Ui
import org.jfree.ui.RectangleInsets
end
end
Binary file added jfreechart/junit.jar
Binary file not shown.
Binary file added jfreechart/servlet.jar
Binary file not shown.
Binary file added jfreechart/swtgraphics2d.jar
Binary file not shown.

0 comments on commit f98aa9c

Please sign in to comment.