我的酒吧后面有标签(这里没有显示),这导致了更长的值范围(即在这种情况下想要删除“125”)。 如何删除ValueAxis / RangeAxis中的最后一个值(NumberAxis?)?
首先想到我可以通过覆盖refreshTicks()
方法删除最高值,如下所示:JFreechart: Displaying X axis with values after specific units
但是我失去了那个空位。然后我想我可以覆盖“drawTickMarksAndLabels”方法:
ValueAxis axis = new ValueAxis(plot.getDomainAxis().getLabel(),
null) {
@Override
protected AxisState drawTickMarksAndLabels(Graphics2D g2,
double cursor, Rectangle2D plotArea,
Rectangle2D dataArea, RectangleEdge edge) {
// changes
}
plot.setRangeAxis(axis);
这个问题是我在未指定NullpointerException
时得到TickUnitSource
。知道如何为自定义TickUnitSource
提供正确的ValueAxis
吗?
源代码:
public class BarChartDemo extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public BarChartDemo(final String title) {
super(title);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Returns a sample dataset.
*
* @return The dataset.
*/
private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
// column keys...
final String category1 = "Category 1";
final String category2 = "Category 2";
final String category3 = "Category 3";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(70.0, series1, category1);
dataset.addValue(40.0, series1, category2);
dataset.addValue(100.0, series1, category3);
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo", // chart title
"Category", // domain axis label
"%", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips?
false // URLs?
);
CategoryPlot plot = chart.getCategoryPlot();
final int maxPercent = 100;
// Adding some extra space
NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setRange(0, maxPercent+(maxPercent*0.4));
// ValueAxis axis = new ValueAxis(plot.getRangeAxis().getLabel(),
// null) {
//
// @Override
// protected AxisState drawTickMarksAndLabels(Graphics2D g2,
// double cursor, Rectangle2D plotArea,
// Rectangle2D dataArea, RectangleEdge edge) {
//
// AxisState state = new AxisState(cursor);
//
// if (isAxisLineVisible()) {
// drawAxisLine(g2, cursor, dataArea, edge);
// }
//
// double ol = getTickMarkOutsideLength();
// double il = getTickMarkInsideLength();
//
// List ticks = refreshTicks(g2, state, dataArea, edge);
// state.setTicks(ticks);
// g2.setFont(getTickLabelFont());
// Iterator iterator = ticks.iterator();
// while (iterator.hasNext()) {
// ValueTick tick = (ValueTick) iterator.next();
// if (isTickLabelsVisible()) {
// g2.setPaint(getTickLabelPaint());
// float[] anchorPoint = calculateAnchorPoint(
// tick, cursor, dataArea, edge);
//
// System.out.println(tick.getText());
//
// TextUtilities.drawRotatedString(tick.getText(),
// g2, anchorPoint[0], anchorPoint[1],
// tick.getTextAnchor(), tick.getAngle(),
// tick.getRotationAnchor());
// }
//
// if (isTickMarksVisible()) {
// float xx = (float) valueToJava2D(
// tick.getValue(), dataArea, edge);
// Line2D mark = null;
// g2.setStroke(getTickMarkStroke());
// g2.setPaint(getTickMarkPaint());
// if (edge == RectangleEdge.LEFT) {
// mark = new Line2D.Double(cursor - ol, xx,
// cursor + il, xx);
// } else if (edge == RectangleEdge.RIGHT) {
// mark = new Line2D.Double(cursor + ol, xx,
// cursor - il, xx);
// } else if (edge == RectangleEdge.TOP) {
// mark = new Line2D.Double(xx, cursor - ol,
// xx, cursor + il);
// } else if (edge == RectangleEdge.BOTTOM) {
// mark = new Line2D.Double(xx, cursor + ol,
// xx, cursor - il);
// }
// g2.draw(mark);
// }
// }
//
// // need to work out the space used by the tick labels...
// // so we can update the cursor...
// double used = 0.0;
// if (isTickLabelsVisible()) {
// if (edge == RectangleEdge.LEFT) {
// used += findMaximumTickLabelWidth(ticks, g2,
// plotArea, isVerticalTickLabels());
// state.cursorLeft(used);
// } else if (edge == RectangleEdge.RIGHT) {
// used = findMaximumTickLabelWidth(ticks, g2,
// plotArea, isVerticalTickLabels());
// state.cursorRight(used);
// } else if (edge == RectangleEdge.TOP) {
// used = findMaximumTickLabelHeight(ticks, g2,
// plotArea, isVerticalTickLabels());
// state.cursorUp(used);
// } else if (edge == RectangleEdge.BOTTOM) {
// used = findMaximumTickLabelHeight(ticks, g2,
// plotArea, isVerticalTickLabels());
// state.cursorDown(used);
// }
// }
//
// return state;
// }
//
// @Override
// protected void autoAdjustRange() {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public double java2DToValue(double arg0, Rectangle2D arg1,
// RectangleEdge arg2) {
// // TODO Auto-generated method stub
// return 0;
// }
//
// @Override
// public double valueToJava2D(double arg0, Rectangle2D arg1,
// RectangleEdge arg2) {
// // TODO Auto-generated method stub
// return 0;
// }
//
// @Override
// public void configure() {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public AxisState draw(Graphics2D arg0, double arg1,
// Rectangle2D arg2, Rectangle2D arg3,
// RectangleEdge arg4, PlotRenderingInfo arg5) {
// // TODO Auto-generated method stub
// return null;
// }
//
// @Override
// public List refreshTicks(Graphics2D arg0, AxisState arg1,
// Rectangle2D arg2, RectangleEdge arg3) {
// // TODO Auto-generated method stub
// return null;
// }
// };
//
// plot.setRangeAxis(axis);
return chart;
}
public static void main(final String[] args) {
final BarChartDemo demo = new BarChartDemo("Bar Chart Demo");
demo.pack();
demo.setVisible(true);
}
}
答案 0 :(得分:1)
管理以删除最后一个值:
NumberAxis rangeAxis = new NumberAxis() {
@Override
protected AxisState drawTickMarksAndLabels(Graphics2D g2,
double cursor, Rectangle2D plotArea, Rectangle2D dataArea,
RectangleEdge edge) {
// CHANGES
}
plot.setRangeAxis(rangeAxis);