Incorrect points positions

i am trying to make Graph,its almost done…last thing i need to fix,is points positions…i completely dont know why they placing like that…any ideas?


Graph.pde:

ArrayList<Float> arr = new ArrayList<Float>();
GraphView graph = new GraphView(0,0,450,300,arr);

void setup() {
  size(600,600);
  graph.add(20);
  graph.add(10);
  graph.add(40);
  graph.add(10);
  graph.add(20);
  graph.add(10);
  graph.add(4);
  graph.add(1);
  graph.add(40);
  graph.add(32);
  graph.draw();
  image(graph.rendered,0,0);
}

void draw() {
  graph.onHoverCycle();
}

GraphView.pde:

import java.util.*;

class GraphView {
  public PGraphics rendered;
  public float width;
  public float height;
  public float x;
  public float y;
  public ArrayList<Float> arr = new ArrayList<Float>();
  private ArrayList<PVector> value_coords = new ArrayList<PVector>();
  private ArrayList<PVector> value_data = new ArrayList<PVector>();
  private ArrayList<Integer> vertical = new ArrayList<Integer>();
  private ArrayList<PVector> vert_coords = new ArrayList<PVector>();
  private ArrayList<Integer> horizontal = new ArrayList<Integer>();
  private ArrayList<PVector> horiz_coords = new ArrayList<PVector>();
  private int textsize = 0;
  private int textoffset = 5;
  protected boolean cycleready = false;
  public GraphView(float x,float y,float width,float height,ArrayList<Float> arr) {
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
    this.arr = arr;
    vertical.add(0);
    vertical.add(1);
  }
  public void add(float value) {
    arr.add(value);
    if(!isNull(horizontal,horizontal.size()-1)) {
      if(horizontal.size()<width/((textsize+textoffset)*horizontal.size())) {
        horizontal.add(horizontal.get(horizontal.size()-1)+1);
      } else {
        throw new IndexOutOfBoundsException("This version of code does not support that much values!size up your Graph Width or add less values!Seems like this Graph support only "+horizontal.size()+" values!"); 
      }
    } else {
      horizontal.add(1);
    }
    if(vertical.get(0)<value) {
      int size;
      if(value>=5) {
        size = 5;
      } else {
        size = (int)value;
      }
      vertical = split(size,(int)value);
    }
    value_data.add(new PVector(value,horizontal.get(horizontal.size()-1)));
    vert_coords = new ArrayList<PVector>();
    horiz_coords = new ArrayList<PVector>();
    value_coords = new ArrayList<PVector>();
    for(int i = 1;i < vertical.size()+1;i++) {
      vert_coords.add(new PVector(0,i*((this.height-textoffset)/vertical.size())-textsize/2));
    }
    for(int i = 1;i < horizontal.size();i++) {
      if(!isNull(horizontal,i-1)) {
        horiz_coords.add(new PVector(i*((this.width-textsize/textoffset)/horizontal.size()),this.height+textsize-textoffset));
      }
    }
    for(int i = 1;i<arr.size();i++) {
      if(horiz_coords.size()>0) {
        value_coords.add(new PVector(horiz_coords.get(i-1).x,compare(vertical,vert_coords,arr.get(i-1))));
      }
      if(i >= horiz_coords.size()) {
        value_coords.add(new PVector(horiz_coords.get(horiz_coords.size()-1).x,compare(vertical,vert_coords,value)));
      }
    }
  }
  public void print() {
    println("---DEBUG INFO---");
    println("Graph Name:"+this);
    println("Graph Coordinates:"+this.x+" "+this.y);
    println("Graph Dimensions:"+this.width+" "+this.height);
    println("Values Coordinates:"+PVectorArrayToString(value_coords));
    println("Values Data:"+PVectorArrayToString(value_data));
    println("Lines Coordinates:"+PVectorArrayToString(vert_coords));
    println("Collumns Coordinates:"+PVectorArrayToString(horiz_coords));
    println("Values:"+arr.toString());
    println("Lines:"+vertical.toString());
    println("Collumns:"+horizontal.toString());
    println("---DEBUG INFO---");
  }
  public void draw() {
    textsize = (int)(this.height/vertical.size()-textoffset*2);
    rendered = createGraphics((int)(this.width+textsize+textoffset),(int)(this.height+textsize+textoffset));
    rendered.beginDraw();
    rendered.rect(x+textsize+textoffset*2,y,x+this.width,y+this.height);
    for(int i = 1;i < vertical.size()+1;i++) {
      rendered.textSize(textsize);
      rendered.fill(0);
      rendered.text(vertical.get(i-1).toString(),0,i*((this.height-textoffset)/vertical.size()));
      rendered.stroke(200);
      rendered.line(x+textsize+textoffset*2,i*((this.height-textoffset)/vertical.size())-textsize/2,x+textsize+textoffset*2+this.width,i*((this.height-PI)/vertical.size())-textsize/2);
      vert_coords.add(new PVector(0,i*((this.height-textoffset)/vertical.size())-textsize/2));
    }
    for(int i = 1;i < horizontal.size()+1;i++) {
      rendered.textSize(textsize);
      rendered.fill(0);
      if(!isNull(horizontal,i-1)) {
        rendered.text(horizontal.get(i-1).toString(),i*((this.width-textsize/textoffset)/horizontal.size()),this.height+textsize-textoffset);
      }
    }
    for(int i = 1;i < value_coords.size()+1;i++) {
      if(!isNull(value_coords,i)) {
        rendered.stroke(0,255,0);
        rendered.strokeWeight(3);
        rendered.line(value_coords.get(i).x+10,value_coords.get(i).y+10,value_coords.get(i-1).x+10,value_coords.get(i-1).y+10);
      }
    }
    for(int i = 1;i < value_coords.size()+1;i++) {
      rendered.fill(0,255,0);
      rendered.strokeWeight(1);
      rendered.stroke(0);
      rendered.rect(value_coords.get(i-1).x+5,value_coords.get(i-1).y+5,10,10,25,25,25,25);
    }
    rendered.endDraw();
    cycleready = true;
    this.print();
  }
  public void onHoverCycle() {
    if(cycleready) {
      PGraphics graphic = createGraphics((int)this.width,(int)this.height);
      graphic.beginDraw();
      //graphic.rect(mouseX-25,mouseY-25,50,50);
      for(int i = 0;i < value_coords.size();i++) {
        PVector vec = (PVector)value_coords.get(i);
        if(mouseX<vec.x+15&&mouseX>vec.x+5&&mouseY<vec.y+15&&mouseY>vec.y+5) {
          image(rendered,x,y);
          String str = "Line:"+(int)value_data.get(i).x+" Collumn:"+(int)value_data.get(i).y;
          graphic.fill(0,0,0,127);
          float val = ((textsize/3.5)*str.length()+width/10)/1.75;
          float val2 = textsize+textoffset;
          if(vec.x+val-width/(width/val*PI)<width) {
            graphic.rect(vec.x-3-val2,vec.y,val,height/15,25,25,25,25);
            graphic.textSize(textsize/3.5);
            graphic.fill(255);
            graphic.text(str,vec.x+27-val2-(str.length()/textsize+textoffset),vec.y+15);
          } else {
            graphic.rect(vec.x+25-val2,vec.y,-val,height/15,25,25,25,25);
            graphic.textSize(textsize/3.5);
            graphic.fill(255);
            graphic.text(str,vec.x+5-(5*str.length()+width/10)-val2-(str.length()/textsize+textoffset),vec.y+15);
          }
          graphic.fill(0,255,255);
          graphic.rect(vec.x+5-val2,vec.y+5,10,10,25,25,25,25);
        } else {
          image(rendered,x,y);
        }
      }
      graphic.endDraw();
      image(graphic,x+textsize+textoffset,y);
    }
  }
  private ArrayList<Integer> split(int size,int val) {
    ArrayList<Integer> ret = new ArrayList<Integer>();
      for(int i = 1;i < size+1;i++) {
        ret.add(val*i/size);
      }
      Collections.reverse(ret);
      return ret;
  }
  private boolean isNull(ArrayList arr,int index) {
    boolean ret = false;
    try {
      Object obj = (Object)arr.get(index);
    } catch(NullPointerException e){
      ret = true;
    } catch(IndexOutOfBoundsException e2) {
      ret = true;
    }
    return ret;
  }
  public String PVectorArrayToString(ArrayList<PVector> arr) {
    final StringBuilder str = new StringBuilder();
    str.append("[");
    for(int i = 0;i < arr.size();i++) {
      str.append("(");
      str.append(String.valueOf(arr.get(i).x));
      str.append(",");
      str.append(String.valueOf(arr.get(i).y));
      str.append(")");
      if(!(i>=arr.size()-1)) {
        str.append(", ");
      }
    }
    str.append("]");
    return str.toString();
  }
  private float compareTo(float to,float from, float max) {
    return from/max*to;
  }
  private float compare(ArrayList<Integer> arr,ArrayList<PVector> arr1,float value) {
    float comp = 0;
    for(int j = 1;j<arr.size();j++) {
      if(value>arr.get(j)&&value<=arr.get(j-1)) {
          comp = compareTo(arr.get(j-1),arr1.get(j).y,arr1.get(j-1).y);
      }
    }
    return comp;
  }
}

