Class: DYI::Chart::PieChart

Inherits:
Base
  • Object
show all
Includes:
Legend
Defined in:
lib/dyi/chart/pie_chart.rb

Overview

PieChart creates the image of pie chart.

Basic Usage

Using PieChart and ArrayReader (or sub class of ArrayReader), you can create the pie chart as the following:

require 'rubygems'
require 'dyi'

# Nominal GDP of Asian Countries (2010)
chart_data = [['China', 5878],
              ['Japan', 5459],
              ['India', 1538],
              ['South Koria', 1007],
              ['Other Countries', 2863]]
reader = DYI::Chart::ArrayReader.read(chart_data, :schema => [:name, :value])

# Creates the Pie Chart
chart = DYI::Chart::PieChart.new(450,250)
chart.load_data(reader)
chart.save('asian_gdp.svg')

See ArrayReader about how to set the chart data.

The chart options of PieChart are specified at the constractor. See Instance Attribute of this class, Base and Legend for the attributes that can be specified. The specified attributes can be refered and set.

# Creates the Pie Chart
chart = DYI::Chart::PieChart.new(500,250,
          :center_point => [130, 100],
          :legend_point => [250, 50],
          :represent_3d => true,
          :_3d_settings => {:dy => 20},
          :legend_format => "{?name}\t{!e}{?value:#,0}\t{!e}({?percent:0.0%})",
          :chart_stroke_color => 'white')
puts chart.represent_3d?    # => true
chart.show_baloon = false
puts chart.show_baloon?     # => false

chart.load_data(reader)
chart.save('asian_gdp.svg')

Adds Custom Elements to Chart

Using canvas attribute, you can add arbitrary graphical elements.

# Creates the Pie Chart
chart = DYI::Chart::PieChart.new(500,250,
          :center_point => [130, 100],
          :legend_point => [250, 50],
          :represent_3d => true,
          :_3d_settings => {:dy => 20},
          :legend_format => "{?name}\t{!e}{?value:#,0}\t{!e}({?percent:0.0%})",
          :chart_stroke_color => 'white')

DYI::Drawing::Pen.black_pen.draw_text(chart.canvas,
          [250, 20],
          'Nominal GDP of Asian Countries (2010)',
          :text_anchor => 'middle')
chart.load_data(reader)
chart.save('asian_gdp.svg')

See Also:

Since:

  • 0.0.0

Constant Summary

Constants inherited from Base

Base::DEFAULT_CHART_COLOR

Instance Attribute Summary collapse

Attributes inherited from Base

#canvas, #data

Instance Method Summary collapse

Methods inherited from Base

#clear_real_size, #height, #height=, #initialize, #load_data, #puts_in_io, #save, #set_real_size, #string, #width, #width=

Methods included from OptionCreator

#opt_accessor, #opt_reader, #opt_writer

Constructor Details

This class inherits a constructor from DYI::Chart::Base

Instance Attribute Details

#chart_canvasShape::ShapeGroup (readonly)

Returns the container element which body of chart is drawn on.

Returns:

Since:

  • 0.0.0



95
96
97
# File 'lib/dyi/chart/pie_chart.rb', line 95

def chart_canvas
  @chart_canvas
end

#data_label_canvasShape::ShapeGroup (readonly)

Returns the container element which data labels is drawn on.

Returns:

Since:

  • 0.0.0



100
101
102
# File 'lib/dyi/chart/pie_chart.rb', line 100

def data_label_canvas
  @data_label_canvas
end

#legend_canvasShape::ShapeGroup (readonly)

Returns the container element which legend is drawn on.

Returns:

Since:

  • 0.0.0



105
106
107
# File 'lib/dyi/chart/pie_chart.rb', line 105

def legend_canvas
  @legend_canvas
end

Instance Method Details

#back_translate_valueObject

Since:

  • 0.0.0



223
224
225
# File 'lib/dyi/chart/pie_chart.rb', line 223

def back_translate_value
  {:dy => (Length.new_or_nil(_3d_settings[:dy]) || chart_radius_y.quo(2))}
end

#get_baloon_background_color(index) ⇒ Object

Since:

  • 1.0.0



228
229
230
231
232
# File 'lib/dyi/chart/pie_chart.rb', line 228

def get_baloon_background_color(index)
  (baloon_background_colors && baloon_background_colors[index]) ||
      baloon_background_color ||
      chart_color(index).merge('white', 0.7)
end

#get_baloon_border_color(index) ⇒ Object

Since:

  • 1.0.0



235
236
237
238
239
# File 'lib/dyi/chart/pie_chart.rb', line 235

def get_baloon_border_color(index)
  (baloon_border_colors && baloon_border_colors[index]) ||
      baloon_border_color ||
      chart_color(index).merge('black', 0.3)
end