ChartDirector Ver 4.1 (Ruby Edition)

Wide Angular Meters


          

This example demonstrates angular meters of which the width is large compared to the height. It also demonstrates various background effects.

The meters in this example are created by using large meter radii, but small angular spans. The center (pivot for the pointer) can then be put outside the meter region and the meters can be reduced in height.

This example also demonstrates metallic background effects and background patterns. The backgrounds are configured using BaseChart.setBackground. The metallic colors are created using ChartDirector.goldColor, ChartDirector.silverColor and ChartDirector.metalColor. The background patterns are created by loading image files as pattern colors using BaseChart.patternColor2.

This example also shows the effect of the semi-transparent pointers, which can avoid blocking the labels on the meter.

Source Code Listing

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

class WideameterController < ApplicationController

    def index()
        @title = "Wide Angular Meters"
        @ctrl_file = File.expand_path(__FILE__)
        @noOfCharts = 6
        render :template => "templates/chartview"
    end

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

        # Create an AugularMeter object of size 200 x 100 pixels with rounded corners
        m = ChartDirector::AngularMeter.new(200, 100)
        m.setRoundedFrame()

        # Set meter background according to a parameter
        if params["img"] == "0"
            # Use gold background color
            m.setBackground(ChartDirector::goldColor(), 0x000000, -2)
        elsif params["img"] == "1"
            # Use silver background color
            m.setBackground(ChartDirector::silverColor(), 0x000000, -2)
        elsif params["img"] == "2"
            # Use metallic blue (9898E0) background color
            m.setBackground(ChartDirector::metalColor(0x9898e0), 0x000000, -2)
        elsif params["img"] == "3"
            # Use a wood pattern as background color
            m.setBackground(m.patternColor2(File.dirname(__FILE__) + "/wood.png"),
                0x000000, -2)
        elsif params["img"] == "4"
            # Use a marble pattern as background color
            m.setBackground(m.patternColor2(File.dirname(__FILE__) + "/marble.png"),
                0x000000, -2)
        else
            # Use a solid light purple (EEBBEE) background color
            m.setBackground(0xeebbee, 0x000000, -2)
        end

        # Set the meter center at (100, 235), with radius 210 pixels, and span from -24 to
        # +24 degress
        m.setMeter(100, 235, 210, -24, 24)

        # Meter scale is 0 - 100, with a tick every 1 unit
        m.setScale(0, 10, 1)

        # Set 0 - 6 as green (99ff99) zone, 6 - 8 as yellow (ffff00) zone, and 8 - 10 as
        # red (ff3333) zone
        m.addZone(0, 6, 0x99ff99, 0x808080)
        m.addZone(6, 8, 0xffff00, 0x808080)
        m.addZone(8, 10, 0xff3333, 0x808080)

        # Add a title at the bottom of the meter using 10 pts Arial Bold font
        m.addTitle2(ChartDirector::Bottom, "OUTPUT POWER LEVEL\n", "arialbd.ttf", 10)

        # Add a semi-transparent black (80000000) pointer at the specified value
        m.addPointer(value, 0x80000000)

        # 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.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/wideameter.rb
#!/usr/bin/env ruby
require("chartdirector")

def createChart(img)

    # The value to display on the meter
    value = 6.5

    # Create an AugularMeter object of size 200 x 100 pixels with rounded corners
    m = ChartDirector::AngularMeter.new(200, 100)
    m.setRoundedFrame()

    # Set meter background according to a parameter
    if img == "0"
        # Use gold background color
        m.setBackground(ChartDirector::goldColor(), 0x000000, -2)
    elsif img == "1"
        # Use silver background color
        m.setBackground(ChartDirector::silverColor(), 0x000000, -2)
    elsif img == "2"
        # Use metallic blue (9898E0) background color
        m.setBackground(ChartDirector::metalColor(0x9898e0), 0x000000, -2)
    elsif img == "3"
        # Use a wood pattern as background color
        m.setBackground(m.patternColor2("wood.png"), 0x000000, -2)
    elsif img == "4"
        # Use a marble pattern as background color
        m.setBackground(m.patternColor2("marble.png"), 0x000000, -2)
    else
        # Use a solid light purple (EEBBEE) background color
        m.setBackground(0xeebbee, 0x000000, -2)
    end

    # Set the meter center at (100, 235), with radius 210 pixels, and span from -24
    # to +24 degress
    m.setMeter(100, 235, 210, -24, 24)

    # Meter scale is 0 - 100, with a tick every 1 unit
    m.setScale(0, 10, 1)

    # Set 0 - 6 as green (99ff99) zone, 6 - 8 as yellow (ffff00) zone, and 8 - 10 as
    # red (ff3333) zone
    m.addZone(0, 6, 0x99ff99, 0x808080)
    m.addZone(6, 8, 0xffff00, 0x808080)
    m.addZone(8, 10, 0xff3333, 0x808080)

    # Add a title at the bottom of the meter using 10 pts Arial Bold font
    m.addTitle2(ChartDirector::Bottom, "OUTPUT POWER LEVEL\n", "arialbd.ttf", 10)

    # Add a semi-transparent black (80000000) pointer at the specified value
    m.addPointer(value, 0x80000000)

    # output the chart
    m.makeChart("wideameter%s.png" % img)
end

createChart("0")
createChart("1")
createChart("2")
createChart("3")
createChart("4")
createChart("5")