UPD:coordinates for points are setting up in method add()

i play with

void setup() {
  size(700,600);
  graph.add(1);
  graph.add(2);
  graph.add(3);
  graph.add(4);
  graph.add(5);
  graph.add(6);
  graph.add(7);
  graph.add(8);
  graph.add(9);
  graph.add(10.0);
  graph.draw();
  image(graph.rendered,0,0);
}

get a graph

        value_coords.add(new PVector(horiz_coords.get(i-1).x,compare(vertical,vert_coords,arr.get(i-1))));
        println("i "+i+" horiz_coords.get(i-1).x "+horiz_coords.get(i-1).x+" arr.get(i-1) "+arr.get(i-1)+" compare(vertical,vert_coords,arr.get(i-1)) "+compare(vertical,vert_coords,arr.get(i-1)));

and see

i 1 horiz_coords.get(i-1).x 60.0 arr.get(i-1) 1.0 compare(vertical,vert_coords,arr.get(i-1)) 0.0
i 2 horiz_coords.get(i-1).x 120.0 arr.get(i-1) 2.0 compare(vertical,vert_coords,arr.get(i-1)) 0.0
i 3 horiz_coords.get(i-1).x 180.0 arr.get(i-1) 3.0 compare(vertical,vert_coords,arr.get(i-1)) 5.0
i 4 horiz_coords.get(i-1).x 240.0 arr.get(i-1) 4.0 compare(vertical,vert_coords,arr.get(i-1)) 5.0
i 5 horiz_coords.get(i-1).x 300.0 arr.get(i-1) 5.0 compare(vertical,vert_coords,arr.get(i-1)) 8.0
i 6 horiz_coords.get(i-1).x 360.0 arr.get(i-1) 6.0 compare(vertical,vert_coords,arr.get(i-1)) 8.0
i 7 horiz_coords.get(i-1).x 420.0 arr.get(i-1) 7.0 compare(vertical,vert_coords,arr.get(i-1)) 12.0
i 8 horiz_coords.get(i-1).x 480.0 arr.get(i-1) 8.0 compare(vertical,vert_coords,arr.get(i-1)) 12.0
i 9 horiz_coords.get(i-1).x 540.0 arr.get(i-1) 9.0 compare(vertical,vert_coords,arr.get(i-1)) 20.0

