Giter VIP home page Giter VIP logo

Comments (13)

eselmeister avatar eselmeister commented on September 15, 2024

Thanks for the feedback. We will inspect this issue.

from swtchart.

buchen avatar buchen commented on September 15, 2024

@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.

eselmeister avatar eselmeister commented on September 15, 2024

from swtchart.

buchen avatar buchen commented on September 15, 2024

I can check on the swtchart code base later this week.

from swtchart.

eselmeister avatar eselmeister commented on September 15, 2024

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.

fbuloup avatar fbuloup commented on September 15, 2024

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.

buchen avatar buchen commented on September 15, 2024

@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.
Bildschirmfoto 2019-12-27 um 20 50 54

@fbuloup - thanks for the code snippets. Works fine for me.

from swtchart.

buchen avatar buchen commented on September 15, 2024

@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?

JPEG
Performance-Diagramm_(Dashboard)

PNG
Performance-Diagramm_(Dashboard)

from swtchart.

fbuloup avatar fbuloup commented on September 15, 2024

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 :

PNG Result :
PNG File

JPEG result :
JPEG File

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.

laeubi avatar laeubi commented on September 15, 2024

@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.

laeubi avatar laeubi commented on September 15, 2024

@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.

fbuloup avatar fbuloup commented on September 15, 2024

@laeubi : i’ve created a bugzilla n°558678 yesterday. Will fork, commit little changes and open a PR

from swtchart.

fbuloup avatar fbuloup commented on September 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.