middleman-gnuplot
This extension to the Middleman framework provides helper functions to generate plots using gnuplot.
Setup
Add the gem to your Gemfile:
gem 'middleman-gnuplot', :path => "path-to/middleman-gnuplot"
Update your gem list:
$ bundle install
Modify the config.rb of your project as follows:
activate :gnuplot do |opts|
opts.gp_tmpdir = 'tmp' # path, where plots are stored before copied to build dir
opts.gp_outdir = 'img' # directory holding plot files with build dir
opts.gp_format = 'png' # determines output format (currently supports png and pdf)
end
If not specified, the plots will be generated with random filenames,
which are 6 characters long. This setting can be overridden in config.rb
to e.g. 8 using the following line:
set :gp_rndfilelength, 8
Usage
All functions return the output filename of the plot, which can be used
as argument for the image_tag helper function.
The middleman-gnuplot extension provides the following helper methods:
-
plot_functions ([{:expression, :style, :color}], filename, plot title): Plot simple expressions, which can be defined as single hash or array thereof. The function returns a filename, which can be used as argument for theimage_taghelper.expressioncan hold terms that can be interpreted by gnuplot, e.g. sin(x) or similar.style: lines, points, etc.colortakes an RGB color definition in hex format.filenamedefines the output filename. If nil, a random filename is generated and returned by the function.image_tag plot_functions([{:expression => "sin(x)", :style => "lines", :color => "#4575B4"}, {:expression => "tan(x)", :color => "#D73027"}], "filename", "Plot Title")
-
plot_script(script, filename, title): This helper can be used to generate a plot with a gnuplot script.scriptshould contain the path to the gnuplot script.filenamedefines the output filename. If nil, a random filename is generated and returned by the function. Thetitleparameter can be used to define a plot title.image_tag plot_script("path_to_gnuplot_script", "filename", "Plot Title")
-
plot_data(data, series, filename, title): Plots data points from a given arraydatadirectly. The data series can be specified as hashes within the arrayseries. Allowed fields are:x,:y,:titleand:style(lines, points, linespoints, ...). The default style is lines.filenamedefines the output filename. If nil, a random filename is generated and returned by the function. Thetitleparameter can be used to define a plot title.image_tag plot_data([[1,2,1],[2,5,3],[3,3,1],[4,1,5]], [{:x => 1, :y => 2, :title => "Series 1"}, {:x => 1, :y => 3, :title => "Series 2", :style => "points"}], "filename", "Plot Title"
Note
The build summary of middleman lists plot files that were generated in previous runs as being removed from the build directory. Nevertheless, the plots generated in the current run are moved to the build directory after the middleman sitemap is evaluated and therefore do not show up in this list.
Feel free to contact me, if you have a better solution for this issue!
Feedback & Contributions
- Fork it ( http://github.com/hermanndetz/middleman-gnuplot/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request