How to display data point with various fields in kendo chart? - kendo-ui

I want to display all data point in a tooltip.
My data have 2 series,
a series have 2 points,
a point have 3 fields(value , base and date)
I try this http://trykendoui.telerik.com/OCeB but the x-axis are repeated
If there is solution i would like use datasource

Usually the x values in a series should not repeat, you have two options to
fix your chart:
Option 1:
Define both series' values on each datapoint
var internetUsers = [
{
"S1Value" : 1,
"S1base" : 2,
"S2Value" : 3,
"S2base" : 2,
"date" : 2011
},
{
"S1Value" : 5,
"S1base" : 6,
"S2Value" : 4,
"S2base" : 7,
"date" : 2013
},
]
and define a tooltip for each:
series: [{
field: "S1Value",
name: "United States",
tooltip: {
visible: true,
background: "#FFFFFF",
template:
"#= series.name # <br /> " +
"Fecha = #= category # <br /> " +
"Valor = #= value # <br/> " +
"Base = #= dataItem.S1base # ",
format: "n2",
}
},{
field: "S2Value",
name: "Mexico",
tooltip: {
visible: true,
background: "#FFFFFF",
template:
"#= series.name # <br /> " +
"Fecha = #= category # <br /> " +
"Valor = #= value # <br/> " +
"Base = #= dataItem.S1base # ",
format: "n2",
}
}
http://trykendoui.telerik.com/OCeB/2
Option 2:
Split your series into two:
var internetUsersS1 = [
{
"S1" : 1,
"base" : 2,
"date" : 2011
},
{
"S1" : 5,
"base" : 6,
"date" : 2013
}
]
var internetUsersS2 = [
{
"S2" : 3,
"base" : 2,
"date" : 2011
},
{
"S2" : 4,
"base" : 7,
"date" : 2013
}
]
...now give each series it's own datasource:
series: [{
data: internetUsersS1,
field: "S1",
name: "United States"
},{
data: internetUsersS2,
field: "S2",
name: "Mexico"
}
]
http://trykendoui.telerik.com/OCeB/3

Related

Adding tooltip to each tile in a kendo stacked bar chart

I have created a stacked bar chart using Kendo UI. I want to show tooltip for each tile in stacked bar chart and use another array for this purpose which contains the values to be shown as tooltip.
For ex: When I hover over, USA for year 2000, tooltip should show, NYC: 60% & SFO: 40% (as shown in image).
Here is a fiddle.
This is what I am trying to achieve (in this case showing tooltip for year 2000 for USA):
Problem is when I use series click or series hover events, I am not able to identify tile (inside the stacked bar chart) which is making it harder to show tooltip.
This is the code:
html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }
<script src="https://kendo.cdn.telerik.com/2019.1.115/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2019.1.115/js/kendo.all.min.js"></script>
var CityData = [{country: "USA", children:[{"NYC": ["60%", "70%", "80%"]}, {"SFO": ["40%", "30%", "20%"]}]},
{country: "Mexico", children:[{"Mexico City": ["80%", "80%", "80%"]}, {"Cancun": ["20%", "20%", "20%"]}]},
{country: "Canada", children:[{"Toronto": ["50%", "60%", "60%"]}, {"Vancouver": ["50%",
"40%", "40%"]}]}
];
function createChart() {
$("#chart").kendoChart({
title: {
text: "City data"
},
legend: {
visible: false
},
seriesDefaults: {
type: "column",
stack: {
type: "100%"
}
},
series: [{
name: "USA",
stack: {
group: "Country"
},
data: [854622, 925844, 984930]
}, {
name: "Canada",
stack: {
group: "Country"
},
data: [490550, 555695, 627763]
}, {
name: "Mexico",
stack: {
group: "Country"
},
data: [379788, 411217, 447201]
}
],
seriesColors: ["yellow", "green", "red"],
valueAxis: {
line: {
visible: false
}
},
categoryAxis: {
categories: [2000, 2005, 2010],
majorGridLines: {
visible: false
}
},
tooltip: {
visible: true,
template: "#= series.stack.group #, city #= series.name #"
}
});
}
$(document).ready(createChart);
$(document).bind("kendo:skinChange", createChart);
</script>
You need to set tooltip: { shared: true } and it will work, I included other customizable properties of tooltip below also.
Working demo: https://dojo.telerik.com/OfeMiHUb/4
Snippet:
tooltip: {
shared: true,
visible: true,
background: "#000",
template: "#= series.stack.group #, city #= series.name #"
}
or you can try this if you want another template of your tooltip: https://dojo.telerik.com/OfeMiHUb/3
UPDATE:
What changed?:
tooltip: {
template: `USA- #= cityData[0]
.children
.map(itm => Object.keys(itm)[0]) #`
}
OP clarified further what he wanted, as per new information, please see new working example: https://dojo.telerik.com/OfeMiHUb/9
You can retrieve your city-data through indexing the keys of the children-object like this: cityData[0].children.map(itm => Object.keys(itm)[0])
Possible additions:
If you want the series.name to be dynamically added to the tooltip, instead of explicitly typing it in. You can use: series.name.
Like this:
tooltip: {
template: `#= series.name # - #=
cityData[0]
.children
.map(itm => Object.keys(itm)[0]) #`}
Change the ArrayIndex of cityData[index] to select a nations cities.
i.e.
0: USA
1: Canada
2: Mexico
UPDATE 2:
After reading through what you wrote (3000x) + looking at the image, I am interpreting that u also want the percentage showing (even tho it seems like in the clarifying comment below that u dont?).
Anyhow:
series: [{
name: "USA",
stack: {
group: "Country"
},
tooltip: {template: `#= series.name # - #=
cityData[0]
.children
.map(itm => '[' + Object.keys(itm)[0] + ']' + ' : ' + Object.values(itm)[0][0]) #`},
data: [854622, 925844, 984930]
}, {
name: "Canada",
stack: {
group: "Country"
},
tooltip: {template: `#= series.name # - #=
cityData[1]
.children
.map(itm => '[' + Object.keys(itm)[0] + ']' + ' : ' + Object.values(itm)[0][0]) #`},
data: [490550, 555695, 627763]
}, {
name: "Mexico",
stack: {
group: "Country"
},
tooltip: {template: `#= series.name # - #=
cityData[2]
.children
.map(itm => '[' + Object.keys(itm)[0] + ']' + ' : ' + Object.values(itm)[0][0]) #`},
data:[379788, 411217, 447201]
}
],
Almost got the percentage / series working.
Right now I'm struggling with extracting the series-index in this selector: Object.values(itm)[0][SERIES_INDEX_SHOULD_BE_HERE]
TO BE CONTINUED...

