ChartDirector 6.0 (Ruby Edition)

Simple Bar Chart (1)




This example demonstrates the basic steps in creating bar charts.

Source Code Listing

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

class SimplebarController < ApplicationController

    def index()
        @title = "Simple Bar Chart (1)"
        @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"]

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

        # Set the plotarea at (30, 20) and of size 200 x 200 pixels
        c.setPlotArea(30, 20, 200, 200)

        # Add a bar chart layer using the given data
        c.addBarLayer(data)

        # 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.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/simplebar.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"]

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

# Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200)

# Add a bar chart layer using the given data
c.addBarLayer(data)

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

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