条形图相互连接,但我需要将它分开

时间:2016-12-08 04:27:44

标签: java jfreechart

一切似乎都运转良好,唯一的问题是应该生成的图表是一个没有相互连接的条形图,但我最终得到的图表并不是我想要的图表。

我关注了视频,代码似乎相似,所以我无法弄清楚出了什么问题:

try {
    int rubyPoints = Integer.parseInt(getHP("Ruby"));
    int amberPoints = Integer.parseInt(getHP("Amber"));
    int pearlPoints = Integer.parseInt(getHP("Pearl"));
    int sapphirePoints = Integer.parseInt(getHP("Sapphire"));
    DefaultCategoryDataset barChartData = new DefaultCategoryDataset();
    barChartData.setValue(rubyPoints, "House Points", "Ruby");
    barChartData.setValue(amberPoints, "House Points", "Amber");
    barChartData.setValue(pearlPoints, "House Points", "Pearl");
    barChartData.setValue(sapphirePoints, "House Points", "Sapphire");
    JFreeChart barChart = ChartFactory.createAreaChart("House Points",
        "All houses", "Amount of points", barChartData, PlotOrientation.VERTICAL, false, true, false);

    CategoryPlot barchrt = barChart.getCategoryPlot();
    barchrt.setRangeGridlinePaint(Color.BLUE);
    ChartPanel barPanel = new ChartPanel(barChart);
    panelChart.removeAll();
    panelChart.add(barPanel, BorderLayout.CENTER);
    panelChart.validate();
} catch (IOException ioe) {
    // error handling
}

图表看起来像这样(这不是我需要的样子):

enter image description here

1 个答案:

答案 0 :(得分:1)

这非常简单:您创建的图表不是barChart,而是areaChart

为了获得您想要的结果,只需将实例化图表的行更改为:

JFreeChart barChart = ChartFactory.createBarChart("House Points", 
    "All houses", "Amount of points", barChartData, PlotOrientation.VERTICAL, false, true, false);

你会得到类似的东西:

enter image description here