Re: newbie- can I make a bar graph?

Hi Jen,

> Hi. I am new to VisAD but it seems really great. Problem is, it's so
> powerful that I'm having trouble figuring out the simple stuff. I've
> been looking at the (very helpful) tutorial at:
> 
> http://www.ssec.wisc.edu/~billh/tutorial/s2/Section2.html
> 
> I want to do a plot similar to the one in example P2_03 or P2_04, but
> insead of points to represent the data I would like bars. I want to make
> a bar graph, where the bottom of each bar is at a certain point on the x
> axis (in this example, maybe the midpoint of each bar would be at its
> corresponding x value) and the top of each bar is at the height of the
> corresponding y value. Do I have to make a VisADQuadArray for every bar
> I want or is there a simpler solution? Does anybody have/know of a
> simple Bar Chart example in 2d or 3d using VisAD?

You can probably do this using ScalarMaps to Display.Shape.
Say you have a FlatField 'field' with MathType
(index -> (mid, top)) and you want the midpoint of bars at 
mid values and tops of bars at top values.

You can probably do this using ScalarMaps to Display.Shape.
Say you have a FlatField 'field' with MathType
(index -> (mid, top)) and you want the midpoint of bars at
mid values and tops of bars at top values.  Then do
something like:

  // create new_field with (top - mid) values
  RealType dif = new RealType("dif");
  RealTupleType range = new RealTupleType(mid, top, dif);
  FunctionType new_func = new FunctionType(index, range);
  Set set = field.getDomainSet();
  int length = set.getLength();
  float[][] old_values = field.getFloats();
  float[][] new_values = new float[3][length];
  new_values[0] = old_values[0];
  new_values[1] = old_values[1];
  float min_mid = Float.MAXIMUM_VALUE;
  float max_mid = Float.MINIMUM_VALUE;
  float max_dif = Float.MINIMUM_VALUE;
  for (int i=0; i<length; i++) {
    if (new_values[0][i] < min_mid) min_mid = new_values[0][i];
    if (new_values[0][i] > max_mid) max_mid = new_values[0][i];
    new_values[2][i] = new_values[1][i] - new_values[0][i];
    if (new_values[2][i] > max_mid) max_mid = new_values[2][i];
  }
  FlatField new_field = new FlatField(new_func, set);
  new_field.setSamples(new_values);

  // some tuning values for display
  float dif_resolution = ...; // desired resolution of dif values
  int ndifs = 1 + (int) (max_dif / dif_resolution); // # of dif samples
  float width = ...; // width of bar (e.g., 0.02f)

  // set up Scalarmaps for display
  display.addMap(new ScalarMap(index, Display.XAxis));
  ScalarMap mid_map = new ScalarMap(mid, Display.YAxis);
  display.addMap(mid_map);
  mid_map.setRange(min_mid, max_mid);
  display.addMap(new ScalarMap(mid, Display.YAxis));
  ScalarMap dif_map = new ScalarMap(dif, Display.Shape);
  display.addMap(dif_map);

  // set up shapes
  ShapeControl control = (ShapeControl) dif_map.getControl();
  Linear1DSet dif_set = new Linear1DSet(dif, 0.0, max_dif, ndifs);
  float[][] difs = dif_set.getSamples();
  VisADQuadArray[] dif_shapes = new VisADQuadArray[ndifs];
  for (int i=0; i<ndifs; i++) {
    dif_shapes[i] = new VisADQuadArray();
    dif_shapes[i].vertexCount = 4;
    float hw = 0.5f * width;
    float hh = 0.5f * difs[0][i] * (2.0f / (max_mid - min_mid));
    dif_shapes[i].coordinates = new float[]
      {hw, hh, 0.0f,  -hw, hh, 0.0f,  -hw, -hh, 0.0f,  hw, -hh, 0.0f};
  }
  control.setShapeSet(dif_set);
  control.setShapes(dif_shapes);

I haven't tested this, so it may have bugs, but its the general
idea.

Cheers,
Bill
----------------------------------------------------------
Bill Hibbard, SSEC, 1225 W. Dayton St., Madison, WI  53706
hibbard@xxxxxxxxxxxxxxxxx  608-263-4427  fax: 608-263-6738
http://www.ssec.wisc.edu/~billh/vis.html


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