1+ {% extends "base.html" %}
2+
3+ {% block extra_head %}
4+ < script src ="https://cdn.jsdelivr.net/npm/chart.js "> </ script >
5+ {% endblock %}
6+
7+ {% block content %}
8+ < div class ="max-w-5xl mx-auto my-5 p-2 bg-base-200 shadow-lg ">
9+ < div class ="p-2 lg:p-5 rounded-xl border-2 border-solid border-primary ">
10+ < h1 class ="text-4xl font-bold text-center my-5 "> Support Vector Regression</ h1 >
11+
12+
13+ < form method ="POST " class ="space-y-4 " id ="svmForm ">
14+ {% csrf_token %}
15+ < div class ="px-4 ">
16+ < label class ="block text-lg font-semibold mb-2 "> Enter values:</ label >
17+ < div class ="flex flex-col gap-2 mb-4 ">
18+ < div class ="flex gap-2 ">
19+ < input type ="number " id ="tempInput " name ="temp_value " class ="input input-bordered w-full max-w-xs " placeholder ="Temperature (°C) " step ="any ">
20+ < input type ="number " id ="humInput " name ="hum_value " class ="input input-bordered w-full max-w-xs " placeholder ="Humidity (%) " step ="any " min ="0 " max ="100 ">
21+ </ div >
22+ < button type ="button " onclick ="addValues() " class ="btn btn-secondary w-full max-w-xs "> Add</ button >
23+ </ div >
24+ < div id ="valuesList " class ="mb-4 ">
25+ < p > < strong > Added Values:</ strong > < span id ="displayValues "> None</ span > </ p >
26+ </ div >
27+ < input type ="hidden " name ="values " id ="valuesInput ">
28+ </ div >
29+
30+ < button type ="submit " class ="btn btn-primary w-full "> Submit</ button >
31+ </ form >
32+
33+ {% if error %}
34+ < div class ="px-4 py-4 ">
35+ < div class ="alert alert-error ">
36+ < p > {{ error }}</ p >
37+ </ div >
38+ </ div >
39+ {% endif %}
40+
41+ {% if processed_values %}
42+ < div class ="px-4 py-4 ">
43+ < div class ="card bg-base-100 shadow-xl ">
44+ < div class ="card-body ">
45+ < h2 class ="card-title text-2xl "> Results</ h2 >
46+ < div class ="grid gap-4 ">
47+ <!-- Predicted Values -->
48+ < div class ="stats shadow ">
49+ < div class ="stat ">
50+ < div class ="stat-title "> Predicted Temperature</ div >
51+ < div class ="stat-value text-primary "> {{ predicted_value.temp }}°C</ div >
52+ </ div >
53+ < div class ="stat ">
54+ < div class ="stat-title "> Predicted Humidity</ div >
55+ < div class ="stat-value text-secondary "> {{ predicted_value.hum }}%</ div >
56+ </ div >
57+ </ div >
58+ <!-- Input Values -->
59+ < div >
60+ < h3 class ="text-lg font-semibold mb-2 "> Input Values</ h3 >
61+ < div class ="overflow-x-auto ">
62+ < table class ="table table-zebra w-full ">
63+ < thead >
64+ < tr >
65+ < th > #</ th >
66+ < th > Temperature (°C)</ th >
67+ < th > Humidity (%)</ th >
68+ </ tr >
69+ </ thead >
70+ < tbody >
71+ {% for point in processed_values %}
72+ < tr >
73+ < td > {{ point.index }}</ td >
74+ < td > {{ point.temp }}</ td >
75+ < td > {{ point.hum }}</ td >
76+ </ tr >
77+ {% endfor %}
78+ </ tbody >
79+ </ table >
80+ </ div >
81+ </ div >
82+ </ div >
83+ </ div >
84+ </ div >
85+ </ div >
86+ {% endif %}
87+
88+ {% if show_chart %}
89+ < div class ="px-4 py-4 ">
90+ < div class ="card bg-base-100 shadow-xl ">
91+ < div class ="card-body ">
92+ < h2 class ="card-title "> Support Vector Regression Chart</ h2 >
93+ < canvas id ="svmChart "> </ canvas >
94+ </ div >
95+ </ div >
96+ </ div >
97+ {% endif %}
98+ </ div >
99+ </ div >
100+ {% endblock %}
101+
102+ {% block scripts %}
103+ < script >
104+ let values = [ ] ;
105+
106+ function addValues ( ) {
107+ const tempInput = document . getElementById ( 'tempInput' ) ;
108+ const humInput = document . getElementById ( 'humInput' ) ;
109+ const temp = parseFloat ( tempInput . value ) ;
110+ const hum = parseFloat ( humInput . value ) ;
111+
112+ if ( ! isNaN ( temp ) && ! isNaN ( hum ) ) {
113+ values . push ( [ temp , hum ] ) ;
114+ updateValuesDisplay ( ) ;
115+ tempInput . value = '' ; // Clear input fields
116+ humInput . value = '' ;
117+ } else {
118+ alert ( 'Please enter valid numbers for both temperature and humidity' ) ;
119+ }
120+ }
121+
122+ function updateValuesDisplay ( ) {
123+ const display = document . getElementById ( 'displayValues' ) ;
124+ const valuesInput = document . getElementById ( 'valuesInput' ) ;
125+
126+ if ( values . length > 0 ) {
127+ display . textContent = values . map ( v => `(${ v [ 0 ] } °C, ${ v [ 1 ] } %)` ) . join ( ', ' ) ;
128+ valuesInput . value = JSON . stringify ( values ) ; // Store values in hidden input
129+ } else {
130+ display . textContent = 'None' ;
131+ valuesInput . value = '' ;
132+ }
133+ }
134+
135+ // Update hidden input before form submission
136+ document . getElementById ( 'svmForm' ) . addEventListener ( 'submit' , function ( ) {
137+ document . getElementById ( 'valuesInput' ) . value = JSON . stringify ( values ) ;
138+ } ) ;
139+
140+
141+ { % if show_chart % }
142+ // Prepare data for Chart.js
143+ const chartData = { { chart_data| safe } } ;
144+ const ctx = document . getElementById ( 'svmChart' ) . getContext ( '2d' ) ;
145+
146+ // Prepare datasets
147+ const tempData = chartData . historical . map ( point => ( {
148+ x : point . index ,
149+ y : point . temp
150+ } ) ) . concat ( [ {
151+ x : chartData . predicted . index ,
152+ y : chartData . predicted . temp
153+ } ] ) ;
154+
155+ const humData = chartData . historical . map ( point => ( {
156+ x : point . index ,
157+ y : point . hum
158+ } ) ) . concat ( [ {
159+ x : chartData . predicted . index ,
160+ y : chartData . predicted . hum
161+ } ] ) ;
162+
163+ // Render line chart
164+ new Chart ( ctx , {
165+ type : 'line' ,
166+ data : {
167+ datasets : [
168+ {
169+ label : 'Temperature (°C)' ,
170+ data : tempData ,
171+ borderColor : 'rgba(255, 99, 132, 1)' ,
172+ backgroundColor : 'rgba(255, 99, 132, 0.2)' ,
173+ fill : false ,
174+ yAxisID : 'y-temp' ,
175+ pointRadius : ( context ) => context . dataIndex === tempData . length - 1 ? 8 : 5 ,
176+ pointBackgroundColor : ( context ) => context . dataIndex === tempData . length - 1 ? 'rgba(255, 99, 132, 1)' : 'rgba(255, 99, 132, 0.5)' ,
177+ pointBorderColor : 'rgba(255, 99, 132, 1)' ,
178+ tension : 0.1
179+ } ,
180+ {
181+ label : 'Humidity (%)' ,
182+ data : humData ,
183+ borderColor : 'rgba(54, 162, 235, 1)' ,
184+ backgroundColor : 'rgba(54, 162, 235, 0.2)' ,
185+ fill : false ,
186+ yAxisID : 'y-hum' ,
187+ pointRadius : ( context ) => context . dataIndex === humData . length - 1 ? 8 : 5 ,
188+ pointBackgroundColor : ( context ) => context . dataIndex === humData . length - 1 ? 'rgba(54, 162, 235, 1)' : 'rgba(54, 162, 235, 0.5)' ,
189+ pointBorderColor : 'rgba(54, 162, 235, 1)' ,
190+ tension : 0.1
191+ }
192+ ]
193+ } ,
194+ options : {
195+ responsive : true ,
196+ scales : {
197+ x : {
198+ title : {
199+ display : true ,
200+ text : 'Time Step'
201+ } ,
202+ type : 'linear' ,
203+ ticks : {
204+ stepSize : 1
205+ }
206+ } ,
207+ 'y-temp' : {
208+ type : 'linear' ,
209+ display : true ,
210+ position : 'left' ,
211+ title : {
212+ display : true ,
213+ text : 'Temperature (°C)'
214+ } ,
215+ grid : {
216+ drawOnChartArea : true
217+ }
218+ } ,
219+ 'y-hum' : {
220+ type : 'linear' ,
221+ display : true ,
222+ position : 'right' ,
223+ title : {
224+ display : true ,
225+ text : 'Humidity (%)'
226+ } ,
227+ grid : {
228+ drawOnChartArea : false
229+ } ,
230+ min : 0 ,
231+ max : 100
232+ }
233+ } ,
234+ plugins : {
235+ legend : {
236+ display : true
237+ } ,
238+ tooltip : {
239+ callbacks : {
240+ label : function ( context ) {
241+ const isPredicted = context . dataIndex === context . dataset . data . length - 1 ;
242+ const label = context . dataset . label ;
243+ const value = context . parsed . y ;
244+ return `${ label } : ${ value } ${ label . includes ( 'Temperature' ) ? '°C' : '%' } ${ isPredicted ? ' (Predicted)' : '' } ` ;
245+ }
246+ }
247+ }
248+ }
249+ }
250+ } ) ;
251+ { % endif % }
252+ </ script >
253+ {% endblock %}
0 commit comments