📄 barrenderer.java
字号:
/**
* Returns the fallback position for positive item labels that don't fit
* within a bar.
*
* @return The fallback position (<code>null</code> possible).
*
* @see #setPositiveItemLabelPositionFallback(ItemLabelPosition)
*/
public ItemLabelPosition getPositiveItemLabelPositionFallback() {
return this.positiveItemLabelPositionFallback;
}
/**
* Sets the fallback position for positive 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).
*
* @see #getPositiveItemLabelPositionFallback()
*/
public void setPositiveItemLabelPositionFallback(
ItemLabelPosition position) {
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).
*
* @see #setPositiveItemLabelPositionFallback(ItemLabelPosition)
*/
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).
*
* @see #getNegativeItemLabelPositionFallback()
*/
public void setNegativeItemLabelPositionFallback(
ItemLabelPosition position) {
this.negativeItemLabelPositionFallback = position;
notifyListeners(new RendererChangeEvent(this));
}
/**
* Returns the flag that controls whether or not the base value for the
* bars is included in the range calculated by
* {@link #findRangeBounds(CategoryDataset)}.
*
* @return <code>true</code> if the base is included in the range, and
* <code>false</code> otherwise.
*
* @since 1.0.1
*
* @see #setIncludeBaseInRange(boolean)
*/
public boolean getIncludeBaseInRange() {
return this.includeBaseInRange;
}
/**
* Sets the flag that controls whether or not the base value for the bars
* is included in the range calculated by
* {@link #findRangeBounds(CategoryDataset)}. If the flag is changed,
* a {@link RendererChangeEvent} is sent to all registered listeners.
*
* @param include the new value for the flag.
*
* @since 1.0.1
*
* @see #getIncludeBaseInRange()
*/
public void setIncludeBaseInRange(boolean include) {
if (this.includeBaseInRange != include) {
this.includeBaseInRange = include;
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 = plot.getRangeAxisForDataset(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 barLow = Math.min(this.base, value);
double barHigh = Math.max(this.base, value);
if (barHigh < lclip) { // bar is not visible
return null;
}
if (barLow > uclip) { // bar is not visible
return null;
}
barLow = Math.max(barLow, lclip);
barHigh = Math.min(barHigh, uclip);
return new double[] {barLow, barHigh};
}
/**
* Returns the range of values the renderer requires to display all the
* items from the specified dataset. This takes into account the range
* of values in the dataset, plus the flag that determines whether or not
* the base value for the bars should be included in the range.
*
* @param dataset the dataset (<code>null</code> permitted).
*
* @return The range (or <code>null</code> if the dataset is
* <code>null</code> or empty).
*/
public Range findRangeBounds(CategoryDataset dataset) {
Range result = DatasetUtilities.findRangeBounds(dataset);
if (result != null) {
if (this.includeBaseInRange) {
result = Range.expandToInclude(result, this.base);
}
}
return result;
}
/**
* 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 (possibly <code>null</code>).
*/
public LegendItem getLegendItem(int datasetIndex, int series) {
CategoryPlot cp = getPlot();
if (cp == null) {
return null;
}
// check that a legend item needs to be displayed...
if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) {
return null;
}
CategoryDataset 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 = lookupSeriesPaint(series);
Paint outlinePaint = lookupSeriesOutlinePaint(series);
Stroke outlineStroke = lookupSeriesOutlineStroke(series);
LegendItem result = new LegendItem(label, description, toolTipText,
urlText, true, shape, true, paint, isDrawBarOutline(),
outlinePaint, outlineStroke, false, new Line2D.Float(),
new BasicStroke(1.0f), Color.black);
result.setDataset(dataset);
result.setDatasetIndex(datasetIndex);
result.setSeriesKey(dataset.getRowKey(series));
result.setSeriesIndex(series);
if (this.gradientPaintTransformer != null) {
result.setFillPaintTransformer(this.gradientPaintTransformer);
}
return result;
}
/**
* 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -