Skip to content

Commit

Permalink
Refactor of Groups, Table renderers to easily extend its functionality.
Browse files Browse the repository at this point in the history
New Groups Renderer added, as an example to create new renderers using 3rd party libraries like JFreeChart.
New Table Renderer added.
Fix some bugs in renderers
  • Loading branch information
edubecks committed Aug 20, 2012
1 parent 67a936e commit 54f395b
Show file tree
Hide file tree
Showing 31 changed files with 562 additions and 372 deletions.
4 changes: 2 additions & 2 deletions Legend API/nbproject/genfiles.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build.xml.data.CRC32=f28c262b
build.xml.data.CRC32=37b9d246
build.xml.script.CRC32=8104c559
build.xml.stylesheet.CRC32=a56c6a5b@2.50.1
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=f28c262b
nbproject/build-impl.xml.data.CRC32=37b9d246
nbproject/build-impl.xml.script.CRC32=2a84ad6f
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.50.1
8 changes: 8 additions & 0 deletions Legend API/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
<specification-version>0.8.0.2</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.jfree</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.0</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.explorer</code-name-base>
<build-prerequisite/>
Expand Down
28 changes: 14 additions & 14 deletions Legend API/src/org/gephi/legend/api/CustomTableItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ public interface CustomTableItemBuilder {
/**
* This function receives the objects that need to be filled in order to
* build the the table.
* <code>verticalLabels</code> and
* <code>verticalColors</code> should have the same size.
* <code>horizontalLabels</code> and
* <code>horizontalColors</code> should have the same size too. Finally,
* <code>rowLabels</code> and
* <code>rowLabelColors</code> should have the same size.
* <code>columnLabels</code> and
* <code>columnLabelColors</code> should have the same size too. Finally,
* <code>values</code> represents the table and its number of rows should be
* equal to the number of elements in
* <code>horizontalLabels</code> as well as the number of columns should be
* <code>rowLabels</code> as well as the number of columns should be
* equal to the number of elements in
* <code>verticalValues</code>
* <code>columnLabels</code>
*
* @param labels an arraylist of one single value containing the selection
* of labels: <code>TableItem.LabelSelection.VERTICAL</code> or
* <code>TableItem.LabelSelection.HORIZONTAL</code>
* <code>TableItem.LabelSelection.BOTH</code>
* @param horizontalLabels list of labels that will be displayed at the
* @param rowLabels list of labels that will be displayed at the
* sides
* @param verticalLabels list of labels that will be displayed at top or
* @param columnLabels list of labels that will be displayed at top or
* bottom
* @param values table values representing the matrix
* @param horizontalColors colors used to represent the horizontal colors
* @param verticalColors colors used to represent the vertical colors
* @param rowLabelColors colors used to represent the row labels
* @param columnLabelColors colors used to represent the column labels
* @param valueColors table colors representing the matrix
*/
public void retrieveData(ArrayList<TableItem.LabelSelection> labels,
ArrayList<String> horizontalLabels,
ArrayList<String> verticalLabels,
ArrayList<String> rowLabels,
ArrayList<String> columnLabels,
ArrayList<ArrayList<String>> values,
ArrayList<Color> horizontalColors,
ArrayList<Color> verticalColors,
ArrayList<Color> rowLabelColors,
ArrayList<Color> columnLabelColors,
ArrayList<ArrayList<Color>> valueColors);

}
12 changes: 6 additions & 6 deletions Legend API/src/org/gephi/legend/api/renderers/Bundle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
LegendItemRenderer.name = Legend Item
TextItemRenderer.name = Text Item
TableItemRenderer.name = Table Item
GroupsItemRenderer.name = Groups Item
ImageItemRenderer.name = Image Item
DescriptionItemRenderer.name = Description Item
LegendItemRenderer.name = Legend Legend Renderer
TextItemRenderer.name = Legend Text Renderer
TableItemRenderer.name = Legend Table Renderer
GroupsItemRenderer.name = Legend Groups Renderer
ImageItemRenderer.name = Legend Image Renderer
DescriptionItemRenderer.name = Legend Description Renderer


Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
public class GroupsItemRenderer extends LegendItemRenderer {

@Override
protected void renderToGraphics(Graphics2D graphics2D, AffineTransform origin, Integer width, Integer height) {

protected void renderToGraphics(Graphics2D graphics2D,
AffineTransform origin,
Integer width,
Integer height) {

if (labels.isEmpty()) {
return;
}
Expand All @@ -46,17 +49,18 @@ protected void renderToGraphics(Graphics2D graphics2D, AffineTransform origin, I
FontMetrics fontMetrics = graphics2D.getFontMetrics();


int elementHeight = (height - (numRows - 1) * paddingBetweenElements) / numRows;
int elementWidth = (width - (numColumns - 1) * paddingBetweenElements) / numColumns;
// int elementWidth = width / numColumns - paddingBetweenElements;
// int elementHeight = (height - (numRows - 1) * paddingBetweenElements) / numRows;
// int elementWidth = (width - (numColumns - 1) * paddingBetweenElements) / numColumns;
int elementWidth = width / numColumns - paddingBetweenElements;
int elementHeight = height / numRows - paddingBetweenElements;

int maxLabelWidth = Integer.MIN_VALUE;
int maxLabelHeight = Integer.MIN_VALUE;

// computing max label height
for (StringBuilder label : labels) {
maxLabelHeight = (int) Math.max(maxLabelHeight, computeVerticalTextSpaceUsed(graphics2D, label.toString(), labelFont, elementWidth));

}

// computing max label width
Expand Down Expand Up @@ -96,8 +100,8 @@ else if (labelPosition == Direction.UP || labelPosition == Direction.DOWN) {
// }

for (int i = 0; i < labels.size(); i++) {
int x = (i % numColumns) * elementWidth + paddingBetweenElements ;
int y = (i / numColumns) * elementHeight + paddingBetweenElements;
int x = (i % numColumns) * (elementWidth + paddingBetweenElements);
int y = (i / numColumns) * (elementHeight + paddingBetweenElements);
int xShape = 0, yShape = 0, xLabel = 0, yLabel = 0;
switch (labelPosition) {
case RIGHT: {
Expand Down Expand Up @@ -131,10 +135,59 @@ else if (labelPosition == Direction.UP || labelPosition == Direction.DOWN) {
}

drawShape(graphics2D, shape, colors.get(i), xShape, yShape, shapeWidth, shapeHeight, values.get(i));
legendDrawText(graphics2D, labels.get(i).toString(), labelFont, colors.get(i), xLabel, yLabel, labelWidth, labelHeight, LegendItem.Alignment.CENTER);
drawElementLabel(graphics2D, labels.get(i).toString(), labelFont, colors.get(i), xLabel, yLabel, labelWidth, labelHeight);
}
}

/**
* Override this function to draw each element label in a different way.
*
* @param graphics2D the Graphics object to draw to
* @param label text to be displayed as label
* @param labelFont font used to draw the label
* @param labelColor rendering color for the label
* @param x the x coordinate of the area containing the label
* @param y the y coordinate of the area containing the label
* @param width the width of the area containing the label
* @param height the height of the area containing the label
*
*/
protected void drawElementLabel(Graphics2D graphics2D,
String label,
Font labelFont,
Color labelColor,
int x,
int y,
Integer width,
Integer height) {
legendDrawText(graphics2D, label, labelFont, labelColor, x, y, width, height, LegendItem.Alignment.CENTER);
}

/**
* Override this function to draw each element shape or representative
* element in a different way.
*
* @param graphics2D the Graphics object to draw to
* @param shape shape specified by the user, it could be one the three
* values:
* @param value value to express as a shape
* @param valueColor rendering color for the value
* @param x the x coordinate of the area containing the label
* @param y the y coordinate of the area containing the label
* @param width the width of the area containing the label
* @param height the height of the area containing the label
*/
protected void drawElementShape(Graphics2D graphics2D,
Shape shape,
Float value,
Color valueColor,
int x,
int y,
Integer width,
Integer height) {
drawShape(graphics2D, shape, valueColor, x, y, width, height, value);
}

private void drawShape(Graphics2D graphics2D, Shape shape, Color color, int x, int y, Integer width, Integer height, float scale) {

int shapeWidth = (int) (width * scale);
Expand Down Expand Up @@ -173,8 +226,8 @@ protected void readOwnPropertiesAndValues(Item item, PreviewProperties propertie
String newLabel = properties.getStringValue(GroupsProperty.getLabelProperty(itemIndex, i));
label.replace(0, newLabel.length(), newLabel);
}


colors = item.getData(GroupsItem.COLORS);
values = item.getData(GroupsItem.VALUES);

Expand All @@ -185,8 +238,8 @@ protected void readOwnPropertiesAndValues(Item item, PreviewProperties propertie
shape = (LegendItem.Shape) properties.getValue(LegendManager.getProperty(GroupsProperty.OWN_PROPERTIES, itemIndex, GroupsProperty.GROUPS_SHAPE));
labelFont = properties.getFontValue(LegendManager.getProperty(GroupsProperty.OWN_PROPERTIES, itemIndex, GroupsProperty.GROUPS_LABEL_FONT));
isScalingShapes = properties.getBooleanValue(LegendManager.getProperty(GroupsProperty.OWN_PROPERTIES, itemIndex, GroupsProperty.GROUPS_SCALE_SHAPE));
//

//
ArrayList<Float> valuesNormalized = new ArrayList<Float>();
if (isScalingShapes) {
float maxValue = Collections.max(values);
Expand All @@ -199,9 +252,9 @@ protected void readOwnPropertiesAndValues(Item item, PreviewProperties propertie
valuesNormalized.add(1f);
}
}

values = valuesNormalized;

paddingBetweenTextAndShape = properties.getIntValue(LegendManager.getProperty(GroupsProperty.OWN_PROPERTIES, itemIndex, GroupsProperty.GROUPS_PADDING_BETWEEN_TEXT_AND_SHAPE));
paddingBetweenElements = properties.getIntValue(LegendManager.getProperty(GroupsProperty.OWN_PROPERTIES, itemIndex, GroupsProperty.GROUPS_PADDING_BETWEEN_ELEMENTS));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.batik.svggen.*;
import org.apache.commons.codec.binary.*;
Expand Down Expand Up @@ -40,7 +41,6 @@ protected void renderToGraphics(Graphics2D graphics2D, AffineTransform origin, I
if (imageFile.exists()) {
BufferedImage before = ImageIO.read(imageFile);


graphics2D.setTransform(origin);
if (before.getWidth() == width && before.getHeight() == height) {
graphics2D.drawImage(before, 0, 0, null);
Expand All @@ -55,44 +55,23 @@ protected void renderToGraphics(Graphics2D graphics2D, AffineTransform origin, I
AffineTransformOp scaleOperation = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
after = scaleOperation.filter(before, after);


// EXPORTING TO SVG
// // EXPORTING TO SVG
if (graphics2D instanceof SVGGraphics2D) {
SVGGraphics2D svgGraphics2D = (SVGGraphics2D) graphics2D;
SVGGeneratorContext svgGeneratorContext = svgGraphics2D.getGeneratorContext();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(after, "png", baos);
baos.flush();
String encodedImage = Base64.encodeBase64String(baos.toByteArray());
baos.close(); // should be inside a finally block
Document svgDocument = svgGeneratorContext.getDOMFactory();

Element imageBase64 = svgDocument.createElementNS("http://www.w3.org/2000/svg", "image");
imageBase64.setAttribute("width", "" + after.getWidth());
imageBase64.setAttribute("height", "" + after.getHeight());
// imageBase64.setAttribute("transform", "scale("+scaleWidth+","+scaleHeight+") translate("+origin.getTranslateX()+","+origin.getTranslateY()+")");
imageBase64.setAttribute("transform", "scale(1,1) translate(" + origin.getTranslateX() + "," + origin.getTranslateY() + ")");
imageBase64.setAttribute("xlink:href", DATA_PROTOCOL_PNG_PREFIX + encodedImage);

svgDocument.getLastChild().appendChild(imageBase64);



int x = (int) origin.getTranslateX();
int y = (int) origin.getTranslateY();

renderImageToSVGGraphics(graphics2D, after, x, y);

}
else {
graphics2D.drawImage(after, 0, 0, null);
}




}
}

} catch (Exception e) {
System.out.println("@Var: e: "+e);
System.out.println("@Var: e: " + e);


}
Expand Down Expand Up @@ -121,6 +100,43 @@ public boolean needsItemBuilder(ItemBuilder itemBuilder, PreviewProperties prope
return itemBuilder instanceof ImageItemBuilder;
}

/**
* Renders an image into an SVGGraphics2D object
*
* @param graphics2D Graphics2D instance used to render legend
* @param image image that will be renderer
* @param x the x coordinate of the the position where the image will be
* rendered
* @param y the y coordinate of the the position where the image will be
* rendered
*/
public static void renderImageToSVGGraphics(Graphics2D graphics2D, BufferedImage image, int x, int y) {
try {
SVGGraphics2D svgGraphics2D = (SVGGraphics2D) graphics2D;
SVGGeneratorContext svgGeneratorContext = svgGraphics2D.getGeneratorContext();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
String encodedImage = Base64.encodeBase64String(baos.toByteArray());
baos.close(); // should be inside a finally block
Document svgDocument = svgGeneratorContext.getDOMFactory();

Element imageBase64 = svgDocument.createElementNS("http://www.w3.org/2000/svg", "image");
imageBase64.setAttribute("width", "" + image.getWidth());
imageBase64.setAttribute("height", "" + image.getHeight());
// imageBase64.setAttribute("transform", "scale("+scaleWidth+","+scaleHeight+") translate("+origin.getTranslateX()+","+origin.getTranslateY()+")");
imageBase64.setAttribute("transform", "scale(1,1) translate(" + x + "," + y + ")");
imageBase64.setAttribute("xlink:href", DATA_PROTOCOL_PNG_PREFIX + encodedImage);

svgDocument.getLastChild().appendChild(imageBase64);
// svgDocument.getFirstChild().appendChild(imageBase64);
} catch (Exception e) {
System.out.println("@Var: e: " + e);
}

}

// OWN PROPERTIES
private File imageFile;
// encoding
Expand Down
Loading

0 comments on commit 54f395b

Please sign in to comment.