Reputation: 449
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
Upvotes: 0
Views: 151
Reputation: 31
The post is old, but as there is not so much information on nebulas's xygraph available, here is my take anyway:
I used milliseconds, not days, so I said:
xyGraph.getPrimaryXAxis().setTimeUnit(Calendar.MILLISECOND);
For the data, I created Samples for each point and used the milliseconds of the respective date/time of each point as the x-value.
Additionally, I specified the lowest and the highest milliseconds-value as the lower and upper range of the x-axis, so that I now get a graph that shows dates between the lowest and highest date/time.
Upvotes: 0