Comments (13)
Thanks for the feedback. We will inspect this issue.
from swtchart.
@eselmeister Does the Transform work for you on macOS? I tried to re-apply this to my code base but for me the Transform has no effect (the code by @fbuloup works)
And: Does it make sense to change the Chart#save
method?
from swtchart.
from swtchart.
I can check on the swtchart code base later this week.
from swtchart.
That would be great. I prefer to use the Transform method as it's easier to use and transform should do the trick. As you said, the code of @fbuloup works, but we should try to rely on system functions whenever possible. Moreover, I would have to check that the code of @fbuloup has not been copied and pasted from somewhere else.
from swtchart.
As I mentioned in my original post, I took the above code example from this link :
http://www.java2s.com/Tutorial/Java/0300__SWT-2D-Graphics/WriteanImagetoaPNGfile.htm
But I think there is a much more simple solution which probably works on all platforms.
Just create the graphical context on current chart instance and copy the entire area into the new created image. Then you can get the image data from it. Finally it appears that with this solution, you don't need any geometrical transformation nor image data manipulation. I've tested this on MacOS and it works fine for jpeg and bmp image formats.
Here is the small code :
...
Image image = new Image(Display.getDefault(), theChart.getBounds());
GC gc = new GC(theChart);
gc.copyArea(image, 0, 0);
gc.dispose();
ImageData imageData = image.getImageData();
image.dispose();
...
from swtchart.
@eselmeister - I had the chance to test the code now. I ran the "org.eclipse.swtchart.examples" projects with "Run as Eclipse application" and then selected one of the top right charts, right-click and saved as PNG or JPEG. The image is unfortunately still rotated. It is not clear to me what goes wrong. The Transform is setup, but it seems to me that the SWT code overrides it with some other Transform.
BTW, the target platform definition seems to be incomplete as one class was complaining about missing org.apache.batik dependencies - see below. I was using the target platform form the "o.e.s.targetplatform" project.
@fbuloup - thanks for the code snippets. Works fine for me.
from swtchart.
@fbuloup upon further inspection: it works for JPEG, but I see some artifacts when saving the chart as PNG file. I see straight diagonal lines across the whole image - compare the JPEG and PNG versions. 🤔 Any ideas?
from swtchart.
Sorry but I've not been able to reproduce this strange diagonal line for PNG files ! It's probably a problem related to SWT version.
I've investigated in SWT OS X java code and seen that there is a strange applied transformation in the print method of the control class :
...
public boolean print (GC gc) {
checkWidget ();
if (gc == null) error (SWT.ERROR_NULL_ARGUMENT);
if (gc.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
NSGraphicsContext.static_saveGraphicsState();
NSGraphicsContext.setCurrentContext(gc.handle);
NSAffineTransform transform = NSAffineTransform.transform (); // *****
transform.translateXBy (0, view.bounds().height); // *****
transform.scaleXBy (1, -1); // *****
transform.concat (); // *****
view.displayRectIgnoringOpacity(view.bounds(), gc.handle);
NSGraphicsContext.static_restoreGraphicsState();
return true;
}
...
When you comment lines that I've marked with stars, the image is no longer flipped vertically ! Do you have any idea on the utility of these transformations ? 🤔
Nevertheless and after a couple of hours, I've been able to modify the ImageSupplier class in order to export chart properly to BMP, PNG or JPEG image. Here is the resulting java code :
...
public class ImageSupplier {
public void saveImage(ImageData imageData, String fileName, int format) {
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[]{imageData};
imageLoader.save(fileName, format);
}
public ImageData getImageData(BaseChart baseChart) {
// Force to redraw chart immediately to be sure that any
// Shell dialog won't be a part of the copied image
baseChart.redraw();
baseChart.update();
// Create the image provider
ImageDataProvider imageDataProvider = new ImageDataProvider() {
@Override
public ImageData getImageData(int zoom) {
if(zoom != 100) return null;
int width = baseChart.getSize().x;
int height = baseChart.getSize().y;
PaletteData palette = new PaletteData(0xFF , 0xFF00 , 0xFF0000);
ImageData imageData = new ImageData(width, height, 24, palette);
return imageData;
}
};
// Copy chart into the image
Image image = new Image(baseChart.getDisplay(), imageDataProvider);
GC gc = new GC(baseChart);
gc.copyArea(image, 0, 0);
// Retrieve image data
ImageData imageData = image.getImageData(100);
gc.dispose();
image.dispose();
return imageData;
}
/// No longer necessary
// private boolean isMacOS() {
// return (getOperatingSystem().indexOf("mac") >= 0);
// }
//
// private String getOperatingSystem() {
// return System.getProperty("os.name").toLowerCase();
// }
}
I've made some tests with MacOS 10.14.6 and windows 7. Windows was running with VirtualBox. For now, I'm using SWT 4763 on Mac OSX and 4924 on Windows. Perhaps this strange diagonal line is related to SWT releases differences... Here are two exported images :
It's also working for BMP format, but not supported by GitHub.
BTW, in order to run org.eclipse.swtchart.extensions.examples, it has been necessary to update target platform with all these bundles :
org.apache.batik.constants_1.9.1.v20180227-1645.jar
org.apache.batik.css_1.9.1.v20181015-1528.jar
org.apache.batik.dom_1.9.1.v20190730-1743.jar
org.apache.batik.ext.awt_1.9.1.v20190730-1743.jar
org.apache.batik.i18n_1.9.1.v20180227-1645.jar
org.apache.batik.svggen_1.9.1.v20190730-1743.jar
org.apache.batik.util_1.9.1.v20180703-1553.jar
org.apache.batik.xml_1.9.1.v20190730-1743.jar
org.apache.commons.io_2.6.0.v20190123-2029.jar
org.apache.xalan_2.7.1.v201005080400.jar
org.apache.xml.serializer_2.7.1.v201005080400.jar
org.apache.xmlgraphics_2.3.0.v20190515-0436.jar
But there is still a problem on OS X because the exported image size is twice the chart size !
It appears that the latest SWT release is 4930 and I will try to update to this version hoping everything will be solved ! I will give a feedback ASAP.
from swtchart.
@fbuloup do you think you can supply your changes on a fork of this repository and open a PR? This would make things a lot easier (commenting, seeing diffs, possibly merging), let me know if you need any guidance.
BTW if there is a bug in SWT/Cocoa it would be best to report it here https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform
from swtchart.
@buchen I created #91 for that, the problem is that the examples are not part of the build and the target platform only contains necessary bundles for building the bundles, so eclipse might get confused.
from swtchart.
@laeubi : i’ve created a bugzilla n°558678 yesterday. Will fork, commit little changes and open a PR
from swtchart.
It seems that a check has failed for the PR :
eclipsefdn/eca — The author(s) of the pull request is not covered by necessary...
from swtchart.
Related Issues (20)
- Delete series - speed up deleting all series
- Area Selection - set specific cursor
- Update to Java 17
- Refactor SVG LineSeries Export
- Clipboard Copy Action - support specific handlers e.g. 'image/svg+xml'
- SVG Export - refactor the template method
- Clipboard needs to be disposed
- Series Mapper - catch problems when import settings
- Plot Area - enable to draw centered image and text
- Support Custom Series (Experimental)
- Custom Series - add support for rectangle and line
- Switch default font name to Verdana HOT 1
- Resource leak in the Chart when the parent is disposed before the chart itself. HOT 11
- axis tick labels are not properly shown in some cases HOT 7
- Custom method to get/set status of "Buffered Selection"
- org.eclipse.swtchart.extensions bundle activation prints unnecessary exception to syserr HOT 1
- org.eclipse.swtchart.Constants class has been removed. HOT 1
- Replace deprecated createImage by createImageWithDefault method
- Line Series - Area Strict Option HOT 3
- Refactor comments/code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swtchart.