ChartDirector Ver 4.1 (Ruby Edition)

Multi-Color Bar Chart




This example demonstrates creating a multi-color bar chart, in which each bar has a different color. It also demonstrates metallic background colors and various 3D border effects.

A multi-color bar layer is a bar layer in which each bar has a different color. It is created using XYChart.addBarLayer3. The colors can be specified explicitly (as is in this example), or can be taken automatically from the color palette.

Source Code Listing

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

class ColorbarController < ApplicationController

    def index()
        @title = "Multi-Color 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 = [85, 156, 179.5, 211, 123]

        # The labels for the bar chart
        labels = ["Mon", "Tue", "Wed", "Thu", "Fri"]

        # The colors for the bar chart
        colors = [0xb8bc9c, 0xa0bdc4, 0x999966, 0x333366, 0xc3c3e6]

        # Create a XYChart object of size 300 x 220 pixels. Use golden background color.
        # Use a 2 pixel 3D border.
        c = ChartDirector::XYChart.new(300, 220, ChartDirector::goldColor(), -1, 2)

        # Add a title box using 10 point Arial Bold font. Set the background color to
        # metallic blue (9999FF) Use a 1 pixel 3D border.
        c.addTitle("Daily Network Load", "arialbd.ttf", 10).setBackground(
            ChartDirector::metalColor(0x9999ff), -1, 1)

        # Set the plotarea at (40, 40) and of 240 x 150 pixels in size
        c.setPlotArea(40, 40, 240, 150)

        # Add a multi-color bar chart layer using the given data and colors. Use a 1 pixel
        # 3D border for the bars.
        c.addBarLayer3(data, colors).setBorderColor(-1, 1)

        # Set the labels on the x axis.
        c.xAxis().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.rhtml
<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/colorbar.rb
#!/usr/bin/env ruby
require("chartdirector")

# The data for the bar chart
data = [85, 156, 179.5, 211, 123]

# The labels for the bar chart
labels = ["Mon", "Tue", "Wed", "Thu", "Fri"]

# The colors for the bar chart
colors = [0xb8bc9c, 0xa0bdc4, 0x999966, 0x333366, 0xc3c3e6]

# Create a XYChart object of size 300 x 220 pixels. Use golden background color. Use
# a 2 pixel 3D border.
c = ChartDirector::XYChart.new(300, 220, ChartDirector::goldColor(), -1, 2)

# Add a title box using 10 point Arial Bold font. Set the background color to
# metallic blue (9999FF) Use a 1 pixel 3D border.
c.addTitle("Daily Network Load", "arialbd.ttf", 10).setBackground(
    ChartDirector::metalColor(0x9999ff), -1, 1)

# Set the plotarea at (40, 40) and of 240 x 150 pixels in size
c.setPlotArea(40, 40, 240, 150)

# Add a multi-color bar chart layer using the given data and colors. Use a 1 pixel 3D
# border for the bars.
c.addBarLayer3(data, colors).setBorderColor(-1, 1)

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

# output the chart
c.makeChart("colorbar.png")