codice:
<%@ Page Language="VB" Strict="false" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="javascript" type="text/javascript">
// <!CDATA[
function $()
{
var elements = new Array();
for (var i = 0; i < arguments.length; i++)
{
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function Button1_onclick()
{
var s = "&x1=" + $("x1").value
+ "&x2=" + $("x2").value
+ "&x3=" + $("x3").value
+ "&x4=" + $("x4").value
+ "&x5=" + $("x5").value
+ "&y1=" + $("y1").value
+ "&y2=" + $("y2").value
+ "&y3=" + $("y3").value
+ "&y4=" + $("y4").value
+ "&y5=" + $("y5").value;
var v = document.getElementById("grafico");
v.style.display = "";
v.src = "grafico.aspx?grafico=1" + s;
}
// ]]>
</script>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
lbll.Text = "Calcolo della retta dei minimi quadrati "
Dim grafico As String = Me.Request.QueryString("grafico")
If grafico IsNot Nothing AndAlso grafico = "1" Then
creaGrafico()
End If
End Sub
Sub creaGrafico()
Dim valori(5, 5) As Double
Dim I, Sx, Sy, Sxy, Sx2, m, q, n As Double
Dim y01, y02 As Double
Dim objBitmap As New Bitmap(200, 200)
Dim objGraphic As Graphics = Graphics.FromImage(objBitmap)
Dim whiteBrush As New SolidBrush(Color.White)
Dim redpen As New Pen(Color.Red, 2)
Dim blackPen As New Pen(Color.Black, 2)
valori(1, 1) = Me.Request.QueryString("x1")
valori(1, 2) = Me.Request.QueryString("y1")
valori(2, 1) = Me.Request.QueryString("x2")
valori(2, 2) = Me.Request.QueryString("y2")
valori(3, 1) = Me.Request.QueryString("x3")
valori(3, 2) = Me.Request.QueryString("y3")
valori(4, 1) = Me.Request.QueryString("x4")
valori(4, 2) = Me.Request.QueryString("y4")
valori(5, 1) = Me.Request.QueryString("x5")
valori(5, 2) = Me.Request.QueryString("y5")
n = 5
Sx = 0
Sy = 0
Sx2 = 0
Sxy = 0
For I = 1 To 5
valori(I, 3) = valori(I, 1) * valori(I, 1)
valori(I, 4) = valori(I, 1) * valori(I, 2)
Sx = Sx + valori(I, 1)
Sy = Sy + valori(I, 2)
Sx2 = Sx2 + valori(I, 3)
Sxy = Sxy + valori(I, 4)
Next I
m = (n * Sxy - Sx * Sy) / (n * Sx2 - (Sx) * (Sx))
q = (Sx2 * Sy - Sxy * Sx) / (n * Sx ^ 2 - (Sx) * (Sx))
If q > 0 Then
lbl2.Text = "Risultato : Y=" & m & "X+" & q
Else
lbl2.Text = "Risultato : Y=" & m & "X" & q
End If
y01 = 195 - q
y02 = 195 - ((195 * m) + q)
objGraphic.FillRectangle(whiteBrush, 0, 0, 200, 200)
objGraphic.DrawLine(blackPen, New Point(0, 195), New Point(195, 195))
objGraphic.DrawLine(blackPen, New Point(5, 5), New Point(5, 200))
objGraphic.DrawLine(redpen, New Point(5, y01), New Point(195, y02))
img.ImgWriteText(objBitmap, lbl2.Text, Brushes.Red, New Font("arial", 12, FontStyle.Regular, GraphicsUnit.Pixel), 10, 0)
Response.ContentType = "image/gif"
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
Me.Response.End()
End Sub
</script>
<html>
<body>
<form id="Form1" runat="server">
<asp:Label ID="lbll" runat="server" ForeColor="#0000FF" Font-Bold="true" /></p>
x1:
<asp:TextBox ID="x1" Columns="10" runat="server" Text="1" />
y1:
<asp:TextBox ID="y1" Columns="10" runat="server" Text="2" />
x2:
<asp:TextBox ID="x2" Columns="10" runat="server" Text="3" />
y2:
<asp:TextBox ID="y2" Columns="10" runat="server" Text="4" />
x3:
<asp:TextBox ID="x3" Columns="10" runat="server" Text="5" />
y3:
<asp:TextBox ID="y3" Columns="10" runat="server" Text="6" />
x4:
<asp:TextBox ID="x4" Columns="10" runat="server" Text="7" />
y4:
<asp:TextBox ID="y4" Columns="10" runat="server" Text="8" />
x5:
<asp:TextBox ID="x5" Columns="10" runat="server" Text="9" />
y5:
<asp:TextBox ID="y5" Columns="10" runat="server" Text="10" />
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
<asp:Label ID="lbl2" runat="server" /></p>
[img]grafico.aspx?grafico=1[/img]
</form>
</body>
</html>
In sintesi, crei un <img. Quando imposti via javascript la proprietà src, viene fatta la richiesta della pagina inviando i parametri necessarii via get. La pagina legge i parametri e restituisce il grafico.