Skip to content

Repository files navigation

Inkplot

Build clear SVG and PNG charts from plain Ruby data.

CI Ruby 3.2+ MIT license

Inkplot draws line, scatter, bar, area, histogram, and step charts without runtime dependencies. SVG output uses only Ruby's standard library; optional PNG output needs tessel and glyphic.

Install

Install the gem:

gem install inkplot

Quick start

require "inkplot"

chart = Inkplot.line([3, 1, 4, 1, 5, 9], title: "A small series")
chart.save("series.svg")

Use the builder to combine series and configure axes:

chart = Inkplot.plot(width: 720, height: 420, theme: :dark) do |plot|
  plot.title "Quarterly requests"
  plot.x_axis(label: "Quarter")
  plot.y_axis(label: "Requests", min: 0)
  plot.line(%w[Q1 Q2 Q3 Q4], [18, 23, 28, 36], label: "Current")
  plot.line(%w[Q1 Q2 Q3 Q4], [14, 19, 21, 30], label: "Previous", dash: true)
  plot.hline(25, label: "Target")
  plot.legend(position: :top_right)
end

svg = chart.to_svg

PNG output

PNG rendering is optional and uses the pure Ruby Tessel and Glyphic gems:

gem install tessel glyphic
chart = Inkplot.line([3, 1, 4, 1, 5, 9], title: "A small series")
chart.save("series.png", scale: 2)
png_bytes = chart.to_png

Set Inkplot.config.font to a TrueType font path for Unicode text. Without a font path, PNG output uses Glyphic's small ASCII bitmap font; SVG uses the viewer's system fonts.

Data and output

Charts accept arrays, hashes, CSV tables, and records with string or symbol keys. Scales include linear, logarithmic, time (Time and Date), and ordered categories. Light and dark themes, grouped and stacked bars, annotations, and terminal sparklines are included.

latencies = [12, 13, 15, 15, 18, 21, 26, 42]
Inkplot.histogram(latencies, bins: :auto, x_label: "ms").save("latencies.svg")

For dated data, configure the x axis with type: :time. plot.annotate(x, y, "text") places text at a data point; hline and vline can also carry labels.

The gallery contains 20 runnable examples. Regenerate its SVG previews with:

ruby examples/gallery.rb

API contracts

  • to_svg returns a standalone SVG string; save writes .svg or .png files. to_png returns PNG bytes and to_image returns a Tessel::Image.
  • Paired x/y arrays must have the same length. Non-finite y values break line segments by default; use gaps: :connect to bridge them.
  • Explicit axis limits must be ordered. Log scales omit non-positive values. Time axes reject mixed time zones while preserving local daylight-saving transitions.
  • Invalid scales, colors, dimensions, and file extensions raise ArgumentError.
  • Charts provide _repr_svg_ for notebook frontends and to_inlay for Inlay; neither is a runtime dependency.

Development

bundle install
bundle exec rake verify

License

MIT. See LICENSE.txt.

About

Build clear SVG and PNG charts from plain Ruby data.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages