Introduction | Back to top |
So why develop yet another? There are lots of reasons:
What is usually the case in science is that we need histograms, scatterplots, and functions, and this is what this package provides (so far just scatterplots and histograms but functions are on the way). No bar charts, pie charts, etc, just basic histograms and scatter plots.
Access | Back to top |
<script src="/location/plots3.7.js"></script>where "location" is of course dependent on where you put the code.
<div id="mydiv"></div>Note that if you leave off the </div>, then HTML won't complain but the entire hierarchy of what's in that div will be messed up. So don't forget to close the div!!
let plot = new UMD_plot("mydiv");where "mydiv" is the id of the <div> that you put in step 2.
All plots, whether histograms,scatter plots, or functions (or whatever is implemented) are drawn in an HTML5 canvas (<canvas>) and that is created in the constructor ("new..."). See below for details.
Version | Back to top |
let plot = new UMD_plot("mydiv"); . . . console.log(plot.version);
Usage | Back to top |
To make a scatterplot, let's say you have some data you want to plot, here we will make an array called y and fill it with 100 random numbers.
let y = []; for (var i=0; i<100; i++) x.push(Math.random());If the <div> id is "divid", then we invoke a new UMD_plot object via:
let plot = new UMD_plot("divid");This will set up all the values and methods for plotting the function onto a canvas that will live inside the "divid" div. To plot, you do this:
plot.plotit(options)The variable options is a javascript object that you fill in, and it will contain things like the size of the plot in pixels, the data to plot, colors, fonts, etc. The table below will show you the available options, and the defaults.
The following lists the different ways available to show the plot
{ data: y, // y is an array of data points for the vertical type: "line", // or "circle" or "box", or "rect" not case sensitive color: "green", // or "blue", or "#31421" or "rgb(122,32,51)", not case sensitive size: 0.5, // line thickness for type "line", radius for "circle", side for "box" divide_by: 100, // divide each data point by this value (default=1) fill: true // true for fill, false for stroke (open box, open circle, not applied to "line") }This means that if you wanted to have the above object in the options, you would do this:
let yobj = { data: y, type: "line", color: "green", size: 0.5, fill: true }; options = { ydata: [yobj], . . . };If you wanted to plot 2 different data sets, say y1 and y2, then you would need a yobj1 for y1 and yobj2 for y2 and you would put them into your options like this:
options = { ydata: [yobj1,yobj2], . . . };and of course with all other options you might want (see below)
Also, if you just want to plot ydata values sequentially, using the index as the x-axis points, just do this:
xdata: "sequential"Actually, that's the default so if you leave it out, it should still work, and plot the values in the ydata objects sequentially.
let plot = UMD_plot("mydiv"); let yobj1 = { data: y, type: "line", color: "blue", size: 0.5, }; let yobj2 = { data: y, type: "circle", color: "red", size: 3, fill: false }; options = { ydata: [yobj1,yobj2], title: "this is the overall plot title", xtitle: "this goes along the bottom horizontal", ytitle: "this goes along the vertical to the left" width: 500, height: 600, }; . . . plot.plotit(options);There is an additional way to plot individual x,y points. Create an item in options called "points", which will hold an array of objects, with the objects being pairs of points to plot. The object needs the following items defined: x,y,type,color,fill,size (type, color, fill, size are defined the same for ydata objects, defaults are "circle", "blue", true, and 3 respectively...see above) For instance, if you want to plot the point x,y=2,5 as a blue closed circle and x,y=1,4 as a red open circle both of radius 5 then add the option:
options = { . . . points: [ {x:2,y:5,type:"circle",size:"5",color:"blue",fill:true}, {x:1,y:4,type:"circle",size:"5",color:"red",fill:false} ], . . . }
Options | Back to top |
Option | Description | Example | Default |
---|---|---|---|
xdata | horizontal data points | array [] or "sequential" | see above |
ydata | vertical data point | array of objects | see above |
points | object with individual sets of points | [{x:2,y:3},{x:1,y:3.4}] | see above |
background | background color | "grey" or "#A5A5A5" | "white" |
use_xlabels | boolean to control using x-labels (see below) | true | false |
xlabels | array of labels along x axis | [0,2,4,6] | n/a |
title | title string | "My Title" | blank (see below) |
font | this is the font used for everything | "14px arial" | "Courier" |
font_size | size of the font | 14 | 12 |
font_color | color of all fonts | "white" | "black" |
grid | boolean to control displaying grid | false | true |
grid_color | color of horizontal and vertical grid lines | "black" | "#d0d0d0" |
xgrid | number of grid lines and labels along x-axis | 4 | 5 |
showxgrid | boolean for showing the vertical grid lines | false | true |
ygrid | number of grid lines and labels along y-axis | 5 | 6 |
showygrid | boolean for showing the horizontal grid lines | false | true |
width | canvas width in pixels | 500 | 400 |
height | canvas height in pixels | 500 | 400 |
xtitle | horizontal axis title string | "Age" | blank |
ytitle | vertical axis title string | "Number" | blank |
xmin | horizontal min value for displaying | 1 | minimum x value from array |
xmax | horizontal max value for displaying | 1 | maximum x value from array |
ymin | vertical axis min for displaying | 0 | maximum y value from array |
ymax | vertical axis max for displaying | 20 | minimum y value from array |
The title string allows you to have different colors for different parts of the title. For instance, if you want to have a title that looks like this:
The blue part of the title followed by the red part of the title
you can do this by adding this to the title string that you pass:
/"blue"/The blue part of the title /"black"/folowed by /"red"/the red part of the titleWhat is bewteen the "/" characters will be the color for the words coming after.
Histograms | Back to top |
let hist = UMD_plot("divid");To make the histogram, you call the method histogram with options:
hist.histogram(options);The options argument is the same object that you use for scatterplot, with an additional argument that is used for histograms:
options = { title : "your title", . . . hist : { bins: "number of bins", data: "1-d array of points to bin", xlow: "low edge of low bin", xhigh: "high edge of high bin", color: "color of the histogram", // default is "blue" fill: "boolean for fill (true) or stroke (false)", // default is true stats: "boolean, true for display, false to not display", // default is false errors: "boolean true for sqrt(n)", // default is false ecolor: "color of error bars" // default is "yellow" } }Calling hist.histogram(options) will bin the histogram and pipe the arguments off to the scatterplot method for drawing.
Interactions | Back to top |
If you double click on the x-axis labels, it will restore to the original limits
Double click on the y-axis labels to reset the vertical scale back to defaults.
Testing | Back to top |
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law.