Show only specific labels on x-axis - d3.js

I am trying to select only specific range of labels to show up in my X-axis. Basically its a Time series and I wanted to select every alternate month to show up in the axis labels. But unable to figure out the way to do so.
Here is my fiddle so far
http://jsfiddle.net/sourabhtewari/1n19kwpv/
my code looks like this for the moment
var stuff = [{
"year": 2015,
"month": 12,
"s1": 0.38
},
{
"year": 2016,
"month": 1,
"s1": 0.39
},
{
"year": 2016,
"month": 2,
"s1": 0.43
},
{
"year": 2016,
"month": 3,
"s1": 0.40
},
{
"year": 2016,
"month": 4,
"s1": 0.39
},
{
"year": 2016,
"month": 5,
"s1": 0.39
},
{
"year": 2016,
"month": 6,
"s1": 0.38
},
{
"year": 2016,
"month": 7,
"s1": 0.37
},
{
"year": 2016,
"month": 8,
"s1": 0.37
},
{
"year": 2016,
"month": 9,
"s1": 0.35
},
{
"year": 2016,
"month": 10,
"s1": 0.37
},
{
"year": 2016,
"month": 11,
"s1": 0.36
},
{
"year": 2016,
"month": 12,
"s1": 0.37
},
{
"year": 2017,
"month": 1,
"s1": 0.35
},
{
"year": 2017,
"month": 2,
"s1": 0.36
},
{
"year": 2017,
"month": 3,
"s1": 0.37
},
{
"year": 2017,
"month": 4,
"s1": 0.38
},
{
"year": 2017,
"month": 5,
"s1": 0.35
},
{
"year": 2017,
"month": 0.36,
"s1": 0.36
},
{
"year": 2017,
"month": 7,
"s1": 0.36
},
{
"year": 2017,
"month": 8,
"s1": 0.35
},
{
"year": 2017,
"month": 9,
"s1": 0.36
},
{
"year": 2017,
"month": 10,
"s1": 0.37
},
];
var vals = [];
for (var i = 0; i < stuff.length; ++i) {
var date = new Date(stuff[i]["year"], stuff[i]["month"] - 1, 1);
vals.push({
x: date,
y: stuff[i]["s1"]
});
}
nv.addGraph(function() {
var data = [{
"values": vals,
"key": "s1",
"color": "#000000"
}];
var chart = nv.models.lineChart()
.forceY([0, 0.5]);
chart.xAxis.axisLabel("Date (m/y)")
.tickFormat(function(d, i) {
if (i % 2 == 1) d3.select(this).remove();
console.log(i);
return d3.time.format("%b-%y")(new Date(d))
});
chart.yAxis.axisLabel("s1").tickFormat(d3.format(","));
d3.select("#chart svg")
.datum(data)
.call(chart);
nv.utils.windowResize(function() {
d3.select("#chart svg").call(chart)
});
return chart;
});

Try adding ticks(d3.utcMonth) for xAxis.
chart.xAxis.axisLabel("Date (m/y)")
.ticks(d3.utcMonth)
.tickFormat(function(d,i) {
return d3.time.format("%b-%y")(new Date(d))
}) ;
Updated fiddle: http://jsfiddle.net/1n19kwpv/10/

I believe this answer to this is yes, you can, with one possible caveat (below).
According to this, you can do something like:
// Add the X Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.ticks(d3.timeMonth.every(2)));
The one thing I'm not positive about here is whether the timeMonth method is included in d3v3. It seems like nvd3 doesn't support d3v4 yet, and this example was made in d3v4.
Hope this helps.

Related

Y-Axis label prefix for XY Amcharts

