Lista! Preciso de ajuda para plotar
um grafico de uma regressao linear usando a JClass. Tenho o
grafico
*simples, com os pontos que eu preciso, lendo eles de um arquivo e mostrando num grafico do tipo scatter_plot *e tenho o algoritmo para calcular os coeficientes da regressao linear, mas agora PRECISO usar esses coeficientes *que eu calculei pra plotar a reta que melhor representa esse conjunto de pontos. Jah tentei usar o JCDRawStyle, *mas nao tenho um pacote necessario pra usar essa classe. Entao preciso encontrar outro meio de fazer isso. Alguem *pode me ajudar??? Segue o codigo do scatter_plot e, em seguida, o codigo da regressao linear! *Thanks! *Gina. Scatter_Plot:
package com.bt.gina;
//util
import java.util.*; //swing
import javax.swing.*; //awt
import java.awt.*; import java.awt.geom.*; import java.awt.event.*; //JClass50K
import com.klg.jclass.chart.*; import com.klg.jclass.chart.data.*; import com.klg.jclass.util.swing.*; import com.klg.jclass.util.legend.*; /**
* Basic example of Chart use. Load data from * a file and displays it as a scatter plot chart. */ public
class Plot extends JPanel
{ JCAxis xaxis = null; JCAxis yaxis = null; /** Chart instance */ protected JCChart chart; //JCLineStyle line; /** * Default constructor for this class. Loads data and * sets up chart. */ static Color bg_gray = new Color(0xe5, 0xe5, 0xe5); //static Color color = new Color(0xe5, 0xe5, 0xe5); public Plot() { setLayout(new GridLayout(1,1)); setPreferredSize(new Dimension(1050, 500)); // Create new chart instance. chart = new JCChart(JCChart.SCATTER_PLOT); // Set Colors chart.setBackground(bg_gray); chart.getChartArea().setOpaque(true); chart.getChartArea().getPlotArea().setBackground(new Color(0xcc, 0xcc, 0xcc)); chart.setBorder(BorderFactory.createLoweredBevelBorder());
// X
Axis
if (xaxis == null) xaxis = chart.getChartArea().getXAxis(0); xaxis.setEditable(false); xaxis.setOriginPlacement(JCAxis.ZERO); //xaxis.setAnnotationMethod(JCAxis.POINT_LABELS); xaxis.setGridVisible(true); xaxis.getGridStyle().getLineStyle().setColor(bg_gray); xaxis.setTitle(new JCAxisTitle("Distance")); xaxis.setNumSpacing(10.0); // Y Axis if (yaxis == null) yaxis = chart.getChartArea().getYAxis(0); yaxis.setEditable(false); yaxis.setOriginPlacement(JCAxis.ZERO); //yaxis.setAnnotationMethod(JCAxis.POINT_LABELS); yaxis.setGridVisible(true); yaxis.getGridStyle().getLineStyle().setColor(bg_gray); yaxis.setTitle(new JCAxisTitle("Time")); // Load data for chart
ChartDataModel cdm = null; try
{ // Use JCFileDataSource to load data from specified file String fname = "E:\\com\\bt\\gina\\chart1.dat"; cdm = new JCFileDataSource(fname); } catch (Exception
e)
{ e.printStackTrace(System.out); } if (cdm !=
null)
{ chart.getDataView(0).setDataSource(cdm); // Make footer visible chart.getFooter().setVisible(true); // By default, footer is a JLabel - set its Text property ((JLabel)chart.getFooter()).setText("Time X Distance"); // Make header visible chart.getHeader().setFont(new Font("TimesRoman", Font.BOLD, 18)); chart.getHeader().setVisible(true); // By default, header is a JLabel - set its Text property ((JLabel)chart.getHeader()).setText("Scatter Plot"); // Borders chart.getChartArea().setBorder(BorderFactory.createLoweredBevelBorder()); // Chart fonts chart.getChartArea().setFont(new Font("Helvetica", Font.PLAIN, 12));
// Make legend
visible
chart.getLegend().setVisible(true); } // Add chart to panel for display. add(chart); // Try to use chart3D to change the line style of one series of a scatter plot /* Chart3dPointData pointData=(Chart3dPointData)dataView.getElevationData(); ArrayList series=pointData.getSeries(); Chart3dPointSeries pointSeries=(Chart3dPointSeries)series.get(2); JCChartStyle chartStyle=pointSeries.getChartStyle(); chartStyle.setLineStyle(new JCLineStyle(2, color.blue, JCLineStyle.SOLID)); */ /* */ //Try to use JCDrawStyle to draw a line in the sactter_plot chart /*JCDrawStyle ds = new JCDrawStyle(); ds.setLineWidth(new JCUnit.Measure(JCUnit.POINTS, 5)); ArrayList list = new ArrayList(); list.add(new JCUnit.Point(JCUnit.CM, 65, 75)); list.add(new JCUnit.Point(JCUnit.CM, 220, 75)); frame.drawLine(ds, list);*/
// Try to use JCLineStyle to draw a line in the sactter_plot
chart
/* line = new JCLineStyle(5, Color.black, 0); DrawSth drawSth = new DrawSth(); line.drawLine(g, 60, 75, 220, 75); add(line); public void paint(Graphics g) { g.drawLine(60,75,220,75); } */ } public static void main(String args[]) { JCExitFrame f = new JCExitFrame("Scatter Plot"); Plot p = new Plot(); //f.getContentPane().add(g); f.getContentPane().add(p); f.pack(); f.setVisible(true); } } Regressao Linear:
package com.bt.gina;
//awt
import java.awt.*; import java.awt.geom.*; import java.awt.event.*; //begin
public class Test extends Frame { protected Point2D[] mPoints; public Test() { createUI(); wireEvents(); } protected void createUI()
{ setLayout(new GridLayout(1,1)); setSize(new Dimension(326, 326)); setLocation(310, 0); setVisible(true); mPoints = new Point2D[4]; //
Points
mPoints[0] = new Point2D.Double(60.0, 75.0); mPoints[1] = new Point2D.Double(100.0, 50.0); mPoints[2] = new Point2D.Double(180.0, 100.0); mPoints[3] = new Point2D.Double(210.0, 75.0); } protected void wireEvents()
{ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } public void paint(Graphics g)
{ paint((Graphics2D)g); } public void paint(Graphics2D g)
{ g.setPaint(Color.black); // Draws the line of the
linear regression statistical method
double
S1=0,S2=0,S3=0,S4=0;
double A, B; int N = 4; for (int i=0;i<N;i++)
{ S1 += mPoints[i].getX() * mPoints[i].getY(); S2 += mPoints[i].getX(); S3 += mPoints[i].getY(); S4 += java.lang.Math.pow(mPoints[i].getX(),2); } // Calculates the coeficients
of the linear regression (A = slope and B =
y-intercept)
A = (N*S1-S2*S3)/(N*S4-java.lang.Math.pow(S2,2)); B = (S3-A*S2)/N; // Claculates two points for the line of the linear regression using the minimum and maximun value of X double calculatedY1 = (A*60) + B; double calculatedY2 = (A*210) + B; // Draws the line Line2D l = new Line2D.Float(); l.setLine(60, calculatedY1,
210, calculatedY2);
g.draw(l); for (int i = 0; i <
mPoints.length; i++)
{ g.setPaint(Color.black); //
Draws the
point
g.fill(getControlPoint(mPoints[i])); } } protected Shape getControlPoint(Point2D p) { // Creates a small square around the given point int side = 4; return new Rectangle2D.Double(p.getX() - side / 2, p.getY() - side / 2, side, side); } public static void main(String[] args) { Test t = new Test(); } }
|