Reputation: 467
I need to make bar chart that has date on the vertical axis. It will shows timestamp of last usage of the software.
It can looks like standard bar chart but on the vertical axis I need Date instead just number.
Thank you for any help
Edit: Here is working example of creating chart, that looks almost like I want.
private void createSoftwareLastUsageChart() throws ParseException {
Date from = new Date(new Date().getTime() - (15 * 24 * 60 * 60 * 1000L));
List<SwLastUsed> databaseStatisticsItems = getTestData();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (SwLastUsed soft : databaseStatisticsItems) {
dataset.addValue(soft.lastUsed.getTime() - from.getTime(), "Software", soft.name);
}
JFreeChart chart = ChartFactory.createBarChart("Software last usage", "Values", "Time", dataset, PlotOrientation.VERTICAL, false, false, false);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = (CategoryAxis) plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
JFrame window = new JFrame();
JPanel pnl = new JPanel();
ChartPanel chPnl = new ChartPanel(chart);
chPnl.setPreferredSize(new Dimension(800, 600));
pnl.add(chPnl);
window.setContentPane(pnl);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
private List<SwLastUsed> getTestData() throws ParseException {
List<SwLastUsed> sw = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sw.add(new SwLastUsed("CHROME", format.parse("2014-11-03 11:24:20.456+01")));
sw.add(new SwLastUsed("INTERNET_EXPLORER 10.* x64", format.parse("2014-11-03 11:15:28.124+01")));
sw.add(new SwLastUsed("INTERNET_EXPLORER 11.*", format.parse("2014-11-03 11:15:28.124+01")));
sw.add(new SwLastUsed("INTERNET_EXPLORER 10.* x86", format.parse("2014-11-03 11:08:35.359+01")));
sw.add(new SwLastUsed("FIREFOX x86", format.parse("2014-10-21 09:38:17.109+02")));
sw.add(new SwLastUsed("SAFARI", format.parse("2014-10-20 15:00:26.526+02")));
sw.add(new SwLastUsed("FIREFOX x64", format.parse("2014-10-20 14:55:47.886+02")));
return sw;
}
private class SwLastUsed {
public String name;
public Date lastUsed;
public SwLastUsed(String name, Date lastUsed) {
this.name = name;
this.lastUsed = lastUsed;
}
}
The one thing what I want to do is change time values in milliseconds on the left vertical axis to Date-Time format.
Upvotes: 0
Views: 1189
Reputation: 1725
DateAxis xAxis = new DateAxis("Date");
xAxis.setVerticalTickLabels(true);
plot.setDomainAxis(xAxis);
Upvotes: 0
Reputation: 205785
Use ChartFactory.createXYBarChart()
and specify true
for the dateAxis
parameter and PlotOrientation.HORIZONTAL
for the orientation
parameter.
Upvotes: 2