رسم نمودار توسط Kendo Chart
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۳/۱۱/۰۹ ۹:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
برای رسم نمودار میتوانیم به صورت زیر عمل کنیم:
1- ابتدا باید استایلهای مربوط به Data Visualization را به صفحه اضافه کنیم:
<link href="Content/kendo.dataviz.min.css" rel="stylesheet" /> <link href="Content/kendo.dataviz.default.min.css" rel="stylesheet" />
<div id="chart"></div>
<div id="chart" style="width: 400px; height: 600px"></div>
$("#chart").kendoChart();
$("#chart").kendoChart({
title: {
text: "چارت آزمایشی"
}
}); $("#chart").kendoChart({
title: {
text: "عنوان چارت"
},
series: [
{ name: "دادههای چارت", data: [200, 450, 300, 125] }
]
}); $("#chart").kendoChart({
title: {
text: "عنوان چارت"
},
series: [
{
name: "دادههای چارت",
data: [200, 450, 300, 125]
}
],
categoryAxis: {
categories: [2000, 2001, 2002, 2003]
}
}); public class ProductsController : ApiController
{
public IEnumerable<ProductViewModel> Get()
{
var products = _productService.GetAllProducts();
var query = products.GroupBy(p => p.Name).Select(p => new ProductViewModel
{
Value = p.Key,
Count = p.Count()
});
return query;
}
}
public class ProductViewModel
{
public string Value { get; set; }
public int Count { get; set; }
} var productsDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "api/products",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
}
},
error: function (e) {
alert(e.errorThrown.stack);
},
pageSize: 5,
sort: { field: "Id", dir: "desc" }
});
$("#chart").kendoChart({
title: {
text: "عنوان چارت"
},
dataSource: productsDataSource,
series: [
{
field: "Count",
categoryField: "Value",
aggregate: "sum"
}
]
}); (function($) {
$.fn.ShowChart = function(options) {
var defaults = {
url: '/',
text: 'نمودار دایره ایی',
theme: 'blueOpal',
font: '13px bbc-nassim-bold',
legendPosition: 'left',
seriesField: 'Count',
seriesCategoryField: 'Value',
titlePosition: 'top',
chartWidth: 400,
chartHeight: 400
};
var options = $.extend(defaults, options);
return this.each(function() {
var chartDataSource = new kendo.data.DataSource({
transport: {
read: {
url: options.url,
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
}
},
error: function (e) {
// handle error
}
});
$(this).kendoChart({
chartArea: {
height: options.chartHeight
},
theme: options.theme,
title: {
text: options.text,
font: options.font,
position: options.titlePosition
},
legend: {
position: options.legendPosition,
labels: {
font: options.font
}
},
seriesDefaults: {
labels: {
visible: false,
format: "{0}%"
}
},
dataSource: chartDataSource,
series: [
{
type: "pie",
field: options.seriesField,
categoryField: options.seriesCategoryField,
aggregate: "sum",
}
],
tooltip: {
visible: true,
template: "${category}: ${value}",
font: options.font
}
});
});
};
})(jQuery); title : جهت تعیین عنوان چارت
legend : جهت تنظیم ویژگیهای قسمت گروهبندی چارت
tooltip : جهت تنظیم ویژگیهای مربوط به نمایش tooltip در هنگام hover بر روی چارت.
لازم به ذکر است در قسمت series میتوانید نوع چارت را تعیین کنید.
$('#chart').ShowChart({
url: "/Report/ByUnit",
legendPosition: "bottom"
});
<div id="chart" style="direction: ltr"> </div>
<div id="chart"></div>
<script>
$("#chart").kendoChart({
series: [{
data: [1, 2, 3]
}],
valueAxis: {
notes: {
label: {
rotation: 90
},
data: [{ value: 1 }]
}
}
});
</script> tooltip:
{
visible: true,
template: "${category}: ${value}",
font: options.font
},
valueAxis: {
notes: {
label: {
rotation: 45
}
}
}, categoryAxis: [{
labels: {
rotation: -45
}
}] <div id="chart"></div>
<script>
$("#chart").kendoChart({
pdf: {
fileName: "Products.pdf"
},
legend: {
position: "bottom"
},
series: [
{ name: "Series 1", data: [1, 2, 3] },
{ name: "Series 2", data: [3, 4, 5] }
]
});
var chart = $("#chart").getKendoChart();
chart.saveAsPDF();
</script> var chart = $("#chart").data("kendoChart");
chart.saveAsPDF(); http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.default.min.css http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.min.css
kendo.pdf.defineFont({
/*"Verdana": "/fonts/Verdana.ttf", // this is a URL
"Verdana|Bold": "/fonts/Verdana_Bold.ttf",
"Verdana|Bold|Italic": "/fonts/Verdana_Bold_Italic.ttf",
"Verdana|Italic": "/fonts/Verdana_Italic.ttf"*/
"Iranian Sans":"/fonts/irsans.ttf"
});
$(document).ready(function () {
//چارت 4
$.ajax({
type: 'POST',
url: '@Url.Action("TierStatistics4", "Dashboard", new { area = "Tier" })',
dataType: 'json',
data: { CostCenterId: $("#CostCenterId").val(), QueryIndex: $("#QueryIndex").val() },
success: function (data) {
dataSource = new kendo.data.DataSource({
data: data,
group: {
field: "Series"
},
sort:
{
field: "Year",
dir: "desc"
}
});
$("#chart4").kendoChart({
title: {
text: "آمار تعداد/هزینه(ریال) تایر مصرفی شعب به تفکیک مراکز هزینه و ماههای سال",
font: "irsans",
},
dataSource: dataSource,
series: [{
type: "column",
field: "Value",
categoryField: "Year",
name: "#= group.value #",
}],
categoryAxis: {
font: "irsans",
labels: {
font: "irsans"
}
},
valueAxis:
{
labels: {
font: "irsans",
visible: true,
}
},
seriesDefaults: {
style: "smooth",
labels: {
position: "Bottom",
visible: false,
fromat: "{0:n0}",
font: "irsans",
font: "irsans",
format: "{0:n0}",
}
},
legend: {
position: "top",
labels: {
font: "irsans"
}
},
tooltip: {
visible: true,
format: "{0:n0}",
template: "#= series.name #: #= value #",
font: "irsans",
}
})
},
error: function (ex) {
alert('خطا در بازیابی اطلاعات' + ex);
}
});
return false;
}); public class DashoardViewModel : System.Object
{
public DashoardThirdViewModel()
{
}
public string Series { get; set; }
// **********
public int Year { get; set; }
//**********
public long Value { get; set; }
// **********
} dataBound: function (e) {
e.sender.options.categoryAxis.categories.sort();
}