JSPlot Documentation: Vector graphics
Tutorial
- The plotting canvas
- Plotting data
- Graph plotting styles
- Plotting functions
- Vector graphics
API reference
Introduction to the vector graphics classes
As well as graphs, JSPlot can draw some basic vector graphics objects – rectangles, circles, ellipses, arrows and text – onto the drawing canvas. They can be used on their own, or alongside graphs, for example to add captions or highlight parts of a figure. Each object is added to a JSPlot_Canvas in the same way as a graph, as described on the plotting canvas page. The settings for each class are listed in the vector graphics API reference.
Shapes and text
Each object is created from an object of settings. Its position is given by its origin, in pixels, with x increasing to the right and y increasing downwards. For a rectangle the origin is its top-left corner, and for a circle or ellipse it is the centre:
var canvas = new JSPlot_Canvas({
"rectangle": new JSPlot_Rectangle({
"origin": [0, 0],
"width": 160,
"height": 100
}),
"circle": new JSPlot_Circle({
"origin": [300, 50],
"radius": 50
}),
"ellipse": new JSPlot_Ellipse({
"origin": [500, 50],
"major_axis": 60,
"minor_axis": 30,
"position_angle": 45
}),
"rectangle_label": new JSPlot_Text({"origin": [80, 130], "text": "JSPlot_Rectangle"}),
"circle_label": new JSPlot_Text({"origin": [300, 130], "text": "JSPlot_Circle"}),
"ellipse_label": new JSPlot_Text({"origin": [500, 130], "text": "JSPlot_Ellipse"})
}, {});
canvas.renderToCanvas($("#tutorial_vector_shapes")[0]);
An ellipse's major_axis and minor_axis are its radii along its longest and shortest directions, and its position_angle is the angle of its long axis, in degrees clockwise from vertical. The canvas is sized automatically to fit everything on it, so only the positions of objects relative to one another matter.
Colors, outlines and stacking order
Shapes have a fillColor for their interior, and a strokeColor and strokeLineWidth for their outline. Set a color to null to leave out the fill or the outline. Colors are JSPlot_Color objects, and their fourth component sets their opacity, from 0 (transparent) to 1 (opaque).
Where objects overlap, the one with the higher z_index is drawn on top. Here, the three translucent circles are drawn above the grey square, because they have a higher z_index:
var canvas = new JSPlot_Canvas({
"background": new JSPlot_Rectangle({
"origin": [-140, -140],
"width": 280,
"height": 280,
"fillColor": new JSPlot_Color(0.85, 0.85, 0.85, 1),
"strokeColor": null
}),
"red": new JSPlot_Circle({
"origin": [0, -40], "radius": 70, "z_index": 1, "strokeColor": null,
"fillColor": new JSPlot_Color(1, 0, 0, 0.5)
}),
"green": new JSPlot_Circle({
"origin": [-40, 30], "radius": 70, "z_index": 1, "strokeColor": null,
"fillColor": new JSPlot_Color(0, 0.8, 0, 0.5)
}),
"blue": new JSPlot_Circle({
"origin": [40, 30], "radius": 70, "z_index": 1, "strokeColor": null,
"fillColor": new JSPlot_Color(0, 0, 1, 0.5)
})
}, {});
canvas.renderToCanvas($("#tutorial_vector_colors")[0]);
Arrows and text
A JSPlot_Arrow is drawn from its origin to its target. Its arrowType chooses where the arrow heads go: "single" (at the target), "back" (at the origin), "double" (at both ends) or "none" (a plain line).
A JSPlot_Text object's h_align and v_align settings control how the text sits relative to its origin. Its font is set with fontSize (in pixels), fontFamily, fontWeight and fontStyle:
var canvas = new JSPlot_Canvas({}, {});
var arrow_types = ["single", "back", "double", "none"];
for (var i = 0; i < arrow_types.length; i++) {
canvas.addItem("arrow_" + i, new JSPlot_Arrow({
"origin": [0, i * 50],
"target": [200, i * 50],
"arrowType": arrow_types[i],
"strokeLineWidth": 2
}));
canvas.addItem("label_" + i, new JSPlot_Text({
"origin": [220, i * 50],
"text": 'arrowType: "' + arrow_types[i] + '"',
"h_align": "left",
"v_align": "center",
"fontFamily": "monospace"
}));
}
canvas.addItem("title", new JSPlot_Text({
"origin": [0, -50],
"text": "Arrow heads",
"h_align": "left",
"fontSize": 20,
"fontWeight": "bold"
}));
canvas.renderToCanvas($("#tutorial_vector_arrows_and_text")[0]);
Combining shapes with graphs
Vector graphics objects share the canvas with any graphs on it, and use the same pixel coordinates. A graph's origin is the bottom-left corner of its plotting area, so objects can be placed relative to it. This example highlights a region of a graph with a translucent rectangle, and adds a caption:
var canvas = new JSPlot_Canvas({
"my_graph": new JSPlot_Graph([
new JSPlot_FunctionEvaluator("sin(x)", {}, [Math.sin]).evaluate_linear_raster(0, 10, 200, false)
], {
"width": 600,
"widthIncludesAxes": false,
"aspect": 0.5
}),
"highlight": new JSPlot_Rectangle({
"origin": [60, -300],
"width": 120,
"height": 300,
"fillColor": new JSPlot_Color(1, 0.8, 0, 0.3),
"strokeColor": null,
"z_index": -1
}),
"caption": new JSPlot_Text({
"origin": [120, -310],
"text": "First peak",
"v_align": "bottom"
})
}, {});
canvas.renderToCanvas($("#tutorial_vector_with_graphs")[0]);
For another example of these classes in use, see the vector graphics demo.