transpose the graph to the left

edited April 2017 in Library Questions

Is it possible to transpose the barcharts to the left instead of the right when transposeAxis(true)? Please help Example Code:

import org.gicentre.utils.stat.*;
void setup()
{
  size(300,200);

  barChart = new BarChart(this);
  barChart.setData(new float[] {0.76, 0.24, 0.39, 0.18, 0.20});

  // Scaling
  barChart.setMinValue(0);
  barChart.setMaxValue(1);

  // Axis appearance
  textFont(createFont("Serif",10),10);

  barChart.showValueAxis(true);
  barChart.setValueFormat("#%");
  barChart.setBarLabels(new String[] {"Cynthia","Daniel","Eli",
                                       "Fasil","Gertrude"});
  barChart.showCategoryAxis(true);

  // Bar colours and appearance
  barChart.setBarColour(color(200,80,80,150));
  barChart.setBarGap(4);

  // Bar layout
  barChart.transposeAxes(true);
}
void draw()
{
  background(255);
  barChart.draw(15,15,width-30,height-30); 
}

Answers

  • edited April 2017

    @leeoko1 --

    "transposeAxes()" means that flip or swap -- x axis = y; y axis = x.

    translate means slide -- x = x + 1; y = y + 1

    What are you trying to do?

  • This is an idea. You could just implement a FloatList as a container for your data. I am assuming that it is enough to set the elements in the barChart. You might need to reset the labels as well.

    The barChart part is not tested.

    Kf

    float[] data=new float[] {0.76, 0.24, 0.39, 0.18, 0.20};
    
    void draw()
    {
      background(255);
      barChart.draw(15,15,width-30,height-30);
    }
    
    void keyReleased(){
    
      if(key!='r')
         return;
    
      FloatList fdata=new FloatList(data);
      println("\n\nReverting....");
      fdata.reverse();
      data=fdata.array();
      barChart.setData(data);   //Not tested....
    }
    
  • edited April 2017

    @jeremydouglass Thanks for the reply

    after applying the transpose function from gicentre, the graph changes its axis, X axis = y and Y axis= x. The bars are drawn from left to right. I would like to have it drawn from right to left.

  • @kfrajer Thanks for the reply I tested the code, it changes the order of the data from last value to first value instead first value to last value. but this is not what I am looking for. Can the reverse () apply to the bar chart so that it flips the chart(sideway)* instead of the values?

    *reflect on the y-axis

  • edited May 2017

    @leeoko1 -- Ah, I think I get it. You want your bar data to present like this:

    I don't think this is possible using the API -- I think it may require a hack.

    1. there are no right-aligned layout settings listed in the BarChart API http://gicentre.org/utils/reference/org/gicentre/utils/stat/BarChart.html
    2. setReverseCategories works on categories (the transposed Y) -- not on the values http://gicentre.org/utils/reference/org/gicentre/utils/stat/BarChart.html#setReverseCategories-boolean-
    3. you cannot reverse the inputs by setting setMinValue / setMaxValue on the transposed X axis -- if min>max nothing is displayed http://gicentre.org/utils/reference/org/gicentre/utils/stat/BarChart.html#setMinValue-float-

    The hack:

    Draw the BarChart mirrored using scale(-1,1):

    void draw()
    {
      background(255);
      scale(-1,1);
      translate(-width,0);
      barChart.draw(15,15,width-30,height-30); 
    }
    

    ...however this also mirrors the axis label text, so it would require drawing several times: draw the x axis, then draw the y axis (shifted right), then draw the data with no axes or labels (mirrored). Awkward, but you could turn the whole three-step procedure into a function.

Sign In or Register to comment.