|
|
Controller Code
using Highsoft.Web.Mvc.Stocks;
using MVC_Demo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace MVC_Demo.Areas.Highstock.Controllers.Shared
{
public partial class SharedController : Controller
{
public ActionResult FlagsGeneral()
{
List<LineSeriesData> currencyData = new List<LineSeriesData>();
using (var db = new HighstockDataEntities())
{
foreach (Flag flag in db.Flags)
{
currencyData.Add(new LineSeriesData
{
Y = Convert.ToDouble(flag.Value),
X = Convert.ToDouble(flag.Date)
}
);
}
}
ViewBag.CurrencyData = currencyData.OrderBy(o => o.X).ToList();
List<FlagsSeriesData> flagsData = new List<FlagsSeriesData>();
flagsData.Add(new FlagsSeriesData
{
X = DateToUTC(new DateTime(2015, 5, 8)),
Title = "c",
Text = "Stocks fall on Greece, rate concerns; US dollar dips"
}
);
flagsData.Add(new FlagsSeriesData
{
X = DateToUTC(new DateTime(2015, 5, 12)),
Title = "d",
Text = "Zimbabwe ditches 'worthless' currency for the US dollar "
}
);
flagsData.Add(new FlagsSeriesData
{
X = DateToUTC(new DateTime(2015, 5, 19)),
Title = "e",
Text = "US Dollar Declines Over the Week on Rate Timeline"
}
);
flagsData.Add(new FlagsSeriesData
{
X = DateToUTC(new DateTime(2015, 5, 26)),
Title = "e",
Text = "US Dollar Declines Over the Week on Rate Timeline"
}
);
ViewBag.FlagsData = flagsData;
return View(ViewBag);
}
private void FlagsGeneral_JsonDataToDatabase()
{
string json;
using (WebClient wc = new WebClient())
{
json = wc.DownloadString("http://www.highcharts.com/samples/data/jsonp.php?filename=usdeur.json&callback=?");
}
json = json.Substring(json.IndexOf('[') + 1);
json = json.Substring(json.IndexOf('[') + 1);
using (var db = new HighstockDataEntities())
{
while (true)
{
if (json.IndexOf('[') == -1)
break;
string entity = json.Substring(0, json.IndexOf(']'));
string[] values = entity.Split(',');
string year = values[0].Substring( values[0].IndexOf("(") + 1 , 4);
string month = values[1];
string day = values[2].Substring(0, values[2].IndexOf(")"));
string value = values[3];
try
{
DateTime date = new DateTime(Convert.ToInt16(year), Convert.ToInt16(month), Convert.ToInt16(day));
db.Flags.Add(
new Flag
{
Date = DateToUTC(date),
Value = Convert.ToDouble(value)
}
);
}
catch (Exception)
{
}
json = json.Substring(json.IndexOf('[') + 1);
}
db.SaveChanges();
}
}
public double DateToUTC(DateTime theDate)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = theDate.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
}
}
Controller Code
@using Highsoft.Web.Mvc.Stocks
@using Highsoft.Web.Mvc.Stocks.Rendering
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
@{ var chartOptions =
new Highstock
{
RangeSelector = new RangeSelector
{
Selected = 1
},
Title = new Title
{
Text = "USD to EUR exchange rate"
},
Tooltip = new Tooltip
{
ValueDecimals = 4,
Shared = true
},
YAxis = new List<YAxis>
{
new YAxis
{
Title = new YAxisTitle
{
Text = "Exchange rate"
}
}
},
Series = new List<Series>
{
new LineSeries
{
Data = ViewBag.CurrencyData as List<LineSeriesData>,
Name = "USD to EUR",
Id = "currency",
Tooltip = new LineSeriesTooltip
{
ValueDecimals = 2
}
},
new FlagsSeries
{
Data = ViewBag.FlagsData as List<FlagsSeriesData>,
OnSeries = "currency",
Shape = "Circlepin"
}
},
PlotOptions = new PlotOptions
{
Series = new PlotOptionsSeries
{
TurboThreshold = 5000
}
}
};
chartOptions.ID = "chart";
var renderer = new HighstockRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())