简体   繁体   English

Plotly ASP.NET Core MVC Ajax Json 调用

[英]Plotly ASP.NET Core MVC Ajax Json Call

Hello I am using to Plotly JavaScript Open Source Graphing Library on my .NET CORE MVC project.您好,我在我的 .NET CORE MVC 项目中使用了 Plotly JavaScript 开源图形库。 I don't know how can i use my JSON data at my bar graph's axes.我不知道如何在条形图的轴上使用我的 JSON 数据。 Here i used to sample default data to use bar graph.在这里,我曾经对默认数据进行采样以使用条形图。

Index.cshtml索引.cshtml

<div id="locationDiv"></div>

<script>
    var trace1 = {
        type: 'bar',
        x: ["İstanbul", "Ankara", "İzmir", "Antalya"],
        y: [50, 40, 20, 15],
        marker: {
            color: '#C8A2C8',
            line: {
                width: 2.5
            }
        }
    };

    var locationData = [trace1];

    var locationLayout = {
        title: 'Locations',
        yaxis: {
            title: 'number'
        },
        xaxis: {
            title: 'location name'
        },
        font: { size: 18 }
    };

    Plotly.newPlot('locationDiv', locationData, locationLayout, { responsive: true });
</script>

here output view this code这里输出查看此代码

And I try to get x-axis values from my database.我尝试从我的数据库中获取 x 轴值。

Index.cshtml索引.cshtml

<div id="locationDiv"></div>

@section scripts {
    <script>
        $.ajax({
            type: "POST",
            url: "/Line/Dashboards",
            dataSrc: "",
            success: function (response) {
                successFunc(response);
            }
        });

        function successFunc(jsondata) {
            var trace1 = {
                type: 'bar',
                json: jsondata, 
                x: "locationName", //Default value:= x: ["İstanbul", "Ankara", "İzmir"],
//POSTMAN result:= [{"locationName": "İstanbul"},{"locationName": "Ankara"},{"locationName": "İzmir"}]
                y: [50, 40, 20],
                marker: {
                    color: '#3399FF',
                    line: {
                        width: 2.5
                    }
                }
            };
            var locationData = [trace1];

            var locationLayout = {
                yaxis: {
                    title: 'number'
                },
                xaxis: {
                    title: 'location name'
                },
                font: { size: 18 }
            };

            Plotly.newPlot('locationDiv', locationData, locationLayout, { responsive: true });
        }
</script>
}

LineController.cs线控器.cs

[HttpPost]
public IActionResult Dashboards()
{
    var dashboardDataList = _lineService.GetLineList();
    var totalRecords = dashboardDataList.Count();

    var DashboardDataResult = from line in dashboardDataList
                              select new
                             {
                                 LocationName = line.Location.LocationName                                         
                             };

    DashboardDataResult = DashboardDataResult.Distinct();

    return Json(DashboardDataResult);
}

My path is true.我的路是真的。 I tried it with Postman and the result was我用 Postman 试了一下,结果是

[{"locationName": "İstanbul"},{"locationName": "Ankara"},{"locationName": "İzmir"}]

Try the following method :尝试以下方法:

function successFunc(jsondata) {
        function unpack(rows, key) {
            return rows.map(function(row) { return row[key]; });
        }

        var x = unpack(jsondata, 'locationName');
        var trace1 = {
            type: 'bar',
            json: jsondata,
            x: x, 
            y: [50, 40, 20,15],
            marker: {
                color: '#3399FF',
                line: {
                    width: 2.5
                }
            }
        };
        var locationData = [trace1];

        var locationLayout = {
            title:'Locations',
            yaxis: {
                title: 'number'
            },
            xaxis: {
                title: 'location name'
            },
            font: { size: 18 }
        };

        Plotly.newPlot('locationDiv', locationData, locationLayout, { responsive: true });
    }

Reference: https://plot.ly/javascript/axes/#set-and-style-axes-title-labels-and-ticks参考: https : //plot.ly/javascript/axes/#set-and-style-axes-title-labels-and-ticks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Core MVC中将XML转换为JSON - XML to JSON in ASP.NET Core MVC JSON Call from view to controller and show JSON object data in view in ASP.NET Core 6 MVC - JSON Call from view to controller and show JSON object data in view in ASP.NET Core 6 MVC 在asp.net core 2 MVC中通过ajax在视图中显示来自控制器的json字符串 - displaying json strings from controller in view over ajax in asp.net core 2 MVC Ajax-Call从Asp.Net MVC中的控制器(返回IList)获取JSON结果总是错误 - Ajax-Call to get JSON Result from Controller (returning IList) in Asp.Net MVC always errors Json Length太长时,对ASP.NET MVC控制器的Ajax调用返回404 - Ajax call to ASP.NET MVC Controller Returns 404 when Json Length is too Long 对Asp.Net MVC JsonResult的JQuery.Ajax调用给出12031错误,Json Web服务工作正常 - JQuery.Ajax call to Asp.Net MVC JsonResult gives 12031 error, Json webservice works fine ASP.Net MVC(1)-如何传递JSON以查看但不能作为单独的Ajax调用 - ASP.Net MVC (1) - how can I pass JSON to view but not as separate ajax call 在ASP.Net Core MVC中读取JSON post数据 - Read JSON post data in ASP.Net Core MVC 设置Asp.Net核心MVC Json选项 - Set Asp.Net Core MVC Json options 将 JSON 字符串发送到 ASP.NET Core MVC 控制器 - Sending JSON string to ASP.NET Core MVC Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM