I'm new to JFreeChart
, and don't know how to solve this issue; neither I know if it's possible to do what I pretend. The issue is this, I'm trying to show a graph in a Java Swing application, I pass the dates and the values to show. The dates are chosen by an user, and maybe are not in a row;, I mean, if user chooses from thursday to monday, saturday and sunday are avoided and pass to the graph thursday,friday and monday. The problem is that the graph takes from thursday to monday (saturday and sunday includeed) and show them in the X-axis, when it should not. Based on this example, my class is the following:
ArrayList<StringBuffer> dates={20-02-2015,23-02-2015,24-02-2015,25-02-2015,26-02-2015};
args="tittle";
frame=(the frame where I do the call,although it's not needed in myChartPanelDemo2,just added for setting errorDialogs purpose);
ArrayList<StringBuffer> amount = {"serie1","serie2"};
myChartPanelDemo2.init(amount,args,dates,frame);
public class myChartPanelDemo2 {
static int datasetIndex;
private static ChartPanel chartPanel;
public static JPanel init(ArrayList<StringBuffer> amount, String args,
ArrayList<StringBuffer> dates, JFrame frame) {
//*****just getting the values***********//
datasetIndex = 0;
int cantity = amount.size();
int cantity2 = fechas.size();
float[][] resultArray = new float[cantity][cantity];
float[] result = new float[cantity2];
for (int x = 0; x < cantity; x++) {
for (int y = 0; y < cantity2; y++) {
result[y] = callingDataBase.main(amount.get(x), args, dates.get(y));
}
for (int v = 0; v < result.length; v++) {
resultArray[x][v] = result[v];
}
//resultArray will be the array from where chartPanel will take the values
}
XYDataset roiData = createDataset(args, dates, resultArray, amount);
JFreeChart chart = ChartFactory.createTimeSeriesChart(args, "Days",
"Values", roiData, true, true, false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
chartPanel = new ChartPanel(chart);
JPanel f = new JPanel(new BorderLayout());
f.add(chartPanel, BorderLayout.CENTER);
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTrace(), BorderLayout.SOUTH);
f.add(panel, BorderLayout.SOUTH);
f.setVisible(true);
chartPanel.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setMaximumSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
return f;
}
/** @see https://stackoverflow.com/a/5522583/230513 */
private static JComboBox<String> createTrace() {
final JComboBox<String> trace = new JComboBox<String>();
final String[] traceCmds = {"Enable Trace", "Disable Trace"};
trace.setModel(new DefaultComboBoxModel<String>(traceCmds));
trace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (traceCmds[0].equals(trace.getSelectedItem())) {
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
chartPanel.repaint();
} else {
chartPanel.setHorizontalAxisTrace(false);
chartPanel.setVerticalAxisTrace(false);
chartPanel.repaint();
}
}
});
return trace;
}
private static XYDataset createDataset(String name,
ArrayList<StringBuffer> dates, float[][] resultArray,
ArrayList<StringBuffer> amount) {
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int x = 0; x < GraficoEmpresas.size(); x++) {
tsc.addSeries(createSeries(GraficoEmpresas.get(x).toString(), fechas,
resultArray, x));
}
return tsc;
private static TimeSeries createSeries(String name,
ArrayList<StringBuffer> dates, float[][] resultArray, int x) {
final TimeSeries series = new TimeSeries(name);
int cantity2 = dates.size();
RegularTimePeriod t = new Day();
for (int y = 0; y < cantity2; y++) {
Date fecha;
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
day = new Date();
try {
day = formatoDelTexto.parse(fechas.get(y).toString());
} catch (ParseException e) {
e.printStackTrace();
}
RegularTimePeriod p = new Day(day);
series.addOrUpdate(p, resultArray[x][y]);
}
return series;
}
}
I can't upload a picture, not enough reputation, but the result is a graph which x-axis values: {20-02-2015 -- 21-02-2015 -- 22-02-2015 --23-02-2015 -- 24-02-2015 -- 25-02-2015 -- 26-02-2015} with dataset values in every point except 21 and 22, but 21 and 22 are added in the axis label.
I'm using both JFreechart and JCommon 1.0.16. I guess this is an autocomplete problem, but it's only a guess, I've not been able to check whether it's true or not. Does anyone know how to avoid this or it is impossible? Thank you!!
Absent a complete example it's hard to be sure, but you may be able to apply a SegmentedTimeline
to the DataAxis
created by your chosen ChartFactory
. Use the static factory method newMondayThroughFridayTimeline()
to create a Monday through Friday SegmentedTimeline
, as shown here. You can get a reference to the DataAxis
from the chart's plot; alternatively, skip the ChartFactory
and create the chart de novo, as shown here.