我使用此代码生成 gagawa 图表:
private Img createChart(LatencyHistogram current, LatencyHistogram baseLine) {
Img image = null;
final CategoryDataset dataset = fillDataSet(current, baseLine);
final JFreeChart chart = ChartFactory.createBarChart(
"Latecny histogram", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(java.awt.Color.white);
// save it to an image
try {
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String latency_graph = Constants.LATENCY_GRAPH_NAME;
final String pathname = Constants.HTML_PAGES_PATH + "images/"+latency_graph;
final File file1 = new File(pathname);
ChartUtilities.saveChartAsPNG(file1, chart, 2200, 1000, info);
image = new Img("latency", "../images/"+latency_graph).setWidth("1300");
return image;
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
我得到这张图:
如何放大文本(x 轴、y 轴、图例和标题)?
最佳答案
看来 JFreeChart (Javadoc)类包含一个字体对象。很遗憾看到开发人员默认情况下不启用字体修改,但无论如何我是这样解决这个问题的……
class CustomChartFactory extends ChartFactory{
public static JFreeChart createCustomBarChart(String title,
String categoryAxisLabel, String valueAxisLabel,
CategoryDataset dataset, PlotOrientation orientation,
boolean legend, boolean tooltips, boolean urls, Font font) {
ParamChecks.nullNotPermitted(orientation, "orientation");
CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
BarRenderer renderer = new BarRenderer();
if (orientation == PlotOrientation.HORIZONTAL) {
ItemLabelPosition position1 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
renderer.setBasePositiveItemLabelPosition(position1);
ItemLabelPosition position2 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
renderer.setBaseNegativeItemLabelPosition(position2);
} else if (orientation == PlotOrientation.VERTICAL) {
ItemLabelPosition position1 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
renderer.setBasePositiveItemLabelPosition(position1);
ItemLabelPosition position2 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
renderer.setBaseNegativeItemLabelPosition(position2);
}
if (tooltips) {
renderer.setBaseToolTipGenerator(
new StandardCategoryToolTipGenerator());
}
if (urls) {
renderer.setBaseItemURLGenerator(
new StandardCategoryURLGenerator());
}
CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
renderer);
plot.setOrientation(orientation);
JFreeChart chart = new JFreeChart(title, font,
plot, legend);
currentTheme.apply(chart);
return chart;
}
}
这实际上是在翻录他们的整个源代码,发现 here ,并用构造函数中给定的字体对象替换默认字体对象。
对于你的代码,替换
final JFreeChart chart = ChartFactory.createBarChart(
"Latecny histogram", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
与:
Font customFont = new Font("SansSerif", Font.BOLD, 25);
final JFreeChart chart = CustomChartFactory.createBarChart(
"Latecny histogram", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false, // urls
customFont //font
);
假设您已指定 (customFont),应该可以解决问题。
作为引用,默认字体是
Static Final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
这样一来,整个图表都使用了相同的字体,基本上替换了默认字体。 SAHIL.R2050 的答案也是有效的,尽管它确实要求图表的每个单独部分都设置了字体。
我的回答适合为所有要使用的自定义图表设置新的默认字体,而 SAHIIL.R2050 的回答更符合包的正确用法。 SAHIL.R2050 的答案还允许您指定每个部分的大小,而不是一次指定所有部分。
总结一下 ChartFactory/CustomChartFactory,它真正做的就是创建一个 ChartTheme,然后使用它的 apply() 方法将它应用于图表。
希望这对您有所帮助。 :)
关于java - 如何放大gagawa图中的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394113/