0

I have some stock data that I have imported from excel to model in Nebula. I have the visualizations for the graph working, but I'm having trouble formatting the time along the x-axis properly. I tried creating a temp x array with 1 data point for each coordinate value, and I've successfully created a date array of the same length. It seems to me I should be able to tell Nebula to use my date array for the labels on the x-axis, but I can't figure out what parameter of get x-axis to modify. Right now, with the date format enabled, it starts at Oct 1 1969 and then has unlabeled tick marks until the last one which also says Oct 1 1969 (default value?) Here's some of my code:

    //Data source
    List<Book> stocks = readBooksFromCSV("COVID_DJI.csv");
    for (Book b : stocks) {
        System.out.println(b);
    }

            //Create arrays that can hold output
            double[] opening;
            double[] closing;
            String[] time;
            int len = stocks.size();
            opening = new double[len];
            closing = new double[len];
            time = new String[len];
            int j = 0;
            for (Book x : stocks) {
                double o_value = x.getOpen();
                double c_value = x.getClose();
                String d_value = x.getDate();
                opening[j] = o_value;
                closing[j] = c_value;
                time[j] = d_value;
                j++;
            }

            Date[] dates = new Date[len];
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            for(int i = 0; i < len; i++) {
                try {
                    dates[i] = df.parse(time[i]);
                }
                catch(ParseException e) {
                    System.out.println("Ooof...");
                }
            }

            //Create temp x array
            double[] temp;
            temp = new double[len];
            for (int i = 0; i < len; i++) {
                temp[i] = i;
            }

            //Shell window generation and lightweight system- omitted

    XYGraph xyGraph = new XYGraph();
    xyGraph.setTitle("DJIA PPS During COVID-19 Outbreak");

    //Set Axis Bounds
    double min = opening[0];
    for(int i = 0; i < len; i++) {
        if(opening[i] < min) {
            min = opening[i];
        }
    }
    double max = opening[0];
    for(int i = 0; i < len; i++) {
        if(opening[i] > max) {
            max = opening[i];
        }
    }
    double drop = ((max - min)/max) * 100;
    System.out.println("Peak to trough drop: " + drop + "%");

    //Set axis parameters **I think this is where I need to change a parameter
    xyGraph.getPrimaryXAxis().setRange(0, len);
    xyGraph.getPrimaryXAxis().setDateEnabled(true);
    xyGraph.getPrimaryXAxis().setTimeUnit(Calendar.DATE);
    xyGraph.getPrimaryXAxis().setFormatPattern("yyyy-MM-dd");
    xyGraph.getPrimaryXAxis().setMajorGridStep(7);
    xyGraph.getPrimaryYAxis().setRange(min-100,max+100);
    xyGraph.getPrimaryYAxis().setFormatPattern("$00000.00");
    xyGraph.getPrimaryXAxis().setTitle("Day");
    xyGraph.getPrimaryYAxis().setTitle("Price Per Share");

    //Plot graph
    lws.setContents(xyGraph);

    //Trace implementation- omitted
Zach Rieck
  • 419
  • 1
  • 4
  • 23
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). If your library needs an old-fashioned `Date`, convert just before calling (and still stay away from `SimpleDateFormat`). – Ole V.V. Apr 02 '20 at 04:49
  • 1
    @OleV.V. Thank you! I will make this update so my API can be more relevant. I still believe I need to make a change to x-axis formatting in order for it to plot though considering it will not take this type of array as an argument (only double). Hopefully someone who knows Nebula well will chime in – Zach Rieck Apr 06 '20 at 16:24

0 Answers0