We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Gradient fill is not working for Diagram Shape

I have a feature to color a shape and connectors in multiple colors (like on the screenshot).
Multiple coloring for shapes and connectors.png (10.6 KB)
I have tried to achieve this with Aspose gradient fill of Shape, but unfortunately getting NullPointerExceptions while saving a diagram. With use of stylesheets for this there are no effect.
Attaching code snippet for shape coloring of shape below:

String[] colors = getColorPalette().split(";"); // RGB strings
if (colors.length > 1) {
  // Approach with StyleSheet, using gradient of shape fill directly (nodeShape.getFill()) produces nullPointerExceptions
  StyleSheet ss = new StyleSheet();
  GradientFill gf = ss.getFill().getGradientFill();
  // Adding stops
  DoubleValue gradientPosition = new DoubleValue();
  gradientPosition.setValue(0.0);
  for (String rgb : colors) {
    gf.getGradientStops().add(gradientPosition, IteraplanVisioUtils.getColorValue(rgb)); // ColorValue is defined from RGB string
    gradientPosition.setValue(gradientPosition.getValue() + (1.0 / colors.length));
  }
  // Adding other gradient properties
  DoubleValue gradientAngle = new DoubleValue();
  gradientAngle.setValue(0.5);
  gf.setGradientAngle(gradientAngle);
  gf.getGradientDir().setValue(GradientFillDir.LINEAR);
  gf.setGradientEnabled(new BoolValue(BOOL.TRUE, MeasureConst.BOOL));

  nodeShape.setFillStyle(ss); // setting style with gradient coloring
} else {
  // Single value coloring, works fine
  nodeShape.getFill().setFillForegnd(IteraplanVisioUtils.getColorValue(colors[0]));
}

@iteraplan,

Please send all details of the scenario, including source drawing, code and stencil file (if any). We will investigate your scenario in our environment, and share our findings with you.

@imran.rafique, here is Stencil template for the Diagram: StencilTmp.zip (12.1 KB)

Gradient coloring is made in the createVisio() method with other shape decorations (which work fine):

public Object createVisio(IFDVisioContext ctx) throws Exception {
long shapeId;
AffineTransform tr = ctx.getParentTransform();
AffineTransform nt = new AffineTransform();
nt.concatenate(tr);
double xCentr = (getPosition().getX() + getRectangle().getCenterX());
double yCentr = (getPosition().getY() + getRectangle().getCenterY());
nt.translate(xCentr, -yCentr);
double translateX = nt.getTranslateX();
double translateY = nt.getTranslateY();
shapeId = ctx.getDiagram().addShape(translateX, translateY, ctx.getMasterNodeName(), ctx.getCurrentPage().getID());
Shape nodeShape = ctx.getDiagram().getPages().getPage(ctx.getCurrentPage().getID()).getShapes().getShape(shapeId);
nodeShape.setWidth(getRectangle().getWidth() / IteraplanVisioUtils.VISIO_SCALE);
nodeShape.setHeight(getRectangle().getHeight() / IteraplanVisioUtils.VISIO_SCALE);
// Node text
if (!getName().isEmpty() && getName() != null) {
nodeShape.getText().getValue().clear();
nodeShape.getText().getValue().add(new Txt(getName()));
// Node text color
StyleSheet textStyle = new StyleSheet();
textStyle.getFill().setFillForegnd(IteraplanVisioUtils.getColorValue(getTextColor()));
nodeShape.setTextStyle(textStyle);
}
// Node color
String[] colors = getColorPalette().split(";"); // array of rgb stringss (e.g [“rgb(255,255,255)”, “rgb(200,200,200)”])
if (colors.length > 1) {
// This code produces nullPointerExceptions after diagram.save
GradientFill gf = nodeShape.getFill().getGradientFill();
DoubleValue gradientPosition = new DoubleValue();
gradientPosition.setValue(0.0);
gf.getGradientStops().clear();
for (String rgb : colors) {
gf.getGradientStops().add(gradientPosition, IteraplanVisioUtils.getColorValue(rgb)); // IteraplanVisioUtils.getColorValue(rgb) returns Aspose ColorValue object from rgb.
gradientPosition.setValue(gradientPosition.getValue() + (1.0 / colors.length));
}
DoubleValue gradientAngle = new DoubleValue();
gradientAngle.setValue(0.5);
gf.getGradientDir().setValue(GradientFillDir.LINEAR);
gf.setGradientAngle(gradientAngle);
gf.setGradientEnabled(new BoolValue(BOOL.TRUE, MeasureConst.BOOL));
} else {
// Single value coloring
nodeShape.getFill().setFillForegnd(IteraplanVisioUtils.getColorValue(colors[0]));
}

return nodeShape;

}

Please, send your questions to clarify, if something is still necessary for your investigation.

@iteraplan,

The source code is not in fully compilable form. Please cut off additional classes, unknown objects, and only include Aspose.Diagram API related lines of code, and then share simplified code with us. Your response is awaited.

@imran.rafique, moved out the code for gradient coloring in separate function (with only Aspose code). Hope this simplified one will fully represent the problem:

private void setShapeColoring(Shape nodeShape) {
    Random random = new Random(); // used for random hex color
    // Gradient fill of shape to edit
    GradientFill gf = nodeShape.getFill().getGradientFill();
    gf.getGradientStops().clear();
    // Setting 3 gradient stops
    DoubleValue gradientPosition = new DoubleValue();
    gradientPosition.setValue(0.0);
    for (Integer i = 0; i < 3; i++) {
      //Random hex color for Aspose ColorValue
      Integer nextInt = random.nextInt(256 * 256 * 256);
      String colorCode = String.format("#%06x", nextInt);
      ColorValue cv = new ColorValue(colorCode, MeasureConst.COLOR);
      // Current gradient stop
      gf.getGradientStops().add(gradientPosition, cv);
      // Setting gradient position for next stop
      gradientPosition.setValue(gradientPosition.getValue() + (1.0 / 3.0));
    }
    // Linear gradient
    gf.getGradientDir().setValue(GradientFillDir.LINEAR);
    // Setting gradient angle
    DoubleValue gradientAngle = new DoubleValue();
    gradientAngle.setValue(0.5);
    gf.setGradientAngle(gradientAngle);
    // Enabling gradient
    gf.setGradientEnabled(new BoolValue(BOOL.TRUE, MeasureConst.BOOL));
  }

@iteraplan,

We managed to replicate the null pointer error in our environment. It has been logged under the ticket ID DIAGRAMJAVA-50605 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

@imran.rafique, thank you for notifying

@imran.rafique, I see that DIAGRAMJAVA-50605 is resolved. When should we expect this fix in new release of Aspose.Diagram?

@iteraplan,

The linked ticket ID DIAGRAMJAVA-50605 has been resolved, and the fix will be included in next version 18.7 of Aspose.Diagram for Java API, which is expected to be released in the mid of July, 2018.

@imran.rafique, thanks for the info! How will be notified when the new release is available?

@iteraplan,

We will notify you in this thread once the next version 18.7 of Aspose.Diagram for Java API is published.

Hello, I noticed that 18.7 is already released. Where can I find the changenotes?

@iteraplan

Thank you for getting back to us.

Please visit Release Notes for Aspose.Diagram for Java 18.7 for your kind reference.

Thanks for providing the release notes.