Code Monkey home page Code Monkey logo

gruff's Introduction

Gruff Graphs

CI Gem Version

A library for making beautiful graphs.

Built on top of rmagick; see its web page for a list of the system-level prerequisities (ImageMagick etc) and how to install them.

Installation

Add this line to your application's Gemfile:

gem 'gruff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install gruff

Font

Gruff uses Roboto font as default font which is licensed under the Apache License, Version 2.0.

Usage

require 'gruff'
g = Gruff::Line.new
g.title = 'Wow!  Look at this!'
g.labels = { 0 => '5/6', 1 => '5/15', 2 => '5/24', 3 => '5/30', 4 => '6/4',
             5 => '6/12', 6 => '6/21', 7 => '6/28' }
g.data :Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]
g.data :Charles, [80, 54, 67, 54, 68, 70, 90, 95]
g.data :Julie, [22, 29, 35, 38, 36, 40, 46, 57]
g.data :Jane, [95, 95, 95, 90, 85, 80, 88, 100]
g.data :Philip, [90, 34, 23, 12, 78, 89, 98, 88]
g.data :Arthur, [5, 10, 13, 11, 6, 16, 22, 32]
g.write('exciting.png')

Examples

You can find many examples in the test directory along with their resulting charts in the expected directory.

Accumulator bar chart

Accumulator bar chart

Area chart

Area chart

Bar chart

Bar chart

Bezier chart

In progress!

Bezier chart

Bullet chart

In progress!

Bullet chart

Dot chart

Dot chart

Line chart

Line chart

LineXY chart

LineXY chart

Net chart

Net chart

Pie chart

Pie chart

Scatter chart

Scatter chart

Side bar chart

Side bar chart

Side stacked bar chart

Side stacked bar chart

Spider chart

Spider chart

Stacked area chart

Stacked area chart

Stacked bar chart

Stacked bar chart

Histogram chart

Histogram chart

Box chart

Box chart

Candlestick

Candlestick

Bubble chart

Bubble chart

Documentation

http://www.rubydoc.info/github/topfunky/gruff/frames

Supported Ruby Versions

  • Ruby 2.6 or later
  • JRuby 9.3.x or later

Development

  1. Build docker image
$ ./docker-build.sh
  1. Launch docker image
$ ./docker-launch.sh
  1. Run tests
$ bundle exec rake

If you have made changes that involve updating the expected image, you need to update the image with the following command after running tests.

$ bundle exec rake test:image:update

Contributing

Source

The source for this project is now kept at GitHub:

http://github.com/topfunky/gruff

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

gruff's People

Contributors

albertein avatar alexrozanski avatar bankair avatar brimil01 avatar dce avatar donv avatar eduardoghdez avatar elrayle avatar erebor avatar flameeyes avatar fordguo avatar gawyn avatar gwolf avatar h0tl33t avatar house9 avatar iox avatar jcoglan avatar jej avatar jjulian avatar jordimassaguerpla avatar joshgoebel avatar kappie avatar marshally avatar mcauser avatar nikosd avatar olleolleolle avatar scriptedworld avatar topfunky avatar vicvega avatar watson1978 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gruff's Issues

line with nil in dataset

this is my first time using github, so let me know if I'm putting this in the wrong spot.
line.rb:
line 156 prevents prev_x and prev_y from ever being set to nil..

That means line 181 won't need to check for null value.

font directive is not setting font in charts

I'm am trying to set the font used in my Gruff charts.

I am on Rails 3.2.11 and I'm using the asset pipeline.

I am using g.font = font_path("rokkitt-bold-webfont.ttf") where g = Gruff::StackedBar.new.

At the command line I can see this path expands to "/assets/rokkitt-bold-webfont.ttf"

Entering that URL into the browser I can see that it points to the font as it attempts to save the font as a download.

I know the font is valid as I'm using it successfully in my HTML.

Is the font method broken with the asset pipeline?

Add support for spacing between groups of bar graphs

From what I've read, it is possible to add space between bars. And how about group of bars?

I mean, is it possible to define some spacing between bars and another spacing between groups of bars? If not, here is my feature request :)

Congratulations for Gruff!

Baseline drawn at incorrect position

I would expect the following code to draw a baseline at y=3, two thirds of the way between y=1 and y=4:

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.baseline_value = 3

g.write('my_fruity_graph.jpg') 