I didn't find any solution how to add prefix/suffix for Y-Axis of XYAmChart. Seems like labelFunction are not supported for valueAxes in XY chart type. Any suggestions?
var chart = AmCharts.makeChart("chartdiv", {
"type": "xy",
"theme": "light",
"dataDateFormat": "DD-MM-YYYY",
"graphs": [
{
"id":"g8",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet":"round",
"bulletBorderAlpha":1,
"bulletColor":"#FFFFFF",
"bulletSize":5,
"dashLength":0,
"hideBulletsCount":50,
"lineThickness":2,
"lineColor":"#67b7dc",
"title":"Store 8",
"useLineColorForBulletBorder":true,
"xField":"d-1-c2",
"yField":"p-1-c2",
"xAxis":"g8",
"balloonText":"<span style='font-size:18px;'>[[d-1-c2]]</span><br>07/1/2017-12/31/2017"
},
{
"id":"g8-copy1",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet":"round",
"bulletBorderAlpha":1,
"bulletColor":"#FFFFFF",
"bulletSize":5,
"dashLength":15,
"hideBulletsCount":50,
"lineThickness":2,
"lineColor":"#67b7dc",
"title":"Store 8",
"useLineColorForBulletBorder":true,
"xField":"d-1-c1",
"yField":"p-1-c1",
"xAxis":"g8-copy1",
"balloonText":"<span style='font-size:18px;'>[[d-1-c1]]</span><br>1/1/2017-6/29/2017"
}
],
"valueAxes": [
{
"id": "g8",
"axisAlpha": 1,
"gridAlpha": 1,
"axisColor": "#b0de09",
"color": "#b0de09",
"dashLength": 5,
"centerLabelOnFullp": true,
"position": "bottom",
"type": "date",
"minp": "DD-MM-YYYY",
"markPeriodChange": false,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
},
{
"id": "g8-copy1",
"gridAlpha": 0,
"axisAlpha": 1,
"centerLabelOnFullp": false,
"position": "bottom",
"offset": 40,
"type": "date",
"minp": "DD-MM-YYYY",
'markPeriodChange': false,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}
],
"dataProvider": [
{
"d-1-c2":"01/01/2017",
"p-1-c2":"5684.6400"
},{
"d-1-c2":"01/02/2017",
"p-1-c2":"6468.9600"
},{
"d-1-c2":"01/03/2017",
"p-1-c2":"9032.7600"
},{
"d-1-c2":"01/04/2017",
"p-1-c2":"6385.9200"
},{
"d-1-c2":"01/05/2017",
"p-1-c2":"10087.3900"
},{
"d-1-c2":"01/06/2017",
"p-1-c2":"6136.3300"
},
{
"d-1-c1":"01/07/2017",
"p-1-c1":"4659.7000"
},{
"d-1-c1":"01/08/2017",
"p-1-c1":"9719.7100"
},{
"d-1-c1":"01/09/2017",
"p-1-c1":"4789.7300"
},{
"d-1-c1":"01/10/2017",
"p-1-c1":"7448.3900"
},{
"d-1-c1":"01/11/2017",
"p-1-c1":"6202.7200"
},{
"d-1-c1":"01/12/2017",
"p-1-c1":"9274.0300"
},{
"d-1-c1":"01/01/2018"
},{
"d-1-c2":"01/07/2017"
}
]
});
#chartdiv {
width: 100%;
height: 500px;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
labelFunction is definitely supported as you can see below.
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
} ],
Demo:
var chart = AmCharts.makeChart( "chartdiv", {
"type": "xy",
"theme": "light",
"balloon":{
"fixedPosition":true,
},
"dataProvider": [ {
"y": 10,
"x": 14,
"value": 59,
"y2": -5,
"x2": -3,
"value2": 44
}, {
"y": 5,
"x": 3,
"value": 50,
"y2": -15,
"x2": -8,
"value2": 12
}, {
"y": -10,
"x": 8,
"value": 19,
"y2": -4,
"x2": 6,
"value2": 35
}, {
"y": -6,
"x": 5,
"value": 65,
"y2": -5,
"x2": -6,
"value2": 168
}, {
"y": 15,
"x": -4,
"value": 92,
"y2": -10,
"x2": -8,
"value2": 102
}, {
"y": 13,
"x": 1,
"value": 8,
"y2": -2,
"x2": 0,
"value2": 41
}, {
"y": 1,
"x": 6,
"value": 35,
"y2": 0,
"x2": -3,
"value2": 16
} ],
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
} ],
"startDuration": 1.5,
"graphs": [ {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "circle",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value",
"xField": "x",
"yField": "y",
"maxBulletSize": 100
}, {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "diamond",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value2",
"xField": "x2",
"yField": "y2",
"maxBulletSize": 100
} ]
} );
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
You can also use unit and unitPosition if your custom labeling doesn't
require any additional logic that you can leverage through labelFunction.
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"unit": "unit-prefix:",
"unitPosition": "left"
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"unit": "unit-prefix:",
"unitPosition": "left"
} ],
Demo below.
var chart = AmCharts.makeChart( "chartdiv", {
"type": "xy",
"theme": "light",
"balloon":{
"fixedPosition":true,
},
"dataProvider": [ {
"y": 10,
"x": 14,
"value": 59,
"y2": -5,
"x2": -3,
"value2": 44
}, {
"y": 5,
"x": 3,
"value": 50,
"y2": -15,
"x2": -8,
"value2": 12
}, {
"y": -10,
"x": 8,
"value": 19,
"y2": -4,
"x2": 6,
"value2": 35
}, {
"y": -6,
"x": 5,
"value": 65,
"y2": -5,
"x2": -6,
"value2": 168
}, {
"y": 15,
"x": -4,
"value": 92,
"y2": -10,
"x2": -8,
"value2": 102
}, {
"y": 13,
"x": 1,
"value": 8,
"y2": -2,
"x2": 0,
"value2": 41
}, {
"y": 1,
"x": 6,
"value": 35,
"y2": 0,
"x2": -3,
"value2": 16
} ],
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"unit": "unit-prefix:",
"unitPosition": "left"
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"unit": "unit-prefix:",
"unitPosition": "left"
} ],
"startDuration": 1.5,
"graphs": [ {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "circle",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value",
"xField": "x",
"yField": "y",
"maxBulletSize": 100
}, {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "diamond",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value2",
"xField": "x2",
"yField": "y2",
"maxBulletSize": 100
} ]
} );
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>

