📄 barrenderer.java
字号:
this.positiveItemLabelPositionFallback = position;
notifyListeners(new RendererChangeEvent(this));
}
/**
* Returns the fallback position for negative item labels that don't fit
* within a bar.
*
* @return The fallback position (<code>null</code> possible).
*/
public ItemLabelPosition getNegativeItemLabelPositionFallback() {
return this.negativeItemLabelPositionFallback;
}
/**
* Sets the fallback position for negative item labels that don't fit
* within a bar, and sends a {@link RendererChangeEvent} to all registered
* listeners.
*
* @param position the position (<code>null</code> permitted).
*/
public void setNegativeItemLabelPositionFallback(
ItemLabelPosition position) {
this.negativeItemLabelPositionFallback = position;
notifyListeners(new RendererChangeEvent(this));
}
/**
* Returns the lower clip value. This value is recalculated in the
* initialise() method.
*
* @return The value.
*/
public double getLowerClip() {
// TODO: this attribute should be transferred to the renderer state.
return this.lowerClip;
}
/**
* Returns the upper clip value. This value is recalculated in the
* initialise() method.
*
* @return The value.
*/
public double getUpperClip() {
// TODO: this attribute should be transferred to the renderer state.
return this.upperClip;
}
/**
* Initialises the renderer and returns a state object that will be passed
* to subsequent calls to the drawItem method. This method gets called
* once at the start of the process of drawing a chart.
*
* @param g2 the graphics device.
* @param dataArea the area in which the data is to be plotted.
* @param plot the plot.
* @param rendererIndex the renderer index.
* @param info collects chart rendering information for return to caller.
*
* @return The renderer state.
*/
public CategoryItemRendererState initialise(Graphics2D g2,
Rectangle2D dataArea,
CategoryPlot plot,
int rendererIndex,
PlotRenderingInfo info) {
CategoryItemRendererState state = super.initialise(
g2, dataArea, plot, rendererIndex, info
);
// get the clipping values...
ValueAxis rangeAxis = getRangeAxis(plot, rendererIndex);
this.lowerClip = rangeAxis.getRange().getLowerBound();
this.upperClip = rangeAxis.getRange().getUpperBound();
// calculate the bar width
calculateBarWidth(plot, dataArea, rendererIndex, state);
return state;
}
/**
* Calculates the bar width and stores it in the renderer state.
*
* @param plot the plot.
* @param dataArea the data area.
* @param rendererIndex the renderer index.
* @param state the renderer state.
*/
protected void calculateBarWidth(CategoryPlot plot,
Rectangle2D dataArea,
int rendererIndex,
CategoryItemRendererState state) {
CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex);
CategoryDataset dataset = plot.getDataset(rendererIndex);
if (dataset != null) {
int columns = dataset.getColumnCount();
int rows = dataset.getRowCount();
double space = 0.0;
PlotOrientation orientation = plot.getOrientation();
if (orientation == PlotOrientation.HORIZONTAL) {
space = dataArea.getHeight();
}
else if (orientation == PlotOrientation.VERTICAL) {
space = dataArea.getWidth();
}
double maxWidth = space * getMaximumBarWidth();
double categoryMargin = 0.0;
double currentItemMargin = 0.0;
if (columns > 1) {
categoryMargin = domainAxis.getCategoryMargin();
}
if (rows > 1) {
currentItemMargin = getItemMargin();
}
double used = space * (1 - domainAxis.getLowerMargin()
- domainAxis.getUpperMargin()
- categoryMargin - currentItemMargin);
if ((rows * columns) > 0) {
state.setBarWidth(Math.min(used / (rows * columns), maxWidth));
}
else {
state.setBarWidth(Math.min(used, maxWidth));
}
}
}
/**
* Calculates the coordinate of the first "side" of a bar. This will be
* the minimum x-coordinate for a vertical bar, and the minimum
* y-coordinate for a horizontal bar.
*
* @param plot the plot.
* @param orientation the plot orientation.
* @param dataArea the data area.
* @param domainAxis the domain axis.
* @param state the renderer state (has the bar width precalculated).
* @param row the row index.
* @param column the column index.
*
* @return The coordinate.
*/
protected double calculateBarW0(CategoryPlot plot,
PlotOrientation orientation,
Rectangle2D dataArea,
CategoryAxis domainAxis,
CategoryItemRendererState state,
int row,
int column) {
// calculate bar width...
double space = 0.0;
if (orientation == PlotOrientation.HORIZONTAL) {
space = dataArea.getHeight();
}
else {
space = dataArea.getWidth();
}
double barW0 = domainAxis.getCategoryStart(
column, getColumnCount(), dataArea, plot.getDomainAxisEdge()
);
int seriesCount = getRowCount();
int categoryCount = getColumnCount();
if (seriesCount > 1) {
double seriesGap = space * getItemMargin()
/ (categoryCount * (seriesCount - 1));
double seriesW = calculateSeriesWidth(
space, domainAxis, categoryCount, seriesCount
);
barW0 = barW0 + row * (seriesW + seriesGap)
+ (seriesW / 2.0) - (state.getBarWidth() / 2.0);
}
else {
barW0 = domainAxis.getCategoryMiddle(
column, getColumnCount(), dataArea, plot.getDomainAxisEdge()
) - state.getBarWidth() / 2.0;
}
return barW0;
}
/**
* Calculates the coordinates for the length of a single bar.
*
* @param value the value represented by the bar.
*
* @return The coordinates for each end of the bar (or <code>null</code> if
* the bar is not visible for the current axis range).
*/
protected double[] calculateBarL0L1(double value) {
double lclip = getLowerClip();
double uclip = getUpperClip();
double bb = this.base;
if (uclip <= 0.0) { // cases 1, 2, 3 and 4
if (value >= uclip) {
return null; // bar is not visible
}
bb = uclip;
if (value <= lclip) {
value = lclip;
}
}
else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
if (value >= uclip) {
value = uclip;
}
else {
if (value <= lclip) {
value = lclip;
}
}
}
else { // cases 9, 10, 11 and 12
if (value <= lclip) {
return null; // bar is not visible
}
bb = lclip;
if (value >= uclip) {
value = uclip;
}
}
return new double[] {bb, value};
}
/**
* Returns a legend item for a series.
*
* @param datasetIndex the dataset index (zero-based).
* @param series the series index (zero-based).
*
* @return The legend item.
*/
public LegendItem getLegendItem(int datasetIndex, int series) {
CategoryPlot cp = getPlot();
if (cp == null) {
return null;
}
CategoryDataset dataset;
dataset = cp.getDataset(datasetIndex);
String label = getLegendItemLabelGenerator().generateLabel(dataset,
series);
String description = label;
String toolTipText = null;
if (getLegendItemToolTipGenerator() != null) {
toolTipText = getLegendItemToolTipGenerator().generateLabel(
dataset, series);
}
String urlText = null;
if (getLegendItemURLGenerator() != null) {
urlText = getLegendItemURLGenerator().generateLabel(dataset,
series);
}
Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0);
Paint paint = getSeriesPaint(series);
Paint outlinePaint = getSeriesOutlinePaint(series);
Stroke outlineStroke = getSeriesOutlineStroke(series);
return new LegendItem(label, description, toolTipText, urlText,
true, shape, true, paint,
isDrawBarOutline(), outlinePaint, outlineStroke,
false, new Line2D.Float(), new BasicStroke(1.0f),
Color.BLACK);
}
/**
* Draws the bar for a single (series, category) data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the data area.
* @param plot the plot.
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @param pass the pass index.
*/
public void drawItem(Graphics2D g2,
CategoryItemRendererState state,
Rectangle2D dataArea,
CategoryPlot plot,
CategoryAxis domainAxis,
ValueAxis rangeAxis,
CategoryDataset dataset,
int row,
int column,
int pass) {
// nothing is drawn for null values...
Number dataValue = dataset.getValue(row, column);
if (dataValue == null) {
return;
}
double value = dataValue.doubleValue();
PlotOrientation orientation = plot.getOrientation();
double barW0 = calculateBarW0(
plot, orientation, dataArea, domainAxis, state, row, column
);
double[] barL0L1 = calculateBarL0L1(value);
if (barL0L1 == null) {
return; // the bar is not visible
}
RectangleEdge edge = plot.getRangeAxisEdge();
double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
double barL0 = Math.min(transL0, transL1);
double barLength = Math.max(
Math.abs(transL1 - transL0), getMinimumBarLength()
);
// draw the bar...
Rectangle2D bar = null;
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(
barL0, barW0, barLength, state.getBarWidth()
);
}
else {
bar = new Rectangle2D.Double(
barW0, barL0, state.getBarWidth(), barLength
);
}
Paint itemPaint = getItemPaint(row, column);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -