@{
    ViewBag.Title = "Charts using CSV & AJAX";
}

<div id="chartContainer" style="height: 400px; width: 100%;"></div>

<script type="text/javascript">
    $(document).ready(function () {

        var dataPoints = [];

        function getDataPointsFromCSV(csv) {
            var dataPoints = csvLines = points = [];
            csvLines = csv.split(/[\r?\n|\r|\n]+/);

            for (var i = 0; i < csvLines.length; i++)
                if (csvLines[i].length > 0) {
                    points = csvLines[i].split(",");
                    dataPoints.push({
                        x: parseFloat(points[0]),
                        y: parseFloat(points[1])
                    });
                }
            return dataPoints;
        }

        $.get("/data/datapoints?count=10&type=csv", function (data) {
            var chart = new CanvasJS.Chart("chartContainer", {
                animationEnabled: true,
                theme: "light2",//light1
                title: {
                    text: "CanvasJS Charts in ASP.Net MVC using CSV & AJAX",
                },
                data: [{
                    type: "line",
                    dataPoints: getDataPointsFromCSV(data)
                }]
            });

            chart.render();

        });

    });
</script>