Can I use a timeline with a D3 Force Network?

Can I bring this to life with force diagram?
I have a data set that comprises a series of objects that occur along a time line:
letters
orders
memos
phone calls
twitter messages
invoices
emails
The quantity of objects in the data set is driven by a query that extracts relevant objects from a database.
In a simple example there might be 10 objects for a single date or those ten might be spread over 2 or three days or be weeks apart.
All objects are linked, not necesarily directly, to each other.
A might join to B and to C, but D is linked to A only through a link to C.
The links need to be color coded based on the type of link and the line thickness needs to vary to represent the strngth of the link.
I have several ways of displaying this data using fixed coordinates based on the object date. They do the job but they don't look great.
I have created a D3 force diagram that plots the objects (nodes) and draws the links, but I'm at a complete lost as to how I can incoporate the time element.
Is there a way that I can plot the data so that the objects are roughly positioned along a date line? The positions do not need to be exact but the overall result should give the user a good indication of temproal proximity.
I'd also like the links to be drawn with an open B-spline which is something else that I can't get my head around
See sample data and code.
JSON Data
{
"nodes": [
{ "DocID": "77304", "date": "2001-01-21", "Type": "L" },
{ "DocID": "65884", "date": "2001-01-26", "Type": "F" },
{ "DocID": "77005", "date": "2001-02-02", "Type": "L" },
{ "DocID": "66162", "date": "2001-02-07", "Type": "E" },
{ "DocID": "93085", "date": "2001-03-20", "Type": "L" },
{ "DocID": "93101", "date": "2001-03-21", "Type": "P" },
{ "DocID": "93118", "date": "2001-03-28", "Type": "L" },
{ "DocID": "75890", "date": "2001-04-09", "Type": "L" },
{ "DocID": "93189", "date": "2001-04-11", "Type": "L" },
{ "DocID": "93225", "date": "2001-04-12", "Type": "L" },
{ "DocID": "75535", "date": "2001-04-19", "Type": "L" },
{ "DocID": "74916", "date": "2001-05-07", "Type": "I" },
{ "DocID": "58259", "date": "2001-05-16", "Type": "L" },
{ "DocID": "93565", "date": "2001-05-16", "Type": "O" },
{ "DocID": "95504", "date": "2001-05-17", "Type": "O" },
{ "DocID": "74408", "date": "2001-05-21", "Type": "L" },
{ "DocID": "95521", "date": "2001-05-21", "Type": "L" },
{ "DocID": "74343", "date": "2001-05-22", "Type": "L" }
],
"links": [
{ "source": 0, "target": 5, "strength": 9, "colour": 4 },
{ "source": 1, "target": 7, "strength": 4, "colour": 3 },
{ "source": 2, "target": 17, "strength": 2, "colour": 1 },
{ "source": 3, "target": 2, "strength": 3, "colour": 2 },
{ "source": 4, "target": 8, "strength": 9, "colour": 4 },
{ "source": 5, "target": 8, "strength": 5, "colour": 3 },
{ "source": 6, "target": 2, "strength": 3, "colour": 5 },
{ "source": 7, "target": 11, "strength": 5, "colour": 1 },
{ "source": 8, "target": 12, "strength": 3, "colour": 5 },
{ "source": 9, "target": 13, "strength": 9, "colour": 4 },
{ "source": 10, "target": 9, "strength": 4, "colour": 5 },
{ "source": 11, "target": 7, "strength": 5, "colour": 2 },
{ "source": 12, "target": 11, "strength": 3, "colour": 2 },
{ "source": 13, "target": 9, "strength": 3, "colour": 5 },
{ "source": 14, "target": 9, "strength": 2, "colour": 5 },
{ "source": 15, "target": 11, "strength": 3, "colour": 5 },
{ "source": 16, "target": 9, "strength": 6, "colour": 5 },
{ "source": 17, "target": 13, "strength": 4, "colour": 5 },
{ "source": 1, "target": 14, "strength": 2, "colour": 5 },
{ "source": 9, "target": 7, "strength": 2, "colour": 5 },
{ "source": 2, "target": 5, "strength": 5, "colour": 5 },
{ "source": 11, "target": 9, "strength": 7, "colour": 1 }
]
}
JS Code
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(150)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("Resources/testData.json", function (error, graph) {
if (error) throw error;
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.style("stroke", function (d) { return color(d.colour); })
.attr("class", "link")
.style("stroke-width", function (d) { return d.strength; });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.call(force.drag);
node.append("title")
.text(function (d) { return d.DocID; });
force.on("tick", function () {
link.attr("x1", function (d) { return d.source.x; })
.attr("y1", function (d) { return d.source.y; })
.attr("x2", function (d) { return d.target.x; })
.attr("y2", function (d) { return d.target.y; });
node.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
});
});

How to implement drill down in Stacked Bar Chart(amchart)

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>

D3 Multi-line Chart with categorical data on X-axis

I'm trying to draw a multiple line chart by using company branches data consisting of week days and values.
var data = [
{
"label": "branch-1",
"values": [
{ "day": "Monday", "value": 20},
{ "day": "Tuesday", "value": 18},
{ "day": "Wednesday", "value": 29},
{ "day": "Thursday", "value": 31},
{ "day": "Friday", "value": 37},
{ "day": "Saturday", "value": 25},
{ "day": "Sunday", "value": 19}
]
},
{
"label": "branch-2",
"values": [
{ "day": "Monday", "value": 32},
{ "day": "Tuesday", "value": 29},
{ "day": "Wednesday", "value": 37},
{ "day": "Thursday", "value": 41},
{ "day": "Friday", "value": 31},
{ "day": "Saturday", "value": 28},
{ "day": "Sunday", "value": 17}
]
}
]
FIDDLE LINK
Firstly, I'm having problem with days on x-axis. Lines are not generated due to "d" attributes have Nan values instead of correct x-coordinates on svg paths. I know that I should not use minX, maxX for x.domain, but I can't find what to use instead.
Secondly, I use d3.scale.category20() function to generate colors. But I'd like to know if I can use more than 20 colors in case of having more than 20 branches. Is it possible by using CSS?
Thanks in advance.
Ordinal scales need to have their domain set explicitly, that is, there's no notion of "min" or "max" for an ordinal domain. You have to do something like
x.domain(data[0].values.map(function (d) { return d.day; }));
Complete demo here. As for the colours, you can set anything you want in CSS or in the D3 code.

Why thekendo column chart on x-axis values are getting displayed not in order

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;
}
}

Resources