Re: newbie- can I make a bar graph?

Hi Stewart and hi all,

I've got some code to draw a 2D display with "bars". I've used
VisADLineArray rather than VisADQuadArray, because I've got a display
showing some 365 bars, which are packed up together so closely that one
don't want them thick. (Thickness is nevertheless controlled with
constant maps; see code below).

The idea here is to plot daily precipitation values as "bars" (i.e.
thick lines).

So here we go:

day =   new RealType( "day", null, null );
precip  =  new RealType( "Precipitation", null, null );

// the function precip = f( days )
FunctionType func_Prec = new FunctionType( day , precip );  

// here the day values, as a set, we'll have 365 values
daySet = (Set) new Linear1DSet(  day  , (float) firstDay, (float)
lastDay, 365);

// the flat field
ff_Prec = new FlatField( func_Prec, daySet ); 

// the line array
 pBars = new VisADLineArray();

// scalar maps
xMap    = new  ScalarMap( day, Display.XAxis );
yMap    = new  ScalarMap( precip, Display.YAxis);

// more scalar maps: 1st for shaps, and 2nd for shape scale, i.e,
responsible for sacling the shape according to RealType precip

pBarMap = new ScalarMap(precip, Display.Shape);
pBarScaleMap = new ScalarMap(precip, Display.ShapeScale);

// create a dispay...
display1 = new DisplayImplJ2D("display1");
// initalize pBars: 2 vertex (with (x,y,z), where z is ignored)
// and coordinates: draw some "arbitrary" line; it's be scaled according
to values of RealType precip

pBars.coordinates = new float[] {0.0f,  0.0f, 0.0f,    0.0f, -0.10f,
// let samples_precip be you precipitatios values:

float[][] samples_Precip = new float[ 1 ][ 365 ];

// fill array with values...
// code to fill array...

// set samples:

// adda maps to display:

display1.addMap( xMap );
display1.addMap( yMap1);

// get shapes control:

ShapeControl scontrol = (ShapeControl) pBarMap.getControl();
scontrol.setShapeSet(new Integer1DSet(1));  // only one value
scontrol.setShapes(new VisADGeometryArray[] {pBars});

// constant map to set bar color and thickness

        ConstantMap[] PrecMaps = {new ConstantMap(0.20f, Display.Red),
                           new ConstantMap(0.50f, Display.Green),
                           new ConstantMap(1.0f, Display.Blue),
                           new ConstantMap(01.50f, Display.LineWidth)};

// set reference's data
refer_Pre = new ReferenceImpl("refer_Pre" );

refer_Pre.setData( ff_Prec  );

// add reference to display, using the constant maps

display1.addReference( refer_Pre, PrecMaps); 
// show display...

I hope it works, because I cut and pasted the code above from a more
complex application. You can see the resulting display at:

You could also use a Gridded2D (?) set (actually, and array of those),
which is by far simpler (as Bill said, VisAD sahpes are rather complex:
some of the credits for code above goes to Bill, anyway; especially the
magic parts ;-)

hope it has helped clear up a bit.



  • 2000 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: