Using the JFreechart library:
I have a two dimensional double array and I want to display all timestamps (unix time) as a Date format (like dd/MM/yyyy). I can generate the chart but it shows the X-Axis (time) as unix time obviously. I tried to generate the X.Axis as DateAxis but it just showed it as one day in 1970 and it couldn't recognize and convert the unix timestamps in the array. How could I solve that problem?
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;
import org.xml.sax.SAXException;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.*;
import java.awt.Color;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
public class Verbrauchsdiagramm extends JFrame {
private static final long serialVersionUID = -7794334701144090065L;
double[][] A;
double[][] B;
XYLineAndShapeRenderer renderer;
DefaultXYDataset dataset;
NumberAxis xax;
NumberAxis yax;
XYPlot plot;
JFreeChart chart2;
ApplicationFrame punkteframe;
ChartPanel chartPanel2;
double xmin = 1514000000;
double xmax = 1570000000;
public Verbrauchsdiagramm() throws ParseException, SAXException, IOException, ParserConfigurationException {
createDia();
}
public void createDia() throws ParseException, SAXException, IOException, ParserConfigurationException {
convertArray();
renderer = new XYLineAndShapeRenderer(true, false);
dataset = new DefaultXYDataset();
xax = new NumberAxis("Zeit in Unixtime");
yax = new NumberAxis("Verbrauch");
xax.setRange(xmin,xmax);
plot = new XYPlot(dataset,xax,yax, renderer);
chart2 = new JFreeChart(plot);
punkteframe = new ApplicationFrame("Diagramm");
chartPanel2 = new ChartPanel(chart2);
dataset.addSeries("Bezug", A);
dataset.addSeries("Einspeisung", B);
punkteframe.setContentPane(chartPanel2);
punkteframe.pack();
punkteframe.setVisible(true);
}
public void convertArray() throws ParseException, SAXException, IOException, ParserConfigurationException {
SDATReader convert = new SDATReader();
A = convert.vecToArrayMS();
B = convert.vecToArrayMST();
}
public static void main (String[] args) throws ParseException, SAXException, IOException, ParserConfigurationException {
Verbrauchsdiagramm vdiagramm = new Verbrauchsdiagramm();
}
}