ChartDirector 5.1 (Ruby Edition)

Multi Radar Chart




This example demonstrates a radar chart with two layers and a number of chart formatting effects.

Source Code Listing

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

class MultiradarController < ApplicationController

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

    #
    # Render and deliver the chart
    #
    def getchart()
        # The data for the chart
        data0 = [90, 60, 85, 75, 55]
        data1 = [60, 80, 70, 80, 85]

        # The labels for the chart
        labels = ["Speed", "Reliability", "Comfort", "Safety", "Efficiency"]

        # Create a PolarChart object of size 480 x 380 pixels. Set background color to
        # gold, with 1 pixel 3D border effect
        c = ChartDirector::PolarChart.new(480, 380, ChartDirector::goldColor(), 0x000000,
            1)

        # Add a title to the chart using 15 pts Times Bold Italic font. The title text is
        # white (ffffff) on a deep blue (000080) background
        c.addTitle("Space Travel Vehicles Compared", "timesbi.ttf", 15, 0xffffff
            ).setBackground(0x000080)

        # Set plot area center at (240, 210), with 150 pixels radius, and a white (ffffff)
        # background.
        c.setPlotArea(240, 210, 150, 0xffffff)

        # Add a legend box at top right corner (470, 35) using 10 pts Arial Bold font. Set
        # the background to silver, with 1 pixel 3D border effect.
        b = c.addLegend(470, 35, true, "arialbd.ttf", 10)
        b.setAlignment(ChartDirector::TopRight)
        b.setBackground(ChartDirector::silverColor(), ChartDirector::Transparent, 1)

        # Add an area layer to the chart using semi-transparent blue (0x806666cc). Add a
        # blue (0x6666cc) line layer using the same data with 3 pixel line width to
        # highlight the border of the area.
        c.addAreaLayer(data0, 0x806666cc, "Model Saturn")
        c.addLineLayer(data0, 0x6666cc).setLineWidth(3)

        # Add an area layer to the chart using semi-transparent red (0x80cc6666). Add a
        # red (0xcc6666) line layer using the same data with 3 pixel line width to
        # highlight the border of the area.
        c.addAreaLayer(data1, 0x80cc6666, "Model Jupiter")
        c.addLineLayer(data1, 0xcc6666).setLineWidth(3)

        # Set the labels to the angular axis as spokes.
        c.angularAxis().setLabels(labels)

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

# The data for the chart
data0 = [90, 60, 85, 75, 55]
data1 = [60, 80, 70, 80, 85]

# The labels for the chart
labels = ["Speed", "Reliability", "Comfort", "Safety", "Efficiency"]

# Create a PolarChart object of size 480 x 380 pixels. Set background color to gold,
# with 1 pixel 3D border effect
c = ChartDirector::PolarChart.new(480, 380, ChartDirector::goldColor(), 0x000000, 1)

# Add a title to the chart using 15 pts Times Bold Italic font. The title text is
# white (ffffff) on a deep blue (000080) background
c.addTitle("Space Travel Vehicles Compared", "timesbi.ttf", 15, 0xffffff
    ).setBackground(0x000080)

# Set plot area center at (240, 210), with 150 pixels radius, and a white (ffffff)
# background.
c.setPlotArea(240, 210, 150, 0xffffff)

# Add a legend box at top right corner (470, 35) using 10 pts Arial Bold font. Set
# the background to silver, with 1 pixel 3D border effect.
b = c.addLegend(470, 35, true, "arialbd.ttf", 10)
b.setAlignment(ChartDirector::TopRight)
b.setBackground(ChartDirector::silverColor(), ChartDirector::Transparent, 1)

# Add an area layer to the chart using semi-transparent blue (0x806666cc). Add a blue
# (0x6666cc) line layer using the same data with 3 pixel line width to highlight the
# border of the area.
c.addAreaLayer(data0, 0x806666cc, "Model Saturn")
c.addLineLayer(data0, 0x6666cc).setLineWidth(3)

# Add an area layer to the chart using semi-transparent red (0x80cc6666). Add a red
# (0xcc6666) line layer using the same data with 3 pixel line width to highlight the
# border of the area.
c.addAreaLayer(data1, 0x80cc6666, "Model Jupiter")
c.addLineLayer(data1, 0xcc6666).setLineWidth(3)

# Set the labels to the angular axis as spokes.
c.angularAxis().setLabels(labels)

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