Class: Chart::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/chart/line.rb

Constant Summary collapse

LINE_STYLE =
{
  "dash"      => ChartDirector::DashLine,
  "dot"       => ChartDirector::DotLine,
  "dotdash"   => ChartDirector::DotDashLine,
  "alternate" => ChartDirector::AltDashLine
}
LINE_SYMBOLS =
{
  "square"   => ChartDirector::SquareSymbol,
  "diamond"  => ChartDirector::DiamondSymbol,
  "triangle" => ChartDirector::TriangleSymbol,
  "circle"   => ChartDirector::CircleSymbol
}

Instance Attribute Summary

Attributes inherited from Base

#angle, #colors, #data, #extra_fields, #height, #inside, #label_format, #labels, #legend, #line_width, #margins, #multicolor, #object, #plot_area, #plot_top_margin, #position, #radius, #style, #symbols, #width

Instance Method Summary collapse

Methods inherited from Base

#helper, #initialize, #write

Constructor Details

This class inherits a constructor from Chart::Base

Instance Method Details

#generateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chart/line.rb', line 22

def generate
  # set the plot area
  @object.setPlotArea(@margins[0], @margins[1], @plot_area[0], @plot_area[1], 0xe9e9e9, 0xf5f5f5)
  
  # set the legend position
  @object.addLegend(@margins[0], 0, false, "normal", 8).setBackground(ChartDirector::Transparent)
  
  # set the top margin
  @object.yAxis.setTopMargin(@plot_top_margin)
  @object.xAxis.setMargin(10, 10)
  
  # set the labels
  @object.xAxis.setLabels(@labels)
  
  # create the layer
  layer = @object.addLineLayer2
  
  # set line width
  layer.setLineWidth(@line_width)
  
  # set label format
  layer.setDataLabelFormat(@label_format) unless label_format.nil?
  
  # add data to layer
  @data.each_with_index do |data, index|        
    # set line style or color
    if @style && LINE_STYLE[@style[index].to_s]
      line_style = @object.dashLineColor(@colors[index], LINE_STYLE[style[index].to_s])
    else
      line_style = @colors[index]
    end
    
    # add dataset
    dataset = layer.addDataSet(data, line_style, legend[index])
    
    # set symbol
    symbol = LINE_SYMBOLS[@symbols[index]]
    
    if symbol
      dataset.setDataSymbol(symbol, 7, -1, @colors[index], 1)
    elsif @symbols[index] && File.file?(@symbols[index])
      dataset.setDataSymbol2(@symbols[index])
    end
  end
end

#instantiateObject



17
18
19
20
# File 'lib/chart/line.rb', line 17

def instantiate
  # instantiate a new chart
  @object = ChartDirector::XYChart.new(@width, @height) if @width && @height
end