so that compare() statement should map the values to
360 - value(i)*?range?

where you find that function? compare(a,b,c) ?
java knows a “compare(a, b)”
possibly change to

map(value[i],0,valuemax,360,0)

@kll
why 360 -…?
why value[i]?value is float,not array…if you mean value from array so im using arr.get(i)…
how to use map?i tried,but result the same as i tried to fix(flat line on x axis)…

@kll i found that compareTo(int a,int b,int c) in…uhh…my friend said that would work…and that worked,but a bit strange…pattern of points is right…but inverted…less value = point goes up,higher value = point goes down…but Y coordinates in fact are not right…

yes, i must admit that that compare does something,
but the point position is very wrong.
and add there are loop errors. like play with one point only.

looks like that is not your code anyhow,
ask the one who made that code for help,
if you not want to program a graph from scratch yourself
i could recommend using a library, like
install / import

> graphica
https://jagracar.com/grafica.php
example already using your data:


import grafica.*;

void setup() {
  size(500, 350);
  background(200,200,0);

  int nPoints = 10;
  GPointsArray points = new GPointsArray(nPoints);

//  for (int i = 0; i < nPoints; i++) {
//    points.add(i, 10*noise(0.1*i));
//  }
  points.add(0, 20);
  points.add(1, 10);
  points.add(2, 40);
  points.add(3, 10);
  points.add(4, 20);
  points.add(5, 10);
  points.add(6,  4);
  points.add(7,  1);
  points.add(8, 40);
  points.add(9, 32);
  // Create a new plot and set its position on the screen
  GPlot plot = new GPlot(this);
  plot.setPos(25, 25);
  // or all in one go
  // GPlot plot = new GPlot(this, 25, 25);

  // Set the plot title and the axis labels
  plot.setTitleText("A very simple example");
  plot.getXAxis().setAxisLabelText("x axis");
  plot.getYAxis().setAxisLabelText("y axis");

  // Add the points
  plot.setPoints(points);

  // Draw it!
  plot.defaultDraw();
}

