ChartDirector 6.0 (Ruby Edition)

Simple Rose Chart




This example demonstrates how to create a rose chart.

A rose chart is basically a polar chart with sectors of variable radius. This can be achieved by creating a PolarChart object as the graph paper, and adding sector zones on it using AngularAxis.addZone.

To enable auto-scale of the axis, in this example, we also add the radius data to a transparent line layer using PolarChart.addLineLayer. The line layer has no visible effect, but it causes the radial axis to auto-scale so that it covers the radius data.

Source Code Listing

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

class RoseController < ApplicationController

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

    #
    # Render and deliver the chart
    #
    def getchart()
        # Data for the chart
        data = [9.4, 1.8, 2.1, 2.3, 3.5, 7.7, 8.8, 6.1, 5.0, 3.1, 6.0, 4.3, 5.1, 2.6, 1.5, 2.2, 5.1,
            4.3, 4.0, 9.0, 1.7, 8.8, 9.9, 9.5]
        angles = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255,
            270, 285, 300, 315, 330, 345]

        # Create a PolarChart object of size 460 x 460 pixels, with a silver background and a 1
        # pixel 3D border
        c = ChartDirector::PolarChart.new(460, 460, ChartDirector::silverColor(), 0x000000, 1)

        # Add a title to the chart at the top left corner using 15pt Arial Bold Italic font. Use
        # white text on deep blue background.
        c.addTitle("Polar Vector Chart Demonstration", "arialbi.ttf", 15, 0xffffff).setBackground(
            0x000080)

        # Set plot area center at (230, 240) with radius 180 pixels and white background
        c.setPlotArea(230, 240, 180, 0xffffff)

        # Set the grid style to circular grid
        c.setGridStyle(false)

        # Set angular axis as 0 - 360, with a spoke every 30 units
        c.angularAxis().setLinearScale(0, 360, 30)

        # Add sectors to the chart as sector zones
        0.upto(data.length - 1) do |i|
            c.angularAxis().addZone(angles[i], angles[i] + 15, 0, data[i], 0x33ff33, 0x008000)
        end

        # Add an Transparent invisible layer to ensure the axis is auto-scaled using the data
        c.addLineLayer(data, 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/rose.rb
#!/usr/bin/env ruby
require("chartdirector")

# Data for the chart
data = [9.4, 1.8, 2.1, 2.3, 3.5, 7.7, 8.8, 6.1, 5.0, 3.1, 6.0, 4.3, 5.1, 2.6, 1.5, 2.2, 5.1, 4.3,
    4.0, 9.0, 1.7, 8.8, 9.9, 9.5]
angles = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270,
    285, 300, 315, 330, 345]

# Create a PolarChart object of size 460 x 460 pixels, with a silver background and a 1 pixel 3D
# border
c = ChartDirector::PolarChart.new(460, 460, ChartDirector::silverColor(), 0x000000, 1)

# Add a title to the chart at the top left corner using 15pt Arial Bold Italic font. Use white text
# on deep blue background.
c.addTitle("Polar Vector Chart Demonstration", "arialbi.ttf", 15, 0xffffff).setBackground(0x000080)

# Set plot area center at (230, 240) with radius 180 pixels and white background
c.setPlotArea(230, 240, 180, 0xffffff)

# Set the grid style to circular grid
c.setGridStyle(false)

# Set angular axis as 0 - 360, with a spoke every 30 units
c.angularAxis().setLinearScale(0, 360, 30)

# Add sectors to the chart as sector zones
0.upto(data.length - 1) do |i|
    c.angularAxis().addZone(angles[i], angles[i] + 15, 0, data[i], 0x33ff33, 0x008000)
end

# Add an Transparent invisible layer to ensure the axis is auto-scaled using the data
c.addLineLayer(data, ChartDirector::Transparent)

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