Creating labels for a graph
in
Programming Questions
•
9 months ago
Hello, I have been given a large dataset to represent, and have come up with a radial diagram for a series of gyroscope entries in 3 dimensions. the code is as follows:
- String[] lines;
- float[] AX, AY, AZ, GX, GY, GZ;
- float scalar = 115;
- float sumAX, sumAY, sumAZ, sumGX, sumGY, sumGZ = 0.0;
- float maxGX, maxGY, maxGZ = 0.0; //Maximum values
- float minGX, minGY, minGZ = 0.0; //Minimum values
- float meanAX, meanAY, meanAZ, meanGX, meanGY, meanGZ = 0.0; //Mean averages
- float lqGX, lqGY, lqGZ = 0.0; //Lower quartile values
- float medGX, medGY, medGZ = 0.0; //Median averages
- float uqGX, uqGY, uqGZ = 0.0; //Upper quartile values
- float rangeGX, rangeGY, rangeGZ = 0.0; //Interquartile range
- float MaxGX, MaxGY, MaxGZ = 0.0;
- float MeanGX, MeanGY, MeanGZ = 0.0;
- float MinGX, MinGY, MinGZ = 0.0;
- float upperGX, upperGY, upperGZ = 0.0;
- float MedGX, MedGY, MedGZ = 0.0;
- float lowerGX, lowerGY, lowerGZ = 0.0;
- float RangeGX, RangeGY, RangeGZ = 0.0;
- int xo;
- int yo;
- float log10 (float x) {
- return (log(x) / log(10));
- }
- int numRows = 3442;
- int lowerIndex = floor(numRows/4); //For calculating quartiles
- int medianIndex = round(numRows/2);
- int upperIndex = ceil(3*numRows/4);
- float translateX = 0.0;
- float translateY = 0.0;
- float mouseRad = 0.0;
- float mouseAngle = 0.0;
- PFont myFont;
- void setup() {
- size(950, 950);
- background(0);
- xo = width/2;
- yo = height/2;
- smooth();
- noLoop();
- lines = loadStrings("imu-sensor-data-mconduct-new.csv");
- ellipseMode(RADIUS);
- AX = new float[3442];
- AY = new float[3442];
- AZ = new float[3442];
- GX = new float[3442];
- GY = new float[3442];
- GZ = new float[3442];
- myFont = createFont("Arial Bold", 12);
- textFont(myFont);
- // Label label = new Label(txt, x, y);
- for (int lineNumber = 1; lineNumber < lines.length; lineNumber++) {
- String[] pieces = split(lines[lineNumber], ',');
- int index = lineNumber-1;
- //Separating values of each line
- AX[index] = float(pieces[1]);
- AY[index] = float(pieces[2]);
- AZ[index] = float(pieces[3]);
- GX[index] = float(pieces[4]);
- GY[index] = float(pieces[5]);
- GZ[index] = float(pieces[6]);
- sumAX += AX[index];
- sumAY += AY[index];
- sumAZ += AZ[index];
- sumGX += GX[index];
- sumGY += GY[index];
- sumGZ += GZ[index];
- }
- meanAX = sumAX/3442;
- meanAY = sumAY/3442;
- meanAZ = sumAZ/3442;
- meanGX = sumGX/3442;
- meanGY = sumGY/3442;
- meanGZ = sumGZ/3442;
- minGX = min(GX);
- minGY = min(GY);
- minGZ = min(GZ);
- maxGX = max(GX);
- maxGY = max(GY);
- maxGZ = max(GZ);
- GX = sort (GX);
- GY = sort (GY);
- GZ = sort (GZ);
- lqGX = GX[lowerIndex];
- medGX = GX[medianIndex];
- uqGX = GX[upperIndex];
- lqGY = GY[lowerIndex];
- medGY = GY[medianIndex];
- uqGY = GY[upperIndex];
- lqGZ = GZ[lowerIndex];
- medGZ = GZ[medianIndex];
- uqGZ = GZ[upperIndex];
- rangeGX = uqGX - lqGX;
- rangeGY = uqGY - lqGY;
- rangeGZ = uqGZ - lqGZ;
- //For error checking
- println("\nGX: Min., Mean, Max.");
- println(minGX + ", " + meanGX + ", " + maxGX);
- println("\nGX: LQ, Median, UQ, Range");
- println(lqGX + ", " + medGX + ", " + uqGX + ", " + rangeGX);
- println("\nGY: Min., Mean, Max.");
- println(minGY + ", " + meanGY + ", " + maxGY);
- println("\nGY: LQ, Median, UQ, Range");
- println(lqGY + ", " + medGY + ", " + uqGY + ", " + rangeGY);
- println("\nGZ: Min., Mean, Max.");
- println(minGZ + ", " + meanGZ + ", " + maxGZ);
- println("\nGZ: LQ, Median, UQ, Range");
- println(lqGZ + ", " + medGZ + ", " + uqGZ + ", " + rangeGZ);
- //To sort out issue of log(1)=0, absolute values will need to be increased by 1 to avoid tending to 0 too quickly
- MaxGX = scalar*log10(abs(maxGX)+1);
- MaxGY = scalar*log10(abs(maxGY)+1);
- MaxGZ = scalar*log10(abs(maxGZ)+1);
- MeanGX = scalar*log10(abs(meanGX)+1);
- MeanGY = scalar*log10(abs(meanGY)+1);
- MeanGZ = scalar*log10(abs(meanGZ)+1);
- MinGX = scalar*log10(abs(minGX)+1);
- MinGY = scalar*log10(abs(minGY)+1);
- MinGZ = scalar*log10(abs(minGZ)+1);
- upperGX = scalar*log10(abs(uqGX)+1);
- upperGY = scalar*log10(abs(uqGY)+1);
- upperGZ = scalar*log10(abs(uqGZ)+1);
- MedGX = scalar*log10(abs(medGX)+1);
- MedGY = scalar*log10(abs(medGY)+1);
- MedGZ = scalar*log10(abs(medGZ)+1);
- lowerGX = scalar*log10(abs(lqGX)+1);
- lowerGY = scalar*log10(abs(lqGY)+1);
- lowerGZ = scalar*log10(abs(lqGZ)+1);
- RangeGX = scalar*rangeGX;
- RangeGY = scalar*rangeGY;
- RangeGZ = scalar*rangeGZ;
- translateX = map(mouseX, 0, width, -xo, xo);
- translateY = map(mouseY, 0, height, -yo, yo);
- mouseRad = sqrt(sq(translateY) + sq(translateX));
- mouseAngle = atan2(translateY, translateX);
- if (mouseAngle < 0) {
- mouseAngle = PI + (PI + mouseAngle);
- }
- // String[] fontList = PFont.list();
- // println(fontList);
- }
- void draw() {
- fill(255);
- //Setting up background
- // for (int x = 0; x <= width; x += xo) {
- // drawGradient(xo, yo, width, height, 0, 155);
- // }
- translate(xo, yo);
- stroke(255);
- strokeWeight(3);
- ellipse(0, 0, 2, 2);
- strokeWeight(1);
- stroke(255, 200);
- line (-xo, 0, xo, 0);
- noFill();
- stroke(255, 50);
- ellipse(0, 0, scalar*log10(2), scalar*log10(2));
- ellipse(0, 0, scalar*log10(3), scalar*log10(3));
- ellipse(0, 0, scalar*log10(4), scalar*log10(4));
- ellipse(0, 0, scalar*log10(5), scalar*log10(5));
- ellipse(0, 0, scalar*log10(6), scalar*log10(6));
- ellipse(0, 0, scalar*log10(7), scalar*log10(7));
- ellipse(0, 0, scalar*log10(8), scalar*log10(8));
- ellipse(0, 0, scalar*log10(9), scalar*log10(9));
- ellipse(0, 0, scalar*log10(10), scalar*log10(10));
- stroke(255);
- ellipse(0, 0, scalar*log10(11), scalar*log10(11));
- ellipse(0, 0, scalar*log10(101), scalar*log10(101));
- ellipse(0, 0, scalar*log10(1001), scalar*log10(1001));
- ellipse(0, 0, scalar*log10(10001), scalar*log10(10001));
- ellipse(0, 0, scalar*log10(100001), scalar*log10(100001));
- textAlign(CENTER, CENTER);
- text("+", xo-6, -7);
- text("+", -xo+6, -7);
- text("-", xo-4, 3);
- text("-", -xo+6, 3);
- text("10", scalar*(0.8), -scalar*(0.8));
- text("100", scalar*(1.5), -scalar*(1.5));
- text("1,000", scalar*(2.23), -scalar*(2.23));
- text("10,000", scalar*(2.95), -scalar*(2.95));
- text("100,000", scalar*(3.67), -scalar*(3.67));
- text("GX", 23*cos(7*PI/6), 23*sin(7*PI/6));
- text("GY", -1, -23);
- text("GZ", 23*cos(11*PI/6), 23*sin(11*PI/6));
- strokeWeight(3);
- stroke(255, 0, 0, 120);
- line(-MaxGX, 0, MinGX, 0);
- line(MaxGX*cos(4*PI/3), MaxGX*sin(4*PI/3), MinGX*cos(PI/3), MinGX*sin(PI/3));
- stroke(0, 255, 0, 120);
- line(MaxGY*cos(4*PI/3), MaxGY*sin(4*PI/3), MinGY*cos(PI/3), MinGY*sin(PI/3));
- line(MaxGY*cos(5*PI/3), MaxGY*sin(5*PI/3), MinGY*cos(2*PI/3), MinGY*sin(2*PI/3));
- stroke(0, 0, 255, 120);
- line(MaxGZ*cos(5*PI/3), MaxGZ*sin(5*PI/3), MinGZ*cos(2*PI/3), MinGZ*sin(2*PI/3));
- line(MaxGZ, 0, -MinGZ, 0);
- stroke(255, 0, 0);
- arc(0, 0, MaxGX, MaxGX, PI, 4*PI/3);
- arc(0, 0, MinGX, MinGX, 0, PI/3);
- stroke(0, 255, 0);
- arc(0, 0, MaxGY, MaxGY, 4*PI/3, 5*PI/3);
- arc(0, 0, MinGY, MinGY, PI/3, 2*PI/3);
- stroke(0, 0, 255);
- arc(0, 0, MaxGZ, MaxGZ, 5*PI/3, 2*PI);
- arc(0, 0, MinGZ, MinGZ, 2*PI/3, PI);
- noStroke();
- // if (mouseRad > 0 && mouseRad <= upperGX && mouseAngle > PI && mouseAngle <= 4*PI/3) {
- // Label label = new Label(String txt, float x, float y);
- // txt = "Range GX: " + rangeGX;
- // label(txt, translateX, translateY);
- // }
- fill(255, 0, 0, 70);
- arc(0, 0, upperGX, upperGX, PI, 4*PI/3);
- arc(0, 0, lowerGX, lowerGX, 0, PI/3);
- fill(0, 255, 0, 70);
- arc(0, 0, upperGY, upperGY, 4*PI/3, 5*PI/3);
- arc(0, 0, lowerGY, lowerGY, PI/3, 2*PI/3);
- fill(0, 0, 255, 70);
- arc(0, 0, upperGZ, upperGZ, 5*PI/3, 2*PI);
- arc(0, 0, lowerGZ, lowerGZ, 2*PI/3, PI);
- noFill();
- strokeWeight(2);
- stroke(255, 100, 0);
- arc(0, 0, MeanGX, MeanGX, 0, PI/3); //Negative
- stroke(255, 0, 0);
- arc(0, 0, MedGX, MedGX, PI, 4*PI/3);
- stroke(0, 255, 100);
- arc(0, 0, MeanGY, MeanGY, PI/3, 2*PI/3); //Negative
- stroke(0, 255, 0);
- arc(0, 0, MedGY, MedGY, PI/3, 2*PI/3); //Negative
- stroke(100, 0, 255);
- arc(0, 0, MeanGZ, MeanGZ, 5*PI/3, 2*PI);
- stroke(0, 0, 255);
- arc(0, 0, MedGZ, MedGZ, 2*PI/3, PI); //Negative
- }
- class Label {
- Label (String txt, float x, float y) {
- x = translateX;
- y = translateY;
- float labelW = textWidth(txt);
- //Check if label would go beyond screen dimensions
- if (x + labelW + 20 > width) {
- x -= labelW + 20;
- }
- stroke(0);
- strokeWeight(1);
- fill(255);
- rect(x, y - 15, 15, labelW);
- }
- }
- //For background
- //void drawGradient(int x, int y, float w, float h, color c1, color c2) {
- // noFill();
- // for (int r = 0; r <= sqrt(sq(xo) + sq(yo)); ++r) {
- // float inter = map(r, x, x+w, y, y+h);
- // color c = lerpColor(c1, c2, inter);
- // stroke(c);
- // ellipse(xo, yo, r, r);
- // }
- //}
The console also has the following text:
"GX: Min., Mean, Max.
-11015.0, -2.0453224, 9558.0
GX: LQ, Median, UQ, Range
-94.0, 37.0, 322.0, 416.0
GY:Min., Mean, Max.
-3824.0, -56.239105, 7502.0
GY:LQ, Median, UQ, Range
-242.0, -26.0, 122.0, 364.0
GZ:Min., Mean, Max.
-6981.0, 7.352702, 8776.0
GZ:LQ, Median, UQ, Range
-391.0, -79.0, 93.0, 484.0"
What I need is to create labels for the data. I was hoping to be able to have floating labels that appear when the mouse floats over the appropriate areas, but have struggled to get the class Label right. Can anyone help me? If it's too difficult without the .csv file I can provide it.
1