use of org.jfree.chart.labels.CategoryItemLabelGenerator in project SIMVA-SoS by SESoS.
the class GroupedStackedBarRenderer method drawItem.
/**
* Draws a stacked bar for a specific item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the plot area.
* @param plot the plot.
* @param domainAxis the domain (category) axis.
* @param rangeAxis the range (value) axis.
* @param dataset the data.
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @param pass the pass index.
*/
@Override
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();
Comparable group = this.seriesToGroupMap.getGroup(dataset.getRowKey(row));
PlotOrientation orientation = plot.getOrientation();
double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, row, column);
double positiveBase = 0.0;
double negativeBase = 0.0;
for (int i = 0; i < row; i++) {
if (group.equals(this.seriesToGroupMap.getGroup(dataset.getRowKey(i)))) {
Number v = dataset.getValue(i, column);
if (v != null) {
double d = v.doubleValue();
if (d > 0) {
positiveBase = positiveBase + d;
} else {
negativeBase = negativeBase + d;
}
}
}
}
double translatedBase;
double translatedValue;
boolean positive = (value > 0.0);
boolean inverted = rangeAxis.isInverted();
RectangleEdge barBase;
if (orientation == PlotOrientation.HORIZONTAL) {
if (positive && inverted || !positive && !inverted) {
barBase = RectangleEdge.RIGHT;
} else {
barBase = RectangleEdge.LEFT;
}
} else {
if (positive && !inverted || !positive && inverted) {
barBase = RectangleEdge.BOTTOM;
} else {
barBase = RectangleEdge.TOP;
}
}
RectangleEdge location = plot.getRangeAxisEdge();
if (value > 0.0) {
translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, location);
translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, location);
} else {
translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, location);
translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, location);
}
double barL0 = Math.min(translatedBase, translatedValue);
double barLength = Math.max(Math.abs(translatedValue - translatedBase), getMinimumBarLength());
Rectangle2D bar;
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
} else {
bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
}
getBarPainter().paintBar(g2, this, row, column, bar, barBase);
CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
if (generator != null && isItemLabelVisible(row, column)) {
drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
}
// collect entity and tool tip information...
if (state.getInfo() != null) {
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addItemEntity(entities, dataset, row, column, bar);
}
}
}
use of org.jfree.chart.labels.CategoryItemLabelGenerator in project SIMVA-SoS by SESoS.
the class LevelRenderer method drawItem.
/**
* 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.
*/
@Override
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 if the row index is not included in the list with
// the indices of the visible rows...
int visibleRow = state.getVisibleSeriesIndex(row);
if (visibleRow < 0) {
return;
}
// 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, visibleRow, column);
RectangleEdge edge = plot.getRangeAxisEdge();
double barL = rangeAxis.valueToJava2D(value, dataArea, edge);
// draw the bar...
Line2D line;
double x, y;
if (orientation == PlotOrientation.HORIZONTAL) {
x = barL;
y = barW0 + state.getBarWidth() / 2.0;
line = new Line2D.Double(barL, barW0, barL, barW0 + state.getBarWidth());
} else {
x = barW0 + state.getBarWidth() / 2.0;
y = barL;
line = new Line2D.Double(barW0, barL, barW0 + state.getBarWidth(), barL);
}
Stroke itemStroke = getItemStroke(row, column);
Paint itemPaint = getItemPaint(row, column);
g2.setStroke(itemStroke);
g2.setPaint(itemPaint);
g2.draw(line);
CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
if (generator != null && isItemLabelVisible(row, column)) {
drawItemLabel(g2, orientation, dataset, row, column, x, y, (value < 0.0));
}
// submit the current data point as a crosshair candidate
int datasetIndex = plot.indexOf(dataset);
updateCrosshairValues(state.getCrosshairState(), dataset.getRowKey(row), dataset.getColumnKey(column), value, datasetIndex, barW0, barL, orientation);
// collect entity and tool tip information...
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addItemEntity(entities, dataset, row, column, line.getBounds());
}
}
use of org.jfree.chart.labels.CategoryItemLabelGenerator in project opennms by OpenNMS.
the class ChartUtils method customizeSeries.
/**
* @param barChart TODO
* @param chartConfig
*/
private static void customizeSeries(JFreeChart barChart, BarChart chartConfig) {
/*
* Set the series colors and labels
*/
CategoryItemLabelGenerator itemLabelGenerator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0"));
SeriesDef[] seriesDefs = chartConfig.getSeriesDef();
CustomSeriesColors seriesColors = null;
if (chartConfig.getSeriesColorClass().isPresent()) {
try {
seriesColors = (CustomSeriesColors) Class.forName(chartConfig.getSeriesColorClass().get()).newInstance();
} catch (InstantiationException e) {
LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
} catch (IllegalAccessException e) {
LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
} catch (ClassNotFoundException e) {
LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
}
}
for (int i = 0; i < seriesDefs.length; i++) {
SeriesDef seriesDef = seriesDefs[i];
Paint paint = Color.BLACK;
if (seriesColors != null) {
Comparable<?> cat = (Comparable<?>) ((BarRenderer) barChart.getCategoryPlot().getRenderer()).getPlot().getCategories().get(i);
paint = seriesColors.getPaint(cat);
} else if (seriesDef.getRgb().isPresent()) {
final Rgb rgb = seriesDef.getRgb().get();
paint = new Color(rgb.getRed().getRgbColor(), rgb.getGreen().getRgbColor(), rgb.getBlue().getRgbColor());
}
((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesPaint(i, paint);
((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelsVisible(i, seriesDef.getUseLabels());
((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelGenerator(i, itemLabelGenerator);
}
}
use of org.jfree.chart.labels.CategoryItemLabelGenerator in project SIMVA-SoS by SESoS.
the class AbstractCategoryItemRenderer method drawItemLabel.
/**
* Draws an item label.
*
* @param g2 the graphics device.
* @param orientation the orientation.
* @param dataset the dataset.
* @param row the row.
* @param column the column.
* @param x the x coordinate (in Java2D space).
* @param y the y coordinate (in Java2D space).
* @param negative indicates a negative value (which affects the item
* label position).
*/
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation, CategoryDataset dataset, int row, int column, double x, double y, boolean negative) {
CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
if (generator != null) {
Font labelFont = getItemLabelFont(row, column);
Paint paint = getItemLabelPaint(row, column);
g2.setFont(labelFont);
g2.setPaint(paint);
String label = generator.generateLabel(dataset, row, column);
ItemLabelPosition position;
if (!negative) {
position = getPositiveItemLabelPosition(row, column);
} else {
position = getNegativeItemLabelPosition(row, column);
}
Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), x, y, orientation);
TextUtilities.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
}
}
use of org.jfree.chart.labels.CategoryItemLabelGenerator in project SIMVA-SoS by SESoS.
the class BarRenderer method drawItem.
/**
* 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.
*/
@Override
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 if the row index is not included in the list with
// the indices of the visible rows...
int visibleRow = state.getVisibleSeriesIndex(row);
if (visibleRow < 0) {
return;
}
// nothing is drawn for null values...
Number dataValue = dataset.getValue(row, column);
if (dataValue == null) {
return;
}
final double value = dataValue.doubleValue();
PlotOrientation orientation = plot.getOrientation();
double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, visibleRow, column);
double[] barL0L1 = calculateBarL0L1(value);
if (barL0L1 == null) {
// the bar is not visible
return;
}
RectangleEdge edge = plot.getRangeAxisEdge();
double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
// in the following code, barL0 is (in Java2D coordinates) the LEFT
// end of the bar for a horizontal bar chart, and the TOP end of the
// bar for a vertical bar chart. Whether this is the BASE of the bar
// or not depends also on (a) whether the data value is 'negative'
// relative to the base value and (b) whether or not the range axis is
// inverted. This only matters if/when we apply the minimumBarLength
// attribute, because we should extend the non-base end of the bar
boolean positive = (value >= this.base);
boolean inverted = rangeAxis.isInverted();
double barL0 = Math.min(transL0, transL1);
double barLength = Math.abs(transL1 - transL0);
double barLengthAdj = 0.0;
if (barLength > 0.0 && barLength < getMinimumBarLength()) {
barLengthAdj = getMinimumBarLength() - barLength;
}
double barL0Adj = 0.0;
RectangleEdge barBase;
if (orientation == PlotOrientation.HORIZONTAL) {
if (positive && inverted || !positive && !inverted) {
barL0Adj = barLengthAdj;
barBase = RectangleEdge.RIGHT;
} else {
barBase = RectangleEdge.LEFT;
}
} else {
if (positive && !inverted || !positive && inverted) {
barL0Adj = barLengthAdj;
barBase = RectangleEdge.BOTTOM;
} else {
barBase = RectangleEdge.TOP;
}
}
// draw the bar...
Rectangle2D bar;
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0, barLength + barLengthAdj, state.getBarWidth());
} else {
bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj, state.getBarWidth(), barLength + barLengthAdj);
}
if (getShadowsVisible()) {
this.barPainter.paintBarShadow(g2, this, row, column, bar, barBase, true);
}
this.barPainter.paintBar(g2, this, row, column, bar, barBase);
CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
if (generator != null && isItemLabelVisible(row, column)) {
drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
}
// submit the current data point as a crosshair candidate
int datasetIndex = plot.indexOf(dataset);
updateCrosshairValues(state.getCrosshairState(), dataset.getRowKey(row), dataset.getColumnKey(column), value, datasetIndex, barW0, barL0, orientation);
// add an item entity, if this information is being collected
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addItemEntity(entities, dataset, row, column, bar);
}
}
Aggregations