Amila Iddamalgoda:
私が使用してはJFreeChartを、次のことは私が開発したチャートと関連するコードのスクリーンショットです。
private void getBarChart(List<Data> data) {
JFreeChart barChart = ChartFactory.createBarChart("", "", "", createDataset(data), PlotOrientation.VERTICAL, false, true, false);
CategoryPlot plot = barChart.getCategoryPlot();
plot.getRenderer().setSeriesPaint(0, new Color(7, 43, 97));
barChart.getCategoryPlot().getRangeAxis().setLowerBound(0);
barChart.getCategoryPlot().getRangeAxis().setUpperBound(1);
NumberAxis xAxis2 = (NumberAxis) barChart.getCategoryPlot().getRangeAxis();
xAxis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
plot.getRenderer().setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());
plot.getRenderer().setSeriesItemLabelsVisible(1, true);
plot.getRenderer().setBaseItemLabelsVisible(true);
plot.getRenderer().setBaseSeriesVisible(true);
barChart.getCategoryPlot().setRenderer(plot.getRenderer());
BarRenderer.setDefaultBarPainter(new StandardBarPainter());
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
BufferedImage image = new BufferedImage(650, 250, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
Rectangle r = new Rectangle(0, 0, 650, 250);
barChart.draw(g2, r);
BufferedImage chartImage = barChart.createBufferedImage(600, 400, null);
}
質問1) 期待されるグラフのようなあたりとしてのx軸lablesをフォーマットする方法は?(barChart.getCategoryPlotでCategoryLables又はTickLabels()。getDomainAxis())
質問2)の値は、各バー(SeriesItemLabels)の上部に表示さは、Y軸と同様パーセンテージマーク(%)でフォーマットする必要があります。(また、私は私がxAxis2.setNumberFormatOverrideで行っているように、値は自動的に100%掛け合わされます。今のところ、それが唯一の小数点以下の値を表示すると思います)。これを達成するには?
私を助けてください。ありがとうございました。
Topaco:
1)次の行は、上り勾配で軸ラベルを可能にします。
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
45度の角度を示し、UPは右上の左下から向きを意味します。あなたはまた、任意の角度を定義することができる(例えば22.5°)と
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.toRadians(22.5)));
ことに留意すべきであるcreateUpRotationLabelPositions
ラジアンで角度を期待しています。
2)次の行は、パーセントで直列0のバーのラベルをフォーマットします。
DecimalFormat labelFormat = new DecimalFormat("##0.0 %");
labelFormat.setMultiplier(100);
plot.getRenderer().setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator("{2}", labelFormat));
plot.getRenderer().setSeriesItemLabelsVisible(0, true);
{0} =系列と、{1} =カテゴリ、{2} =値
別の方法として、あなたは、例えば、独自のラベルジェネレータを定義することができます。
class CstmStandardCategoryItemLabelGenerator extends StandardCategoryItemLabelGenerator {
@Override
public String generateLabel(CategoryDataset dataset, int row, int column) {
return String.format("%.1f %%", dataset.getValue(row, column).doubleValue() * 100.0);
}
}
次のようにこれは簡単に使用することができます。
plot.getRenderer().setSeriesItemLabelGenerator(0, new CstmStandardCategoryItemLabelGenerator());
この結果には: