001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.io; 025 026 import java.io.File; 027 import java.io.OutputStream; 028 import java.io.OutputStreamWriter; 029 import java.io.Writer; 030 031 import org.ujmp.core.Matrix; 032 import org.ujmp.core.util.StringUtil; 033 import org.ujmp.core.util.io.IntelligentFileWriter; 034 035 public abstract class ExportMatrixPLT { 036 037 public static void toFile(File file, Matrix matrix, Object... parameters) throws Exception { 038 IntelligentFileWriter writer = new IntelligentFileWriter(file); 039 toWriter(writer, matrix, parameters); 040 writer.close(); 041 } 042 043 public static void toStream(OutputStream outputStream, Matrix matrix, Object... parameters) 044 throws Exception { 045 OutputStreamWriter writer = new OutputStreamWriter(outputStream); 046 toWriter(writer, matrix, parameters); 047 writer.close(); 048 } 049 050 public static void toWriter(Writer writer, Matrix matrix, Object... parameters) 051 throws Exception { 052 String EOL = System.getProperty("line.separator"); 053 boolean xy = false; 054 boolean logx = false; 055 boolean logy = false; 056 for (Object o : parameters) { 057 String s = StringUtil.getString(o); 058 if ("logx".equalsIgnoreCase(s)) { 059 logx = true; 060 } else if ("logy".equalsIgnoreCase(s)) { 061 logy = true; 062 } else if ("xy".equalsIgnoreCase(s)) { 063 xy = true; 064 } 065 } 066 067 writeData(writer, matrix, xy, logx, logy); 068 writer.write("pause -1 'Export plot to eps?'" + EOL); 069 writer.write("set output 'plot" + System.currentTimeMillis() + ".eps'" + EOL); 070 writer.write("set terminal postscript eps" + EOL); 071 writer.write("replot" + EOL); 072 writeData(writer, matrix, xy, logx, logy); 073 } 074 075 private static void writeData(Writer writer, Matrix matrix, boolean xy, boolean logx, 076 boolean logy) throws Exception { 077 String EOL = System.getProperty("line.separator"); 078 writer.write("set key outside below" + EOL); 079 writer.write("set autoscale fix" + EOL); 080 if (logx && logy) { 081 writer.write("set log xy" + EOL); 082 } else if (logx) { 083 writer.write("set log x" + EOL); 084 } else if (logx) { 085 writer.write("set log x" + EOL); 086 } 087 if (xy) { 088 String x = matrix.getColumnLabel(0); 089 x = x == null ? "column 0" : x; 090 writer.write("set xlabel '" + x + "'" + EOL); 091 } else { 092 writer.write("set xlabel 'column'" + EOL); 093 } 094 // writer.write("set ylabel 'value'" + EOL); 095 writer.write("set title '" + StringUtil.format(matrix.getLabel()) + "'" + EOL); 096 writer.write("plot "); 097 098 int startColumn = xy ? 1 : 0; 099 for (int c = startColumn; c < matrix.getColumnCount(); c++) { 100 String x = matrix.getColumnLabel(c); 101 x = x == null ? "column " + c : x; 102 writer.write("'-' using 1:2 title '" + x + "' with linespoints"); 103 if (c < matrix.getColumnCount() - 1) { 104 writer.write(", "); 105 } else { 106 writer.write(EOL); 107 } 108 } 109 110 if (xy) { 111 for (int c = 1; c < matrix.getColumnCount(); c++) { 112 for (int r = 0; r < matrix.getRowCount(); r++) { 113 writer.write(matrix.getAsDouble(r, 0) + " " + matrix.getAsDouble(r, c) + EOL); 114 } 115 writer.write("e" + EOL); // data end 116 } 117 } else { 118 for (int c = 0; c < matrix.getColumnCount(); c++) { 119 for (int r = 0; r < matrix.getRowCount(); r++) { 120 writer.write(r + " " + matrix.getAsDouble(r, c) + EOL); 121 } 122 writer.write("e" + EOL); // data end 123 } 124 } 125 } 126 }