'Unable to plot graph and send it to frontend
I am trying to make a ASP.NET core razor application, which is to display live stock market graphs. For that I am using the free API by Alpha Vantage and for plotting the graph, im using Plotly.NET. Now I managed to get the graph plotted by using fixed variable (for the company, eg APPL, IBM), but its like, the output is not what I want. Ideally, I want the user to input his desired company name's code, then to plot the graph, but I have problem, both in the Naming part and the Plotting part.
Code: Stocks.cshtml
@page
@model Hexed_Relics.Pages.StocksModel
@{
ViewData["Title"] = "Stonks";
}
<h1 class="HCenterP">Stocks Page</h1>
<body>
<div class="center">
<p>
Enter Your desired stock's code to view
</p>
<form action="GetF" method="post">
<input type="text" name="Code" />
</form>
</div>
</body>
Stocks.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json.Serialization;
using System.Text.Json;
using System.Web;
using Newtonsoft.Json;
using Plotly.NET.Interactive;
using Plotly.NET;
namespace Hexed_Relics.Pages
{
public class StocksModel : PageModel
{
[BindProperty]
public Inputs Input { get; set; }
public class Inputs
{
public string Code { get; set; }
}
[HttpPost]
public IActionResult GetF(string Code)
{
string QUERY_URL = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=" + Code + "&interval=5min&apikey=demo";
Uri queryUri = new Uri(QUERY_URL);
using (WebClient client = new WebClient())
{
// if using .NET Core (System.Text.Json)
// using .NET Core libraries to parse JSON is more complicated. For an informative blog post
dynamic json_data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(client.DownloadString(queryUri));
List<string> XValues = new List<string>();
List<float> YValues = new List<float>();
foreach (var key in json_data["Time Series (5min)"])
{
string X = key.ToString();
X = X.Substring(12, 8);
XValues.Add(X);
foreach(var Key in key.Children()["1. open"])
{
YValues.Add(float.Parse(Key.ToString()));
}
}
Chart2D.Chart.Point<string, float, string>(XValues.ToArray(), YValues.ToArray()).Show();
}
}
}
}
Naming problem: I Tried submitting a code from the test box that is created on the page, and expected it to go through the GetF action, creating the graph, but instead, it is trying to go to GetF page, which does not exist. How do I resolve that?
Plotting problem: So from the code
Chart2D.Chart.Point<string, float, string>(XValues.ToArray(), YValues.ToArray()).Show();
creates the graph and display's it, but it is doing so, by creating a temporary file and creating the graph there. I am unable to pass the Code input from the user either. I am not sure where I am lacking.
Any assistance would be amazing!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
