javascript - ASP.NET Core Razor 页面 - 获取要在 Chart.js 图表上显示的列表数据
问题描述
我有一个已填充的列表 (lstObjects),并试图让该数据显示在我的 Chartist.js 图表上。当我将 lstObjects 悬停在调试模式下时,我可以看到我的列表计数和对象类型。
在我的<script>
标签中,我目前有:
var chartOptions = {
showLines: true,
responsive: true,
maintainAspectRatio: false,
legend: {
display: false
},
tooltips: {
displayColors: false
},
scales: {
xAxes: [{
display: true,
ticks: {
fontColor: 'black',
padding: 20
},
gridLines: {
borderDash: [1, 5],
tickMarkLength: 0,
zeroLineBorderDash: [1, 5],
drawBorder: false,
padding: 5
}
}],
yAxes: [{
display: true,
ticks: {
fontColor: 'black',
padding: 20
},
gridLines: {
color: 'rgba(85, 85, 85, 0.5)',
borderDash: [1, 5],
tickMarkLength: 0,
zeroLineBorderDash: [1, 5],
drawBorder: false,
padding: 5
}
}]
}
};
// Total Transactions Chart
var achChart = new Chart(document.getElementById('myChart').getContext('2d'), {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'],
datasets: [{
label: '',
lineTension: 0,
fill: false,
backgroundColor: 'rgb(25, 255, 102)',
borderColor: 'rgba(25, 255, 102, 0.4)',
borderWidth: 5,
data: [0, 600, 200, 450, 800, 900, 550, 700, 800, 600]
}]
},
options: chartOptions,
});
我希望数据数据做这样的事情:
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
data: [100, 120, 180, 200, 300, 155, 220, 430, 275, 350, 120, 450]
我知道我需要将列表项转换为字符串并尝试加入我的 SubmitDate 但我无法让它工作。
var submitDate = string.Join("','", @Model.lstObjects.Select(x => x.RecordMonth).ToList());
有人对我如何完成这项工作有任何建议吗?
解决方案
我能够使用逗号上的@Html.Raw
and来完成这项工作。String.Join
我还添加了刻度来围绕 Month 字符串,例如'Jan'
.
下面是我所做的一个示例。
data: {
labels: [@Html.Raw("'" + String.Join("','", (Model.lstObjects.Select(x => x.RecordMonth).ToList())) + "'")],
推荐阅读
- r - 在 ggplot - R 中使用 geom_area 时创建平滑线
- javascript - 在视图 mvc 中从网格中添加列的值
- google-cloud-platform - 如何从 GCP 中 Redis 实例的 Memorystore 检索日志
- python - Scrapy 不断给我错误的 AttributeError: 'str' object has no attribute 'text'
- python - if/elif/else 如何用 min() 和 max() 代替?
- python - (分层)KFold 与 train_test_split - 使用什么训练数据?
- sql - SQL需要10分钟内发生的事件列表的最小时间实例和总持续时间
- c# - C# Where 子句不会从可为空的布尔值中返回空值
- c# - 作为 Debug Web API 发布的 Release 程序集的性质是什么
- swift - 如何快速设置 UIButton 的高度锚点?