Experiment_8_record_format
Experiment_8_record_format
Create a service in react that fetches the weather information from open weathermap.org and the
display the current and historical weather information using graphical representation using chart.js
Aim:
To create a React service that fetches weather information from openweathermap.org and displays
current and historical weather data using Chart.js.
Objective:
Procedure:
Step 1:
Install Node.js from https://nodejs.org/en.
Step 2:
Step 3:
Open the terminal in VS Code and install react by typing “npx create-react-app weather-info”.
Step 4:
Step 5:
Remove the code in App.js and write the corresponding code and create separate components.
Step 6:
Register all the required components (like scales, elements, plugins) by typing
“Chart.register(...registerables)” in the functional component.
Step 6:
Open the terminal and run the program by typing “npm start”.
Code Snippet:
App.js:
import './App.css';
function App() {
return (
<div className="App">
<WeatherInfo/>
</div>
);
WeatherInfo.js :
let [city,setCity]=useState('')
Chart.register(...registerables);
event.preventDefault()
console.log("hi")
if(data!== null) {
let [time,temp]=weatherParameters(data)
displayData(time,temp)
}
}
function displayData(time,temp) {
new Chart(ctx, {
type: 'line',
data: {
labels: time,
datasets: [{
label: 'temperature',
data: temp,
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: false
});
console.log(temp)
return [time,temp]
console.log(city)
try {
if (data.cod == '200') {
return data
else {
return null
} catch (error) {
console.error("error",error)
return null
setCity(e.target.value)
return (
<div>
<h1>Weather Report</h1>
<form>
</form>
<canvas id='myChart'></canvas>
</div>
Conclusion:
Successfully implemented a React service to fetch weather data and displayed using Chart.js.