我一直在寻找知道如何更改每个条形颜色,因为我不喜欢默认条形颜色的渐变设置。
他们都建议用或多或少的代码覆盖BarRenderer
:
class CustomRenderer extends BarRenderer {
private Paint[] colors;
// add your custom colors
public CustomRenderer() {
this.colors = new Paint[] { Color.red, Color.blue, Color.green,
Color.yellow, Color.orange, Color.cyan, Color.magenta,
Color.blue };
}
public Paint getItemPaint(final int row, final int column) {
return this.colors[column % this.colors.length];
}
}
并且要使用它,应该放
CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);
但我得到的只是错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type ChartDemo is accessible. Must qualify the allocation with an enclosing instance of type ChartDemo (e.g. x.new A() where x is an instance of ChartDemo).
我不知道如何解决这个问题,这些答案似乎都不承认这个问题。
以下是我的完整代码:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class ChartDemo {
public static void main(String[] args) {
writeChartToPDF(generateBarChart(), 500, 400, "D://barchart.pdf");
}
public static void writeChartToPDF(JFreeChart chart, int width, int height,
String fileName) {
PdfWriter writer = null;
Document document = new Document(new Rectangle(width, height));
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
class CustomRenderer extends BarRenderer {
private Paint[] colors;
public CustomRenderer() {
this.colors = new Paint[] { Color.red, Color.blue, Color.green,
Color.yellow, Color.orange, Color.cyan, Color.magenta,
Color.blue };
}
public Paint getItemPaint(final int row, final int column) {
return this.colors[column % this.colors.length];
}
}
public static JFreeChart generateBarChart() {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(100, "Population", "2");
dataSet.setValue(78, "Population", "4");
dataSet.setValue(62, "Population", "6");
dataSet.setValue(50, "Population", "8");
dataSet.setValue(39, "Population", "10");
JFreeChart chart = ChartFactory.createBarChart("", "Team number",
"Solved problems (%)", dataSet, PlotOrientation.VERTICAL,
false, true, false);
chart.setBackgroundPaint(Color.WHITE);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(new Color(204, 204, 204));
plot.setRangeGridlinePaint(new Color(204, 204, 204));
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);
final BarRenderer renderer1 = (BarRenderer) plot.getRenderer();
renderer1.setDrawBarOutline(false);
renderer1.setShadowVisible(false);
renderer1.setDrawBarOutline(false);
renderer1.setSeriesPaint(0, Color.gray);
renderer1.setSeriesPaint(1, Color.RED);
return chart;
}
}
PS。我的目标是尽可能地模仿flot.js
的风格(平面/最小设计)。
答案 0 :(得分:4)
您的问题与JFreeChart无关,而是Java问题:您有一个嵌套的非静态类CustomRenderer。要实例化它,您需要一个封闭类的实例(ChartDemo
)或者(在您的情况下可能是更好的方法),您需要将其设置为静态:
static class CustomRenderer extends BarRenderer {
// ...
}
有关详情,请参阅SO上的this answer。
HTH,
- 马丁