Revision 2529

tmp/org.txm.chartsengine.r.core/src/org/txm/chartsengine/r/core/RChartsEngine.java (revision 2529)
1 1
package org.txm.chartsengine.r.core;
2

  
2 3
import java.io.File;
3 4
import java.util.ArrayList;
4 5

  
......
22 23
 *
23 24
 */
24 25
public class RChartsEngine extends ChartsEngine {
25

  
26

  
27

  
26
	
27
	
28
	
28 29
	/**
29 30
	 * The charts engine internal name.
30 31
	 */
31 32
	public final static String NAME = "r_charts_engine"; //$NON-NLS-1$
32

  
33
	
33 34
	/**
34 35
	 * The charts engine description.
35 36
	 */
36 37
	public final static String DESCRIPTION = "R";
37

  
38

  
38
	
39
	
39 40
	/**
40 41
	 * Constants for extra output formats.
41 42
	 */
42 43
	// FIXME : for tests or further output formats implementations
43
	//public final static String OUTPUT_FORMAT_RGRAPHICS = "rgraphics", OUTPUT_FORMAT_IPLOT = "iplots", OUTPUT_FORMAT_JAVAGD = "javagd"; //$NON-NLS-1$ //$NON-NLS-2$
44

  
45

  
44
	// public final static String OUTPUT_FORMAT_RGRAPHICS = "rgraphics", OUTPUT_FORMAT_IPLOT = "iplots", OUTPUT_FORMAT_JAVAGD = "javagd"; //$NON-NLS-1$ //$NON-NLS-2$
45
	
46
	
46 47
	/**
47 48
	 * The R device to use for generating charts.
48 49
	 */
49 50
	protected RDevice rDevice;
50

  
51

  
51
	
52
	
52 53
	/**
53 54
	 * Creates a R charts engine implementation with the specified output format as current.
55
	 * 
54 56
	 * @param outputFormat
55 57
	 */
56 58
	public RChartsEngine(String outputFormat) {
......
58 60
		this.directComputing = false;
59 61
		this.setOutputFormat(outputFormat);
60 62
	}
61

  
63
	
62 64
	/**
63 65
	 * Creates a R charts engine implementation.
64 66
	 */
65 67
	public RChartsEngine() {
66 68
		this(RChartsEnginePreferences.getInstance().getString(RChartsEnginePreferences.OUTPUT_FORMAT));
67 69
	}
68

  
69 70
	
71
	
70 72
	@Override
71
	public File createChartFile(Object chart, File file)	{
73
	public File createChartFile(Object chart, File file) {
72 74
		return this.plot(file, (String) chart);
73 75
	}
74 76
	
75

  
77
	
76 78
	/**
77 79
	 * Plots the specified expression using R workspace instance and current charts engine output format.
78 80
	 * 
79 81
	 * @param file
80 82
	 * @param cmd
81 83
	 */
82
	public File plot(File file, String cmd)	{
84
	public File plot(File file, String cmd) {
83 85
		return this.plot(file, cmd, null, null, null);
84 86
	}
85 87
	
......
89 91
	 * @param file
90 92
	 * @param cmd
91 93
	 * @param result
94
	 * @param title
95
	 * @param subtitle
96
	 * @return
92 97
	 */
93
	public File plot(File file, String cmd, ChartResult result, String title, String subtitle)	{
98
	public File plot(File file, String cmd, ChartResult result, String title, String subtitle) {
94 99
		try {
95

  
100
			
96 101
			if (!cmd.endsWith(";") && !cmd.endsWith("\n")) { //$NON-NLS-1$
97 102
				cmd += ";"; //$NON-NLS-1$
98 103
			}
99 104
			
100 105
			// FIXME: margin tests
101
			//cmd += "windows.options(width=100, height=10);\n"; //$NON-NLS-1$
102
			//cmd += "par(mar = c(50,6,4,1) + .1);\n"; //$NON-NLS-1$
106
			// cmd += "windows.options(width=100, height=10);\n"; //$NON-NLS-1$
107
			// cmd += "par(mar = c(50,6,4,1) + .1);\n"; //$NON-NLS-1$
103 108
			
104 109
			// FIXME: outer margin to not cut of title
105
			//cmd += "par(oma = c(10,2,2,2));\n"; //$NON-NLS-1$
106

  
110
			// cmd += "par(oma = c(10,2,2,2));\n"; //$NON-NLS-1$
111
			
107 112
			// font size
108
			//cmd += "par(cex.main = 3.5);\n"; //$NON-NLS-1$
109
			//cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5
113
			// cmd += "par(cex.main = 3.5);\n"; //$NON-NLS-1$
114
			// cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5
110 115
			
111
			if(result != null)	{
116
			if (result != null) {
112 117
				// draw grid
113
				if(result.isGridVisible())	{
118
				if (result.isGridVisible()) {
114 119
					cmd += this.getGridPlotCmd();
115 120
				}
116 121
				// draw title and subtitle
117
				if(result.isTitleVisible())	{
118
					if(title != null)	{
122
				if (result.isTitleVisible()) {
123
					if (title != null) {
119 124
						cmd += this.getTitlePlotCmd(title);
120 125
					}
121
					if(subtitle != null)	{
126
					if (subtitle != null) {
122 127
						cmd += this.getSubtitlePlotCmd(subtitle);
123 128
					}
124 129
				}
125 130
			}
126 131
			file = RWorkspaceRenderer.getInstance().plot(file, cmd, this.rDevice);
127 132
		}
128
		catch(Exception e) {
133
		catch (Exception e) {
129 134
			e.printStackTrace();
130 135
			return null;
131 136
		}
......
133 138
		return file;
134 139
	}
135 140
	
141
	/**
142
	 * Plots the specified expression using R workspace instance and current charts engine output format.
143
	 * 
144
	 * @param file
145
	 * @param cmd
146
	 * @param result
147
	 * @param title
148
	 * @return
149
	 */
150
	public File plot(File file, String cmd, ChartResult result, String title) {
151
		return this.plot(file, cmd, result, title, null);
152
	}
136 153
	
154
	
137 155
	/**
138 156
	 * Returns a string that represents the R command to draw a title.
157
	 * 
139 158
	 * @param title
140 159
	 * @return
141 160
	 */
142
	public String getTitlePlotCmd(String title)	{
143

  
161
	public String getTitlePlotCmd(String title) {
162
		
144 163
		// FIXME: Tests
145
//		title(main = "Titre principal", sub = "Sous-titre",
146
//			      xlab = "Axe des x", ylab = "Axe des y",
147
//			      cex.main = 2,   font.main= 4, col.main= "red",
148
//			      cex.sub = 0.75, font.sub = 3, col.sub = "green",
149
//			      col.lab ="darkblue"
150
//			      )
164
		// title(main = "Titre principal", sub = "Sous-titre",
165
		// xlab = "Axe des x", ylab = "Axe des y",
166
		// cex.main = 2, font.main= 4, col.main= "red",
167
		// cex.sub = 0.75, font.sub = 3, col.sub = "green",
168
		// col.lab ="darkblue"
169
		// )
151 170
		
152
		//return String.format("title(main = \"%s\");\n", title); //$NON-NLS-1$
171
		// return String.format("title(main = \"%s\");\n", title); //$NON-NLS-1$
153 172
		
154
		// FIXME: tests for adaptive not truncated title 
173
		// FIXME: tests for adaptive not truncated title
155 174
		return String.format("title(main = paste(strwrap(\"%s\", width = 50), collapse = \"\n\"));\n", title); //$NON-NLS-1$
156 175
		
157 176
		
158 177
	}
159

  
178
	
160 179
	/**
161 180
	 * Returns a string that represents the R command to draw a subtitle.
181
	 * 
162 182
	 * @param subtitle
163 183
	 * @return
164 184
	 */
165
	public String getSubtitlePlotCmd(String subtitle)	{
185
	public String getSubtitlePlotCmd(String subtitle) {
166 186
		// NOTE: SJ: sub title always drawn the string at bottom of the plot
167
		//return String.format("title(sub = \"%s\");\n", subtitle); //$NON-NLS-1$
187
		// return String.format("title(sub = \"%s\");\n", subtitle); //$NON-NLS-1$
168 188
		return String.format("title(main = \"\n\n\n%s\");\n", subtitle); //$NON-NLS-1$
169 189
	}
170

  
171 190
	
191
	
172 192
	/**
173 193
	 * Returns a string that represents the R command to draw a grid.
194
	 * 
174 195
	 * @return
175 196
	 */
176
	public String getGridPlotCmd()	{
197
	public String getGridPlotCmd() {
177 198
		return "grid (NULL,NULL, lty = \"dotted\", col = \"lightgray\");\n"; //$NON-NLS-1$
178 199
	}
179 200
	
180 201
	/**
181 202
	 * Replaces or creates the vector named "colors" in the R workspace from the current charts engine theme palette according to specified rendering mode and items count.
203
	 * 
182 204
	 * @param renderingColorMode
183 205
	 * @param itemsCount
184 206
	 */
185
	public void setColors(int renderingColorMode, int itemsCount)	{
207
	public void setColors(int renderingColorMode, int itemsCount) {
186 208
		this.setColors(this.theme.getPaletteAsHexFor(renderingColorMode, itemsCount));
187 209
	}
188 210
	
189 211
	/**
190 212
	 * Replaces or creates the vector named "colors" in the R workspace filled with the specified hexadecimal colors palette.
213
	 * 
191 214
	 * @param hexPalette
192 215
	 */
193
	public void setColors(String[] hexPalette)	{
216
	public void setColors(String[] hexPalette) {
194 217
		try {
195 218
			RWorkspace rw = RWorkspace.getRWorkspaceInstance();
196 219
			rw.eval("rm(colors)"); //$NON-NLS-1$
197 220
			rw.addVectorToWorkspace("colors", hexPalette);  //$NON-NLS-1$
198 221
		}
199
		catch(RWorkspaceException e) {
222
		catch (RWorkspaceException e) {
200 223
			e.printStackTrace();
201 224
		}
202 225
	}
203 226
	
204

  
227
	
205 228
	@Override
206
	public void setOutputFormat(String outputFormat)	{
229
	public void setOutputFormat(String outputFormat) {
207 230
		this.outputFormat = outputFormat;
208

  
231
		
209 232
		// FIXME : SJ: voir comment gérer cela autrement + supprimer certaines méthodes du RDevice devenues inutiles depuis qu'on stocke outputFormat ?
210
		if(outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_JPEG))	{
233
		if (outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_JPEG)) {
211 234
			this.rDevice = RDevice.JPEG;
212 235
		}
213
		else if(outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PDF))	{
236
		else if (outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PDF)) {
214 237
			this.rDevice = RDevice.PDF;
215 238
		}
216
		else if(outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PNG))	{
239
		else if (outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PNG)) {
217 240
			this.rDevice = RDevice.PNG;
218 241
		}
219
		else if(outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PS))	{
242
		else if (outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_PS)) {
220 243
			this.rDevice = RDevice.PS;
221 244
		}
222
		else if(outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_SVG))	{
245
		else if (outputFormat.equals(ChartsEngine.OUTPUT_FORMAT_SVG)) {
223 246
			this.rDevice = RDevice.SVG;
224 247
		}
225 248
	}
226

  
227

  
228 249
	
229

  
250
	
251
	
252
	
230 253
	@Override
231 254
	public ArrayList<String> getSupportedOutputDisplayFormats() {
232
		ArrayList<String> formats = new ArrayList<String>(1);
233

  
255
		ArrayList<String> formats = new ArrayList<>(1);
256
		
234 257
		// FIXME: UI display output format, only SVG
235
		//formats.add(outputFormatDefinitions.get(JFCChartsEngine.OUTPUT_FORMAT_SVG));
258
		// formats.add(outputFormatDefinitions.get(JFCChartsEngine.OUTPUT_FORMAT_SVG));
236 259
		
237 260
		return formats;
238 261
	}
239

  
240 262
	
263
	
241 264
	@Override
242 265
	public ArrayList<String> getSupportedOutputFileFormats() {
243
	ArrayList<String> formats = new ArrayList<String>(5);
266
		ArrayList<String> formats = new ArrayList<>(5);
244 267
		
245 268
		formats.add(OUTPUT_FORMAT_SVG);
246 269
		formats.add(OUTPUT_FORMAT_PNG);
......
251 274
		return formats;
252 275
	}
253 276
	
254

  
255

  
256

  
257

  
277
	
278
	
279
	
280
	
258 281
	@Override
259 282
	public File exportChart(Object chart, File file, String outputFormat, int imageWidth, int imageHeight, int drawingAreaX, int drawingAreaY, int drawingAreaWidth, int drawingAreaHeight) {
260 283
		// FIXME: Not yet implemented
261 284
		Log.severe(this.getClass() + ".exportChart(): Not yet implemented.");
262 285
		return null;
263 286
	}
264

  
287
	
265 288
	@Override
266 289
	public boolean isRunning() {
267 290
		// TODO Auto-generated method stub
268 291
		return false;
269 292
	}
270

  
271

  
293
	
294
	
272 295
	@Override
273 296
	public boolean start(IProgressMonitor monitor) throws Exception {
274 297
		// TODO Auto-generated method stub
275 298
		return false;
276 299
	}
277

  
300
	
278 301
	@Override
279 302
	public boolean stop() throws Exception {
280 303
		// TODO Auto-generated method stub
281 304
		return false;
282 305
	}
283

  
306
	
284 307
	@Override
285 308
	public String getName() {
286 309
		return RChartsEngine.NAME;
287 310
	}
288

  
289 311
	
312
	
290 313
}

Also available in: Unified diff