How to enlarge the text in the Gagawa column?
I am using this code to create a gagawa diagram:
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;
}
and I get this graph:
how to increase the text (x-axis, y-axis, legend and title)?
source to share
It looks like the JFreeChart (Javadoc) class contains a Font object. It's sad to see the developers don't allow modifying the default font, but anyway here's how I resolved it ...
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;
}
}
This literally rips through all of their source code, finds here and replaces the default font object with the given font object from the constructor.
For your code, replacing
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
);
from:
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
);
Should do the trick if you specified (customFont).
For reference the default font
Static Final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
Thus, the entire chart uses the same font, substantially replacing the default font. SAHIL.R2050's answer is also valid, although it requires each individual section of the graphic to have its own fonts.
My answer is good for setting a new default font for all custom charts, while SAHIIL.R2050's answer is more consistent with using the package correctly. SAHIL.R2050's answer also allows you to specify the sizes of each partition, rather than all of them at the same time.
To summarize ChartFactory / CustomChartFactory, all it really does is create a ChartTheme and then apply it to the chart using the apply () method.
Hope it helped. :)
source to share
Maybe this will help you.
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis axis = plot.getDomainAxis();
CategoryPlot p = chart.getCategoryPlot();
ValueAxis axis2 = p.getRangeAxis();
Font font = new Font("Dialog", Font.PLAIN, 25);
axis.setTickLabelFont(font);
Font font2 = new Font("Dialog", Font.PLAIN, 15);
axis2.setTickLabelFont(font2);
Font font3 = new Font("Dialog", Font.PLAIN, 25);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
According to this Question
source to share