Class: Charty::Backends::JFreeChart

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/backends/jfreechart.rb,
lib/charty/backends/jfreechart/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Constructor Details

#initializeJFreeChart

Returns a new instance of JFreeChart.



19
20
21
# File 'lib/charty/backends/jfreechart.rb', line 19

def initialize
  @plot = ::ChartFactory
end

Instance Method Details

#label(x, y) ⇒ Object



23
24
# File 'lib/charty/backends/jfreechart.rb', line 23

def label(x, y)
end

#plot(plot, context, subplot: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/charty/backends/jfreechart.rb', line 52

def plot(plot, context, subplot: false)
  case context.method
  when :bar
    dataset = DefaultCategoryDataset.new
    context.series.each do |data|
      data.ys.to_a.zip(Array.new(data.xs.size, data.label.to_s), data.xs.to_a.map(&:to_s)).each do |d|
        dataset.addValue(*d)
      end
    end
    title = context.title || ""
    xlabel = context.xlabel || ""
    ylabel = context.ylabel || ""
    plot.createBarChart(
      title,
      xlabel,
      ylabel,
      dataset,
      PlotOrientation::VERTICAL,
      true,
      false,
      false
    )
  when :barh
    dataset = DefaultCategoryDataset.new
    context.series.each do |data|
      data.ys.to_a.zip(Array.new(data.xs.size, data.label.to_s), data.xs.to_a.map(&:to_s)).each do |d|
        dataset.addValue(*d)
      end
    end
    title = context.title || ""
    xlabel = context.xlabel || ""
    ylabel = context.ylabel || ""
    plot.createBarChart(
      title,
      xlabel,
      ylabel,
      dataset,
      PlotOrientation::HORIZONTAL,
      true,
      false,
      false
    )
  when :box_plot
    # todo:
    raise NotImplementedError
  when :bubble
    dataset = MatrixSeriesCollection.new
    context.series.each do |data|
      # todo: fix args
      series = MatrixSeries.new(data.label.to_s, 1000, 1000)
      data.xs.to_a.zip(data.ys.to_a, data.zs.to_a).each do |series_param|
        series.update(*series_param)
      end
      dataset.addSeries(series)
    end
    title = context.title || ""
    xlabel = context.xlabel || ""
    ylabel = context.ylabel || ""
    plot.createBubbleChart(
      title,
      xlabel,
      ylabel,
      dataset,
      PlotOrientation::VERTICAL,
      true,
      false,
      false
    )
  when :curve
    dataset = DefaultCategoryDataset.new
    context.series.each do |data|
      data.xs.to_a.zip(data.ys.to_a).each do |a|
        dataset.addValue(a[1], data.label.to_s, a[0])
      end
    end
    title = context.title || ""
    xlabel = context.xlabel || ""
    ylabel = context.ylabel || ""
    plot.createLineChart(
      title,
      xlabel,
      ylabel,
      dataset,
      PlotOrientation::VERTICAL,
      true,
      false,
      false
    )
  when :scatter
    dataset = XYSeriesCollection.new
    context.series.each do |data|
      series = XYSeries.new(data.label.to_s)
      data.xs.to_a.zip(data.ys.to_a).each do |series_param|
        series.add(*series_param)
      end
      dataset.addSeries(series)
    end
    title = context.title || ""
    xlabel = context.xlabel || ""
    ylabel = context.ylabel || ""
    plot.createScatterPlot(
      title,
      xlabel,
      ylabel,
      dataset,
      PlotOrientation::VERTICAL,
      true,
      false,
      false
    )
  when :error_bar
    raise NotImplementedError
  when :hist
    # todo:
    raise NotImplementedError
  end
end

#render(context, filename) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/charty/backends/jfreechart.rb', line 34

def render(context, filename)
  # todo: I'm not sure if I can only render.

  chart = plot(@plot, context)
  # todo: add options for width and height
  width = 1000
  height = 800
  if filename
    FileUtils.mkdir_p(File.dirname(filename))
    javafile = java.io.File.new(filename)
    ChartUtilities.saveChartAsPNG(javafile, chart, width, height)
  end
end

#render_layout(layout) ⇒ Object

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/charty/backends/jfreechart.rb', line 30

def render_layout(layout)
  raise NotImplementedError
end

#save(context, filename) ⇒ Object



48
49
50
# File 'lib/charty/backends/jfreechart.rb', line 48

def save(context, filename)
  # todo:
end

#series=(series) ⇒ Object



26
27
28
# File 'lib/charty/backends/jfreechart.rb', line 26

def series=(series)
  @series = series
end