使用jFreechart和guava lib生成条形图

0 java jfreechart guava

请协助.

我正在使用谷歌的guava lib来生成带有jFreeChart的XY线图.我能够生成简单的XY线图.但是我无法使用它生成bar-char.

任何帮助将不胜感激.

Lev*_*nal 8

从教程指南,我相信它位于这里:pdf

条形图示例

假设我们想要构建一个条形图,比较以下销售人员所获得的利润:Jane,Tom,Jill,John,Fred.

public class BarChartExample {
 public static void main(String[] args) {
 // Create a simple Bar chart
 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(6, "Profit", "Jane");
  dataset.setValue(7, "Profit", "Tom");
  dataset.setValue(8, "Profit", "Jill");
  dataset.setValue(5, "Profit", "John");
  dataset.setValue(12, "Profit", "Fred");
JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman",
  "Salesman", "Profit", dataset, PlotOrientation.VERTICAL,
   false, true, false);
try {
     ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 500, 300);
} catch (IOException e) {
     System.err.println("Problem occurred creating chart.");
}}}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

说明:

要为条形图定义数据集,请使用类的对象

DefaultCategoryDataset.
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Run Code Online (Sandbox Code Playgroud)

可以使用setValue()方法将值添加到数据集.

dataset.setValue(6, “Profit”, “Jane”);
Run Code Online (Sandbox Code Playgroud)

第一个参数指定Jane实现的利润水平.第二个参数指定图例中显示的条形含义.要生成类JFreeChart的条形图对象,请使用ChartFactory的方法createBarChart().它采用与createXYLineChart()所需的参数集相同的参数集.第一个参数表示图形的标题,第二个参数表示x轴的标签,第三个参数表示y轴的标签.

JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman",
"Salesman", "Profit", dataset, PlotOrientation.VERTICAL, false, true, false);
Run Code Online (Sandbox Code Playgroud)

修改:与饼图的情况一样,可以使用createBarChart3D()方法在3D中显示条形.

修改:

可能值得的一件事是调整图形的外观(例如颜色).

chart.setBackgroundPaint(Color.yellow); // Set the background colour of the chart
chart.getTitle().setPaint(Color.blue); // Adjust the colour of the title
CategoryPlot p = chart.getCategoryPlot(); // Get the Plot object for a bar graph
p.setBackgroundPaint(Color.black); // Modify the plot background
p.setRangeGridlinePaint(Color.red); // Modify the colour of the plot gridlines
Run Code Online (Sandbox Code Playgroud)

希望您可以重新设计以满足您的需求,

祝好运!