|
|
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 DynamicClickToAdd()
{
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 addPoint(e) {
// find the clicked values and the series
var x = e.xAxis[0].value,
y = e.yAxis[0].value,
series = this.series[0];
// Add it
series.addPoint([x, y]);
}
function pointClick() {
if (this.series.data.length > 1) {
this.remove();
}
}
</script>
@{ var chartOptions =
new Highcharts
{
Chart = new Highsoft.Web.Mvc.Charts.Chart
{
Events = new ChartEvents
{
Click = "addPoint"
}
},
Title = new Title
{
Text = "User Applied Data"
},
Subtitle = new Subtitle
{
Text = "Click the plot area to add a point. Click a point to remove it."
},
XAxis = new List<XAxis>
{
new XAxis
{
GridLineWidth = 1,
MinPadding = 0.2,
MaxPadding = 0.2,
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Title = new YAxisTitle
{
Text = "Value"
},
MinPadding = 0.2,
PlotLines = new List<YAxisPlotLines>(){
new YAxisPlotLines
{
Value = 0,
Width = 1,
Color = "#808080"
}
}
}
},
Legend = new Legend
{
Enabled = false
},
Exporting = new Exporting
{
Enabled = false
},
PlotOptions = new PlotOptions
{
Series = new PlotOptionsSeries
{
LineWidth = 1,
Point = new PlotOptionsSeriesPoint
{
Events = new PlotOptionsSeriesPointEvents
{
Click = "pointClick"
}
}
}
},
Series = new List<Series>
{
new ScatterSeries
{
Data = new List<ScatterSeriesData>()
{
new ScatterSeriesData { X = 20, Y = 20 },
new ScatterSeriesData { X = 80, Y = 80 }
}
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())