2018-12-20_06-14-29_snap

Wha…not my code?why did you think that?i making that thing 4 days without watching open sources of any libs…

ok, so back to your compare function:

you send in 3 parameter, the “value” is the third
and you use it in

if(value>arr.get(j)&&value<=arr.get(j-1)) {

but not send it into

comp = compareTo(arr.get(j-1),arr1.get(j).y,arr1.get(j-1).y);

so the resulting y position has not much to do with the “value”

pls note, the “arr” what you use inside the compare function
is a local “arr”, NOT the “arr” from main what contains your list of “values”
it is actually

vertical

array what you are handing down into this function

possibly change that naming so you not get confused.

and to show you what i ref to at the first post,
i try like this, i know range is not correct… just first step.

  private float compare(ArrayList<Integer> arr0,ArrayList<PVector> arr1,float value) {
    float comp = 0;
//    for(int j = 1;j<arr0.size();j++) {
//      if(value>arr0.get(j)&&value<=arr0.get(j-1)) {
//          comp = compareTo(arr0.get(j-1),arr1.get(j).y,arr1.get(j-1).y);
//      }
//    }
    float span = (height-vert_coords.get(0).y)/vertical.get(0);
    comp = height-value*span;
    //println("compare_value "+value+" compare_comp "+comp);
    return comp;
  }

i think im dping something wrong…but thats resunlting me a perfect straight line…

that is how it looks here, but we both might have very different versions of you original code by now,
so why you not just post it again.

but anyhow, could you follow my argumentation that your original function
compare(a,b,value)
//does
comp = compareTo(arr.get(j-1),arr1.get(j).y,arr1.get(j-1).y);
so not use value to calculate the result comp

In your Original Code, the values are displayed correctly, but upside down. You just Need to Display it by subtracting the values from the Target yOffset. You can Check that by adding values until one is at what your Graph shows as 0. So that-value will Display ist correctly.

(Edit : and that last point doesn‘t get the right xOffset… i don‘t know why though…)

Additional Note : your really should make your Code more readable. It is also way too complicated for what it does (at least for what i think it should do).

play little bit, only for a view seconds, like use other numbers


that is what i am talking about for days.
the value is only used to be mapped ( by a if(value…) ) to some array of gridlines
and then, as you say, wrongly drawn top down.

i did an easier version of it, just make some changes to get it like urs


code:

float points[] = {10,20,30,40,50,60,70,80,90,0}; //values
int pl = points.length;
int min = 0,max = 100,valueCount = 10; //min and max value of points[], how many vertical values u want
float w,h; //just some stuff to keep things away from edges
void setup() {
  size(600,600);
  w = width * 0.1;
  h = height* 0.1;
}
void draw() {
  background(0);
  stroke(255);
  noFill();
  rect(w,h,w*8,h*8);
  for(int i = 0; i < pl; i++) { //drawing points and lines
    stroke(0,255,0);
    strokeWeight(10);
    point(map(i,0,pl,w,width-w),map(points[i],max,min,h,h*9));
    strokeWeight(2);
    line(map(i,0,pl,w,width-w),map(points[i],max,min,h,h*9),map((i+1 < pl)? i+1:i,0,pl,w,width-w),map(points[(i+1 < pl)? i+1 : i],max,min,h,h*9));
  }
  textAlign(3,3); //text stuff
  textSize(15);
  stroke(255);
  strokeWeight(1);
  for(int i = 0; i < valueCount+1; i++) { //verical text
    text(floor(map(i,0,valueCount,min,max)) + "",w*0.6,map(i,0,valueCount,height - h,h));
    line(w*0.8,map(i,0,valueCount,height - h,h),w*0.9,map(i,0,valueCount,height - h,h));
  }
  for(int i = 0; i < pl; i++) { //horizontal text
    text((i+1) + "",map(i,0,pl,w,width-w),height-h*0.9);
  }
  noLoop();
}

i know its not the best, but i hope that does the trick

oh and if u can change it by setting few values diffrently. (i hope u can get it by code)

1 Like

and it does map all the values
(and i hope its compatible with all widths and heights, i tested 600,600 and 600,900, but i dont know if others work)
and u dont need to change how many points u have, just change: float points[] = {…};

Quick tip: Put surface.setResizable(true); in void setup() to make the window resizable, to be able to check the compatibility easily.

I know, but I was in a sort of rush(idk why) so I didnt remember to do that. Or I could just di surface.setsize so I could do it inside of program with like 1 fps

thanks,that helped me figure out the problem