A plotting library available in many programming languages.
A plotting library available in many programming languages. Goals of this library are to
The library is developed using progsbase, a technology for writing timeless code. As a consequence, it will be available in many programming languages.
The library has the same interface in all supported languages. The example below is in Java with links to complete examples in all supported programming languages.
Complete example available in the following languages:
Java | C | C++ | JavaScript | TypeScript | PHP | C# | Python | Visual Basic | Ruby
RGBABitmapImageReference imageReference = CreateRGBABitmapImageReference();
StringReference errorMessage = new StringReference();
double [] xs = {-2, -1, 0, 1, 2};
double [] ys = {2, -1, -2, -1, 2};
boolean success = DrawScatterPlot(imageReference, 600, 400, xs, ys, errorMessage);
This code produces the following graph. The boundaries are automatically calculated. The plot is linearly interpolated and drawn in black, one pixel wide.
Complete example available in the following languages:
Java | C | C++ | JavaScript | TypeScript | PHP | C# | Python | Visual Basic | Ruby
ScatterPlotSeries series = GetDefaultScatterPlotSeriesSettings();
series.xs = new double [] {-2, -1, 0, 1, 2};
series.ys = new double [] {2, -1, -2, -1, 2};
series.linearInterpolation = true;
series.lineType = "dashed".toCharArray();
series.lineThickness = 2d;
series.color = GetGray(0.3);
ScatterPlotSettings settings = GetDefaultScatterPlotSettings();
settings.width = 600;
settings.height = 400;
settings.autoBoundaries = true;
settings.autoPadding = true;
settings.title = "x^2 - 2".toCharArray();
settings.xLabel = "X axis".toCharArray();
settings.yLabel = "Y axis".toCharArray();
settings.scatterPlotSeries = new ScatterPlotSeries [] {series};
RGBABitmapImageReference imageReference = CreateRGBABitmapImageReference();
boolean success;
StringReference errorMessage = new StringReference();
success = DrawScatterPlotFromSettings(imageReference, settings, errorMessage);
This code produces the following plot. In this plot the line is changed to dashed, 2 pixels wide, light gray. A title has been added and labels for each axis.
Convert to PNG and write to file:
if(success){
double[] pngdata = ConvertToPNG(imageReference.image);
WriteToFile(pngdata, "plot.png");
DeleteImage(imageReference.image);
}
1: "dotted", 2: "dashed", 3: "dotdash", 4: "longdash", "solid" (not shown)
1: "triangles", 2: "dots", 3: "crosses", 4: "circles", 5: "filled triangles" (see blow), 6: "pixels" (see blow)
ScatterPlotSettings
ScatterPlotSeries
RGBABitmapImageReference canvasReference1 = CreateRGBABitmapImageReference();
RGBABitmapImageReference canvasReference2 = CreateRGBABitmapImageReference();
RGBABitmapImageReference canvasReference3 = CreateRGBABitmapImageReference();
RGBABitmapImageReference canvasReference4 = CreateRGBABitmapImageReference();
RGBABitmapImage combined = CreateImage(250*2, 200*2, GetWhite());
RGBABitmapImage image1, image2, image3, image4;
double [] xs = {-2, -1, 0, 1, 2};
double [] ys1 = {2, -1, -2, -1, 2};
double [] ys2 = {-2, 1, 2, 1, -2};
double [] ys3 = {0, 1, 2, 3, 4};
double [] ys4 = {0, -1, -2, -3, -4};
boolean success;
success = DrawScatterPlot(canvasReference1, 250, 200, xs, ys1);
success = success && DrawScatterPlot(canvasReference2, 250, 200, xs, ys2);
success = success && DrawScatterPlot(canvasReference3, 250, 200, xs, ys3);
success = success && DrawScatterPlot(canvasReference4, 250, 200, xs, ys4);
if(success){
image1 = canvasReference1.image;
image2 = canvasReference2.image;
image3 = canvasReference3.image;
image4 = canvasReference4.image;
DrawImageOnImage(combined, image1, 0, 0);
DrawImageOnImage(combined, image2, 0, 200);
DrawImageOnImage(combined, image3, 250, 0);
DrawImageOnImage(combined, image4, 250, 200);
}
This code maps coordinates in the plot (x, y)
to pixel coordinates on the image (xPixels, yPixels)
. This mapping is based on the automatically generated plot settings given an image image
and an array of numbers xs
or ys
.
xPixels = MapXCoordinateAutoSettings(xPlot, image, xs);
yPixels = MapYCoordinateAutoSettings(yPlot, image, ys);
When using 'DrawScatterPlotFromSettings' use 'MapXCoordinateBasedOnSettings' and 'MapYCoordinateBasedOnSettings' instead, as it will take into account all settings when mapping the coordinate.
xPixels = MapXCoordinateBasedOnSettings(xPlot, settings);
yPixels = MapYCoordinateBasedOnSettings(yPlot, settings);
The following code draws a line from (0, 0) to (1, 1) in the plot's coordinate system. The coordinates must be mapped to pixel coordinates before drawing. The mapping is based on the automatic settings calculated from the image dimensions and plot data.
RGBABitmapImageReference canvasReference = CreateRGBABitmapImageReference();
double [] xs = {-2, -1, 0, 1, 2};
double [] ys = {-2, 1, 2, 1, -2};
boolean success;
StringReference errorMessage = new StringReference();
success = DrawScatterPlot(canvasReference, 600, 400, xs, ys, errorMessage);
if(success){
double x1 = MapXCoordinateAutoSettings(0, canvasReference.image, xs);
double y1 = MapYCoordinateAutoSettings(0, canvasReference.image, ys);
double x2 = MapXCoordinateAutoSettings(1, canvasReference.image, xs);
double y2 = MapYCoordinateAutoSettings(1, canvasReference.image, ys);
DrawLine(canvasReference.image, x1, y1, x2, y2, 2, GetGray(0.3));
}
This will produce the following plot.
Other custom drawing tools available:
(x1, y1)
to (x2, y2)
.(x, y)
.(x, y)
with a width w
and heigh h
.(x, y)
and radius r
.(x, y)
and distance from center to the corners h
.(x, y)
.(x, y)
.(x0, y0)
to (x1, y1)
with control point (cx, cy)
.(x0, y0)
to (x1, y1)
with control points (c0x, c0y)
and (c1x, c1y)
.