kplot
–
Cairo plotting library
Version 0.1.15
This is an example usage of kplot , an ISO C, ISC -licensed UNIX programming library for plotting graphs on a Cairo surface.
It handles drawing margins, axis labels, tic labels, tics, borders, grids, and the data itself.
Data may be specified structurally as finite buckets, range histograms, arrays, and vectors.
Data may also be computed from other sources, such as the mean and standard deviation.
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <kplot.h >
int
main(void)
{
struct kpair points1[50], points2[50];
struct kdata *d1, *d2;
struct kplot *p;
cairo_surface_t *surf;
size_t i;
cairo_t *cr;
for (i = 0; i < 50; i++) {
points1[i].x = points2[i].x = (i + 1) / 50.0;
points1[i].y = log((i + 1) / 50.0);
points2[i].y = -log((i + 1) / 50.0) + points1[0].y;
}
d1 = kdata_array_alloc (points1, 50);
d2 = kdata_array_alloc (points2, 50);
p = kplot_alloc (NULL);
kplot_attach_data (p, d1, KPLOT_POINTS, NULL);
kplot_attach_data (p, d2, KPLOT_LINES, NULL);
kdata_destroy (d1);
kdata_destroy (d2);
surf = cairo_image_surface_create
(CAIRO_FORMAT_ARGB32, 600, 400);
cr = cairo_create(surf);
cairo_surface_destroy(surf);
kplot_draw (p, 600.0, 400.0, cr);
cairo_surface_write_to_png
(cairo_get_target(cr), "example0.png ");
cairo_destroy(cr);
kplot_free (p);
return(EXIT_SUCCESS);
}
For a complete reference, consult kplot(3) .
Download sources from kplot.tgz (current version 0.1.15, sha512 ) or
browse the archive .
If you'd like to contribute, please contact Kristaps directly, or use the
GitHub/kplot page.
The source has been tested on OpenBSD, FreeBSD, NetBSD, Mac OS X, and various Linux distributions.
kplot is a BSD.lv project.
2016-11-02 :
version 0.1.15
Merge new
KPLOT_MARKS
and
KPLOT_LINESMARKS
provided in
pull/1 : thanks!
While there, also tune up some of the documentation.
2016-03-02 :
version 0.1.14
Mirror the repository on
GitHub .
Register the project with
Coverity and fixed a minor issues in an error path.
2015-07-06 :
version 0.1.13
Clean up typos in the manpages (patch from Svyatoslav Mishyn—thanks!).
Augment the compatibility framework to compile on systems without
__BEGIN_DECLS
and so forth (e.g.,
musl ).
Compiling with
XQuartz now default for Mac OS X (see the
Makefile for the small change for compiling with the native X11).
2015-03-24 :
version 0.1.12
2015-02-20 :
version 0.1.11
2015-02-11 :
version 0.1.10
Allow for x2axislabel (top) and y2axislabel (right).
Add KPLOTCTYPE_RGBA, which is like KPLOTCTYPE_PATTERN except it can be bound as a source without allocating the pattern object.
Move kplotcfg assignment into
kplot_alloc(3) .
Make kplotcfg colour palette use the kplotccfg (instead of cairo_pattern_t), and use RGBA as the default.
Also make default colours (tics, lines, etc.) use RGBA instead of patterns.
This allows the default allocation of colours not to use dynamic memory (using cairo_source_set_rgba instead of allocating a pattern).
As a side-effect,
kplot_draw(3) no longer does any allocations, and thus has no return value.
2015-02-07 :
version 0.1.9
Stipulated that
kplot_draw(3) can return error.
Overhauled colour management: colours may either be palette-driven, which indexes into a list of patterns; a custom pattern; or
a default that depends on the colour context.
By default, grid components (e.g., tic and grid lines) are assigned specific colours and lines and points are assigned a
palette.
The palette itself is also most robust.
This overhaul works much nicer with the Cairo colour model itself and allows for all sorts of customisation, e.g., linear fading
and so on.
Backed out
kdata_set(3)
, as some sources do extra bookkeeping that this would bypass in general use.
Added
kdata_vector_set(3) ,
kdata_array_fill_ydoubles(3) , and
kdata_array_fill_ysizes(3) .
Fixed clipping model when extrema (which are now in
kplot_draw(3) instead of per-line) are
specified.
2015-01-31 :
version 0.1.8
2015-01-26 :
version 0.1.7
2015-01-22 :
version 0.1.6
Added the
kdata_stddev_alloc(3) function family for computing the running standard
deviation of a data source (by way of its unbiased sample variance).
Also added the
kplot_datas_attach(3) function for multi-set plots, e.g., y-error lines.
2015-01-16 :
version 0.1.5
Removed the
kdata_copy()
function in favour of
kdata_buffer_alloc(3) and
kdata_buffer_copy(3) , which quickly copy only the value pairs of their sources.
The reason for this removal is the addition of
dependant data sources (e.g., the
kdata_mean_alloc(3) ), which act on structural data sources (such as buckets) that
compute values based upon existing value-pairs.
Also removed the
kdata_array_realloc()
function.
For clarity, renamed
kdata_hist_increment(3)
,
kdata_bucket_increment(3)
,
kdata_vector_add(3)
, and
kplot_data_add(3)
to
kdata_hist_add(3) ,
kdata_bucket_add(3) ,
kdata_vector_append(3) , and
kplot_data_attach(3) , respectively.
Added
kdata_array_add(3)
and
kdata_array_set(3)
for completeness.
2014-11-30 :
version 0.1.4
Initial public release.
(Not the initial private release, however.)
The API is very likely to change!