// Drawing a Chart of Twitter Stock Science Majors
// The data...comes from finance.yahoo.com
// It is YTD data for 2022
// We will only use the adjClose price of the stock and plot it.
// Once done you can try to also plot the volume (as they do on Yahoo! site).
// The data arrays
float[] adjClose;
int[] volume;
float minPrice, maxPrice;  // min and max price of stock YTD
// Plot dimensions
float x1, y1, x2, y2;
// Text and Fonts
PFont f;
void setup() {
   size(1000, 600);
   background(255);
   //noLoop();
   // Read data from a data file: TWTR.csv
   // The data file should be downloaded as a CSV file from Yahoo! Finance
   // It should be stored in the Data folder of the sketch
   // Read the data file as an array of strings, each line in a string.
   // First line of data is the names of each field (ignore while plotting)
   String[] lines = loadStrings("TWTR.csv");
   println("Read " + lines.length + " lines.");
   
   // create data arrays
   adjClose = new float[lines.length-1];
   volume = new int[lines.length-1];
   // Why lines.length-1??? Because we are ignoring the first line.
   
   for (int i=1; i < lines.length; i++) {
      String[] pieces = split(lines[i], ",");
   
      adjClose[i-1] = float(pieces[5]); /// Again, ith line gets parsed into i-1th entry
      volume[i-1] = int(pieces[6]);
   }
   
   // Compute min and max price of stock
   minPrice = min(adjClose);
   maxPrice = max(adjClose);
   
   println("Min Price = $" + minPrice);
   println("Max Price = $" + maxPrice);
   
   // Set up lot dimensions
   x1 = width*0.05;
   y1 = height*0.05;
   x2 = width*0.95;
   y2 = height*0.95;
   
   // Font
   f = createFont("Arial", 16, true);
} // setup()
 
void draw() {
   background(255);
   rectMode(CORNERS);
   noStroke();
   fill(200);
   rect(x1, y1, x2, y2);  // draws bounding box for plot
   
   // Draw the line chart for adjClose
   drawGraph(adjClose, minPrice, maxPrice);
   
   // Legend and axes (Adjust the text font and size as needed)
   // title
   fill(0);
   textFont(f, 24);
   textAlign(CENTER);
   text("(TWTR) Twitter Inc. 2022 (up to 10/24/2022)", (x2-x1)/2, y1-5);
   textFont(f, 10);
   textAlign(LEFT);
   text("Source: Yahoo! Finance (finance.yahoo.com)", width-250, height-10);
   
   // y and X axis labels, ticks, etc
   drawYLabels(0, 70);
   drawXLabels(0, adjClose.length);
} // draw()
void drawGraph(float[] data, float yMin, float yMax) {
   stroke(0);
   beginShape();
   for (int i = 0; i < data.length; i++) {
      float x = map(i, 0, data.length-1, x1, x2);
      float y = map(data[i], 0, 70, y2, y1);
      vertex(x, y);
      // Mouse Interaction...
      if (distance(x, y, mouseX, mouseY) < 5) {
         fill(255, 0, 0);
         text(str(data[i]), x, y);
         stroke(255, 0, 0);
         line(x1, y, x, y);
         stroke(0);
         noFill();
      }
      // End of mouse interaction
   }
   endShape();
} // drawGraph()
void drawYLabels(int ymin, int ymax) {
   // draws little tick marks at intervals of 10
   // and the value at that tick
   textFont(f, 10);
   textAlign(CENTER);
   for (int i = ymin; i <= ymax; i = i + 10) {
      float y = map(i, 0, ymax, y2, y1);
      text(str(i), x1-15, y);
      line(x1, y, x1-5, y);
   }
} // drawYLabels()
void drawXLabels(int xmin, int xmax) {
   // Draws the X axis labels, and lines
   textFont(f, 10);
   textAlign(CENTER);
   for (int i = 0; i < xmax; i = i + 10) {
      float x = map(i, 0, xmax-1, x1, x2);
      text(str(i), x, y2+10);
      stroke(175);
      line(x, y2, x, y1);
   }
} // drawXLabels()
float distance(float x1, float y1, float x2, float y2) {
   return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
} // distance()