I've been working with few charts of amCharts 4, and everytime I struggled to subscribe to the event that returns the "selected/clicked" element. I don't know what I've been missing from the doc, but for example, I need to retrieve the selected item from "hit" event but i can't find anywhere (here is a simple i'd like to try this on https://codepen.io/team/amcharts/pen/erojQb)
var chart = am4core.create("chartdiv", am4charts.TreeMap);
chart.data = [{
"name": "First",
"value": 190
}, {
"name": "Second",
"value": 289
}, {
"name": "Third",
"value": 635
}, {
"name": "Fourth",
"value": 732
}, {
"name": "Fifth",
"value": 835
}];
/* Set color step */
chart.colors.step = 2;
/* Define data fields */
chart.dataFields.value = "value";
chart.dataFields.name = "name";
I tried this:
chart.seriesTemplates.template.columns.events.on('hit', function(ev) {
console.log('mlkmlz');
});
but not called, and this:
chart.seriesContainer.events.on('hit', function(ev) {
console.log(ev.target.dataItem)
});
but no dataItem attached
OK, so it's a bit more complicated than that. To attach events, you'll need to to actually create series for the specific level, then attach events on its column template:
var series = chart.seriesTemplates.create("0");
series.columns.template.events.on('hit', function(ev) {
console.log(ev.target.dataItem);
});
Related
I followed the question here Retrieving object from TreeMap but I am unable to get the name out of it like for in
chart.data = [{
"name": "First",
"value": 190
}, {
"name": "Second",
"value": 289
}, {
"name": "Third",
"value": 635
}, {
"name": "Fourth",
"value": 732
}, {
"name": "Fifth",
"value": 835
}]
I want to get name like First, Second and so on. on hit event how do I do that?
Ok so it was bit deeper than I thought:
var series = chart.seriesTemplates.create("0");
series.columns.template.events.on('hit', function(ev) {
console.log(ev.target.dataItem.dataContext.dataContext.name);
});
instead of
console.log(ev.target.dataItem)
How can I change the title of a loaded chart using javascript? The following doesn't work with external data
https://codepen.io/abdfahim/pen/zYOPvPx
var chart = am4core.createFromConfig({
"titles": [
{
"text": "ABCD",
"fontSize": 25,
"marginBottom": 10
}
],
"dataSource": {
"url": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/sample_data_serial.json"
},
"xAxes": [
{
"type": "CategoryAxis",
"dataFields": {
"category": "year"
}
}
],
"yAxes": [
{
"type": "ValueAxis"
}
],
"series": [
{
"type": "ColumnSeries",
"name": "Cars",
"dataFields": {
"categoryX": "year",
"valueY": "cars"
}
}
]
}, "chartdiv", am4charts.XYChart);
function changeTitle()
{
chart.titles[0].text = "New Title";
}
AmCharts v4 uses lists for the majority of its array-like objects, so using subscripts will not work. It's recommended to use the accessor methods provided by the list to get at the object you want to change, such as getIndex:
chart.titles.getIndex(0).title = "New title"
Updated codepen
Just in case some one will hit my same issue, I found this solution working for me
chart.titles.getIndex(0).text = "new title";
this is particularly handy if you are going to refresh the chart each x seconds
When I select any particular field in the bar graph, all the graphs changes accordingly.
I want to introduce a heading above the graphs which should show which data is currently displayed in the graphs and the count.
So what changes should I make in my code?
var data = [{
"city": "New York",
"neighborhood": "N/A",
"hits": 200
}, {
"city": "New York",
"neighborhood": "Brooklyn",
"hits": 225
}, {
"city": "New York",
"neighborhood": "Queens",
"hits": 1
}, {
"city": "San Francisco",
"neighborhood": "Chinatown",
"hits": 268
}, {
"city": "San Francisco",
"neighborhood": "Downtown",
"hits": 22
}, {
"city": "Seattle",
"neighborhood": "N/A",
"hits": 2
}, {
"city": "Seattle",
"neighborhood": "Freemont",
"hits": 25
}];
var pieChart = dc.pieChart("#pieChart"),
rowChart = dc.rowChart("#rowChart");
var ndx = crossfilter(data),
cityDimension = ndx.dimension(function (d) {
return d.city;
}),
cityGroup = cityDimension.group().reduceSum(function (d) {
return d.hits;
}),
neighborhoodDimension = ndx.dimension(function (d) {
return d.neighborhood;
}),
neighborhoodGroup = neighborhoodDimension.group().reduceSum(function (d) {
return d.hits;
});
pieChart.width(200)
.height(200)
.slicesCap(4)
.dimension(cityDimension)
.group(cityGroup);
pieChart.filter = function() {};
rowChart.width(500)
.height(500)
.dimension(neighborhoodDimension)
.group(neighborhoodGroup);
dc.renderAll();
<div id="pieChart"> </div>
<div id="rowChart"> </div>
For displaying the current filters, you can use spans with class='reset' andclass='filter'` within the charts, as demonstrated in the annotated stock example
For displaying the counts of records filtered, you can use the dataCount widget or the numberDisplay widget.
Anything else, you'll have to implement the display itself, probably hooking the filtered event to determine when the filters have changed, and changing your own div using e.g. d3 or jQuery.
http://jsfiddle.net/N8Svz/5/
I'm sure I must be doing something kind of dumb; this seems like a textbook usage of HeirarchicalDataSource as far as I can tell.
var domtree = [{
"id": "linear1",
"element-class": "LinearLayout",
"children": [{
"id": "static1",
"element-class": "Static"
}, {
"id": "static2",
"element-class": "Static",
"children": [{
"id": "static3",
"element-class": "Static"
}]
}, {
"id": "4",
"element-class": "Error"
}]
}];
var inline = new kendo.data.HierarchicalDataSource({
data: domtree,
schema: {
model: {
id: "id",
children: "children"
}
}
});
$("#navtree").kendoTreeView({
dataSource: inline,
dataTextField: "id"
});
A big thank you to anyone who can point out what I'm doing wrong!
I just change the field children to items.
here's a jsfiddle: http://jsfiddle.net/nn007/N8Svz/6/
If you add the schema property like this it may work. You are telling the hierarchical datasource what the key for children is. Something like this:
schema: { model: { children: "children" } }
I have a jsonarray something like this:
var jsonarr= [{"displayName":"Rachita Jain","phoneNumbers":[{"value":"(787) 989-6756"},{"value":"
(897) 867-4666"}]},{"displayName":"Akanksha Mittal","phoneNumbers":[{"value":"(678) 456-4677"}]}]
I want to sort this using knockout based on displayName.
Use sort function array:
var jsonarr = [{
"displayName": "Rachita Jain",
"phoneNumbers": [{
"value": "(787) 989-6756"
}, {
"value": "(897) 867-4666"
}]
}, {
"displayName": "Akanksha Mittal",
"phoneNumbers": [{
"value": "(678) 456-4677"
}]
}];
jsonarr.sort(function (item1, item2) { return (item1.displayName > item2.displayName) ? 1 : -1 });
console.log(jsonarr);
See fiddle