ChartDirector 6.0 (Ruby Edition)

Borderless Bar Chart




This example demonstrates a horizontal bar chart with no axes, grid lines or and plot area border. It also demonstrates using gradient colors for the bars, and a number of other ChartDirector features.

The key features demonstrated in this example are:

Source Code Listing

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

class HbarController < ApplicationController

    def index()
        @title = "Borderless Bar 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 bar chart
        data = [3.9, 8.1, 10.9, 14.2, 18.1, 19.0, 21.2, 23.2, 25.7, 36]

        # The labels for the bar chart
        labels = ["Bastic Group", "Simpa", "YG Super", "CID", "Giga Tech", "Indo Digital",
            "Supreme", "Electech", "THP Thunder", "Flash Light"]

        # Create a XYChart object of size 600 x 250 pixels
        c = ChartDirector::XYChart.new(600, 250)

        # Add a title to the chart using Arial Bold Italic font
        c.addTitle("Revenue Estimation - Year 2002", "arialbi.ttf")

        # Set the plotarea at (100, 30) and of size 400 x 200 pixels. Set the plotarea border,
        # background and grid lines to Transparent
        c.setPlotArea(100, 30, 400, 200, ChartDirector::Transparent, ChartDirector::Transparent,
            ChartDirector::Transparent, ChartDirector::Transparent, ChartDirector::Transparent)

        # Add a bar chart layer using the given data. Use a gradient color for the bars, where the
        # gradient is from dark green (0x008000) to white (0xffffff)
        layer = c.addBarLayer(data, c.gradientColor(100, 0, 500, 0, 0x008000, 0xffffff))

        # Swap the axis so that the bars are drawn horizontally
        c.swapXY(true)

        # Set the bar gap to 10%
        layer.setBarGap(0.1)

        # Use the format "US$ xxx millions" as the bar label
        layer.setAggregateLabelFormat("US$ {value} millions")

        # Set the bar label font to 10pt Times Bold Italic/dark red (0x663300)
        layer.setAggregateLabelStyle("timesbi.ttf", 10, 0x663300)

        # Set the labels on the x axis
        textbox = c.xAxis().setLabels(labels)

        # Set the x axis label font to 10pt Arial Bold Italic
        textbox.setFontStyle("arialbi.ttf")
        textbox.setFontSize(10)

        # Set the x axis to Transparent, with labels in dark red (0x663300)
        c.xAxis().setColors(ChartDirector::Transparent, 0x663300)

        # Set the y axis and labels to Transparent
        c.yAxis().setColors(ChartDirector::Transparent, 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/hbar.rb
#!/usr/bin/env ruby
require("chartdirector")

# The data for the bar chart
data = [3.9, 8.1, 10.9, 14.2, 18.1, 19.0, 21.2, 23.2, 25.7, 36]

# The labels for the bar chart
labels = ["Bastic Group", "Simpa", "YG Super", "CID", "Giga Tech", "Indo Digital", "Supreme",
    "Electech", "THP Thunder", "Flash Light"]

# Create a XYChart object of size 600 x 250 pixels
c = ChartDirector::XYChart.new(600, 250)

# Add a title to the chart using Arial Bold Italic font
c.addTitle("Revenue Estimation - Year 2002", "arialbi.ttf")

# Set the plotarea at (100, 30) and of size 400 x 200 pixels. Set the plotarea border, background
# and grid lines to Transparent
c.setPlotArea(100, 30, 400, 200, ChartDirector::Transparent, ChartDirector::Transparent,
    ChartDirector::Transparent, ChartDirector::Transparent, ChartDirector::Transparent)

# Add a bar chart layer using the given data. Use a gradient color for the bars, where the gradient
# is from dark green (0x008000) to white (0xffffff)
layer = c.addBarLayer(data, c.gradientColor(100, 0, 500, 0, 0x008000, 0xffffff))

# Swap the axis so that the bars are drawn horizontally
c.swapXY(true)

# Set the bar gap to 10%
layer.setBarGap(0.1)

# Use the format "US$ xxx millions" as the bar label
layer.setAggregateLabelFormat("US$ {value} millions")

# Set the bar label font to 10pt Times Bold Italic/dark red (0x663300)
layer.setAggregateLabelStyle("timesbi.ttf", 10, 0x663300)

# Set the labels on the x axis
textbox = c.xAxis().setLabels(labels)

# Set the x axis label font to 10pt Arial Bold Italic
textbox.setFontStyle("arialbi.ttf")
textbox.setFontSize(10)

# Set the x axis to Transparent, with labels in dark red (0x663300)
c.xAxis().setColors(ChartDirector::Transparent, 0x663300)

# Set the y axis and labels to Transparent
c.yAxis().setColors(ChartDirector::Transparent, ChartDirector::Transparent)

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