Still trying to draw histograms in VisAD...

Hi,

I must say, I'm feeling pretty dense about this, but I just can't see how to do this. At the bottom of this message is a Jython program (based on Java code by Ugo Taddei) that attempts to plot a bar chart of some synthetic data (random numbers mostly between 0 and 0.1, but with a '0' and a '1' thrown in for good measure), where the height of each bar represents the data. Of course, it's not much good without a scale, so I've added a set of dummy variables to generate that (everything with a '2' at the end is related to this). (Bill recommended this as the way to go.)

Now, as you can see, I do indeed have bars whose height seems roughly proportional to the data, and I do have a scale. But for some reason that I absolutely can't fathom, the bars start in the middle of the graph. In other words, I have no idea how to make it so that the bars start down on the horizontal axis. In other words, how do I impose the same scale on the two sets of data? Using setRange on the pBar maps does change the display, but nothing seems to be able to affect where the zero is.

I'm pretty much at my wit's end here. I can't use graph.py or subs.py, because I need a stand-alone application (I have one - I'm just trying to get this last thing to work in order to make it usable!) I've had a look through the developer's guide, and I didn't see anything that seemed relevant. I really could use more examples - I'd have given up long ago without Ugo's Java examples. Have I overlooked something? Perhaps there is more documentation out there that I've missed. Somebody else must have done this kind of thing before!

I really need help with this one. Thanks in advance.

Frank Gibbons

PhD, Computational Biologist,
Harvard Medical School BCMP/SGM-322, 250 Longwood Ave, Boston MA 02115, USA.
Tel: 617-432-3555 Fax: 617-432-3557 http://llama.med.harvard.edu/~fgibbons
#!/usr/bin/env jython

import java
from visad import *
from visad.python.JPythonMethods import *
from visad.java2d import DisplayImplJ2D
from javax.swing import *
from java.awt import Toolkit

def make_pBars():
    """Make a vertical bar, to be used by a ScalarMap."""
    # initialize pBars: 2 vertex (with (x,y,z), where z is ignored)
    # and coordinates: draw some "arbitrary" line; it will be scaled according
    # to values of RealType intensity
    pBars = VisADLineArray()
    pBars.vertexCount = 2
    x = 1.0
    pBars.coordinates = [0.0, 0.0, 0.0, # bottom
                         0.0, x, 0.0]   # top
    return pBars

def main():
    day = getRealType("day")
    precip = getRealType("Precipitation")
    precip2 = getRealType("Precipitation2")
    func_Prec = FunctionType(day, precip)  # precip = f( days )
    func_Prec2 = FunctionType(day, precip2)  # precip = f( days )

    index = RealType("index")
    d_p_tuple = RealTupleType(day, precip)
    d_p_tuple2 = RealTupleType(day, precip2)
    func_i_tuple = FunctionType(index, d_p_tuple)
    func_i_tuple2 = FunctionType(index, d_p_tuple2)

    Ndays = 30
    firstDay, lastDay = 0, Ndays
    daySet = Linear1DSet(day, firstDay, lastDay, Ndays)
    ff_Prec = FlatField(func_Prec, daySet)
    ff_Prec2 = FlatField(func_Prec2, daySet)

    index_set = Integer1DSet(index, Ndays)

    xMap = ScalarMap(day, Display.XAxis)
    yMap = ScalarMap(precip2, Display.YAxis)
    pBarMap = ScalarMap(precip, Display.Shape)
    pBarScaleMap = ScalarMap(precip, Display.ShapeScale)

    display = DisplayImplJ2D("display")
    GMC = display.getGraphicsModeControl()
    GMC.setScaleEnable(1)

    for map in (xMap, yMap, pBarMap, pBarScaleMap): #
        display.addMap(map)
        map.setScaleEnable(1)

    yMap.setRange(0, 1.0)
    pBarMap.setRange(0, 1.0)
    pBarScaleMap.setRange(0, 1.0)

    pBars = make_pBars()
    scontrol = pBarMap.getControl()
    scontrol.setShapeSet(Integer1DSet(1)) # only one value
    scontrol.setShapes([pBars])

    samples_Precip = [] #zeros(Ndays).astype(Float) # precipitation values:
    point_vals =  []#arange(Ndays).astype(Float)
    from random import *
    for i in range(0, Ndays):
        point_vals.append(i)
        samples_Precip.append( 0.1*random() )
    samples_Precip[0], samples_Precip[1] = 0.0, 1.0

    points_ff = FlatField(func_i_tuple, index_set)
    data = [ list(point_vals), list(samples_Precip) ]
    points_ff.setSamples(data)
    ff_Prec.setSamples([samples_Precip])

    PointMap = [ConstantMap(0.0, Display.Red),
                ConstantMap(1.0, Display.Green),
                ConstantMap(0.0, Display.Blue),
                ConstantMap(0.5, Display.PointSize)]

    refer_Pt = DataReferenceImpl("refer_Pt")
    refer_Pt.setData(points_ff)
    display.addReference(refer_Pt, PointMap)

    f = JFrame("Bars")
    f.getContentPane().add(display.getComponent())
    location = [300, 400] # of top left-hand corner
    f.setSize(location[0], location[1])
    screenSize = Toolkit.getDefaultToolkit().getScreenSize()
    f.setLocation(screenSize.width/2 - location[0]/2,
                     screenSize.height/2 - location[1]/2)
    f.setVisible(1)


if __name__ == "__main__":
    main()



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