ChartDirector 6.0 (Ruby Edition)

The First Ruby Command Line Project


The following sample code comes from the Ruby Command Line sample programs included in the ChartDirector distribution. If you have not yet tried the sample programs, it is highly recommended you try them now. Please refer to the Installation section for details. They are very useful for exploring and testing the features of ChartDirector.

[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")

The code is almost identical to the charting code in the previous section The First Ruby On Rails Project, so it will not be further explained. Please refer the previous section for the details. The main difference is that instead of streaming the chart to the browser, in this example, the chart is created as an image file using BaseChart.makeChart.