Reputation: 51
I am using Kendo charts and want to add a specific label to each point on the graph, not related to the data value of that point, but rather a symbol relating to a qualitative property of that point.
In this code, I am able to customize the label on a per-series basis, where the default is an up-arrow, India is a *, and Russian Federation is a space. But it doesn't let me vary the label on a per-point basis within each series. Any way to do that? Any way to have an array of label values for each series, or something like that?
Thanks!
--Bruce
<!DOCTYPE html>
<html>
<head>
<base href="http://demos.telerik.com/kendo-ui/bar-charts/column">
<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.504/styles/kendo.common-material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.504/styles/kendo.material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.504/styles/kendo.material.mobile.min.css" />
<script src="https://kendo.cdn.telerik.com/2017.2.504/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.2.504/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example">
<div class="demo-section k-content wide">
<div id="chart" style="background: center no-repeat url('../content/shared/styles/world-map.png');"></div>
</div>
<script>
function createChart() {
$("#chart").kendoChart({
title: {
text: "Gross domestic product growth /GDP annual %/"
},
legend: {
position: "top"
},
seriesDefaults: {
type: "column",
labels: {template: "↑",
visible: true, color: 'red',
position: "outsideEnd"}
},
series: [{
name: "India",
data: [3.907, 7.943, 7.848, 9.284, 9.263, 9.801, 3.890, 8.238, 9.552, 6.855],
labels: {template: "*"}
}, {
name: "Russian Federation",
data: [4.743, 7.295, 7.175, 6.376, 8.153, 8.535, 5.247, -7.832, 4.3, 4.3],
labels: {template: " "}
}, {
name: "Germany",
data: [0.010, -0.375, 1.161, 0.684, 3.7, 3.269, 1.083, -5.127, 3.690, 2.995]
},{
name: "World",
data: [1.988, 2.733, 3.994, 3.464, 4.001, 3.939, 1.333, -2.245, 4.339, 2.727]
}],
valueAxis: {
labels: {
format: "{0}%"
},
line: {
visible: false
},
axisCrossingValue: 0
},
categoryAxis: {
categories: [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011],
line: {
visible: false
},
labels: {
padding: {top: 135}
}
},
tooltip: {
visible: true,
format: "{0}%",
template: "#= series.name #: #= value #"
}
});
}
$(document).ready(createChart);
$(document).bind("kendo:skinChange", createChart);
</script>
</div>
</body>
</html>
Upvotes: 0
Views: 2396
Reputation: 3882
I've used your code to build a solution. You might not use it exactly as it is but you'll get the general idea. See this Plunker.
Basically what I did was the following:
1) Convert the series' items into objects with two fields, one is the actual data and the other is the label identifier.
data: [{value: 1, label: 0}, {value: 2, label: 1}, {value: 3, label: 2}],
2) Make a function that converts the template object (e) to the right label string.
let labels = {
"India": ["label1", "label2", "label3"],
"Russian Federation": [2, 4, 6]
};
let getLabel = function(e) {
return labels[e.series.name][e.dataItem.label];
};
3) Place the function in the template field of the series.
seriesDefaults: {
labels: {
template: getLabel
}
}
Upvotes: 1
Reputation: 24738
You could use the visual property of the labels to set the text based on value:
seriesDefaults: {
type: "column",
labels: {
visible: true, color: 'red',
position: "outsideEnd",
visual: function(e) {
var center = e.rect.center();
var val = parseFloat(e.text);
var text = "↑";
if (val > 4) text = "*";
if (val > 7) text = " ";
return new kendo.drawing.Text(text, [center.x, e.rect.origin.y], {
fill: {
color: "red"
}
});
}
}
},
Upvotes: 0