Olá,
Fiz uma aplicação q usa vetores, e esta em um arquivo. Com os dados desses vetores, preciso gerar graficos, porem o arquivo que gera graficos é outro e preciso usar as funções do mesmo. Acho q se eu conseguisse fazer com q o arquivo que gera os graficos fosse como um import, eu colocaria ele no arquivo dos vetores seria facil gerar os graficos.
Vou enviar o arquivo dos vetores:
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
// imports da planilha excel - JavaExcel
import jxl.Cell;
import jxl.CellType;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
// import do grafico - JfreeChart
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class EWMA{
// entrada de dados da media curta
private static String i_curta1;
private static int i_curta;
private static String f_curta1;
private static int f_curta;
//entrada de dados da media longa
private static String i_longa1;
private static int i_longa;
private static String f_longa1;
private static int f_longa;
// janela de dados
private static int janela_dados_curta;
private static int janela_dados_longa;
// laços
private static int i ;
private static int j ;
private static double intc3;
private static double lambida;
private static double lambida2;
private static String lambida1;
private static double ewma;
private static double stringk11;
//vetores
public static double dados_planilha[];
public static double media_curta[];
public static double media_longa[];
public static double ewma_a[];
public static double ewma_b[];
// somas
private static double soma_curta;
private static double soma_longa;
// médias
private static double l_media;
private static double c_media;
// menu
private static int opt;
private static String opt1;
// menu ewma
private static int opt_ewma;
private static String opt_ewma1;
/** Creates a new instance of Main */
public static void main(String[] args ) throws IOException, BiffException
{
// Pega o valor da opção
opt1 = JOptionPane.showInputDialog("Digite uma opção \n" +
"1 - Calcular EWMA \n" +
"2 - Calcular Média Curta \n" +
"3 - Calcular Média Longa \n" +
"4 - Plotar Gráfico \n" +
"5 - Sair");
opt = Integer.parseInt(opt1); //converte pra inteiro
switch(opt){
case 1 :
ewma();
break;
case 2 :
media_curta();
break;
case 3 :
media_longa();
break;
case 4 :
grafico();
break;
case 5:
System.exit(0);
}
}
public static void ewma() throws IOException, BiffException{
opt_ewma1 = JOptionPane.showInputDialog("Digite uma opção \n" +
"1 - EWMA - Média Curta \n" +
"2 - EWMA - Média Longa \n" +
"3 - Plotar Gráfico \n" +
"4 - Sair");
opt_ewma = Integer.parseInt(opt_ewma1); //converte pra inteiro
if(opt_ewma == 1){
// Pega o valor lambida
lambida1 = JOptionPane.showInputDialog("Digite o valor do Lambida \n" +
"Entre 0 - 1");
lambida = Double.parseDouble(lambida1);
// pega o arquiivo do Excel
String filename = "c:/1995_completo_soja.xls";
File f= new File(filename);
Workbook planilha = Workbook.getWorkbook(f);
// Mostra o nome do arquivo lido
JOptionPane.showMessageDialog(null,"Leu o arquivo " +f);
/* pega a primeira planilha dentro do arquivo XLS */
Sheet sheet = planilha.getSheet(0);
//Pega a quantidade de linhas da planilha
int linhas = sheet.getRows();
// passa a quantidade de linhas pro vetor ewma
ewma_a = new double[linhas];
// passa a quantidade de linhas pro vetor ewma
ewma_b = new double[linhas];
// passa a quantidade de linhas pro vetor dados_planilha
dados_planilha = new double[linhas];
// Mostra a quantidad de linhas
// JOptionPane.showMessageDialog(null,"Quantidade de linhas: "+ linhas);
//Pega a quantidade de colunas da planilha
int colunas = sheet.getColumns();
//EWMA Media Curta
// Mostra o resultado do EWMA
JOptionPane.showMessageDialog(null,"EWMA - Média Longa: " +ewma_b[i+f_longa]);
planilha.close(); // fecha planilha
} // fim do if opção 1
if(opt_ewma == 2){
// Pega o valor lambida
lambida1 = JOptionPane.showInputDialog("Digite o valor do Lambida \n" +
"Entre 0 - 1");
lambida = Double.parseDouble(lambida1);
// pega o arquiivo do Excel
String filename = "c:/1995_completo_soja.xls";
File f= new File(filename);
Workbook planilha = Workbook.getWorkbook(f);
// Mostra o nome do arquivo lido
JOptionPane.showMessageDialog(null,"Leu o arquivo " +f);
/* pega a primeira planilha dentro do arquivo XLS */
Sheet sheet = planilha.getSheet(0);
//Pega a quantidade de linhas da planilha
int linhas = sheet.getRows();
// passa a quantidade de linhas pro vetor ewma
ewma_a = new double[linhas];
// Mostra a quantidad de linhas
// JOptionPane.showMessageDialog(null,"Quantidade de linhas: "+ linhas);
//Pega a quantidade de colunas da planilha
int colunas = sheet.getColumns();
//EWMa Media Longa
// Inicio da Formula EWMA
ewma_a[i+f_curta] = lambida * media_curta[i+f_curta-1] + (1-lambida) * media_curta[i+f_curta];
// Mostra o resultado do EWMA
JOptionPane.showMessageDialog(null,"EWMA - Média Longa: " +ewma_a[i+f_curta]);
// Fim da Formula EWMA
planilha.close(); // fecha planilha
} // fim do if opção 2
if(opt_ewma == 3){
// EWMa Grafico
JOptionPane.showMessageDialog(null,"Testando opção: "+opt_ewma);
} // fim do if opção 3
if(opt_ewma == 4){
// Sair
System.exit(0);
} // fim do if opção 4
};
public static void media_curta() throws IOException, BiffException{
// Pega o valor lambida
lambida1 = JOptionPane.showInputDialog("Configurações: \n" +
"Digite o valor de Lambida \n" +
"Entre 0 <= 1");
lambida = Double.parseDouble(lambida1);
// Pega o valor do intervalo de dados da media Curta
String data1=JOptionPane.showInputDialog("Entre com a data inicial: \n " +
"Ex: 01/10/95");
f_curta1 = JOptionPane.showInputDialog("Média Curta - Intervalo dos Dados \n" +
"Entre 1 e 5 ");
f_curta = Integer.parseInt(f_curta1);
// pega o arquiivo do Excel
String filename = "c:/1995_completo_soja.xls";
File f= new File(filename);
Workbook planilha = Workbook.getWorkbook(f);
// Mostra o nome do arquivo lido
JOptionPane.showMessageDialog(null,"Arquivo Lido \n" +f);
/* pega a primeira planilha dentro do arquivo XLS */
Sheet sheet = planilha.getSheet(0);
//Pega a quantidade de linhas da planilha
int linhas = sheet.getRows();
for(i=1;i<=linhas;i++){
// pega as celulas da coluna, pulando a 1º linha do titulo
Cell k11 = sheet.getCell(0,i+1);
String data2=k11.getContents();
//JOptionPane.showMessageDialog(null,"data: " +data2+"-"+data1);
if(data2.compareToIgnoreCase(data1)==0){
i_curta=i;
break;
}
}
// passa a quantidade de linhas pro vetor media curta
media_curta = new double[linhas];
// passa a quantidade de linhas pro vetor dados_planilha
ewma_a = new double[linhas];
// Mostra a quantidad de linhas
// JOptionPane.showMessageDialog(null,"Quantidade de linhas: "+ linhas);
//Pega a quantidade de colunas da planilha
int colunas = sheet.getColumns();
// Mostra a quantidade de colunas
// JOptionPane.showMessageDialog(null,"Quantidade de colunas: " +colunas);
// Calcula media curta
for(i = i_curta; i <= linhas - f_curta+1; i++){
soma_curta=0;
for(j=i;j<=i+f_curta-1;j++){
// pega as celulas da coluna
Cell k11 = sheet.getCell(11,j);
//JOptionPane.showMessageDialog(null,"Dados: " +stringk11);
// Função pra converter Sring em Double
if (k11.getType() == CellType.NUMBER)
{
NumberCell fc = (NumberCell)k11;
stringk11 = fc.getValue();
//JOptionPane.showMessageDialog(null,"Dados: " +stringk11);
// faz a soma dos dados
soma_curta = soma_curta + stringk11;
}
}//fim do for j
media_curta[i+f_curta] = soma_curta/f_curta; // dados da planilha a serem trabalhados
JOptionPane.showMessageDialog(null,"Média - Média Curta: " +media_curta[i+f_curta]);
// calculo do ewma
ewma_a[i+f_curta] = lambida * media_curta[i+f_curta-1] + (1-lambida) * media_curta[i+f_curta];
JOptionPane.showMessageDialog(null,"EWMA: " +ewma_a[i+f_curta]);
}//fim do for i
};
public static void media_longa() throws IOException, BiffException{
// Pega o valor lambida
lambida1 = JOptionPane.showInputDialog("Configurações: \n" +
"Digite o valor de Lambida \n" +
"Entre 0 e 1");
lambida = Double.parseDouble(lambida1);
String data1 = JOptionPane.showInputDialog("Entre com a data inicial: \n " +
"Ex: 01/10/95");
f_longa1 = JOptionPane.showInputDialog("Média Longa - Intervalo dos Dados \n" +
"Entre 5 e 10 ");
f_longa = Integer.parseInt(f_longa1);
// pega o arquiivo do Excel
String filename = "c:/1995_completo_soja.xls";
File f= new File(filename);
Workbook planilha = Workbook.getWorkbook(f);
// Mostra o nome do arquivo lido
JOptionPane.showMessageDialog(null,"Arquivo Lido \n" +f);
/* pega a primeira planilha dentro do arquivo XLS */
Sheet sheet = planilha.getSheet(0);
//Pega a quantidade de linhas da planilha
int linhas = sheet.getRows();
for(i=1;i<=linhas;i++){
// pega as celulas da coluna, pulando a 1º linha do titulo
Cell k11 = sheet.getCell(0,i+1);
String data2 = k11.getContents();
//JOptionPane.showMessageDialog(null,"data: " +data2+"-"+data1);
if(data2.compareToIgnoreCase(data1)==0){
i_longa = i;
break;
}
}
// passa a quantidade de linhas pro vetor media curta
media_longa = new double[linhas];
// passa a quantidade de linhas pro vetor dados_planilha
ewma_b = new double[linhas];
// Mostra a quantidad de linhas
// JOptionPane.showMessageDialog(null,"Quantidade de linhas: "+ linhas);
//Pega a quantidade de colunas da planilha
int colunas = sheet.getColumns();
// Mostra a quantidade de colunas
// JOptionPane.showMessageDialog(null,"Quantidade de colunas: " +colunas);
// Calcula media curta
for(i = i_longa; i <= linhas - f_longa+1; i++){
soma_longa=0;
for(j=i;j<=i+f_longa-1;j++){
// pega as celulas da coluna
Cell k11 = sheet.getCell(11,j);
// Função pra converter Sring em Double
if (k11.getType() == CellType.NUMBER)
{
NumberCell fc = (NumberCell)k11;
stringk11 = fc.getValue();
//JOptionPane.showMessageDialog(null,"Dados: " +stringk11);
// faz a soma dos dados
soma_longa = soma_longa + stringk11;
}
}//fim do for j
media_longa[i+f_longa] = soma_longa/f_longa; // dados da planilha a serem trabalhados
JOptionPane.showMessageDialog(null,"Média - Média Longa: " +media_longa[i+f_longa]);
ewma_b[i+f_longa] = lambida * media_longa[i+f_longa-1] + (1-lambida) * media_longa[i+f_longa];
JOptionPane.showMessageDialog(null,"EWMA - Média Longa: " +ewma_b[i+f_longa]);
}//fim do for i
};
public static void grafico() throws IOException, BiffException{
};
}
Arquivo q gera graficos:
import java.awt.Font;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A demonstration application showing how to create a vertical combined chart.
*
*/
public class Graficos extends ApplicationFrame {
/**
* Constructs a new demonstration application.
*
* @param title the frame title.
*/
public Graficos(final String title) {
super(title);
final JFreeChart chart = createCombinedChart();
final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
panel.setPreferredSize(new java.awt.Dimension(700,700));
setContentPane(panel);
}
/**
* Creates a combined chart.
*
* @return the combined chart.
*/
private JFreeChart createCombinedChart() {
// create subplot 1...
final XYDataset data1 = createDataset1();
final XYItemRenderer renderer1 = new StandardXYItemRenderer();
final NumberAxis rangeAxis1 = new NumberAxis("Range 1");
final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
final XYTextAnnotation annotation = new XYTextAnnotation("Comprar", 50.0, 10000.0);
annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
annotation.setRotationAngle(Math.PI / 4.0);
subplot1.addAnnotation(annotation);
// create subplot 2...
final XYItemRenderer renderer2 = new StandardXYItemRenderer();
final NumberAxis rangeAxis2 = new NumberAxis("Range 2");
rangeAxis2.setAutoRangeIncludesZero(false);
// parent plot...
final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
plot.setGap(10.0);
// add the subplots...
plot.add(subplot1, 1);
plot.setOrientation(PlotOrientation.VERTICAL);
// return a new chart containing the overlaid plot...
return new JFreeChart("CombinedDomainXYPlot Demo",
JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
/**
* Creates a sample dataset.
*
* @return Series 1.
*/
private XYDataset createDataset1() {
// create dataset 1...
final XYSeries series1 = new XYSeries("aaaaaaaaaaaaa");
series1.add(10.0, 12353.3);
series1.add(20.0, 13734.4);
series1.add(30.0, 14525.3);
series1.add(40.0, 13984.3);
series1.add(50.0, 12999.4);
series1.add(60.0, 14274.3);
series1.add(70.0, 15943.5);
series1.add(80.0, 14845.3);
series1.add(90.0, 14645.4);
series1.add(100.0, 16234.6);
series1.add(110.0, 17232.3);
series1.add(120.0, 14232.2);
series1.add(130.0, 13102.2);
series1.add(140.0, 14230.2);
series1.add(150.0, 11235.2);
final XYSeries series2 = new XYSeries("Média Longa");
series2.add(10.0, 15000.3);
series2.add(20.0, 11000.4);
series2.add(30.0, 17000.3);
series2.add(40.0, 15000.3);
series2.add(50.0, 14000.4);
series2.add(60.0, 12000.3);
series2.add(70.0, 11000.5);
series2.add(80.0, 12000.3);
series2.add(90.0, 13000.4);
series2.add(100.0, 12000.6);
series2.add(110.0, 13000.3);
series2.add(120.0, 17000.2);
series2.add(130.0, 18000.2);
series2.add(140.0, 16000.2);
series2.add(150.0, 17000.2);
final XYSeriesCollection collection = new XYSeriesCollection();
collection.addSeries(series1);
collection.addSeries(series2);
return collection;
}
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.com/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project - please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(final String[] args) {
final Teste_1 demo = new Teste_1("CombinedDomainXYPlot Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Se alguem souber uma outra forma de fazer isso, me da uma força ai.
Anderson Dorneles