1

I got an project in dotnet and i'm trying to use GoogleCharts, but it's not working.

The project is on .net mvc

Here is my code:

Views->Sensor->Chart.cshtml

@{
    ViewData["Title"] = "Chart";
} 

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<div id="chart_div" style="width: 900px; height: 500px;"></div>

<script type="text/javascript">  

    google.charts.load('current', {packages: ['corechart', 'bar']});  
    google.charts.setOnLoadCallback(LoadData);

    function LoadData() {  
        $.ajax({  
            url: '@Url.Action("ChartJson","Sensor")',  
            dataType: "json",  
            type: "GET",  
            error: function(xhr, status, error) {  
                var err = eval("(" + xhr.responseText + ")");  
                toastr.error(err.message);  
            },  
            success: function(data) {  
                CreateChart(data);  
                return false;  
            }  
        });
        return false;  
    }

     function CreateChart(data) {  
        var dataArray = [  
            ['Tag', 'Average value'] 
        ];  
        $.each(data, function(i, item) {  
            dataArray.push([item.tag, item.value]);  
        });  
        var data = google.visualization.arrayToDataTable(dataArray);  
        var options = {  
            title: 'Sensor data per coutry and region',  
            chartArea: {  
                width: '50%'
            },  
            colors: ['#b0120a', '#ffab91'],  
            hAxis: {  
                title: 'Arithmetic average',  
                minValue: 0  
            },  
            vAxis: {  
                title: 'Tag'
            }  
        };  
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));  
        chart.draw(data, options);  
        return false;  
    }

</script>

This view is loaded on the url, but there is no graph on it. I thied to debug the controller class, but the ChartJson() method is never called.

This is the two methods on the controller for the charts draw routine:

Controllers->SensorController.cs

public IActionResult Chart()
{
    return View();
}

public JsonResult ChartJson()
{
    var sensors = from s in _context.Sensor select s;

    var sensorsList = sensors.ToList();

    Console.WriteLine("Entrou");

    List<ChartData> listChart = new List<ChartData>();

    foreach (Sensor s in sensorsList)
    {
        int value;
        bool isNumeric = int.TryParse(s.value, out value);

        if (!isNumeric)
            continue;

        string tag = s.country + "." + s.region;

        for (int i = 0; i < listChart.Count; i++)
        {
            if (listChart[i].tag.Equals(tag))
            {
                listChart[i].AddValue(value);
                break;
            }

            if (i == listChart.Count - 1)
            {
                listChart.Add(new ChartData(tag));
                break;
            }
        }
    }

    return Json(listChart);
}

Can anyone help me? Thanks.

Airn5475
  • 2,452
  • 29
  • 51
BLGO 94
  • 51
  • 3

0 Answers0