Not able to get value from the Kendo Combobox

I am using below code in Kendo Grid editor but unable to access value of selected item value from Combobox.
Moreover, I have done same thing in Kendo drop down list but unable to kendo Combobox, so if anyone has solution please let me know.
Thanks in Advance !
{
field: "SalesBookId",
title: "Sales Book",
template: "#= (typeof SalesBookId != 'undefined') ? GetSalesBookName(SalesBookId):'' #",
editor: function (container, options) {
$('<input required data-text-field="SalesBookName" data-value-field="SalesBookId" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoComboBox({
autoBind: false,
dataSource: dsSalesBookDropDown,
});
}
},
You have not shown the dsSalesBookDropDown, nor GetSalesBookName so it is hard to know what is wrong in your specific case.
This dojo demonstrates that when the configurations, handlers and data all align properly there should not be a problem.
The dojo is a based on the example "Grid with local data", your SalesBook concept is changed to Seller for the example.
Code related to the custom editor include
var sellers = [
{ SellerId: 1, Name: "Andrew" },
{ SellerId: 2, Name: "Basil" },
{ SellerId: 3, Name: "Chuck" },
{ SellerId: 4, Name: "Dennis" },
{ SellerId: 5, Name: "Edward" }
];
var dsSellersDropDown = sellers;
function GetSellerName (id) {
var seller = sellers.find(function(x) {return x.SellerId == id });
return (seller) ? seller.Name : "** invalid id " + id + " **";
}
var products = [{
ProductID : 1,
ProductName : "Chai",
SellerId: 1,
SupplierID : 1,
CategoryID : 1,
. . .
grid config
dataSource: {
data: products,
schema: {
model: {
fields: {
ProductName: { type: "string" },
SellerId: { type: "number" },
and
columns: [
"ProductName",
{ field: "SellerId",
title: "Seller Name",
template: "#= (typeof SellerId != 'undefined') ? GetSellerName(SellerId):'' #",
editor: function (container, options) {
$('<input required data-text-field="Name" data-value-field="SellerId" data-bind="value:'
+
options.field
+ '"/>')
.appendTo(container)
.kendoComboBox({
autoBind: false,
dataSource: dsSellersDropDown,
});
}
},
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" },

NVD3 converting JSON Data

I have json data in the following format :
[{
"label" : "A Label" ,
"value" : -29.765957771107
} ,
{
"label" : "B Label" ,
"value" : 0
} ,
{
"label" : "C Label" ,
"value" : 32.807804682612
} ,
{
"label" : "D Label" ,
"value" : 196.45946739256
}]
I want to create a bar chart in NVD3 where x coordinates will be label and the y coordinates will be the value. So how can I achieve it. I have gone through many examples where the JSON data was used is a different format but my data is as shown above.
EDITED CODE:
Here is the js code through which I am trying to create a chart in NVD3 .
d3.json("http://localhost:8080/api/study", function(data) {
nv.addGraph(function() {
var chart = nv.models.multiBarHorizontalChart().x(function(d) {
return d.label;
}).y(function(d) {
return d.value;
}).margin({
top : 30,
right : 20,
bottom : 50,
left : 175
}).showValues(true)//Show bar value next to each bar.
.tooltips(true)//Show tooltips on hover.
//.transitionDuration(350)
.showControls(true);
//Allow user to switch between "Grouped" and "Stacked" mode.
chart.yAxis.tickFormat(d3.format(',.2f'));
d3.select('#chart11 svg').datum(data).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
});
Most charts in nvd3.js have the following data sceleton:
[
{
key: "<Series name>",
color: "<CSS color>",
values: [
{x: 0, y: 10},
{x: 1, y: 20},
{x: 2, y: 30}
....
]
},
{
key: "<Series name>"
...
}
]
In your case the following format will be valid:
[
{
key: "Series 1",
values: [
{
"label" : "A Label" ,
"value" : -29.765957771107
} ,
{
"label" : "B Label" ,
"value" : 0
} ,
{
"label" : "C Label" ,
"value" : 32.807804682612
} ,
{
"label" : "D Label" ,
"value" : 196.45946739256
}
]
}
]
You will need to specify axis properties accessors:
var chart = nv.models.discreteBarChart()
.x(function(d) { return d.label })
.y(function(d) { return d.value })

Highchart: Bar Chart with time duration

I want to create a bar chart with time duration on the bottom axis. It' working for decimal numbers, like 300.5 seconds, or 50.3 hours, but i would like to have the following format:
Hours:Minutes:Seconds. I tried some thing with datalabelformat: datetime, but with no success.
The data would be for example:
Member1 - 10:05:18
Member2 - 12:52:20
Member3 - 18:10:30
Member4 - 19:20:10
Member5 - 19:30:45
Anybody got an idea how to realize that?
Thanks in advance!
You need to set min value and tickInterval, then set dateTimeLabels.
dateTimeLabelFormats: {
millisecond: '%H:%M:%S',
second: '%H:%M:%S',
minute: '%H:%M:%S',
hour: '%H:%M:%S',
day: '%H:%M:%S',
week: '%H:%M:%S',
month: '%H:%M:%S',
year: '%H:%M:%S'
}
Example:
http://jsfiddle.net/TynTT/1/
I came here searching for a solution to my problem. Not sure if this is exactly what you want, and I cannot put in a comment due to low reputation. But this is how I was able to solve it.
This provides result in hh:mm. You can modify it to include seconds.
http://jsfiddle.net/TynTT/11/
It has the y axis in hh:mm format and also displays datalabels as hh:mm.
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
yAxis: {
type: 'time',
labels: {
formatter: function () {
var time = this.value;
var hours1=parseInt(time/3600000);
var mins1=parseInt((parseInt(time%3600000))/60000);
return (hours1 < 10 ? '0' + hours1 : hours1) + ':' + (mins1 < 10 ? '0' + mins1 : mins1);
}
}
},
series: [{
data: [1000000, 26500000, 17000050, 79000000, 56000000, 124000000],
dataLabels: {
enabled: true,
formatter: function () {
var time = this.y / 1000;
var hours1=parseInt(time/3600);
var mins1=parseInt((parseInt(time%3600))/60);
return (hours1 < 10 ? '0' + hours1 : hours1) + ':' + (mins1 < 10 ? '0' + mins1 : mins1);
}
},
}]
});
});
I improved above configs by reusing the HC methods:
durationbar: {
chart: {
type: 'column'
},
yAxis: {
type: 'datetime',
showFirstLabel: false,
dateTimeLabelFormats: {
millisecond: '%H:%M:%S'
},
units: [[
'millisecond',
[1000]
], [
'second',
[1, 2, 5, 10, 15, 30]
], [
'minute',
[1, 2, 5, 10, 15, 30]
], [
'hour',
[1, 2, 3, 4, 6, 8, 12]
], [
'day',
[1]
], [
'week',
[1]
], [
'month',
[1, 3, 6]
], [
'year',
null
]],
},
tooltip: {
formatter: null,
pointFormat: '{point.name}: <b>{point.dataLabel.text.textStr}</b><br/>'
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
formatter() {
return this.series.chart.time.dateFormat('%H:%M:%S', this.y)
}
}
}
}
},
screenshot

kendo grid how to set the schema.model with a foreign key field

need help about adding a new recort in a kendo grid.
I have a grid with a foreign key field. The grid is populated by a json data with subobject, returned from a webmethod. it looks like this:
[
{
"classe_iva": {
"id_classe_iva": 5,
"desc_classe_iva": "Esente",
"note_classe_iva": null
},
"id_iva": 37,
"desc_iva": "bbb",
"codice_iva": "bbb",
"imposta": 2,
"indetr": 2,
"id_classe_iva": 5,
"note": "dddddfsf",
"predefinito": false,
"id_company": 4
},
{
"classe_iva": {
"id_classe_iva": 6,
"desc_classe_iva": "Escluso",
"note_classe_iva": null
},
"id_iva": 52,
"desc_iva": "o",
"codice_iva": "jj",
"imposta": 1,
"indetr": 1,
"id_classe_iva": 6,
"note": "l",
"predefinito": false,
"id_company": 4
}
]
and this is the schema.model used in the kendo datasource:
model = {
id: "id_iva",
fields: {
id_iva: { type: "string", editable: false },
desc_iva: { type: "string" },
codice_iva: { type: "string" },
imposta: { type: "number" },
indetr: { type: "number" },
id_classe_iva: {type: "string"},
note: { type: "string" },
predefinito: { type: "boolean" },
id_company: { type: "number" }
}
}
.. and below is shown the grid columns format:
toolbar = [{
name: "create",
text: "Aggiungi nuova aliquota IVA"
}];
columns = [
{ field: "desc_iva", title: "Descrizione", width: 45 },
{ field: "codice_iva", title: "Codice", width: 45 },
{ field: "imposta", title: "Imposta", width: 45 },
{ field: "indetr", title: "Indetr", width: 45 },
{ field: "classe_iva.desc_classe_iva", title: "Classe IVA", width: 200, editor: categoryDropDownEditor, template: "#= classe_iva ? classe_iva.desc_classe_iva : 1 #", defaultValue: { id_classe_iva: 1, desc_classe_iva: "Acq. Intra-UE" } },
{ field: "note", title: "Note", width: 45 },
{
command: [{
name: "destroy",
text: "Elimina",
confirmation: "Sei sicuro di voler eliminare questa voce?"
} ,
{
name: "edit",
text: {
edit: "Modifica",
update: "Aggiorna",
cancel: "Cancella"
}
}
]
}
];
Theese settings works fine when i edit a row, and the grid shows the right content into the combobox field.
The problem is when i click on the "add new record" button, because when it tries to add a new row, it doesn't find the subobjects field "classe_iva".
if i change the column.field into this
{ field: "id_classe_iva", title: "Classe IVA", width: 200, editor: categoryDropDownEditor, template: "#= id_classe_iva ? id_classe_iva : 1 #", defaultValue: { id_classe_iva: 1, desc_classe_iva: "Acq. Intra-UE" } },
{ field: "note", title: "Note", width: 45 }
the add button works fine, but when the grid is loaded, the column id_classe_iva doesn't shows me the classe_iva.desc_classe_iva field...
how can i fix the issue??^?
thanks in advance.
to fix this issue, i used a workaround:
the error was thrown because, in the declaration of the field.template there where a not declared variable (classe_iva):
{ field: "id_classe_iva", title: "Classe IVA", width: 200, editor: categoryDropDownEditor, template: "#= classe_iva ? classe_iva.desc_classe_iva : 1 #", defaultValue: { id_classe_iva: 1, desc_classe_iva: "Acq. Intra-UE" } },
then, i declared a global variable
var classe_iva;
in this way, when i add a new record, the code doesn't throw any errors, and by the ternary if, the default value is set.
hope it will help someone.

Resources