Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Generate a graph using Gruff

This Ruby code produced a graph using gruff. The output shows a line graph [twitxr.com] for the different fruits. Source code origin: Gruff Update With Bar Graphs | Ruby on Rails for Newbies [rubyonrails.com]

require 'gruff'

g = Gruff::Line.new
g.title = "My Graph" 

g.data("Apples", [1, 2, 3, 4, 4, 3])
g.data("Oranges", [4, 8, 7, 9, 8, 9])
g.data("Watermelon", [2, 3, 1, 5, 6, 8])
g.data("Peaches", [9, 9, 10, 8, 7, 9])

g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}

g.write('my_fruity_graph.png')


Note: I executed the code within an irb session on my Gentoo box. With Gentoo, Gruff was installed [gentoo-portage.com] using the command emerge -va gruff. I tried installing it on Ubuntu but ran into some difficulty, even with help from the article install rmagick ubuntu [dzone.com].

*udpate 21:48 24-Feb*

The following code does exactly as the same code above, however it uses XML to separate the data from the process, making it easier and more efficient to build graphs.
#!/usr/bin/ruby
# file: xml2gruff.rb

require 'rexml/document'
require 'gruff'
include REXML

class Xml2Gruff
  
  def initialize(filename)
    file = File.new(filename, 'r')
    doc = Document.new(file)
    # get the title
    @title = doc.root.elements['summary/title'].text
    
    @record = Hash.new
    # get each record
    doc.root.elements.each('records/item') {|item|
      avalues = Array.new
      item.elements.each('values/value') { |value| avalues << value.text.to_i }
      @record[item.elements['label'].text] = avalues
    }
    
    # get the summary labels
    @labels = Hash.new  
    doc.root.elements.each('summary/scale/label') {|l| @labels[l.elements['value'].text.to_i] = l.elements['title'].text} 
    
  end
  
  def save_line_graph(filename)

    g = Gruff::Line.new
    g.title = @title 
    @record.each {|label, data| g.data(label, data) }
    g.labels = @labels
    g.write(filename)

  end
end

if __FILE__ == $0
 x2g = Xml2Gruff.new('my_fruit.xml')
 x2g.save_line_graph('my_fruit2.png')
end


file: my_fruit.xml
<graph>
  <summary>
    <title>My Graph</title>
    <scale>
      <label><title>2003</title><value>0</value></label>
      <label><title>2004</title><value>2</value></label>
      <label><title>2005</title><value>4</value></label>
    </scale>
  </summary>
  <records>
    <item>
      <label>Apples</label>
      <values><value>1</value><value>2</value><value>3</value><value>4</value><value>4</value><value>3</value></values>
    </item>
    <item>
      <label>Oranges</label>
      <values><value>4</value><value>8</value><value>7</value><value>9</value><value>8</value><value>9</value></values>
    </item>
    <item>
      <label>Watermelon</label>
      <values><value>2</value><value>3</value><value>1</value><value>5</value><value>6</value><value>8</value></values>
    </item>
    <item>
      <label>Peaches</label>
      <values><value>9</value><value>9</value><value>10</value><value>8</value><value>7</value><value>9</value></values>
    </item>
  </records>
</graph>

Reference: gruff's gruff-0.2.9 Documentation [rubyforge.org]
to ruby rmagick image ubuntu gentoo magick graph chart gruff by jrobertson on Feb 24, 2008

Unicode chart

This PHP-enhanced HTML page will display the first 4,096 (unless you change it) Unicode characters in a neat table. Your browser's ability to render the characters properly may vary.
<HTML>
<HEAD>
<TITLE>Unicode Chart</TITLE>
<LINK REL="Stylesheet" TYPE="text/css" HREF="styles.css">
<STYLE TYPE="text/css">
TH {text-align: center; }
TD {text-align: center; }
</STYLE>
</HEAD>
<BODY>
<TABLE ALIGN=CENTER BORDER=1>
<TR><TH> </TH><TH>0</TH><TH>1</TH><TH>2</TH><TH>3</TH><TH>4</TH><TH>5</TH><TH>6</TH><TH>7</TH><TH>8</TH><TH>9</TH><TH>A</TH><TH>B</TH><TH>C</TH><TH>D</TH><TH>E</TH><TH>F</TH></TR>
<?PHP
 for ($i=0; $i<256; $i++) { //DON'T try to generate the whole chart
  printf('<TR><TD>%04X</TD>', $i);
  for ($j=0; $j<16; $j++) {
   printf('<TD>&#x%X%X;</TD>', $i, $j);
  }
  echo "</TR>\n";
 }
?>
</TABLE>
</BODY>
</HTML>

Java - JFreeChart Example

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class jfcExample extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	private DefaultPieDataset dataset;
	private JFreeChart jfc;

	public jfcExample()
	{
		dataset = new DefaultPieDataset();
	}
	
	public void setValue(String title, Double numDouble)
	{
		dataset.setValue(title, numDouble);
	}
	
	public void setChar(String title)
	{
		jfc = ChartFactory.createPieChart(title, dataset, true, true, false);
		
		PiePlot pp = (PiePlot) jfc.getPlot();
		pp.setSectionOutlinesVisible(false);
		pp.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		pp.setNoDataMessage("Nessun Dato Inserito");
		pp.setCircular(false);
		pp.setLabelGap(0.02);
	}
	
	private JPanel createPanel()
	{
		return new ChartPanel(jfc);
	}
	
	public void Show()
	{
		setContentPane(createPanel());
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		jfcExample j = new jfcExample();
		j.setTitle("Example Chart...");
		j.setSize(640, 430);
		
		j.setValue("UNO", new Double(20.0));
		j.setValue("DUE", new Double(10.0));
		j.setValue("TRE", new Double(20.0));
		j.setValue("QUATTRO", new Double(30.0));
		j.setValue("CINQUE", new Double(20.0));
		
		j.setChar("Example Chart...");
		
		j.Show();
	}
}

PHP last.fm chart creator

This code displays the Top 50 artists XML feed from last.fm in a nice presentable way on your website. Can easily be modified to work with any other feed from last.fm, find the full guide at http://www.strydominc.za.net/index.php?p=projectdetail&d=phplastfmchart

<?php
/**********************************************************************************************
www.strydominc.za.net
Created by Jurgen Strydom, 19-08-2006, jurgen.strydom@gmail.com
Read the readme.txt
Version 1.01, 05-09-2006
**********************************************************************************************/
?>
<link href="lastfmbar.css" rel="stylesheet" type="text/css">
<div>
<?php
//User settings -> needs your attention
$user = "Alkine"; //Your username
$width = 700; //width of the list

//Code you should not worry about
$file = "http://ws.audioscrobbler.com/1.0/user/$user/topartists.xml";
$xml = simplexml_load_file("$file");
$big = $xml->artist[0]->playcount;
$total = count($xml->artist);
$factor =  $width /$big;
?>
<table width="<?php echo $width ?>" border="0" cellpadding="0" cellspacing="0">  
 <?php
 for ($k=0 ; $k<=$total - 1; $k++) {
 	$barlen = round(($xml->artist[$k]->playcount * $factor), 0);
 ?>
  <tr>
    <td width="<?php echo $width ?>" height="10" valign="center"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td width="<?php echo $barlen ?>" height="10" valign="center" class="lastfmbar"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount ," -</div>";
		}
		if ($barlen < ($width / 3)) {
			echo "&nbsp;";
		}		
		?></td>
        <td width="<?php echo $width - $barlen ?>" valign="center"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo "&nbsp;";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"left\"><b>&nbsp;", $xml->artist[$k]->name ,"</b></div>";
		}
		if ($barlen < ($width / 3)) {
			echo "<div align=\"left\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}		
		?></td>
      </tr>
    </table></td>
  </tr>
  <?php
  }
  ?>
</table>
</div>
<?php
/**********************************************************************************************
Changelog:

Version 1.01
Fixed a bug that caused the bar sizes to display incorrectly.

Version 1
First release.
**********************************************************************************************/
?>
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS