Contact: zeng  @  zegraph.com      Last update: September 2016

ZeGraph Examples

Example-1

load("zegraph.dll");

zg=zegraph(500,500,3);

// draw text

tx=zg.add("text");
tx.font(24);
tx.color(0,0,1);
[w,h]=tx.string("ABC<sym>alpha</sym><sup>123</sup>",0,0);
tx.rotatez(45);

zg.show();

tx.disable();

// draw a line in the default coordinate system

line=zg.add("line");
line.solid(3);
v=line.vertex();
v.add(0,0,0, 200,100,0);
zg.show();

// make the line look nicer

line.smooth(true);
zg.show();

// let's change the default colors

zg.color(1,1,1);
zg.bgcolor(0,0,0);
zg.show();

// the line uses its own color if it is set

line.color(1,1,0);
zg.show();

// the line uses per-vertex colors if they are set
color=line.color();
color.add(1,0,0,1, 0,1,0,1);
zg.show();

// a disabled object will not show

line.disable();
zg.show();
line.enable();
zg.show();

// save the image

zg.tofile("test.png");

Example-2

load("zegraph.dll");

zg=zegraph(900,900);

// add a plot

plot=zg.add("plot");

plot.scale(0.7,0.7,1);

// get and set the x-axis

xaxis=plot.xaxis(); 
xaxis.linewidth(3);
xaxis.range(0,10);
xaxis.title("X");
xaxis.tickmarks(0,10,2,2);
xaxis.ticksize(2);
xaxis.tickdigits(0);

// get and set the y-axis

yaxis=plot.yaxis(); 
yaxis.range(0,1);
yaxis.title("Y");
yaxis.tickmarks(0,1,0.2,0);
yaxis.tickdigits(1);

zg.show();

// By default, axises are in the center.
// We can anchor them in different places
// and give them different colors and font.

xaxis.color(0,0,1);
xaxis.font(15);
plot.xaxis(0,-1,0);     // move x-axis to ymin
plot.yaxis(-1,0,0);     // move y-axis to xmin

zg.show();

// Adding x- or y- axis as the second axis is easy.

axis=plot.add("axis");
axis.type("x",false);
axis.range(0,12);
axis.title("Month");
axis.tickmarks(0,12,1,0);
// get ymax in SCS
[x,y,z]=plot.global(0,1,0);
// move the axis to ymax
axis.translate(0,y,0);
// use custom tick lebals
axis.ticklabels("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",true);

zg.show();

// Now we remove objects added to the plot

plot.clear();

// A plot is in 3D, but its z-axis is disabled.
// To see the z-axis, active it and rotate the plot.

plot.xaxis(0,-1,-1);
plot.yaxis(-1,0,-1);

zaxis=plot.zaxis(-1,1,0);
zaxis.title("Z");
zaxis.range(-1,1);
zaxis.tickmarks(-0.5,1,0.5,0);

plot.scale(.5,.5,.5);
plot.rotatez(30);
plot.rotatex(-60);

// Use the plot to call the show function so that 
// you can use the mouse or arrow keys to rotate the plot. 

zg.show(plot);

Example-3

load("zegraph.dll");

zg=zegraph(800,800);

// add a globe

globe=zg.add("globe");

// make globe surface

poly=globe.surface(300);
poly.color(0,0,1);

// add coastlines and grid lines.

line=globe.gshhs("data/gshhs_c.b");
line.color(1,1,1);

line=globe.grid(15);
line.color(.7,.7,.7);

// now let's set the focus to look at Japan

globe.focus(139,36);
globe.light(139,36);

zg.show(globe);

// and draw a line and anchor an object

line=globe.line(139,36,180,0);
line.color(1,1,0);

poly=globe.add("polygon",139,36,0);
poly.color(1,0,0);
poly.sphere(5);

zg.show(globe);

// you can use an image as the texture of the globe surface and 
// set the light position according to time

globe.texture(300,"data/earth.png",true);

globe.sun(1,0,0,0);

zg.show(globe);

Example-4

load("zegraph.dll");

zg=zegraph(800,800);

// add a light to the graph

light=zg.add("light");
light.position(1,1,1);

// add a node to the light to hold objects
// rotating the node affect all objects in it

node=light.add("node");
node.rotatex(-60);

// add shapes to the node
// note the coordinate system is SCS

node2=node.add("node");
poly=node2.add("polygon");
poly.color(0,1,1);
poly.cylinder(100,30);
node2.translate(-30,0,0);

node2=node.add("node");
poly=node2.add("polygon");
poly.color(1,1,0);
poly.cone(100,30);
node2.translate(30,0,0);

poly=node.add("polygon");
poly.color(0,1,0);
poly.torus(200,30);

zg.show(node);

// apply material of jade to the torus

mat=poly.material();
mat.ambient(0.14,0.22,0.16,0.9);
mat.diffuse(0.54,0.89,0.63,0.9);
mat.specular(0.32,0.32,0.32,0.9);
mat.shininess(12.8);

zg.show(node);

Example-5

load("zegraph.dll");

zg=zegraph(800,500);

// add a plot to the graph

plot=zg.add("plot");
plot.scale(.7,.7,1);

// add coast line to the plot

xmin=100;
xmax=240;
ymin=10;
ymax=70;

line=plot.add("line");
line.gshhs("data/gshhs_c.b",xmin,xmax,ymin,ymax);

vert=line.vertex();
[x1,x2,y1,y2]=vert.map(0.5*(xmin+xmax));

// set plot ranges

axis=plot.xaxis();
axis.range(x1,x2);
axis.showticks(false);
axis.showlabels(false);
axis=plot.yaxis();
axis.range(y1,y2);
axis.showticks(false);
axis.showlabels(false);


// add lines as the map border

line=plot.add("line");
line.type("strip");
v=line.vertex();
for (y=ymin; y<=ymax; y++) {
    v.add(xmin,y,0);
}
v.add(xmax,ymax,0);
v.map(0.5*(xmin+xmax));

line=plot.add("line");
line.type("strip");
v=line.vertex();
v.add(xmin,ymin,0);
for (y=ymin; y<=ymax; y++) {
    v.add(xmax,y,0);
}
v.map(0.5*(xmin+xmax));

// add country boders to the plot

line=plot.add("line");
line.color(0,0,1);
line.gshhs("data/wdb_borders_l.b",xmin,xmax,ymin,ymax);

v=line.vertex();
[x1,x2,y1,y2]=v.map(0.5*(xmin+xmax));

zg.show(plot);

Example-6

load("zegraph.dll");

width=900;
height=600;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=-180;
xmax=180;
step=30;
ymin=-90;
ymax=90;

axis=plot.xaxis(0,-1,0);
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,step,0);
axis.tickdigits(0);
axis.title("Longitude");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,step,0);
axis.tickdigits(0);
axis.title("Latitude");

// add a line as frame

line=plot.add("line");
line.solid(2);
line.type("strip");
vert=line.vertex();
vert.add(xmin,ymax,0, xmax,ymax,0, xmax,ymin,0);

// now use a polygon for world map

poly=plot.add("polygon");
poly.type("quads");
vert=poly.vertex();
vert.add(xmin,ymin,0, xmax,ymin,0, xmax,ymax,0, xmin,ymax,0);
// move the polygon down slightly so that it won't mix with other objects
poly.translate(0,0,-0.1);

// use an image as texture

tx=poly.texture();
[s,t]=tx.image("data/earth.png");
ts=poly.texcoord();
ts.add(0,0, s,0, s,t, 0,t);

// add coastlines
line=plot.add("line");
line.gshhs("data/gshhs_c.b",xmin,xmax,ymin,ymax);
line.color(1,1,1);

zg.show();

Example-7

load("zegraph.dll", "matrix.dll", "netcdf.dll");

width=900;
height=600;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=0;
xmax=360;
step=30;
ymin=-90;
ymax=90;

axis=plot.xaxis(0,-1,0);
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,step,0);
axis.tickdigits(0);
axis.title("Longitude");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,step,0);
axis.tickdigits(0);
axis.title("Latitude");

// read surface temperature data; refer to netcdf and matrix libraries.

cdf=netcdf("data/2000010100.cdf");
cdf.variable("T995");
[nr,nc]=cdf.dims();
S=short(nr,nc);
S.import(cdf[*]);
D=double(S);
D*=cdf.scale_factor;
D+=cdf.add_offset;
// flip rows so that data are for lat from -90 to 90
D.flip("r");
// attached data at lon=0 to the end for lon=360
D=D|D[*,0];
nc++;
// data range
dmin=D.min();
dmax=D.max();
d=0.1*(dmax-dmin);
dmin+=d;
dmax-=d;
[dptr,n]=D.ptr();
// x-y data
X=double(nc);
X.fill(0,2.5);
[xptr,n]=X.ptr();
Y=double(nr);
Y.fill(-90,2.5);
[yptr,n]=Y.ptr();

// make colorbar

cbar=zg.add("colorbar");
cbar.add(0,0,1,dmin, 0,1,0,0.5*(dmin+dmax), 1,0,0,dmax);
cbar.interpolate(2);
cbar.labeldigits(1);
cbar.linewidth(2);
cbar.size(scale*width,15);
cbar.translate(0,scale*height/2+50,0);
cbar.discrete(true);

// now use a polygon for contour map

poly=plot.add("polygon");
poly.type("quads");
v=poly.vertex();
v.add(xmin,ymin,0, xmax,ymin,0, xmax,ymax,0, xmin,ymax,0);
poly.translate(0,0,-0.1);
tx=poly.texture();
[s,t]=tx.data(dptr,nr,nc,cbar);
ts=poly.texcoord();
ts.add(0,0, s,0, s,t, 0,t);

line=plot.add("line");
line.gshhs("data/gshhs_c.b",xmin,xmax,ymin,ymax);
line.color(1,1,1);

zg.show();

Example-8

load("zegraph.dll", "matrix.dll", "netcdf.dll");

width=900;
height=600;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=0;
xmax=360;
step=30;
ymin=-90;
ymax=90;

axis=plot.xaxis(0,-1,0);
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,step,0);
axis.tickdigits(0);
axis.title("Longitude");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,step,0);
axis.tickdigits(0);
axis.title("Latitude");

// read surface temperature data; refer to netcdf and matrix libraries.

cdf=netcdf("data/2000010100.cdf");
cdf.variable("T995");
[nr,nc]=cdf.dims();
S=short(nr,nc);
S.import(cdf[*]);
D=double(S);
D*=cdf.scale_factor;
D+=cdf.add_offset;
// flip rows so that data are for lat from -90 to 90
D.flip("r");
// attached data at lon=0 to the end for lon=360
D=D|D[*,0];
nc++;
// data range
dmin=D.min();
dmax=D.max();
d=0.1*(dmax-dmin);
dmin+=d;
dmax-=d;
[dptr,n]=D.ptr();
// x-y data
X=double(nc);
X.fill(0,2.5);
[xptr,n]=X.ptr();
Y=double(nr);
Y.fill(-90,2.5);
[yptr,n]=Y.ptr();

// make colorbar

cbar=zg.add("colorbar");
cbar.add(0,0,1,dmin, 0,1,0,0.5*(dmin+dmax), 1,0,0,dmax);
cbar.interpolate(2);
cbar.labeldigits(1);
cbar.linewidth(2);
cbar.size(scale*width,15);
cbar.translate(0,scale*height/2+50,0);

// coastlines

line=plot.add("line");
line.gshhs("data/gshhs_c.b",xmin,xmax,ymin,ymax);
line.color(.5,.5,.5);

// contour

d=(dmax-dmin)/10.0;
for (iso=dmin; iso<=dmax; iso+=d) {
    line=plot.add("line");
    v=line.contour(dptr,xptr,nc,yptr,nr,iso);
    v.flatten(1,1,0);
    c=cbar.get(iso);
    line.color(c[0],c[1],c[2]);
}

zg.show();

Example-9

load("zegraph.dll", "matrix.dll", "netcdf.dll");

width=900;
height=600;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=0;
xmax=360;
step=30;
ymin=-90;
ymax=90;

axis=plot.xaxis(0,-1,0);
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,step,0);
axis.tickdigits(0);
axis.title("Longitude");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,step,0);
axis.tickdigits(0);
axis.title("Latitude");

// read surface temperature data; refer to netcdf and matrix libraries.

cdf=netcdf("data/2000010100.cdf");
// u-wind
cdf.variable("U995");
[nr,nc]=cdf.dims();
S=short(nr,nc);
S.import(cdf[*]);
U=double(S);
U*=cdf.scale_factor;
U+=cdf.add_offset;
// v-wind
cdf.variable("V995");
S.import(cdf[*]);
V=double(S);
V*=cdf.scale_factor;
V+=cdf.add_offset;

D=sqrt(U*U+V*V);

// data range
dmin=D.min();
dmax=D.max();
d=0.1*(dmax-dmin);
dmin+=d;
dmax-=d;

// make colorbar

cbar=zg.add("colorbar");
cbar.add(0,0,1,dmin, 0,1,0,0.5*(dmin+dmax), 1,0,0,dmax);
cbar.interpolate(2);
cbar.labeldigits(1);
cbar.linewidth(2);
cbar.size(scale*width,15);
cbar.translate(0,scale*height/2+50,0);

for (i=0; i<nr; i++) {
    y=90-i*2.5;
    for (j=0; j<nc; j++) {
        x=1.25+j*2.5;
        line=plot.add("line",x,y,0);
        [r,g,b]=cbar.get(D[i,j]);
        line.color(r,g,b);
        line.vector(U[i,j],V[i,j],3);
    }
}

zg.show();

Example-10

load("zegraph.dll", "matrix.dll", "netcdf.dll");

width=900;
height=600;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=0;
xmax=100;
ymin=-1;
ymax=1;

axis=plot.xaxis();
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,20,0);
axis.tickdigits(0);
axis.title("X");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,0.5,0);
axis.tickdigits(1);
axis.title("Y");

X=double(100,1);
X.fill(0.5,1);
Y=cos(0.01*2*pi()*X);
D=X|Y|0;
[ptr,n]=D.ptr();

line=plot.add("line");
line.color(0,0,1);
line.dash(1.5);
line.smooth(true);
vert=line.vertex();
vert.add(ptr,n);

D=D[0:*:10,*];
[ptr,n]=D.ptr();
point=plot.add("point");
point.color(1,0,0);
point.size(10);
point.smooth(true);
vert=point.vertex();
vert.add(ptr,n);

zg.show();

Example-11

load("zegraph.dll", "matrix.dll", "netcdf.dll");

width=500;
height=300;
zg=zegraph(width,height);

// add a plot to the graph

scale=0.7;
plot=zg.add("plot");
plot.scale(scale,scale,1);

// set plot ranges

xmin=0;
xmax=100;
ymin=-1;
ymax=1;

axis=plot.xaxis();
axis.range(xmin,xmax);
axis.tickmarks(xmin,xmax,20,0);
axis.tickdigits(0);
axis.title("X");

axis=plot.yaxis(-1,0,0);
axis.range(ymin,ymax);
axis.tickmarks(ymin,ymax,0.5,0);
axis.tickdigits(1);
axis.title("Y");

X=double(100,1);
X.fill(0.5,1);
Y=cos(0.01*2*pi()*X);

zg.togifa("test.gif",50);

line=plot.add("line");
line.color(0,0,1);
line.smooth(true);
vert=line.vertex();

for (i=0; i<100; i++) {
    vert.add(X[i],Y[i],0);
    zg.togifa("add");
    csv(i);
}

zg.togifa("end");