Reputation: 2149
public class Example {
public static void main(String args[]){
/*creating timeSeriesCollection*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries timeSeries1 = new TimeSeries("Sample 1");
timeSeries1.add(new Day(8,4, 2012), 7.0);
timeSeries1.add(new Day(19,4, 2012), 5.0);
dataset.addSeries(timeSeries1);
/*Creating the chart*/
JFreeChart chart = ChartFactory.createTimeSeriesChart("Population","Date","Population",dataset,true,true,false);
/*Altering the graph */
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(5.0, 10.0, 10.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));
axis.setAutoTickUnitSelection(false);
axis.setVerticalTickLabels(true);
/* Displaying the chart*/
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);
}
}
I have written the above code which works perfectly fine but when rendering the graph it displays date which does not have values. It displays dates like 9/4/12
,10/4/2012
to 18/4/2012
in y-axis which does not have a value. How can I remove the date which does not have value. And why it is behaving so?
Can any one help me please?
Upvotes: 1
Views: 6465
Reputation: 5923
Try this:
public class Example1 {
public static void main(String args[]){
DefaultKeyedValues data = new DefaultKeyedValues();
data.addValue("8/4/2012" ,7.0);
data.addValue("19/04/2012",5.0);
CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
ChartFrame frame = new ChartFrame("Test", chart);
//Switch from a Bar Rendered to a LineAndShapeRenderer so the chart looks like an XYChart
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseLinesVisible(false); //TUrn of the lines
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRenderer(0, renderer);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
frame.pack();
frame.setVisible(true);
}
}
Rather than using a TimeSeriesChart
use a CategoryDataSet as @Dirk sugested and then switch to a LineAndShapeRenderer
.
Upvotes: 3
Reputation: 1903
The TimeSeries
is displayed continuously, which means that the dates are equally distributed along the axis, even if there is no actual value for this date. If you want only certain dates displayed, you should use some kind of CategoryDataSet.
Upvotes: 2