Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Fixes stock price history widget

Alicia Sykesc4d0ade2764e
diff --git a/docs/widgets.md b/docs/widgets.md
index fb912dfe..3400f738 100644
--- a/docs/widgets.md
+++ b/docs/widgets.md
@@ -1067,7 +1067,7 @@ Shows real-time tube status of the London Underground. All fields are optional.
### Stock Price History
-Shows recent price history for a given publicly-traded stock or share
+Shows recent daily price history for a given publicly-traded stock or share
<p align="center"><img width="400" src="https://pixelflare.cc/alicia/dashy/stocks" /></p>
@@ -1078,6 +1078,7 @@ Shows recent price history for a given publicly-traded stock or share
**`apiKey`** | `string` | Required | API key for [Alpha Vantage](https://www.alphavantage.co/), you can get a free API key [here](https://www.alphavantage.co/support/#api-key)
**`stock`** | `string` | Required | The stock symbol for the asset to fetch data for
**`priceTime`** | `string` | _Optional_ | The time to fetch price for. Can be `high`, `low`, `open` or `close`. Defaults to `high`
+**`dataPoints`** | `number` | _Optional_ | Number of days to plot, between `6` and `99`. Defaults to `30`
**`chartColor`** | `string` | _Optional_ | Color of the chart value. Defaults to `--widget-text-color` which inherits dashboard primary color
**`chartHeight`** | `number` | _Optional_ | The height of rendered chart in px. Defaults to `300`
@@ -1094,7 +1095,7 @@ Shows recent price history for a given publicly-traded stock or share
- **CORS**: 🟒 Enabled
- **Auth**: πŸ”΄ Required
-- **Price**: 🟠 Free plan (up to 500 requests/day)
+- **Price**: 🟠 Free plan (up to 25 requests/day)
- **Host**: Managed Instance Only
- **Privacy**: _See [AlphaVantage Privacy Policy](https://www.alphavantage.co/privacy/)_
diff --git a/src/components/Widgets/StockPriceChart.vue b/src/components/Widgets/StockPriceChart.vue
index 029c8960..1fed24eb 100644
--- a/src/components/Widgets/StockPriceChart.vue
+++ b/src/components/Widgets/StockPriceChart.vue
@@ -23,24 +23,19 @@ export default {
stock() {
return this.options.stock;
},
- /* The time interval between data points, in minutes */
- interval() {
- return `${(this.options.interval || 30)}min`;
- },
/* The users API key for AlphaVantage */
apiKey() {
return this.parseAsEnvVar(this.options.apiKey);
},
/* The formatted GET request API endpoint to fetch stock data from */
endpoint() {
- const func = 'TIME_SERIES_INTRADAY';
- return `${widgetApiEndpoints.stockPriceChart}?function=${func}`
- + `&symbol=${this.stock}&interval=${this.interval}&apikey=${this.apiKey}`;
+ return `${widgetApiEndpoints.stockPriceChart}?function=TIME_SERIES_DAILY`
+ + `&symbol=${this.stock}&apikey=${this.apiKey}`;
},
/* The number of data points to render on the chart */
dataPoints() {
const userChoice = this.options.dataPoints;
- if (typeof usersChoice === 'number' && userChoice < 100 && userChoice > 5) {
+ if (typeof userChoice === 'number' && userChoice < 100 && userChoice > 5) {
return userChoice;
}
return 30;
@@ -85,14 +80,14 @@ export default {
},
});
},
- /* Make GET request to CoinGecko API endpoint */
+ /* Make GET request to AlphaVantage API endpoint */
fetchData() {
request.get(this.endpoint)
.then((response) => {
- if (response.data.note) {
- this.error('API Error', response.data.Note);
- } else if (response.data['Error Message']) {
- this.error('API Error', response.data['Error Message']);
+ const apiError = response.data['Error Message']
+ || response.data.Information || response.data.Note;
+ if (apiError) {
+ this.error('API Error', apiError);
} else {
this.processData(response.data);
}
@@ -108,11 +103,15 @@ export default {
* To improve efficiency, only a certain amount of data points are plotted
*/
processData(data) {
+ const rawMarketData = data['Time Series (Daily)'];
+ if (!rawMarketData) {
+ this.error('Unexpected stock price response format');
+ return;
+ }
const priceLabels = [];
const priceValues = [];
- const dataKey = `Time Series (${this.interval})`;
- const rawMarketData = data[dataKey];
- const interval = Math.round(Object.keys(rawMarketData).length / this.dataPoints);
+ const numPoints = Object.keys(rawMarketData).length;
+ const interval = Math.max(1, Math.round(numPoints / this.dataPoints));
Object.keys(rawMarketData).forEach((timeGroup, index) => {
if (index % interval === 0) {
priceLabels.push(this.formatDate(timeGroup));
@@ -133,11 +132,11 @@ export default {
renderChart() {
this.chartDom = this.generateChart();
},
- /* Format the date for a given time stamp, also include time if required */
+ /* Format the date for a given time stamp */
formatDate(timestamp) {
const localFormat = navigator.language;
const dateFormat = { weekday: 'short', day: 'numeric', month: 'short' };
- return new Date(timestamp).toLocaleDateString(localFormat, dateFormat);
+ return new Date(`${timestamp}T00:00:00`).toLocaleDateString(localFormat, dateFormat);
},
/* Format the price, rounding to given number of decimal places */
formatPrice(priceStr) {