|
|
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Highsoft.Web.Mvc.Charts;
namespace MVC_Demo.Areas.Highcharts.Controllers.Shared
{
public partial class SharedController : Controller
{
public ActionResult SplineInverted()
{
List<double> xValues = new List<double> { 0, 10, 20, 30, 40, 50, 60, 70, 80 };
List<double> yValues = new List<double> { 15, -50, -56.5, -46.5, -22.1, -2.5, -27.7, -55.7, -76.5 };
List<SplineSeriesData> tempData = new List<SplineSeriesData>();
for (int i = 0; i < xValues.Count; i++)
{
SplineSeriesData seriesData = new SplineSeriesData { X = xValues[i], Y = yValues[i] };
tempData.Add(seriesData);
}
ViewData["tempData"] = tempData;
return View();
}
}
}
Controller Code
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
@using Highsoft.Web.Mvc.Charts
@using Highsoft.Web.Mvc.Charts.Rendering
<script type="text/javascript">
function formatXLabels() {
return this.value + 'km';
}
function formatYLabels() {
return this.value + '°';
}
</script>
@{ var chartOptions =
new Highcharts
{
Chart = new Highsoft.Web.Mvc.Charts.Chart
{
Inverted = true,
Type = ChartType.Spline
},
Title = new Title
{
Text = "Atmosphere Temperature by Altitude"
},
Subtitle = new Subtitle
{
Text = "According to the Standard Atmosphere Model"
},
XAxis = new List<XAxis>
{
new XAxis
{
//Reversed = true,
Title = new XAxisTitle
{
Text = "Altitude"
},
Labels = new XAxisLabels
{
Formatter = "formatXLabels"
},
MaxPadding = 0.05,
ShowLastLabel = true
}
},
YAxis = new List<YAxis>
{
new YAxis
{
//Reversed = true,
Title = new YAxisTitle
{
Text = "Temperature"
},
Labels = new YAxisLabels
{
Formatter = "formatYLabels"
},
LineWidth = 2
}
},
Legend = new Legend
{
Enabled = false
},
Tooltip = new Tooltip
{
HeaderFormat = "<b>{series.name}</b><br/>",
PointFormat = "{point.x} km: {point.y}°C"
},
PlotOptions = new PlotOptions
{
Spline = new PlotOptionsSpline
{
Marker = new PlotOptionsSplineMarker
{
Enabled = true
}
}
},
Series = new List<Series>
{
new SplineSeries
{
Name = "Temperature",
Data = ViewData["tempData"] as List<SplineSeriesData>
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())