
// LinePlots.java
// Yuriy Guskov
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* Four line plots on the same graph, two of CPU data and
   two of memory data, which require two y-axes for "%" and
   "RAM". A legend is included, and the x-axis is in
   Hours:Minutes:Seconds format.

   The call to Utils.viewPlot() displays the plot *and*
   saves it to the file "cpuMem.png"


   Usage:
     > compile LinePlots.java
     > run LinePlots
*/


import java.awt.*;
import java.util.*;

import com.github.plot.Plot;
import com.github.plot.Plot.*;


public class LinePlots
{

  public static void main(String[] args)
  {
    double[] tms = makeTimes();

    // configure options
    PlotOptions pOpts = Plot.plotOpts().
             title("CPU/Memory").
             width(1000).height(450). // total plot size
             legend(Plot.LegendFormat.RIGHT);

    // build the plot
    Plot plot = Plot.plot(pOpts);
    plot.xAxis("Time", Plot.axisOpts().
                      format(Plot.AxisFormat.TIME_HMS));
          // x-axis uses hours:minutes:seconds
    plot.yAxis("%", Plot.axisOpts().range(0, 100));  // two y-axes
    plot.yAxis("RAM", Plot.axisOpts().range(0, 8));

    // CPU data 1
    DataSeriesOptions dsOpts1 = Plot.seriesOpts().
                       lineWidth(5).  // the default is 2
                       marker(Plot.Marker.NONE). // no marks for data
                       color(Color.GREEN).  // but the line is colored
                       yAxis("%");   // which y-axis is used
    Plot.Data plotData1 = Plot.data().
                       xy(tms[0], 61).xy(tms[1], 74).
                       xy(tms[2], 73).xy(tms[3], 76).
                       xy(tms[4], 80).xy(tms[5], 75).
                       xy(tms[6], 64).xy(tms[7], 60);
    plot.series("CPU 1", plotData1, dsOpts1);


    // CPU data 2
    DataSeriesOptions dsOpts2 = Plot.seriesOpts().
                       marker(Plot.Marker.CIRCLE).
                       color(Color.BLUE).  
                           // the default color; so could be left out
                       areaColor(new Color(0, 0, 0xff, 30)).
                           // transparent blue
                       yAxis("%");
    Plot.Data plotData2 = Plot.data().
                       xy(tms[2], 51).xy(tms[3], 59).
                       xy(tms[4], 53).xy(tms[5], 55);
    plot.series("CPU 2", plotData2, dsOpts2);
         // each series is added to those already in the plot



    // Memory data 1
    DataSeriesOptions dsOpts3 = Plot.seriesOpts().
                       marker(Plot.Marker.DIAMOND).
                       markerColor(Color.GREEN).
                       color(Color.BLACK).
                       yAxis("RAM");  // using the other y-axis
    Plot.Data plotData3 = Plot.data().
                       xy(tms[2], 5.2).xy(tms[3], 3.4).
                       xy(tms[4], 5.5).xy(tms[5], 5.6).
                       xy(tms[6], 5.4).xy(tms[7], 5.3);
    plot.series("Memory 1", plotData3, dsOpts3);


    // Memory data 2
    DataSeriesOptions dsOpts4 = Plot.seriesOpts().
                       marker(Plot.Marker.SQUARE).
                       markerColor(Color.CYAN).
                       color(Color.BLACK).
                       yAxis("RAM");
    Plot.Data plotData4 = Plot.data().
                       xy(tms[0], 4.7).xy(tms[1], 4.6).
                       xy(tms[2], 4.7).xy(tms[3], 5.0).
                       xy(tms[4], 4.9).xy(tms[5], 4.6).
                       xy(tms[6], 4.4);
    plot.series("Memory 2", plotData4, dsOpts4);

    Utils.viewPlot("CPU/Memory", plot, "cpuMem.png");
               // view the plot, and save an image of it

  }  // end of main()


  private static double[] makeTimes()
  {
    double[] tms = new double[9];

    Calendar cal = Calendar.getInstance();

    cal.set(2222, 01, 01, 22, 22, 22);  
        // year, month, day, hour, min, sec
    tms[0] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 22, 52);
    tms[1] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 23, 22);
    tms[2] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 23, 52);
    tms[3] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 24, 22);
    tms[4] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 24, 52);
    tms[5] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 25, 22);
    tms[6] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 25, 52);
    tms[7] = cal.getTimeInMillis();

    cal.set(2222, 01, 01, 22, 35, 52);
    tms[8] = cal.getTimeInMillis();

    return tms;
  }  // end of makeTimes()

}  // end of LinePlots class