I am trying and I failed to succeed to make my chart
I have 3 trends of different units : one weight, one temperature, one (stirring) speed this will need 3 different range axis. And later will come more data series linked to this series/units.
The first domain axis is a Time axis : I have several sample of data measurement, one sample of all series, at the same moment.
And up to that, I have no problem to make my chart. I am lucky.
Then comes a second domain axis : the second domain axis is the number of hours relative to the beginning time.
Example :
- domain axis 0 = 2017-01-15 14:00, domain axis 1 = 0,0 hours
- domain axis 0 = 2017-01-15 15:00, domain axis 1 = 1,0 hours
- domain axis 0 = 2017-01-16 15:00, domain axis 1 = 25,0 hours
etc.
But I am failing to align the 0,0 hours to the 14:00 on the chart, and same for the greatest time that I am failing to align.
I hope that someone can give me a tip to which Jfree class or method I have to use to realize this.
Here is my code and screen captures :
Here is the overview of the whole chart : https://1drv.ms/i/s!Am8iOaZq2pJfhG2wUjyiUFWhbe5J
Here is the detail of what is not ok : https://1drv.ms/i/s!Am8iOaZq2pJfhG7lCrgxthALu5aT
And here is a short source code to make this, based on MultipleAxisDemo3.java
---------------------------------------------------------------------------------
Code: Select all
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MultipleXYaxis2 extends ApplicationFrame {
public MultipleXYaxis2(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(1000, 600));
setContentPane(chartPanel);
}
private static JFreeChart createChart() {
// DATASET 0 = Weight, DOMAIN AXIS 0 = Time, RANGE AXIS 0 = Weight
XYDataset dataset0Weight = createDatasetDomainTime("Weight (Kg)", COLUMN_WEIGHT );
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Multiple X and Y axis",
"Time of the day",
"Weight",
dataset0Weight,
true,
true,
false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
// DATASET 2 = Temperatures, DOMAIN AXIS 1 = Hours, RANGE AXIS 1 = Temperature
NumberAxis xAxis1 = new NumberAxis("Hours since start");
plot.setDomainAxis(1, xAxis1);
plot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
/*XYSeries serieXY = new XYSeries("Hours since start");
serieXY.add(0.0,0);
serieXY.add(54.3827,0);
xAxis1.setLowerBound(0);
xAxis1.setUpperBound(54.3827);
xAxis1.setRange(0,54.3827);
xAxis1.setAutoRange(true);
xAxis1.setLowerMargin(0);
xAxis1.setUpperMargin(54.3827);
xAxis1.setAutoRangeIncludesZero(true);
xAxis1.setAutoRangeStickyZero(true);
xAxis1.setAutoRange(true);
XYSeriesCollection datasetXY = new XYSeriesCollection();
datasetXY.addSeries(serieXY);
plot.setDataset(3, datasetXY);
plot.mapDatasetToDomainAxis(3, 1);*/
NumberAxis yAxis1Temp = new NumberAxis("Temperature");
plot.setRangeAxis(1, yAxis1Temp);
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
XYDataset datasetTemperature = createDatasetDomainHours("Temperature", COLUMN_TEMPERATURE_PROD);
plot.setDataset(2, datasetTemperature);
plot.mapDatasetToDomainAxis(2, 1);
plot.mapDatasetToRangeAxis(2, 1);
// DATASET 1 = Stirrer, DOMAIN AXIS 0 = Time, RANGE AXIS 2 = Stirrer
NumberAxis yAxis2stirrers = new NumberAxis("Stirrer RPM");
plot.setRangeAxis(2, yAxis2stirrers);
plot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_RIGHT);
XYDataset datasetStirrer = createDatasetDomainTime("Inner stirrer", COLUMN_INNER_STIRRER);
plot.setDataset(1, datasetStirrer);
plot.mapDatasetToDomainAxis(1, 0);
plot.mapDatasetToRangeAxis(1, 2);
plot.setRenderer(1, new XYLineAndShapeRenderer(true, false));
plot.setRenderer(2, new XYLineAndShapeRenderer(true, false));
plot.setRenderer(3, new XYLineAndShapeRenderer(true, false));
ChartUtilities.applyCurrentTheme(chart);
return chart;
}
static int COLUMN_WEIGHT = 0;
static int COLUMN_TEMPERATURE_PROD = 1;
static int COLUMN_INNER_STIRRER = 2;
static float dataSeries[][] = {
{100f, 70f, 20f },
{102f, 65f, 15f },
{110f, 65f, 15f },
{123f, 70f, 15f },
{123f, 75f, 20f },
{110f, 100f, 5f },
{ 90f, 22f, 0f }
};
static String timeSeries[] = { "2017-01-31 13:00:04",
"2017-01-31 18:00:00",
"2017-01-31 22:00:00",
"2017-02-01 02:22:00",
"2017-02-01 06:22:00",
"2017-02-01 07:00:00",
"2017-02-01 11:22:58"
};
static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Second sampleSecond(int i) {
Date d = new Date();
try {
d = df.parse(timeSeries[i]);
} catch(ParseException e) { }
Second s = new Second(d);
return s;
}
private static Double sampleHour(long startTime, int i) {
Date d = new Date();
Double hours;
try {
d = df.parse(timeSeries[i]);
} catch(ParseException e) { }
long timeSample = d.getTime();
hours = ((double) (timeSample - startTime)) / (double) (60 * 60 * 1000);
return hours;
}
// hours since start
private static XYDataset createDatasetDomainHours(String name, int dataSeriesColumn) {
Date startDate = new Date();
long startTime;
Number sampleTime;
try {
startDate = df.parse(timeSeries[0]);
} catch(ParseException e) { }
startTime = startDate.getTime();
XYSeries series = new XYSeries(name);
for( int i=0; i < timeSeries.length; i++) {
sampleTime = sampleHour( startTime, i );
series.add( sampleTime, dataSeries[ i ][dataSeriesColumn]);
//System.out.println(i+" -> "+sampleTime);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
return dataset;
}
private static XYDataset createDatasetDomainTime(String name, int dataSeriesColumn) {
TimeSeries series = new TimeSeries( name );
for( int i=0; i < timeSeries.length; i++) {
Second s = sampleSecond( i );
series.add( s, dataSeries[ i ][dataSeriesColumn]);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
return dataset;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart();
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
MultipleXYaxis2 demo = new MultipleXYaxis2(
"Based on JFreeChart: MultipleAxisDemo3.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}