1 Answer, 1 is accepted

You could either use plotbands or draw on the chart surface.
For plotBands, have a look at categoryAxis.plotBands
$(
"#chart"
).kendoChart({
theme:
"Bootstrap"
,
dataSource: data,
series: [{
type:
"line"
,
field:
"value"
,
categoryField:
"day"
}],
valueAxis: {
name:
"value"
},
categoryAxis: {
name:
"category"
,
plotBands: [
{ from: 15, to: 16, color:
"red"
}
]
},
});
For drawing, on render, you can get the axis objects and use slot and range to get the coordinates of the line to draw:
See Docs for ChartAxis and getAxis
$(
"#chart"
).kendoChart({
theme:
"Bootstrap"
,
dataSource: data,
series: [{
type:
"line"
,
field:
"value"
,
categoryField:
"day"
}],
valueAxis: {
name:
"value"
},
categoryAxis: {
name:
"category"
},
render:
function
(e){
var
chart = e.sender;
var
valueAxis = chart.getAxis(
"value"
);
var
categoryAxis = chart.getAxis(
"category"
);
var
catSlot = categoryAxis.slot(15,16);
var
valRange = valueAxis.range();
var
valSlot = valueAxis.slot(valRange.min, valRange.max);
var
path =
new
kendo.drawing.Path({
stroke: {
color:
"red"
,
width: 3
}
}).moveTo(catSlot.origin.x + catSlot.size.width/2, valSlot.origin.y)
.lineTo(catSlot.origin.x + catSlot.size.width/2, valSlot.bottomRight().y);
chart.surface.draw(path);
}
});
This does not work right with panning and zooming capability enabled as below. How can I draw on the x-axis a vertical guideline based on an x-axis value? For me I have dates on the x-axis and I want to get the slot for that by date value and always have the vertical line drawn over that. Thank you.
pannable: {lock: "none"
},
zoomable: {
mousewheel: {
lock: "none",
},
selection: {
lock: "none"
}
},
You can use the chart events for 'drag', 'dragEnd', 'zoom', 'zoomEnd' to catch user pans and zooms and then re-render the line in the correct place.
For this, I am deleting the line and re-creating it each time:
function RenderVerticalLine(chart) { //remove old line chart.wrapper.find("svg > path").remove(); //calc new position based on pan and zoom var valueAxis = chart.getAxis("value"); var categoryAxis = chart.getAxis("category"); var catSlot = categoryAxis.slot(15,16); var valRange = valueAxis.range(); var valSlot = valueAxis.slot(valRange.min, valRange.max); var yRange = categoryAxis.range(); var xRangeSlot = categoryAxis.slot(yRange.min, yRange.max); //check if line is still in zoomed/panned view if (catSlot.origin.x <= xRangeSlot.origin.x) return; if (catSlot.origin.x >= xRangeSlot.origin.x + xRangeSlot.size.width) return; var path = new kendo.drawing.Path({ stroke: { color: "red", width: 3 } }).moveTo(catSlot.origin.x + catSlot.size.width/2, valSlot.origin.y) .lineTo(catSlot.origin.x + catSlot.size.width/2, valSlot.bottomRight().y); chart.surface.draw(path); }
$("#chart").kendoChart({ theme: "Bootstrap", dataSource: data, series: [{ type: "line", field: "value", categoryField: "day" }], valueAxis: { name: "value" }, categoryAxis: { name: "category", }, render: function(e){ RenderVerticalLine(e.sender); }, zoom: function(e) { RenderVerticalLine(e.sender); }, zoomEnd: function(e) { RenderVerticalLine(e.sender); }, drag: function(e) { RenderVerticalLine(e.sender); }, dragEnd: function(e) { RenderVerticalLine(e.sender); }, pannable: { lock: "none" }, zoomable: { mousewheel: { lock: "none", }, selection: { lock: "none" } }, });
Hi,
This is helpful thank you. My issue is with this line:
categoryAxis.slot(15,16);
I need to get a slot for an axis value that is a date. Imagine the x-axis being Jan-23, Feb-23, Mar-23, Apr-23, May-23 displayed as such. I need to draw the vertical line where x-axis is Apr-23 (today's date). How would I get the slot for that so the line is always just above that?
I updated the DOJO with a date axis:
Just use the date in the slot() call:
var catSlot = categoryAxis.slot(new Date("2011/12/26"),new Date("2011/12/27"));
My demo uses days, you can use months or any other base unit...
Hi,
For the category axis I am using a label template like so:
categoryAxis: {
color: 'white',
labels: {
font: '4px',
rotation: 60,
template: "#= kendo.toString(new Date(value), 'MMM-yy') #"
},
},
I tried doing categoryAxis.slot(kendo.toString(new Date("2011/12/26"), "MMM-yy")); and categoryAxis.slot(new Date("2011/12/26")); and they did not work. I am still having to provide a number for the slot function to draw it in the right spot, but getting that number is difficult with panning/zooming. Without zoom/panning it is trivial to get the index.
this seemed to work
Have you tried setting a Schema on the datasource just for the date column:
schema: {
model: {
fields: {
Timestamp: {
type: "date"
}
}
}
}
Change "Timestamp" to the actual key name for your date field
I also get data back from c# as a string like this:
"2023-04-10T00:00:00"
I then use the dataSource schema to force the conversion to a date object.
Here is a new DOJO Demo with the schema and string date in the data:
Hi,
I am drawing a line and a label with a kendo.drawing.group, how would I remove those from the chart?
var catSlot = categoryAxis.slot(thisMonth.toISOString().split('.')[0], nextMonth.toISOString().split('.')[0]);
Basically need to remove my drawn group instead of this and I should be good to go:
chart.wrapper.find("svg > path").remove();
thank you!
Telerik does not seem to have a way to set an ID for drawing elements (including groups) that would allow for easy selection. With the path, it was the only one at the top level of the svg, so I could just remove all paths found there. With groups, they use groups at the top level for the rest of the chart; so my hack is to create the group with opacity set to 0.99999:
var grp = new kendo.drawing.Group({opacity: 0.99999});
This does not affect display, and the Kendo automatically generated SVG groups are unlikely to have that exact opacity. Then to remove that group:
chart.wrapper.find("svg > g").each(function(idx){ if ($(this).attr('opacity') && $(this).attr('opacity') == 0.99999){ $(this).remove(); } });
Find all top level groups, and if opacity is 0.99999, remove tham.
Updated DEMO
I imagine someone at Telerik has a better way to do this, but this will get you going.