-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handling on click feature for stock buttons
- Loading branch information
1 parent
38f94ce
commit 0ff0e27
Showing
3 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Line } from 'react-chartjs-2'; | ||
import 'chart.js/auto'; | ||
|
||
function StockGraph({ ticker }) { // Use destructuring to get the ticker prop | ||
const [stockData, setStockData] = useState([]); | ||
|
||
useEffect(() => { | ||
// Ensure the ticker value is included in the fetch URL | ||
fetch(`http://localhost:8000/api/graph_stock/${ticker}/`) | ||
.then(response => response.json()) | ||
.then(data => setStockData(data)) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
}, [ticker]); // Include ticker in the dependency array | ||
|
||
const chartData = { | ||
labels: stockData.map(data => data.date), | ||
datasets: [{ | ||
label: `${ticker} Stock Value`, // Dynamic label based on ticker | ||
data: stockData.map(data => data.value), | ||
fill: false, | ||
borderColor: 'rgb(75, 192, 192)', | ||
tension: 0.1 | ||
}] | ||
}; | ||
|
||
return <Line data={chartData} />; | ||
} | ||
|
||
export default StockGraph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters