Node gnuplot
npm install @stoqey/gnuplot
!TypeScript compatible
@stoqey/gnuplot is an easy to use node module to draw charts using gnuplot and ps2pdf. This module is based on Richard Meadows's node-plotter
Prerequisites:
``bashubuntu
sudo apt-get install gnuplot
`
To install package, just run:
``
npm install @stoqey/gnuplot
`javascript
import plot from '@stoqey/gnuplot'
// with callback
plot({
data: [ 3, 1, 2, 3, 4 ],
filename: 'output.png',
finish: (error) => {
}
});
// As promise
const plotted = await plot({
data: [3, 1, 2, 3, 4],
filename: 'output1.png',
});
`
This defaults to .png but specifing format: svg changes the outputformat: pdf
to .svg and changes
the output format to
.pdf.
`javascript
import plot from '@stoqey/gnuplot'
plot({
data: [ 3, 1, 2, 3, 4 ],
filename: 'output.svg',
format: 'svg'
});
`
The following properties can be used:
- title : _Sets the title of the graph_xlabel
- : _Sets the label on the x axis of the graph_ylabel
- : _Sets the label on the y axis of the graph_logscale
- : _Makes the y axis of the graph appear in a log scale_style
- : _The style of the lines on the graph. Possibilites includelines
(default), points and linespoints_nokey
- : _Disables the graph key_hideSeriesTitle
- : _Indicates if plot should include legend_margin
- : _Sets margin if needed_xRange
- : _Sets max and min values for the X axis_yRange
- : _Sets max and min values for the Y axis_decimalsign
- : _Specifies a custom decimal sign_yFormat
- : _Specifies how to format values on the Y axis_font
- : _Specify a custom font_fontSize
- : _Font size_titleSize
- : _Title font size_width
- : _Plot width_height
- : _Plot height_locale
- : _Locale code (run 'set locale' for the exact value)_
The following example shows these in use:
`javascript`
plot({
title: 'example',
data: { 't1' : { 1357162672: 22.2, 1357162782: 23, 1357162892: 24 } },
time: 'hours',
style: 'line',
filename: 'test/output14.png',
format: 'png',
decimalsign: ',',
yFormat: '%.2f USD',
hideSeriesTitle: true,
xlabel: 'Time',
ylabel: 'Price',
margin: {
left: 10,
right: 3,
top: 3,
bottom: 4
},
xRotate: {
value: 45,
yOffset: -1.5,
xOffset: -2
},
hideSeriesTitle: true,
xRange: {
min: 0,
max: 100
}
});
`javascript`
plot({
data: { 'line' : { 1: 5, 5: 6 } },
filename: 'output.png'
});
Instead of specifing an array for data, you can specify an object
with a named series inside.
`javascript`
plot({
data: { 'tick' : [ 3, 1, 2, 3, 4 ], 'line' : { 1: 5, 5: 6 } },
filename: 'output.png'
});
`javascript`
plot({
data: { 'temperature' :
{ 1357162672: 22, 1357162782: 23, 1357162892: 24 } },
time: 'hours',
filename: 'output.png'
});
The x axis can be formatted as a time series if the x values are given
as a unix time. The time
property can be specified with the gnuplot time format.
The options object might additionally contain the following:
Option | Description | Example
-------|-------------|---------
exec | Arguments for the gnuplot process | options.exec = { cwd : '/home/user/images' };finish | Callback executed when the gnuplot process finishes | options.finish = function(){ Console.log('Success!'); };`