我使用java绘制图表,我想在xyplot中为小区域设置背景颜色。喜欢:我想要从x轴(20到30)和y轴(10-20)的区域设置红色,并为剩下的做这样的,我使用价值制造者,但不给我很好的解决方案,因为它填充长标记的颜色和我只想要小面积
这是我的代码:
private JFreeChart createChart(XYDataset dataset) {
/*JFreeChart chart = ChartFactory.createXYLineChart(
"Chart Index Sag",
"Sag Duration",
"Retained Voltage",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);*/
XYLineAndShapeRenderer rr = new XYLineAndShapeRenderer();
rr.setSeriesLinesVisible(2, true);
rr.setSeriesShapesVisible(2, true);
rr.setPaint(Color.red);
// Point label
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setBaseFillPaint(Color.black);
renderer.setSeriesLinesVisible(0, false);
renderer.setBaseItemLabelGenerator(new LabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelFont(new Font("Arial", Font.BOLD, 12));
renderer.setPaint(Color.black);
//plot = (XYPlot) chart.getPlot();
SymbolAxis symbolaxis1 = new SymbolAxis("Sag Duration", new String[] { "0","0.02","0.2", "0.5", "10" });
symbolaxis1.setRange(0,6);
symbolaxis1.setGridBandsVisible(false);
SymbolAxis range = new SymbolAxis("Returned Voltage", new String[] { "0","0.2","0.4", "0.6", "0.8", "1" });
range.setRange(0,5);
range.setGridBandsVisible(false);
plot = new XYPlot(dataset, symbolaxis1, range, renderer);
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.setBackgroundPaint(Color.lightGray);
// Line curve
plot.setDataset(0, dataset);
plot.setDataset(1, dataset1);
final Marker originalEnd = new ValueMarker(1);
originalEnd.setPaint(Color.black);
originalEnd.setStroke(new BasicStroke(1));
plot.addDomainMarker(originalEnd);
final Marker v2 = new ValueMarker(2);
v2.setPaint(Color.black);
v2.setStroke(new BasicStroke(1));
plot.addDomainMarker(v2);
final Marker v3 = new ValueMarker(3);
v3.setPaint(Color.black);
v3.setStroke(new BasicStroke(1));
plot.addDomainMarker(v3);
final Marker v4 = new ValueMarker(4);
v4.setPaint(Color.black);
v4.setStroke(new BasicStroke(1));
plot.addDomainMarker(v4);
Marker r1 = new ValueMarker(1);
r1.setPaint(Color.black);
r1.setStroke(new BasicStroke(1));
plot.addRangeMarker(r1);
Marker r2 = new ValueMarker(2);
r2.setPaint(Color.black);
r2.setStroke(new BasicStroke(1));
plot.addRangeMarker(r2);
Marker r3 = new ValueMarker(3);
r3.setPaint(Color.black);
r3.setStroke(new BasicStroke(1));
plot.addRangeMarker(r3);
Marker r4 = new ValueMarker(4);
r4.setPaint(Color.black);
r4.setStroke(new BasicStroke(1));
plot.addRangeMarker(r4);
float dash[] = {1, 1};
float f1 = 1, f2 = 1, f3 = 10;
/*NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setRange(0, 2);
domainAxis.setTickUnit(new NumberTickUnit(0.2));*/
BasicStroke gridstroke = new BasicStroke(f1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, f2, dash, f3);
plot.setDomainGridlinePaint(Color.blue);
//plot.setRangeGridlinePaint(Color.blue);
plot.setDomainGridlineStroke(gridstroke);
plot.setRangeGridlineStroke(gridstroke);
final JFreeChart chart = new JFreeChart(plot);
chart.getXYPlot().setRenderer(0, rr);
chart.getXYPlot().setRenderer(1, renderer);
return chart;
}
答案 0 :(得分:3)
你应该试试XYBoxAnnotation
,也许是这样的:
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
XYBoxAnnotation xyboxannotation = new XYBoxAnnotation(
1, 1, 2, 2, new BasicStroke(0), new Color(0), new Color(0x30d0d0d0));
renderer.addAnnotation(xyboxannotation, Layer.BACKGROUND);