Этот фрагмент кода поможет вам, просто пропустите код в вашей IDE и запустите.
import java.awt.Color;
import java.awt.GradientPaint;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.data.CategoryDataset;
import org.jfree.data.DefaultCategoryDataset;
/**
*
*
*/
public class BarChart {
/**
* Returns a sample dataset.
*
* @return The dataset.
*/
private CategoryDataset createDataset() {
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String category1 = "Category 1";
String category2 = "Category 2";
String category3 = "Category 3";
String category4 = "Category 4";
String category5 = "Category 5";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);
dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);
dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(2.0, series3, category3);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);
return dataset;
}
public static void main(String[] args) {
String path = System.getProperty("user.dir") + "\\";
BarChart bc = new BarChart();
// create the chart...
JFreeChart chart = bc.createChart(bc.createDataset());
FileOutputStream out = null;
try {
out = new FileOutputStream(path + "jfreechartimg.jpeg");
ChartUtilities.writeChartAsJPEG(out, chart, 600, 400);
} catch (IOException iOException) {
} finally {
try{out.close();}catch(Exception ignore){}
}
System.out.println("finished !");
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private JFreeChart createChart(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo for Mr P.", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(new Color(255,255,91));
// get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();
// set the range axis to display integers only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, new Color(161,29,33),
0.0f, 0.0f, new Color(249,175,154));
GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, new Color(0,108,56),
0.0f, 0.0f, new Color(119,193,88));
GradientPaint gp2 = new GradientPaint(
0.0f, 0.0f, new Color(35,65,139),
0.0f, 0.0f, new Color(185,219,243));
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
return chart;
}
}
Вывод кода: