我设法创建了一个简单的XYLineChart,但现在我需要用条形图而不是线条来完成。这意味着如果Y值低于零,则条形将被反转。
public static void plotmain(double [] ya, int [] xa) {
dataset = new XYSeriesCollection();
XYSeries data = new XYSeries("");
for (int i = 0 ; i < ya.length ; i++) {
data.add(xa[i], ya[i]);
}
dataset.addSeries(data);
showGraph();
}
private static void showGraph() {
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(resx, resy));
final ApplicationFrame frame = new ApplicationFrame("");
frame.setContentPane(chartPanel);
frame.pack();
frame.setVisible(true);
}
private static JFreeChart createChart(final XYDataset dataset) {
final JFreeChart chart = ChartFactory.createXYLineChart(
"", // chart title
"", // x axis label
"", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, false);
renderer.setSeriesPaint(0, Color.blue);
plot.setRenderer(renderer);
return chart;
}