ChartDirector 6.0 (Ruby Edition)

Linear Zone Meter




This example demonstrates a linear meter labelled with zones.

The meter in this example is achieved by using LinearMeter.addZone to add labelled zones. The black lines separating the zones are added by using BaseMeter.addLabel with empty spaces as labels.

Source Code Listing

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

class LinearzonemeterController < ApplicationController

    def index()
        @title = "Linear Zone Meter"
        @ctrl_file = File.expand_path(__FILE__)
        @noOfCharts = 1
        render :template => "templates/chartview"
    end

    #
    # Render and deliver the chart
    #
    def getchart()
        # The value to display on the meter
        value = 85

        # Create an LinearMeter object of size 210 x 45 pixels, using silver background with a 2
        # pixel black 3D depressed border.
        m = ChartDirector::LinearMeter.new(210, 45, ChartDirector::silverColor(), 0, -2)

        # Set the scale region top-left corner at (5, 5), with size of 200 x 20 pixels. The scale
        # labels are located on the bottom (implies horizontal meter)
        m.setMeter(5, 5, 200, 20, ChartDirector::Bottom)

        # Set meter scale from 0 - 100
        m.setScale(0, 100)

        # Add a title at the bottom of the meter with a 1 pixel raised 3D border
        m.addTitle2(ChartDirector::Bottom, "Battery Level", "arialbd.ttf", 8).setBackground(
            ChartDirector::Transparent, -1, 1)

        # Set 3 zones of different colors to represent Good/Weak/Bad data ranges
        m.addZone(50, 100, 0x99ff99, "Good")
        m.addZone(20, 50, 0xffff66, "Weak")
        m.addZone(0, 20, 0xffcccc, "Bad")

        # Add empty labels (just need the ticks) at 0/20/50/80 as separators for zones
        m.addLabel(0, " ")
        m.addLabel(20, " ")
        m.addLabel(50, " ")
        m.addLabel(100, " ")

        # Add a semi-transparent blue (800000ff) pointer at the specified value, using triangular
        # pointer shape
        m.addPointer(value, 0x800000ff).setShape(ChartDirector::TriangularPointer)

        # Output the chart
        send_data(m.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/linearzonemeter.rb
#!/usr/bin/env ruby
require("chartdirector")

# The value to display on the meter
value = 85

# Create an LinearMeter object of size 210 x 45 pixels, using silver background with a 2 pixel black
# 3D depressed border.
m = ChartDirector::LinearMeter.new(210, 45, ChartDirector::silverColor(), 0, -2)

# Set the scale region top-left corner at (5, 5), with size of 200 x 20 pixels. The scale labels are
# located on the bottom (implies horizontal meter)
m.setMeter(5, 5, 200, 20, ChartDirector::Bottom)

# Set meter scale from 0 - 100
m.setScale(0, 100)

# Add a title at the bottom of the meter with a 1 pixel raised 3D border
m.addTitle2(ChartDirector::Bottom, "Battery Level", "arialbd.ttf", 8).setBackground(
    ChartDirector::Transparent, -1, 1)

# Set 3 zones of different colors to represent Good/Weak/Bad data ranges
m.addZone(50, 100, 0x99ff99, "Good")
m.addZone(20, 50, 0xffff66, "Weak")
m.addZone(0, 20, 0xffcccc, "Bad")

# Add empty labels (just need the ticks) at 0/20/50/80 as separators for zones
m.addLabel(0, " ")
m.addLabel(20, " ")
m.addLabel(50, " ")
m.addLabel(100, " ")

# Add a semi-transparent blue (800000ff) pointer at the specified value, using triangular pointer
# shape
m.addPointer(value, 0x800000ff).setShape(ChartDirector::TriangularPointer)

# Output the chart
m.makeChart("linearzonemeter.png")