ChartDirector 5.1 (Ruby Edition)

3D Pie Shading


            

This example demonstrates various sector shading effects applicable to 3D pie charts.

Source Code Listing

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

class ThreedpieshadingController < ApplicationController

    def index()
        @title = "3D Pie Shading"
        @ctrl_file = File.expand_path(__FILE__)
        @noOfCharts = 7
        render :template => "templates/chartview"
    end

    #
    # Render and deliver the chart
    #
    def getchart()
        # The data for the pie chart
        data = [18, 30, 20, 15]

        # The colors to use for the sectors
        colors = [0x66aaee, 0xeebb22, 0xbbbbbb, 0x8844ff]

        # Create a PieChart object of size 200 x 200 pixels. Use a vertical gradient color
        # from blue (0000cc) to deep blue (000044) as background. Use rounded corners of
        # 16 pixels radius.
        c = ChartDirector::PieChart.new(200, 200)
        c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight(), 0x0000cc, 0x000044))
        c.setRoundedFrame(0xffffff, 16)

        # Set the center of the pie at (100, 100) and the radius to 80 pixels
        c.setPieSize(100, 100, 80)

        # Set the pie data
        c.setData(data)

        # Set the sector colors
        c.setColors2(ChartDirector::DataColor, colors)

        # Draw the pie in 3D with a pie thickness of 20 pixels
        c.set3D(20)

        # Demonstrates various shading modes
        if params["img"] == "0"
            c.addTitle("Default Shading", "bold", 12, 0xffffff)
        elsif params["img"] == "1"
            c.addTitle("Flat Gradient", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::FlatShading)
        elsif params["img"] == "2"
            c.addTitle("Local Gradient", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::LocalGradientShading)
        elsif params["img"] == "3"
            c.addTitle("Global Gradient", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::GlobalGradientShading)
        elsif params["img"] == "4"
            c.addTitle("Concave Shading", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::ConcaveShading)
        elsif params["img"] == "5"
            c.addTitle("Rounded Edge", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::RoundedEdgeShading)
        elsif params["img"] == "6"
            c.addTitle("Radial Gradient", "bold", 12, 0xffffff)
            c.setSectorStyle(ChartDirector::RadialShading)
        end

        # Disable the sector labels by setting the color to Transparent
        c.setLabelStyle("", 8, ChartDirector::Transparent)

        # 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/threedpieshading.rb
#!/usr/bin/env ruby
require("chartdirector")

def createChart(img)

    # The data for the pie chart
    data = [18, 30, 20, 15]

    # The colors to use for the sectors
    colors = [0x66aaee, 0xeebb22, 0xbbbbbb, 0x8844ff]

    # Create a PieChart object of size 200 x 200 pixels. Use a vertical gradient
    # color from blue (0000cc) to deep blue (000044) as background. Use rounded
    # corners of 16 pixels radius.
    c = ChartDirector::PieChart.new(200, 200)
    c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight(), 0x0000cc, 0x000044)
        )
    c.setRoundedFrame(0xffffff, 16)

    # Set the center of the pie at (100, 100) and the radius to 80 pixels
    c.setPieSize(100, 100, 80)

    # Set the pie data
    c.setData(data)

    # Set the sector colors
    c.setColors2(ChartDirector::DataColor, colors)

    # Draw the pie in 3D with a pie thickness of 20 pixels
    c.set3D(20)

    # Demonstrates various shading modes
    if img == "0"
        c.addTitle("Default Shading", "bold", 12, 0xffffff)
    elsif img == "1"
        c.addTitle("Flat Gradient", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::FlatShading)
    elsif img == "2"
        c.addTitle("Local Gradient", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::LocalGradientShading)
    elsif img == "3"
        c.addTitle("Global Gradient", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::GlobalGradientShading)
    elsif img == "4"
        c.addTitle("Concave Shading", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::ConcaveShading)
    elsif img == "5"
        c.addTitle("Rounded Edge", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::RoundedEdgeShading)
    elsif img == "6"
        c.addTitle("Radial Gradient", "bold", 12, 0xffffff)
        c.setSectorStyle(ChartDirector::RadialShading)
    end

    # Disable the sector labels by setting the color to Transparent
    c.setLabelStyle("", 8, ChartDirector::Transparent)

    # Output the chart
    c.makeChart("threedpieshading%s.png" % img)
end

createChart("0")
createChart("1")
createChart("2")
createChart("3")
createChart("4")
createChart("5")
createChart("6")