This example demonstrates how to control the axis tick density in auto-scaling.
By default, in auto-scaling, ChartDirector automatically chooses the tick spacing for the axis.
In some cases, you may want to suggest the tick spacing to use. This can be done by using
Axis.setTickDensity.
Note that the actual tick spacing chosen by ChartDirector may not be equal to the suggested tick spacing. The suggested tick spacing is only a lower bound. It is because there may be other constraints when choosing the ticks. For example, the tick positions should be "neat numbers", such as (0, 5, 10, ...), and not arbitrary numbers like (0, 4.792, 9.584, ...). Also, by default, the axis starts and ends at tick positions (configurable using
Axis.setRounding), which means the axis length must be divisible by the tick spacing. These and other constraints will affect the exact ticks being chosen.
In this example, one of the charts is drawn using the default tick spacing. The other chart is drawn using a suggested tick spacing of 10 pixels. Note that the actual tick spacing is slightly larger than 10 pixels.
[Ruby On Rails Version - Controller] app/controllers/ticks_controller.rb
require("chartdirector")
class TicksController < ApplicationController
def index()
@title = "Tick Density"
@ctrl_file = File.expand_path(__FILE__)
@noOfCharts = 2
render :template => "templates/chartview"
end
#
# Render and deliver the chart
#
def getchart()
# This script can draw different charts depending on the chartIndex
chartIndex = (params["img"]).to_i
# The data for the chart
data = [100, 125, 265, 147, 67, 105]
labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
# Create a XYChart object of size 250 x 250 pixels
c = ChartDirector::XYChart.new(250, 250)
# Set the plot area at (27, 25) and of size 200 x 200 pixels
c.setPlotArea(27, 25, 200, 200)
if chartIndex == 1
# High tick density, uses 10 pixels as tick spacing
c.addTitle("Tick Density = 10 pixels")
c.yAxis().setTickDensity(10)
else
# Normal tick density, just use the default setting
c.addTitle("Default Tick Density")
end
# Set the labels on the x axis
c.xAxis().setLabels(labels)
# Add a color bar layer using the given data. Use a 1 pixel 3D border for the bars.
c.addBarLayer3(data).setBorderColor(-1, 1)
# 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/ticks.rb
#!/usr/bin/env ruby
require("chartdirector")
def createChart(chartIndex)
# The data for the chart
data = [100, 125, 265, 147, 67, 105]
labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
# Create a XYChart object of size 250 x 250 pixels
c = ChartDirector::XYChart.new(250, 250)
# Set the plot area at (27, 25) and of size 200 x 200 pixels
c.setPlotArea(27, 25, 200, 200)
if chartIndex == 1
# High tick density, uses 10 pixels as tick spacing
c.addTitle("Tick Density = 10 pixels")
c.yAxis().setTickDensity(10)
else
# Normal tick density, just use the default setting
c.addTitle("Default Tick Density")
end
# Set the labels on the x axis
c.xAxis().setLabels(labels)
# Add a color bar layer using the given data. Use a 1 pixel 3D border for the bars.
c.addBarLayer3(data).setBorderColor(-1, 1)
# Output the chart
c.makeChart("ticks%s.png" % chartIndex)
end
createChart(0)
createChart(1) |
© 2015 Advanced Software Engineering Limited. All rights reserved.