我在图表中有一个水平堆叠条形图,每行有一个动态数字" sub" -bars(数字取决于我程序输入的数据)。我想以某种方式命名子条,最好是在条形内部使用文本。图表中的不同行是独立的。这可能吗?
一个例子:第一行包含5个条形图,它们可以获得标签" A"," B"," C"," D"和" E"第二行有两个名为" F"和" G"。
我猜是没有办法在数据集中附加文字?
谢谢!
当前图表的示例图片:
以下是今天看起来的一些代码:
public void plot(Formatter formatter) {
boolean functionsFound;
int level = 0;
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int offset;
double d;
do {
functionsFound = false;
offset = startTime;
for (FunctionInstance x : functionInstances) {
if (x.getOvlLevel() == level) {
functionsFound = true;
dataset.addValue(d = (double)(x.getFuncExecTimeStart() - x.getPreOverhead() - offset), "- " + Integer.toString(offset), "Level: " + Integer.toString(level));
dataset.addValue(d = (double)x.getPreOverhead(), "pre overhead " + Integer.toString(offset), "Level: " + Integer.toString(level));
dataset.addValue(d = (double)x.getFuncExecTime(), "func exec " + Integer.toString(offset), "Level: " + Integer.toString(level));
dataset.addValue(d = (double)x.getPostOverhead(), "post overhead " + Integer.toString(offset), "Level: " + Integer.toString(level));
offset = x.getFuncExecTimeStart() + x.getFuncExecTime() + x.getPostOverhead();
}
}
level++;
} while (functionsFound);
// create the chart...
JFreeChart chart = ChartFactory.createStackedBarChart(
"Function hierarchy",
"Level", // domain axis label
"Cycles", // range axis label
dataset, // data
PlotOrientation.HORIZONTAL, // orientation
false, // include legend
true, // tooltips?
false // URLs?
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
chart.getTitle().setMargin(2.0, 0.0, 0.0, 0.0);
// get a reference to the plot for further customisation...
CategoryPlot plot = (CategoryPlot) chart.getPlot();
LegendItemCollection items = new LegendItemCollection();
items.add(new LegendItem("Function execution", null, null, null,
new Rectangle2D.Double(-6.0, -3.0, 12.0, 6.0), Color.green));
items.add(new LegendItem("Overhead", null, null, null,
new Rectangle2D.Double(-6.0, -3.0, 12.0, 6.0), Color.red));
plot.setFixedLegendItems(items);
plot.setInsets(new RectangleInsets(5, 5, 5, 20));
LegendTitle legend = new LegendTitle(plot);
legend.setPosition(RectangleEdge.BOTTOM);
chart.addSubtitle(legend);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setUpperMargin(0.0);
// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
Paint gp0 = new Color(0, 0, 0, 0);
GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.red,
0.0f, 0.0f, new Color(64, 0, 0));
GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.green,
0.0f, 0.0f, new Color(0, 64, 0));
GradientPaint gp3 = new GradientPaint(0.0f, 0.0f, Color.red,
0.0f, 0.0f, new Color(64, 0, 0));
for (int y = 0; y < 10; y++) {
renderer.setSeriesPaint(y*4 + 0, gp0);
renderer.setSeriesPaint(y*4 + 1, gp1);
renderer.setSeriesPaint(y*4 + 2, gp2);
renderer.setSeriesPaint(y*4 + 3, gp3);
}
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
BufferedImage img = chart.createBufferedImage(1000, 500, info);
try {
OutputStream ost =
new BufferedOutputStream(new FileOutputStream(new File(Global.tempFileDirectory + "/" + thrInstName + ".png"), false));
ChartUtilities.writeBufferedImageAsPNG(ost, img);
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
return;
}
}