Telerik MVC 图表 - 绑定到 JSON
Telerik MVC Chart - Bind to JSON
如何将 Telerik ASP.Net MVC 图表(不是 jQuery 版本的 Kendo)绑定到 JSON?例如,我想将下面的图表(注意:该系列暂时用虚拟数据存根,但希望你明白了)绑定到 JavaScript 函数 returns JSON。我无法找到如何使用 Telerik ASP.Net MVC Chart 执行此操作的示例。我确实找到了 Kendo UI 用于 jQuery 图表的示例 - 但我没有使用它。
@(Html.Kendo().Chart()
.Name("GallonsPerMonth")
.Title("Total Gallons Per Month")
.Legend(legend => legend
.Position(ChartLegendPosition.Top)
.Visible(true)
)
.Theme("Bootstrap")
.ChartArea(chartArea => chartArea
.Background("transparent")
.Height(600)
)
.Series(series =>
{
series.Column(new double[] { 825, 775, 875, 900, 925, 1111, 1200, 1175, 1100, 1000, 875, 800 }).Name("Estimated");
series.Line(new double[] { 700, 795, 900, 850, 950, 905, 1175, 1100, 1000, 1050, 700, 650 }).Name("Actual").Color("red");
})
.CategoryAxis(axis => axis
.Name("series-axis")
.Line(line => line.Visible(false))
)
.CategoryAxis(axis => axis
.Name("label-axis")
.Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
)
.ValueAxis(axis => axis//.Logarithmic()
.Numeric()
.Labels(labels => labels.Format("{0}"))
// Move the label-axis all the way down the value axis
.AxisCrossingValue(0, int.MinValue)
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0}")
.Template("#= series.name #: #= value #")
)
)
dsd
您可以在没有系列数据的情况下使用 MVC 帮助程序扩展创建图表,因此在文档准备好后使用 JavaScript 添加它。
<script>
$(document).ready(function () {
$.getJSON('your-url', function (data) {
var chart = $("#GallonsPerMonth").data("kendoChart");
var series = chart.options.series;
// first series
series[0].data = data;
chart.redraw();
});
}
</script>
Note I am adding the data to the Fist Series.
如何将 Telerik ASP.Net MVC 图表(不是 jQuery 版本的 Kendo)绑定到 JSON?例如,我想将下面的图表(注意:该系列暂时用虚拟数据存根,但希望你明白了)绑定到 JavaScript 函数 returns JSON。我无法找到如何使用 Telerik ASP.Net MVC Chart 执行此操作的示例。我确实找到了 Kendo UI 用于 jQuery 图表的示例 - 但我没有使用它。
@(Html.Kendo().Chart()
.Name("GallonsPerMonth")
.Title("Total Gallons Per Month")
.Legend(legend => legend
.Position(ChartLegendPosition.Top)
.Visible(true)
)
.Theme("Bootstrap")
.ChartArea(chartArea => chartArea
.Background("transparent")
.Height(600)
)
.Series(series =>
{
series.Column(new double[] { 825, 775, 875, 900, 925, 1111, 1200, 1175, 1100, 1000, 875, 800 }).Name("Estimated");
series.Line(new double[] { 700, 795, 900, 850, 950, 905, 1175, 1100, 1000, 1050, 700, 650 }).Name("Actual").Color("red");
})
.CategoryAxis(axis => axis
.Name("series-axis")
.Line(line => line.Visible(false))
)
.CategoryAxis(axis => axis
.Name("label-axis")
.Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
)
.ValueAxis(axis => axis//.Logarithmic()
.Numeric()
.Labels(labels => labels.Format("{0}"))
// Move the label-axis all the way down the value axis
.AxisCrossingValue(0, int.MinValue)
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0}")
.Template("#= series.name #: #= value #")
)
)
dsd
您可以在没有系列数据的情况下使用 MVC 帮助程序扩展创建图表,因此在文档准备好后使用 JavaScript 添加它。
<script>
$(document).ready(function () {
$.getJSON('your-url', function (data) {
var chart = $("#GallonsPerMonth").data("kendoChart");
var series = chart.options.series;
// first series
series[0].data = data;
chart.redraw();
});
}
</script>
Note I am adding the data to the Fist Series.