[英]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.