Exemple #1
0
// Plot uses HTML5/SVG to create a graph which you can view in your browser
func Plot(location string, r reg.New, yx, xy bool) {
	if location == "stdin" {
		// We don't have a filename or location. Using the current
		// UNIX timestamp in nanoseconds instead. File is created
		// in the working directory.
		cwd, _ := os.Getwd()
		name := fmt.Sprintf("/plot-%d.html", time.Now().UnixNano())
		absPath := cwd + name
		loc, err = os.Create(absPath)
		if err != nil {
			fault.Trap(err, "errFsperm")
		}
		defer loc.Close()
	} else {
		// We have both the filename and the location
		// where to create it. Proceed normally.
		cwd := filepath.Dir(location)
		name := fmt.Sprintf("/plot-for-%s.html", filepath.Base(location))
		absPath := cwd + name
		loc, err = os.Create(absPath)
		if err != nil {
			fault.Trap(err, "errFsperm")
		}
		defer loc.Close()
	}
	// Draw the data points
	// Initialize the graph object with 33x22 grids
	gp := r.GraphInit(xgcount, ygcount)
	fmt.Fprintf(loc, "<!DOCTYPE html>\n\n")
	fmt.Fprintf(loc, "<!-- ratChart -->\n")
	fmt.Fprintf(loc, "<!-- This plot was generated by %s-->\n\n", doc.MiniVer())
	fmt.Fprintf(loc, "<!-- This file is best rendered in modern versions of -->\n")
	fmt.Fprintf(loc, "<!-- Mozilla Firefox/Google Chrome. To render the plot, -->\n")
	fmt.Fprintf(loc, "<!-- open this file with your browser. -->\n\n")
	fmt.Fprintf(loc, "<head>\n")
	fmt.Fprintf(loc, "\t<meta charset=\"%s\">\n", charset)
	fmt.Fprintf(loc, "\t<title>ratchart</title>\n")
	fmt.Fprintf(loc, "\t<style type=\"text/css\">\n")
	fmt.Fprintf(loc, "\t\tbody {background-color: %s; background-attachment: fixed; font-family: \"%s\"}\n", bgcolor, bodyfnt)
	fmt.Fprintf(loc, "\t\t.headerbar {background-color: #116A95; color: %s; font-family: \"%s\";\n\t\t font-size: 20px;font-weight: bold; padding: 2px; text-align: center;}\n", bgcolor, plotfnt)
	fmt.Fprintf(loc, "\t\t.plotarea {background-color: %s; font-family: \"%s\"; font-size: 12px; float: left;}\n", bgcolor, plotfnt)
	fmt.Fprintf(loc, "\t\t.legend {background-color: %s; float: right; color: %s; padding: 15px;}\n", legendcl, bgcolor)
	fmt.Fprintf(loc, "\t</style>\n")
	fmt.Fprintf(loc, "</head>\n")
	fmt.Fprintf(loc, "<body>\n")
	fmt.Fprintf(loc, "\t<div class=\"headerbar\">\n")
	fmt.Fprintf(loc, "\t\t<p>\n")
	if yx && xy || !yx && !xy {
		fmt.Fprintf(loc, "\t\t\tPlot for: %s and %s\n", r.Equation("YonX"), r.Equation("XonY"))
	}
	if yx && !xy {
		fmt.Fprintf(loc, "\t\t\tPlot for: %s\n", r.Equation("YonX"))
	}
	if !yx && xy {
		fmt.Fprintf(loc, "\t\t\tPlot for: %s\n", r.Equation("XonY"))
	}
	fmt.Fprintf(loc, "\t\t</p>\n")
	fmt.Fprintf(loc, "\t</div>\n")
	fmt.Fprintf(loc, "\t<div class=\"plotarea\">\n")
	fmt.Fprintf(loc, "\t\t<svg width=\"1000\" height=\"600\">\n")
	// Tag to show which line is which
	if yx && xy || !yx && !xy {
		fmt.Fprintf(loc, "\t\t\t<line x1=\"40\" y1=\"0\" x2=\"40\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
		fmt.Fprintf(loc, "\t\t\t<line x1=\"190\" y1=\"0\" x2=\"190\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
		fmt.Fprintf(loc, "\t\t\t<line x1=\"40\" y1=\"50\" x2=\"190\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
		fmt.Fprintf(loc, "\t\t\t<text x=\"50\" y =\"20\" fill=\"black\"> Y on X </text>\n")
		fmt.Fprintf(loc, "\t\t\t<text x=\"50\" y =\"40\" fill=\"black\"> X on Y </text>\n")
		fmt.Fprintf(loc, "\t\t\t<line x1=\"100\" y1=\"16\" x2=\"180\" y2=\"16\" style=\"stroke:rgb(248,32,32);stroke-width:3\"/>\n")
		fmt.Fprintf(loc, "\t\t\t<line x1=\"100\" y1=\"36\" x2=\"180\" y2=\"36\" style=\"stroke:rgb(11,92,20);stroke-width:3\"/>\n")
	}
	// Line definition
	// fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(32,42,198);stroke-width:3\"/>\n", 40,20,40,10)
	// Origin tag
	// On the graph, (0,0) starts from (310,500)
	fmt.Fprintf(loc, "\t\t\t<text x=\"260\" y =\"520\" fill=\"black\"> (0,0) </text>\n")
	// // X coordinate tag
	// xtag := fmt.Fprintf("\t\t\t<text x=\"910\" y =\"530\" fill=\"black\"> (%0.f,0) </text>\n", gp.Xgrids[len(gp.Xgrids)-1])
	// // Y coordinate tag
	// ytag := fmt.Fprintf("\t\t\t<text x=\"260\" y =\"25\" fill=\"black\"> (0,%0.f) </text>\n", gp.Ygrids[len(gp.Ygrids)-1])
	// Y- axis
	fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"35\" x2=\"305\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
	// Y -axis label
	fmt.Fprintf(loc, "\t\t\t<text x=\"200\" y=\"285\" fill=\"black\"> (Y) </text>\n")
	// Mirror X
	fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"35\" x2=\"995\" y2=\"35\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
	// X axis
	fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"505\" x2=\"995\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
	// X axis label
	fmt.Fprintf(loc, "\t\t\t<text x=\"620\" y=\"580\" fill=\"black\"> (X) </text>\n")
	// Mirror Y
	fmt.Fprintf(loc, "\t\t\t<line x1=\"995\" y1=\"35\" x2=\"995\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n")
	// Draw framework
	// Draw grids
	// Vertical grids, 33 of them, spacing 20
	for I := 0; ; I++ {
		J := 20.0 * float64(I)
		X, _, term := reg.Translate(J, 0)
		if term {
			break
		}
		fmt.Fprintf(loc, "%s%.2f%s%s%.2f%s%s", x1var, X, y1const, x2var, X, y2const, grstyle)
		if I > 0 && I < (len(gp.Xgrids)-1) {
			fmt.Fprintf(loc, "\t\t\t<text x=\"%.0f\" y=\"520\" fill=\"black\" transform=\"rotate(60 %.0f,520)\">%.2f</text>\n", gp.Xcoord[I], gp.Xcoord[I], gp.Xgrids[I])
		}
	}
	// Horizontal grids, 22 of them, spacing 20
	for I := 0; ; I++ {
		J := 20.0 * float64(I)
		_, Y, term := reg.Translate(0, J)
		if term {
			break
		}
		fmt.Fprintf(loc, "%s%s%.2f%s%s%.2f%s", x1const, y1var, Y, x2const, y2var, Y, grstyle)
		if I > 0 && I < (len(gp.Ygrids)-1) {
			fmt.Fprintf(loc, "\t\t\t<text x=\"240\" y=\"%.0f\" fill=\"black\" transform=\"rotate(0 260,%.0f)\">%.2f</text>\n", gp.Ycoord[I], gp.Ycoord[I], gp.Ygrids[I])
		}
	}

	// Initialize plotter
	pl := gp.PlotInit(r)
	for index, element := range pl.X {
		fmt.Fprintf(loc, "%s%f%s%f%s\n", circstr, element, circmid, pl.Y[index], circsty)
	}

	// Draw regression lines
	rg := r.ReglineInit(gp)

	xxy1, xxy2 := rg.XonXY[0], rg.XonXY[1] // X on Y
	yxy1, yxy2 := rg.YonXY[0], rg.YonXY[1]
	xyx1, xyx2 := rg.XonYX[0], rg.XonYX[1] // Y on X
	yyx1, yyx2 := rg.YonYX[0], rg.YonYX[1]
	if !yx && xy || !yx && !xy { // XonY
		fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(11,92,20);stroke-width:3\"/>\n", xxy1, yxy1, xxy2, yxy2)
	}
	if yx && !xy || !yx && !xy { // YonX
		fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(248,32,32);stroke-width:3\"/>\n", xyx1, yyx1, xyx2, yyx2)
	}
	fmt.Fprintf(loc, "\t\t</svg>\n")
	fmt.Fprintf(loc, "\t</div>\n")
	fmt.Fprintf(loc, "\t<div class=\"legend\">\n")
	fmt.Fprintf(loc, "\t\t<p> <u> AXIS SCALE: </u> </p>\n")
	fmt.Fprintf(loc, "\t\t<p> * X-axis: %.3f units </p>\n", r.GridScale("X", xgcount))
	fmt.Fprintf(loc, "\t\t<p> * Y-axis: %.3f units </p>\n", r.GridScale("Y", ygcount))
	fmt.Fprintf(loc, "\t\t<p> <u> AXIS LABLE: </u> </p>\n")
	fmt.Fprintf(loc, "\t\t<p> * X-axis: %s </p>\n", r.Xlabel)
	fmt.Fprintf(loc, "\t\t<p> * Y-axis: %s </P>\n", r.Ylabel)
	fmt.Fprintf(loc, "\t\t<p> <u> STATS: </u> </p>\n")
	fmt.Fprintf(loc, "\t\t<p> * R value: %.2f </p>\n", r.Rval())
	fmt.Fprintf(loc, "\t\t<p> * RegCoeff (Y on X): %.2f </p>\n", r.Byx())
	fmt.Fprintf(loc, "\t\t<p> * RegCoeff (X on Y): %.2f </p>\n", r.Bxy())
	fmt.Fprintf(loc, "\t\t<p> * Confidence: %.2f%% </p>\n", r.Conf())
	fmt.Fprintf(loc, "\t\t<p> * Covariance: %.2f </p>\n", r.Covariance())
	fmt.Fprintf(loc, "\t\t<p> * StdDev (X): %.1f </p>\n", r.SdX())
	fmt.Fprintf(loc, "\t\t<p> * StdDev (Y): %.1f </p>\n", r.SdY())
	fmt.Fprintf(loc, "\t</div>\n")
	fmt.Fprintf(loc, "</body>\n")
	fmt.Fprintf(loc, "</html>")
}