我需要制作纵轴上有日期的条形图。它将显示软件上次使用的时间戳。
它看起来像标准的条形图,但在垂直轴上,我需要日期,而不是数字。
感谢您的帮助
编辑:这是创建图表的工作示例,看起来就像我想要的一样。
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;
}
}
我想做的一件事是将左纵轴上以毫秒为单位的时间值更改为日期-时间格式。
使用ChartFactory.createXYBarChart()
并指定true
作为dateAxis
参数,指定PlotOrientation.HORIZONTAL
作为orientation
参数。
DateAxis xAxis = new DateAxis("Date");
xAxis.setVerticalTickLabels(true);
plot.setDomainAxis(xAxis);