ChartDirector 6.0 (Ruby Edition)

Icon Pie Chart (2)




This example extends the Icon Pie Chart (1) example to demonstrate using CDML to include multiple fonts and colors in chart title, sector labels and to draw logo. It also demonstrates silver background color, rounded corners, and glass shading effect for title.

Source Code Listing

[Ruby On Rails Version - Controller] app/controllers/iconpie2_controller.rb
require("chartdirector")

class Iconpie2Controller < ApplicationController

    def index()
        @title = "Icon Pie Chart (2)"
        @ctrl_file = File.expand_path(__FILE__)
        @noOfCharts = 1
        render :template => "templates/chartview"
    end

    #
    # Render and deliver the chart
    #
    def getchart()
        # The data for the pie chart
        data = [28, 45, 5, 1, 12]

        # The labels for the pie chart
        labels = ["Excellent", "Good", "Bad", "Very Bad", "Neutral"]

        # The icons for the sectors
        icons = ["laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"]

        # Create a PieChart object of size 560 x 300 pixels, with a silver background, black border,
        # 1 pxiel 3D border effect and rounded corners
        c = ChartDirector::PieChart.new(560, 300, ChartDirector::silverColor(), 0x000000, 1)
        c.setRoundedFrame()

        # Set directory for loading images to current script directory
        c.setSearchPath(File.dirname(__FILE__))

        # Set the center of the pie at (280, 150) and the radius to 120 pixels
        c.setPieSize(280, 150, 120)

        # Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass
        # effect
        c.addTitle(
            "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: " \
            "<*font=timesi.ttf,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>" \
            "TM<*/font*> molecules?", "timesbi.ttf", 15, 0x000080).setBackground(0xa0c8ff, 0x000000,
            ChartDirector::glassEffect())

        # Add a logo to the chart written in CDML as the bottom title aligned to the bottom right
        c.addTitle2(ChartDirector::BottomRight,
            "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*>" \
            "<*font=timesbi.ttf,size=12*>Molecular Engineering\n<*font=arial.ttf,size=10*>" \
            "Creating better molecules")

        # Set the pie data and the pie labels
        c.setData(data, labels)

        # Set 3D style
        c.set3D()

        # Use the side label layout method
        c.setLabelLayout(ChartDirector::SideLayout)

        # Set the label background color to transparent
        c.setLabelStyle().setBackground(ChartDirector::Transparent)

        # Add icons to the chart as a custom field
        c.addExtraField(icons)

        # Configure the sector labels using CDML to include the icon images
        c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)")

        # Explode the 3rd and 4th sectors as a group (index = 2 and 3)
        c.setExplodeGroup(2, 3)

        # Set the start angle to 135 degrees may improve layout when there are many small sectors at
        # the end of the data array (that is, data sorted in descending order). It is because this
        # makes the small sectors position near the horizontal axis, where the text label has the
        # least tendency to overlap. For data sorted in ascending order, a start angle of 45 degrees
        # can be used instead.
        c.setStartAngle(135)

        # Output the chart
        send_data(c.makeChart2(ChartDirector::PNG), :type => "image/png", :disposition => "inline")

    end

end

[Ruby On Rails Version - View] app/views/templates/chartview.html.erb
<html>
<body style="margin:5px 0px 0px 5px">

<!-- Title -->
<div style="font-size:18pt; font-family:verdana; font-weight:bold">
    <%= @title %>
</div>
<hr style="border:solid 1px #000080" />

<!-- Source Code Listing Link -->
<div style="font-size:9pt; font-family:verdana; margin-bottom:1.5em">
    <%= link_to "Source Code Listing", 
        :controller => "cddemo", :action => "viewsource",
        :ctrl_file => @ctrl_file, :view_file => File.expand_path(__FILE__) %>
</div>

<!-- Create one or more IMG tags to display the demo chart(s) -->
<% 0.upto(@noOfCharts - 1) do |i| %>
    <img src="<%= url_for(:action => "getchart", :img => i) %>">
<% end %>

</body>
</html>

[Command Line Version] rubydemo/iconpie2.rb
#!/usr/bin/env ruby
require("chartdirector")

# The data for the pie chart
data = [28, 45, 5, 1, 12]

# The labels for the pie chart
labels = ["Excellent", "Good", "Bad", "Very Bad", "Neutral"]

# The icons for the sectors
icons = ["laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"]

# Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1 pxiel
# 3D border effect and rounded corners
c = ChartDirector::PieChart.new(560, 300, ChartDirector::silverColor(), 0x000000, 1)
c.setRoundedFrame()

# Set the center of the pie at (280, 150) and the radius to 120 pixels
c.setPieSize(280, 150, 120)

# Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass effect
c.addTitle(
    "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: <*font=timesi.ttf,color=000000*>" \
    "Do you like our <*font,color=dd0000*>Hyper<*super*>TM<*/font*> molecules?", "timesbi.ttf", 15,
    0x000080).setBackground(0xa0c8ff, 0x000000, ChartDirector::glassEffect())

# Add a logo to the chart written in CDML as the bottom title aligned to the bottom right
c.addTitle2(ChartDirector::BottomRight,
    "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*>" \
    "<*font=timesbi.ttf,size=12*>Molecular Engineering\n<*font=arial.ttf,size=10*>Creating " \
    "better molecules")

# Set the pie data and the pie labels
c.setData(data, labels)

# Set 3D style
c.set3D()

# Use the side label layout method
c.setLabelLayout(ChartDirector::SideLayout)

# Set the label background color to transparent
c.setLabelStyle().setBackground(ChartDirector::Transparent)

# Add icons to the chart as a custom field
c.addExtraField(icons)

# Configure the sector labels using CDML to include the icon images
c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)")

# Explode the 3rd and 4th sectors as a group (index = 2 and 3)
c.setExplodeGroup(2, 3)

# Set the start angle to 135 degrees may improve layout when there are many small sectors at the end
# of the data array (that is, data sorted in descending order). It is because this makes the small
# sectors position near the horizontal axis, where the text label has the least tendency to overlap.
# For data sorted in ascending order, a start angle of 45 degrees can be used instead.
c.setStartAngle(135)

# Output the chart
c.makeChart("iconpie2.png")