Unable to custsomize the bar chart bars width

久未见 提交于 2020-01-16 09:03:45

问题


I'm able to generate the bar chart using bar jfreechart library.Coming to bar chart customization unable to reduce the bars (narrow bars instead of normal bars)width like below pic.

I am looking output like first graph but I'm getting output like second graph.

I tried chart generation with jfreechart factory.And some cutomization to bars like font and colors changing and adding own legend to the chart.But bars widths related code is not working.

chart.setBorderVisible(false);
chart.getLegend().setPosition(RectangleEdge.BOTTOM);
Font font = new Font("SansSerif", Font.PLAIN, 12);
chart.getTitle().setFont(font);
CategoryPlot plot = chart.getCategoryPlot();
plot.setNoDataMessage("No data available");
plot.setBackgroundPaint(Color.white);
plot.setAxisOffset(RectangleInsets.ZERO_INSETS);
plot.setFixedLegendItems(chartLegend);
plot.getDomainAxis().setLabelFont(font);
plot.getRangeAxis().setLabelFont(font);
plot.setOutlineVisible(false);
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setDefaultItemLabelsVisible(false);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER);
renderer.setDefaultPositiveItemLabelPosition(position);
BarRenderer barRenderer = (BarRenderer) renderer;
for (int i = 0; i < plot.getDataset().getRowCount(); i++) {
    if (plot.getDataset().getRowKey(i).equals("FIRST_BAR")) {
        barRenderer.setSeriesPaint(i, Constants.COLOR_RED);
    }
    if (plot.getDataset().getRowKey(i).equals("SECOND_BAR)) {
        barRenderer.setSeriesPaint(i, Constants.COLOR_YELLOW);
    }
    if (plot.getDataset().getRowKey(i).equals("THIRD_BAR")) {
        barRenderer.setSeriesPaint(i, Constants.COLOR_BLUE);
    }
    if (plot.getDataset().getRowKey(i).equals("FOURTH_BAR")) {
        barRenderer.setSeriesPaint(i, Constants.COLOR_GREEN);
    }
}
barRenderer.setDrawBarOutline(false);
barRenderer.setBarPainter(new StandardBarPainter());

any suggestions that could be more helpful to my report generation.

来源:https://stackoverflow.com/questions/57050099/unable-to-custsomize-the-bar-chart-bars-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!