We have implemented stacked bar chart using amchart. We have displaying the top 5 selling products in the charts.When drill down charts, the product manufactures details will display on the chart.
You can use chart's event clickGraphItem when the user clicks on a column. Then dynamically update chart's dataProvider property and call validateData() method to take in new data.
Here's a complete working demo. It has more stuff in it than just what I mentioned above, like for instance updating labels of current level being displayed, as well as back button to go up a level in drill-down.
var chartData = [{
"category": 2009,
"income": 23.5,
"url":"#",
"description":"click to drill-down",
"months": [
{ "category": 1, "income": 1 },
{ "category": 2, "income": 2 },
{ "category": 3, "income": 1 },
{ "category": 4, "income": 3 },
{ "category": 5, "income": 2.5 },
{ "category": 6, "income": 1.1 },
{ "category": 7, "income": 2.9 },
{ "category": 8, "income": 3.5 },
{ "category": 9, "income": 3.1 },
{ "category": 10, "income": 1.1 },
{ "category": 11, "income": 1 },
{ "category": 12, "income": 3 }
]
}, {
"category": 2010,
"income": 26.2,
"url":"#",
"description":"click to drill-down",
"months": [
{ "category": 1, "income": 4 },
{ "category": 2, "income": 3 },
{ "category": 3, "income": 1 },
{ "category": 4, "income": 4 },
{ "category": 5, "income": 2 },
{ "category": 6, "income": 1 },
{ "category": 7, "income": 2 },
{ "category": 8, "income": 2 },
{ "category": 9, "income": 3 },
{ "category": 10, "income": 1 },
{ "category": 11, "income": 1 },
{ "category": 12, "income": 3 }
]
}, {
"category": 2011,
"income": 30.1,
"url":"#",
"description":"click to drill-down",
"months": [
{ "category": 1, "income": 2 },
{ "category": 2, "income": 3 },
{ "category": 3, "income": 1 },
{ "category": 4, "income": 5 },
{ "category": 5, "income": 2 },
{ "category": 6, "income": 1 },
{ "category": 7, "income": 2 },
{ "category": 8, "income": 2.5 },
{ "category": 9, "income": 3.1 },
{ "category": 10, "income": 1.1 },
{ "category": 11, "income": 1 },
{ "category": 12, "income": 3 }
]
}];
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "none",
"pathToImages": "/lib/3/images/",
"autoMargins": false,
"marginLeft": 30,
"marginRight": 8,
"marginTop": 10,
"marginBottom": 26,
"titles": [{
"text": "Yearly data"
}],
"dataProvider": chartData,
"startDuration": 1,
"graphs": [{
"alphaField": "alpha",
"balloonText": "<span style='font-size:13px;'>[[title]] in [[category]]:<b>[[value]]</b> [[additional]]</span> <br>[[description]]",
"dashLengthField": "dashLengthColumn",
"fillAlphas": 1,
"title": "Income",
"type": "column",
"valueField": "income","urlField":"url"
}],
"categoryField": "category",
"categoryAxis": {
"gridPosition": "start",
"axisAlpha": 0,
"tickLength": 0
}
});
chart.addListener("clickGraphItem", function (event) {
if ( 'object' === typeof event.item.dataContext.months ) {
// set the monthly data for the clicked month
event.chart.dataProvider = event.item.dataContext.months;
// update the chart title
event.chart.titles[0].text = event.item.dataContext.category + ' monthly data';
// let's add a label to go back to yearly data
event.chart.addLabel(
"!10", 25,
"Go back to yearly >",
"right",
undefined,
undefined,
undefined,
undefined,
true,
'javascript:resetChart();');
// validate the new data and make the chart animate again
event.chart.validateData();
event.chart.animateAgain();
}
});
// function which resets the chart back to yearly data
function resetChart() {
chart.dataProvider = chartData;
chart.titles[0].text = 'Yearly data';
// remove the "Go back" label
chart.allLabels = [];
chart.validateData();
chart.animateAgain();
}
#chartdiv {
width : 100%;
height : 300px;
}
<script type="text/javascript" src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="https://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv"></div>
Related
I am using AmCharts with a legend and chart cursor enabled however I don't want the graph values to appear in the legend on mouseover. How do I disable that? Please see my screenshot.
Chart Image
Assuming you're using the regular serial chart (post your code next time), you can suppress the legend value on mouseover by setting the legend's valueText to an empty string, e.g.
AmCharts.makeChart("chartdiv", {
// ...
legend: {
valueText: "",
// ...
},
// ...
});
Demo below:
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"legend": {
"useGraphSettings": true,
"valueText": ""
},
"dataProvider": [{
"year": 1930,
"italy": 1,
"germany": 5,
"uk": 3
}, {
"year": 1934,
"italy": 1,
"germany": 2,
"uk": 6
}, {
"year": 1938,
"italy": 2,
"germany": 3,
"uk": 1
}, {
"year": 1950,
"italy": 3,
"germany": 4,
"uk": 1
}, {
"year": 1954,
"italy": 5,
"germany": 1,
"uk": 2
}, {
"year": 1958,
"italy": 3,
"germany": 2,
"uk": 1
}],
"valueAxes": [{
"integersOnly": true,
"maximum": 6,
"minimum": 1
}],
"startDuration": 0,
"graphs": [{
"balloonText": "place taken by Italy in [[category]]: [[value]]",
"bullet": "round",
"title": "Italy",
"valueField": "italy",
"fillAlphas": 0
}, {
"balloonText": "place taken by Germany in [[category]]: [[value]]",
"bullet": "round",
"title": "Germany",
"valueField": "germany",
"fillAlphas": 0
}, {
"balloonText": "place taken by UK in [[category]]: [[value]]",
"bullet": "round",
"title": "United Kingdom",
"valueField": "uk",
"fillAlphas": 0
}],
"chartCursor": {
"zoomable": false
},
"categoryField": "year"
});
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv" style="width: 100%; height: 300px"></div>
How to create a function to get the month as Jan,feb.. displayed in kendo chart x axis.
var internetUsers = [ {
"Month": "1",
"year": "2010",
"value": 1
}, {
"Month": "2",
"year": "2010",
"value": 2
}, {
"Month": "3",
"year": "2010",
"value": 3
}, {
"Month": "4",
"year": "2010",
"value": 4
}, {
"Month": "5",
"year": "2010",
"value": 5
},
{
"Month": "6",
"year": "2010",
"value": 6
}, {
"Month": "7",
"year": "2010",
"value": 7
}, {
"Month": "8",
"year": "2010",
"value": 8
}];
function createChart() {
$("#chart").kendoChart({
theme: $(document).data("kendoSkin") || "default",
dataSource: {
data: internetUsers,
group: {
field: "year"
},
sort: {
field: "year",
dir: "asc"
}
},
title: {
text: "Sales"
},
legend: {
position: "bottom"
},
seriesDefaults: {
type: "column"
},
series: [{
field: "value"
}],
valueAxis: {
labels: {
format: "{0}$"
},
line: {
visible: false
},
axisCrossingValue: 0
},
categoryAxis: {
field: "Month"
},
tooltip: {
visible: true,
format: "{0}%",
template: "#= series.name #: #= value #"
}
});
}
$(document).ready(function() {
setTimeout(function() {
// Initialize the chart with a delay to make sure
// the initial animation is visible
createChart();
$("#example").bind("kendo:skinChange", function(e) {
createChart();
});
}, 400);
});
</script>
You could use kendoui's time format function:
kendo.toString(new Date(2000, value, 1), "MMMM"); // if value = 9, it would output September
Check out kendoui's date format page. It is VERY helpful.
http://docs.telerik.com/kendo-ui/getting-started/framework/globalization/dateformatting
First add an array to hold the month names, like so:
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"];
then modify the categoryAxis to use this array
categoryAxis: {
field: "Month",
labels: {
template: "#=monthNames[value - 1]#"
},
},
JS Bin
<div id="example" class="k-content">
<div class="chart-wrapper">
<div id="chart"></div>
</div>
<script>
var internetUsers = [ {
"Month": "1",
"year": "2010",
"value": 1
}, {
"Month": "2",
"year": "2010",
"value": 2
}, {
"Month": "3",
"year": "2010",
"value": 3
}, {
"Month": "4",
"year": "2010",
"value": 4
}, {
"Month": "5",
"year": "2010",
"value": 5
},
{
"Month": "6",
"year": "2010",
"value": 6
}, {
"Month": "7",
"year": "2010",
"value": 7
}, {
"Month": "8",
"year": "2010",
"value": 8
},
{
"Month": "9",
"year": "2010",
"value": 9
}, {
"Month": "10",
"year": "2010",
"value": 10
}, {
"Month": "11",
"year": "2010",
"value": 11
},
{
"Month": "12",
"year": "2010",
"value": 17117.00
},
{
"Month": "1",
"year": "2011",
"value": 12
}, {
"Month": "2",
"year": "2011",
"value": 13
}, {
"Month": "3",
"year": "2011",
"value": 4
}, {
"Month": "4",
"year": "2011",
"value": 6
}, {
"Month": "5",
"year": "2011",
"value": 24
},
{
"Month": "6",
"year": "2011",
"value": 3
}, {
"Month": "7",
"year": "2011",
"value": 35
}, {
"Month": "8",
"year": "2011",
"value": 34
},
{
"Month": "9",
"year": "2011",
"value": 22
}, {
"Month": "10",
"year": "2011",
"value": 21
}, {
"Month": "11",
"year": "2011",
"value": 215
},
{
"Month": "12",
"year": "2011",
"value": 12
}];
function createChart() {
$("#chart").kendoChart({
theme: $(document).data("kendoSkin") || "default",
dataSource: {
data: internetUsers,
group: {
field: "year"
},
sort: {
field: "year",
dir: "asc"
}
},
title: {
text: "Sales"
},
legend: {
position: "bottom"
},
seriesDefaults: {
type: "column"
},
series: [{
field: "value"
}],
valueAxis: {
labels: {
format: "{0}$"
},
line: {
visible: false
},
axisCrossingValue: 0
},
categoryAxis: {
field: "Month"
},
tooltip: {
visible: true,
format: "{0}%",
template: "#= series.name #: #= value #"
}
});
}
$(document).ready(function() {
setTimeout(function() {
// Initialize the chart with a delay to make sure
// the initial animation is visible
createChart();
$("#example").bind("kendo:skinChange", function(e) {
createChart();
});
}, 400);
});
</script>
</div>
http://jsbin.com/oxakup/17/edit
You need to sort your dataSource by month instead of year like the following:
sort: { field: "Month", dir: "asc" }
Now your months are sorting correctly based off your values, although the month data types are string instead of number, hence the reason this will now sort by 1, 10, 11, 12, 2, 3 etc..
You can work around this by fixing it in your JSON data source, but if you're unable to change that, you can use the following to format the data in the dataSource before processing:
schema: {
data: function(response) {
for (var i = 0; i < response.length; i++) {
response[i].Month = new Number(response[i].Month);
}
return response;
}
}
I have a JSON data which in the following format:
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
The tree for this JSON data is:
I want to loop through all the nodes and extract the "id" of all data maintaining its parent child hierarchy. How can i do this using recursive function.
The idea is to parse the json structure in ruby.
I just coded a node js code, you can still make it work on a browser in js, but you will need to download the underscore library here.
_und = require('underscore');
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_und.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _und.map(data, parse_tree);
console.log("Result: %j", result);
You can put that into a file, and execute it with node (by download underscore with nmp install node).
On plain js it would be something like:
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript">
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _.map(data, parse_tree);
console.log("Result: %j", result);
</script>
Ruby code:
require 'json'
data = <<EOF
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
EOF
json = JSON.parse(data)
def parse_tree(n)
if n["children"]
{id: n["id"], children: n['children'].map {|c| parse_tree(c)} }
else
{id: n["id"]}
end
end
result = json.map {|n| parse_tree(n) }
puts result
I am trying to display a chart with tool tips but I cannot seem to figure out how you put in tool tips when you write your own JSON for the chart.
Is this possible?
Here is the JSON I have written so far:
var days_of_week_chart = {
"elements": [
{
"type": "bar",
"values": [33,0,0,0,0,0,0,],
}
],
"title": {
"text": "Visitors By Day of Week"
},
"x_axis":{
"stroke":1,
"tick_height":10,
"colour":"#d000d0",
"grid_colour":"#00ff00",
"labels": {
"labels": ["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday",]
}
},
"y_axis": {
"stroke": 4,
"steps": 5,
"tick_length": 5,
"colour": "#d000d0",
"grid_colour": "#00ff00",
"offset": 0,
"max": 33 }
};
You need to have the tooltip element, and tip element in the data series:
{ "elements": [ { "type": "hbar", "values": [ { "right": 4 }, { "right": 8 }, { "right": 3 }, { "right": 4 }, { "right": 7 }, { "right": 8 } ], "colour": "#86BBEF", "tip": "Months: #val#" } ], "title": { "text": "Total hours on project mayhem" }, "x_axis": { "offset": false, "min": 0, "max": 10 }, "y_axis": { "offset": 1, "labels": [ "Jeff", "Geoff", "Bob", "Terry", "Duncan", "monk.e.boy" ] }, "tooltip": { "mouse": 2, "stroke": 1, "colour": "#000000", "background": "#ffffff" } }