Method: Writeexcel::Worksheet#insert_chart

Defined in:
lib/writeexcel/worksheet.rb

#insert_chart(*args) ⇒ Object

:call-seq:

insert_chart(row, col,   chart, x, y, scale_x, scale_y)

Insert a chart into a worksheet. The $chart argument should be a Chart object or else it is assumed to be a filename of an external binary file. The latter is for backwards compatibility.

This method can be used to insert a Chart object into a worksheet. The Chart must be created by the add_chart() Workbook method and it must have the embedded option set.

chart = workbook.add_chart(:type => 'Chart::Line', :embedded => true )

# Configure the chart.
...

# Insert the chart into the a worksheet.
worksheet.insert_chart('E2', chart)

See add_chart() for details on how to create the Chart object and WriteExcel::Chart for details on how to configure it. See also the chart_*.pl programs in the examples directory of the distro.

The x, y, scale_x and scale_y parameters are optional.

The parameters x and y can be used to specify an offset from the top left hand corner of the cell specified by row and col. The offset values are in pixels. See the insert_image method above for more information on sizes.

worksheet1.insert_chart('E2', chart, 3, 3)

The parameters scale_x and scale_y can be used to scale the inserted image horizontally and vertically:

Scale the width by 120% and the height by 150%

worksheet.insert_chart('E2', chart, 0, 0, 1.2, 1.5)

The easiest way to calculate the required scaling is to create a test chart worksheet with WriteExcel. Then open the file, select the chart and drag the corner to get the required size. While holding down the mouse the scale of the resized chart is shown to the left of the formula bar.

Note: you must call set_row() or set_column() before insert_chart() if you wish to change the default dimensions of any of the rows or columns that the chart occupies. The height of a row can also change if you use a font that is larger than the default. This in turn will affect the scaling of your chart. To avoid this you should explicitly set the height of the row using set_row() if it contains a font size that will change the row height.



3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
# File 'lib/writeexcel/worksheet.rb', line 3714

def insert_chart(*args)
  # Check for a cell reference in A1 notation and substitute row and column
  args = row_col_notation(args)

  chart       = args[2]

  if chart.respond_to?(:embedded)
    print "Not a embedded style Chart object in insert_chart()" unless chart.embedded
  else
    # Assume an external bin filename.
      print "Couldn't locate #{chart} in insert_chart()" unless FileTest.exist?(chart)
  end

  @charts << EmbeddedChart.new(self, *args)
end