gpt4 book ai didi

asp.net-mvc - Kendo UI 图表标题动态使用 MVC

转载 作者:行者123 更新时间:2023-11-27 23:36:27 24 4
gpt4 key购买 nike

如何使用来自 MVC Controller 的数据动态更改 x 轴和 y 轴的标题?

Index.cshtml 看起来像这样,

<div class="chart-wrapper">
@(Html.Kendo().Chart(Model)
.Name("chart")
.Title("Price-Performance Ratio")
.Legend(legend => legend
.Visible(true)
.Position(ChartLegendPosition.Bottom)
)
.Series(series =>
{
series.Scatter(model => model.Price, model => model.Performance)
.Name("Price Performance");
})
.XAxis(x => x
.Numeric()
.Title(title => title.Text("Price"))
.Labels(labels => labels.Format("R{0}")).Max(30)
)
.YAxis(y => y
.Numeric()
.Title(title => title.Text("Performance Ratio"))
.Labels(labels => labels.Format("{0}%")).Max(10)
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Template("#= '<b>$' + value.x + ' / ' + dataItem.Family + ' ' + dataItem.Model + ': ' + value.y + '%</b>' #")
)
.ChartArea( size => size.Height(520))
)

家庭 Controller 中的索引操作如下所示

var model = new List<PricePerformance>(){
new PricePerformance(){
Price = 14.4,
Performance = 5.4
},
new PricePerformance(){
Price = 21.4,
Performance = 2
},
new PricePerformance(){
Price = 8.4,
Performance = 1.4
}
};

return View(model);

模型看起来像这样

public class PricePerformance
{
public double Price { get; set; }
public double Performance { get; set; }
public string LabelX {
get { return "Price"; }
}
public string LabelY {
get { return "Performance";}
}
}

最佳答案

创建一个 ViewModel PricePerformanceViewModel 并将 LabelXLabelY 移动到它。为 PricePerformance 创建一个列表。

    public class PricePerformanceViewModel
{
public List<PricePerformance> PricePerformances { get; set; }
public string LabelX
{
get { return "Price"; }
}
public string LabelY
{
get { return "Performance"; }
}
}

public class PricePerformance
{
public double Price { get; set; }
public double Performance { get; set; }
}

改变你的看法如下。

    @model PricePerformanceViewModel                      //Change Model
<div class="chart-wrapper">
@(Html.Kendo().Chart(Model.PricePerformances) //Change Model
.Name("chart")
.Title("Price-Performance Ratio")
.Legend(legend => legend
.Visible(true)
.Position(ChartLegendPosition.Bottom)
)
.Series(series =>
{
series.Scatter(model => model.Price, model => model.Performance)
.Name("Price Performance");
})
.XAxis(x => x
.Numeric()
.Title(title => title.Text(Model.LabelX)) //Change Title
.Labels(labels => labels.Format("R{0}")).Max(30)
)
.YAxis(y => y
.Numeric()
.Title(title => title.Text(Model.LabelY)) //Change Title
.Labels(labels => labels.Format("{0}%")).Max(10)
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Template("#= '<b>$' + value.x + ' / ' + dataItem.Family + ' ' + dataItem.Model + ': ' + value.y + '%</b>' #")
)
.ChartArea( size => size.Height(520))
)

关于asp.net-mvc - Kendo UI 图表标题动态使用 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33674079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com