I'm trying to create a bar chart in Java.
I'd like to have 1H bar unit on the DateAxis
(not tick unit). Is there a way?
After a lot of google search, I haven't found anything interesting about this.
This is what I get:
![BarChart](https://i.stack.imgur.com/OPiIG.jpg)
ChartAdapter.java
public class ChartAdapter extends JFrame {
/**
* Constructs the demo application.
*
* @param title the frame title.
*/
public ChartPanel generateChart(String title, Vector<Vector<String>> dataVector) {
JFreeChart chart = ChartFactory.createXYBarChart(
title,
"X",
true,
"Y",
createDataset(dataVector),
PlotOrientation.VERTICAL,
true,
false,
false
);
final XYItemRenderer renderer = chart.getXYPlot().getRenderer();
final StandardXYToolTipGenerator generator = new StandardXYToolTipGenerator(
"{1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0.00")
);
renderer.setToolTipGenerator(generator);
final XYPlot plot = chart.getXYPlot();
final DateAxis dAxis = (DateAxis) plot.getDomainAxis();
dAxis.setVerticalTickLabels(true);
final NumberAxis nAxis = (NumberAxis) plot.getRangeAxis();
Calendar c = Calendar.getInstance();
c.setTime(dAxis.getMinimumDate());
c.add(Calendar.HOUR, 12);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(600, 300));
return chartPanel;
}
private IntervalXYDataset createDataset(Vector<Vector<String>> dataVector) {
TimeSeries series = new TimeSeries("Value");
for(Vector<String> tempVector : dataVector) {
Timestamp time = Timestamp.valueOf(tempVector.get(0));
series.add(new Millisecond(time), Double.parseDouble(tempVector.get(1)));
}
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
dataset.setDomainIsPointsInTime(false);
return dataset;
}
}
Test.java
public class Test extends JFrame {
private JPanel contentPane;
Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
Vector<String> tempString = null;
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
c.setTime(sdf.parse("2017-01-12 12:00:00"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0; i<10; i++) {
tempString = new Vector<String>();
tempString.add(sdf.format(c.getTime()));
tempString.add(String.valueOf((int)(Math.random()*35)));
c.add(Calendar.HOUR, 1);
dataVector.add(tempString);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
//contentPane.add(chart.generateChart(chart.getMinValue(), chart.getMaxValue()));
contentPane.add(new ChartAdapter().generateChart("Data", dataVector));
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…