0

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.

Daphnis
  • 437
  • 1
  • 5
  • 14

2 Answers2

2

Use ChartFactory.createXYBarChart() and specify true for the dateAxis parameter and PlotOrientation.HORIZONTAL for the orientation parameter.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for your answer, XYBarChart looks good. But I need get Date value to the left vertical axis as a value that depends on (software) category. I know how get Date value to the horizontal axis and category to the vertical one. How can I invert them? – Daphnis Oct 30 '14 at 10:00
  • Doesn't `PlotOrientation.HORIZONTAL` do what you want? – trashgod Oct 30 '14 at 10:35
  • No. It doesn't. I'd like chart like this: [link](https://drive.google.com/file/d/0B95y14BIMoxvMGkxT2pzd0cxS2s/view?usp=sharing) But date value on the vertical axis instead simple numbers. The chart has to display last usage of several software. – Daphnis Oct 30 '14 at 10:50
  • You may need verify your dataset values or apply a format to the `DateAxis`, as shown [here](http://stackoverflow.com/a/22264230/230513). For more specific guidance, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Oct 30 '14 at 11:02
0

DateAxis xAxis = new DateAxis("Date"); xAxis.setVerticalTickLabels(true); plot.setDomainAxis(xAxis);

Borislav Markov
  • 1,495
  • 11
  • 12