However, as you can see from the image below, the baseline is being drawn very close to y=4. I'm not sure whether I've missed something about how baseline_value is meant to work, but this looks like it might be a bug.

Image of baseline

Logarithmic scale

How can I set logarithmic scale for Y axis? It seems that gruff doesn't support that yet, but is there some workaround how to achieve it now? Thanks.

Escape '%' in labels

Currently if you set a label like "Score (%)" it is printed as "Score (" since RMagick uses the "%" for special values.

http://www.imagemagick.org/RMagick/doc/draw.html#annotate

I propose we escape the "%" to allow it to be displayed correctly. This removes the feature of inserting image-specific values as described in the link above, but I think that is an unimportant and maybe undesired feature.

Any opinions?

Scaling a graph leads to fuzzy pictures

I'm generating JPG graphs and when they are set to the default size (800x600 ?) they look great. However, if I try to generate a smaller graph, say 200x200, it looks awful. Is there something I'm doing wrong?

Thanx!
Richard

Gruff::SideStackedBar

I want to generate Side Stacked Bar using gruff library. but i am not bale to understand how to pass data,show labels and color for labels.
could you please give one small example for generating side stack bar

Division By Zero Error on Documented Example

Sorry to disturb, but i`ve faced some problem with templates from documentation:

  g = Gruff::Line.new
  g.title = "A Line Graph"
  g.data 'Fries', [20, 23, 19, 8]
  g.data 'Hamburgers', [50, 19, 99, 29]
  g.write("test/output/line.png")

this produces:

ZeroDivisionError: divided by 0

Can't create gem

I tried to build a new gem for gruff after I ran rake in ruby and jruby. I get the following errors:

C:\Users\Roger\Documents\GitHub\gruff>gem build gruff.gemspec
ERROR: While executing gem ... (Gem::InvalidSpecificationException)
["test/output/bar_y_increment_2.0.png", "test/output/dot_y_increment_2.0.png", "test/output_java
/bar_y_increment_2.0.png", "test/output_java/dot_y_increment_2.0.png"] are not files

This is due to the fact that the test are generating an extra ".0" in some file names. For instance, "bar_y_increment_2.0.png" has become "bar_y_increment_2.0.0.png". I think I can create a fix for this.

Line XY charts

Merge the xy branch to enable Line charts with dataxy data sets.

Add option for floating legend

Allow placing the legend inside the chart either at given coordinates or anchored to a compass direction or even centered.

Ability to hide dots for individual lines

Currently Gruff::Line support hiding dots for all lines by setting g.hide_dots = false. It would be nice to have the possibility to hide dots for individual lines as well.

One possibility would be to allow g.data to take more options than just color.

What do you think?

Correct DOT graph drawing

As far as I can see, the dots in the dot graph are drawn in the wrong position.

Also, the y_axis_increment is not respected.

I am pushing a fix for both.

Feature: Data value markers

I would like to be able to add the actual values to a graph. For example, on a bar graph, it would be nice to have the actual value on top of or inside the bar. This makes it easier to see where a number lies instead of having to guess by looking at the axis.

Is this possible?

zero-width bar entries drawn

On side bar and side stacked bar types, when a field value is zero, a bar with non-zero width is still drawn.

This patch seems to resolve the issue in my case:

diff --git a/lib/gruff/side_bar.rb b/lib/gruff/side_bar.rb
index 6733a22..090cd8b 100644
--- a/lib/gruff/side_bar.rb
+++ b/lib/gruff/side_bar.rb
@@ -44,7 +44,7 @@ class Gruff::SideBar < Gruff::Base

         height[point_index] += (data_point * @graph_width)

-        @d           = @d.rectangle(left_x, left_y, right_x, right_y)
+        @d           = @d.rectangle(left_x, left_y, right_x, right_y) unless difference <= 0

         # Calculate center based on bar_width and current row
         label_center = @graph_top + (@bars_width * point_index + @bars_width / 2)
diff --git a/lib/gruff/side_stacked_bar.rb b/lib/gruff/side_stacked_bar.rb
index 0b51070..9c3be27 100644
--- a/lib/gruff/side_stacked_bar.rb
+++ b/lib/gruff/side_stacked_bar.rb
@@ -52,7 +52,7 @@ class Gruff::SideStackedBar < Gruff::SideBar
          length[point_index] += difference
         height[point_index] += (data_point * @graph_width - 2)

-        @d = @d.rectangle(left_x, left_y, right_x, right_y)
+        @d = @d.rectangle(left_x, left_y, right_x, right_y) unless difference <= 0

         # Calculate center based on bar_width and current row
         label_center = @graph_top + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)

issue with markers that are floats

Code:

line_data = [99.931, 99.876, 99.948, 99.98, 99.985, 99.924, 99.98, 100.0]

g = Gruff::Line.new
g.marker_font_size = 10
g.data("test",line_data)
g.write("test.png")

Output:
tmp

Details:
Notice the markers report "0.99936E2" for example instead of "99.936".
Is there a way to force the markers to a certain number of decimal places?

Vertical background lines in line graphs

It is currently possible to add horizontal background lines to graphs, but not vertical background lines. It should be possible to add both. A sensible strategy would be to match the vertical background lines to the labels on the x axis.

XY Datasets are inconvenient to use. (documentation and/or code is wrong)

Example image: https://github.com/topfunky/gruff/blob/master/test/output/line_xy.png

The '2003' label should correspond with 0, '2004' with 2
Consider Graph Apples (yellow): It has a datapoint on [1, 1]

Confusingly, the yellow bar is at value 1, exactly about the 2003 label.
If we consider the labels, off-by-one with the X datavalues, the [3, 2] should be right about the '2004' label, which is not the case (but [4,5] is).

(Copied from code docs)

# g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3])
...
# g.data("Capples", [1, 1, 2, 2, 3, 3])
# #labels will be drawn at the x locations of the 1st dataset that you
# #passed in. In this example the lables are drawn at x locations 1,4,6

Since the green bar should have the same X values as the green bar, I expect the datapoints to ly on the same Y value, which is totally not the case as can be seen on the second datapoint (slightly shifted to the left).

Also note that the documentation is wrong as it should say at "x locations 1,3,4,5,6,10"

[nathan@psi onderzoeksstage]$ bundle show
Gems included by the bundle:
  * bundler (1.2.1)
  * gruff (0.3.7)
  * rmagick (2.13.1)
[nathan@psi onderzoeksstage]$ 

Clean up data sorting

Hi all!

Currently, "sort" is set to default true which sorts the data sets on average value, largest to smallest. I think the default should be to draw the data sets as they were given. Any opinions?

As a separate feature, some graphs, like "area", should draw the largest values first to avoid hiding the smaller values. This behaviour is chart type dependent.

When the data set is sorted today, the legend is not sorted. I propose we sort the legend to correspond to the order of the data sets.

The automatically generated colours for each data set should be picked after the data sets have been sorted, to comply with the given theme. This has the effect of the developer being able to set the colours (or theme) after the data sets have been given. Some test examples change because of this, for the better, I think.

I have the implementation for this ready, so please be quick with comments.

Explicitly specify overlapping for lines (Gruff::Line)

Is it possible to explicitly specify how given data should overlap? Currently it seems rendering is done from highest to lowest, resulting in data (with lowest totals) being rendered in front of everything else.

Consider the following.

graph = Gruff::Line.new

graph.theme = {
  background: 'transparent'
}

graph.labels = {
  0 => "2013-07-01",
  1 => "2013-07-08"
}

graph.data('first',  [1, 2])
graph.data('second', [2, 3])

graph.write

This will result in the line first being rendered above line second. Is it possible to adjust this by explicitly enforcing what level a line should be rendered at?

Extract separate LineXY chart

Factor out separate XY module for LineXY and Scatter.

Override draw_label to take X-position instead of sample position.

Wrong direction in y_axis_label

I created a simple line graph with the following code:

require 'gruff'

g = Gruff::Line.new(400)
g.title = "Transparent Background"
g.theme = {
:colors => ['black', 'grey'],
:marker_color => 'grey',
:font_color => 'black',
:background_colors => 'white'
}

g.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30',
}
g.y_axis_label = "Bushels"
g.x_axis_label = "Date"
g.data(:apples, [-1, 0, 4, -4])
g.data(:peaches, [10, 8, 6, 3])
g.write("line_transparent.png")

The generated image looks like this:

image

The "Bushels" y-label should go in the other direction IMHO. Do you agree? If so, this is a simple fix and I can create a Pull Request (my first, so be patient) if necessary.

Set encoding to uft-8

Is there any way to set the encoding to uft-8?? i am using the armenian language and whenever i create a graph all my labels are replaced with question marks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.