I've implementing barchart to denote my result set. I am trying to create this design using c3js. I referred the documentation but have not getting the shaded design. Please someone guide me to solve this.
My Code Snippet is,
var chart = c3.generate({
bindto: '#chart',
size : {
width: 600,
height: 200
},
data: {
columns: [
["data1", 40, 20, 20, 20],
["data2", 20, 10, 30, 30]
],
type: 'bar',
groups: [
['data1', 'data2']
],
order: null,
labels: {
format: function(v, id, i, j) {
return v;
}
}
},
bar: {
space: 0.2,
width: {
ratio: 0.2 // this makes bar width 50% of length between ticks
}
},
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.3.0/c3.min.js"></script>
<div id="chart"></div>
My requirement is to design like below image in bars in barchart,
Thanks
The solution in the snippet introduces some d3 SVG manipulation which puts a region over the bars of each targeted series. The region has a hatched grey line and a semi-transparent fill. The function to do this is written to be portable i.e. outside of the C3 construtor params, and is invoked by the onrendered c3 event.
The doClone() function duplicates nodes in place which is useful to know, and the creation of the pattern and use as a colour via 'url(#patternid)' is also a potentially useful technique.
Also available at https://codepen.io/JEE42/pen/yRWbvq
function doClone(node){ // clone the given node
return d3.select(node.parentNode.insertBefore(node.cloneNode(true), node.nextSibling));
}
function hatchBars(hatchTargets){ // Place a hatching pattern over the target bars.
for (var i = 0; i < hatchTargets.length; i = i + 1){
d3.select('.c3-bars-' + hatchTargets[i]).each(function(d, i){
d3.select(this).selectAll('path').each(function(d, i){
var node = d3.select(this).node();
var daClone = doClone(node);
daClone
.style('fill', 'url(#hash4_4)')
.style('stroke', 'url(#hash4_4)');
});
})
}
}
c3.chart.internal.fn.afterInit = function () {
d3.select('defs')
.append('pattern')
.attr('id', "hash4_4") // use id to get handle in a moment
.attr('width', 14)
.attr('height', 14)
.attr('patternUnits', "userSpaceOnUse")
.attr('patternTransform', "rotate(45 0 0 )")
.append("rect")
.attr('width', 14)
.attr('height', 14)
.attr('fill', '#00000000') // transparent background
d3.select('#hash4_4') // get the pattenn
.append('line') // add a line
.attr('y2', 14)
.style('stroke', "#00000044") // semi-transparent bars
.attr('stroke-width', 14)
};
//
// Standard C3 chart render with one twist which is the onrendered event call at the end.
//
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', -30, 200, 200, 400, -150, 250],
['data2', 130, 100, -100, 200, -150, 50],
],
type: 'bar',
groups: [
['data1', 'data2']
]
},
grid: {
y: {
lines: [{value:0}]
}
},
onrendered: function () { // execute after drawn
hatchBars(['data2']); // Place a hatching pattern over the target bars.
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.7/c3.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.7/c3.min.js"></script>
<div class='chart-wrapper'>
<div class='chat' id="chart"></div>
</div>
I have created a kendo window with a grid inside it. I want to create a scroll bar on the grid only and not on the kendo window. I am unable to create the scroll bar on the grid.
Please suggest me the appropriate solution for this issue.
HTML part is as follows :
<html>
<body>
<div id = "mywindow">
<h1>Work Order</h1>
<div id="grid"></div>
</div>
Go
</body>
</html>
javascript part is as follows :
$(document).ready(function () {[screenshot for error in scrollbar][1]
jQuery('#mywindow').kendoWindow({
width:300,
height:400,
animation: false,
autoFocus: true,
visible:false,
resizable : false
});
$('#btnGO').click(function(){
$('#mywindow').data('kendoWindow').center().open();
});
$("#grid").kendoGrid({
height : 100,
selectable: "multiple cell",
allowCopy: true,
columns: [
{ field: "student", title: "student"},
{ field: "student", title:"Mins"}
],
scrollable : true,
dataSource : [
{student: 10,mins: 120},
{student: 11,mins:100},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90},
{student: 12,mins: 90}
]
});
$('#grid .k-grid-content').scroll(function ()
{ alert('I am scrolling ...'); });
});
css part is as follows :
.k-window div.k-window-content
{
overflow: hidden;
}
I do not know where is the logic, but if you initialize the the grid first, then the window it works...
Here are a sample in Telerik Dojo
Let's presume you want to have the kendo grid fill the remainder of kendo window after the <H1>.
Add a window activate event handler that will compute a height for the grid. The desired height is height of containing parent - top position of grid. NOTE: The containing parent is the content of the window.
jQuery('#mywindow').kendoWindow({
... ,
activate: function (e) {
var H = $("#grid").offsetParent().height(); // height of containing parent in which grid is placed
var T = $("#grid").position().top; // top of grid relative to containing parent
$("#grid").getKendoGrid().setOptions ({height: H-T});
},
...
});
Everything else is automatic and your grid will have scroll bars.
Also, in the question code, one of the columns is incorrectly specified:
{ field: "student", title:"Mins"}
if should be
{ field: "mins", title: "Mins"}
If you are still having a problem add more info to the question, such as Kendo version, browser and version, jQuery version.
I want to make a bar chart with two sets of stacked bars which are grouped together to compare the two groups of stacked bars. This should be displayed in the following manner:
I have gone through this link
But it didn't help me plot something like you see in the above image. I even tried sending two data sets like [[s1, s2, s3], [s4, s5, s6]] But it didn't help me plot the chart.
Does anyone know how to do it?
Any help will be greatly appreciated.
Thanks in advance.
Setting the option stackSeries: true will create the desired display for bar charts.
Official sources:
jqPlot Source code: source code, version 1.0.8r1250 of
2013-03-27. For this issue, src/jqplot.core.js lines 2499, 2563, and 2649.
jqPlot Documentation: It says that the API Documentation is most accurate. you can also see the webpage, README.txt,
optionsTutorial.txt, jqPlotOptions.txt, jqPlotOptions.txt,
jqPlotCssStyling.txt, usage.txt, changes.txt in the 1.0.8r1250
general release
The jqPlot documentation is not up to date so I took a look at the source code. Unfortunately, there is no way to directly have two sets of bars with a stacked-bar chart. The jqPlot.stackSeries property is only a boolean value. It's only function is to tell jqPlot to stack each series on top of each other for as many bars as there are values in the different series. Each series is plotted one value per bar with the first series being on the bottom. In other words, all [0] values are plotted in the first bar, [1] values in the second, etc. The amount shown within the bar is the sum of the [n] value for the current series and all prior series. There is no way to specify that there are two, or more, groupings of series. The capability to do what is desired just does not exist in jqPlot.
But you can accomplish what you desire:
The fact that jqPlot does not natively support what you want does not mean that you can not do it, merely that you need to get creative.
The graph you desire can be looked at as being two separate graphs that have been overlaid upon each other with spacing between the bars on the individual graphs permitting enough space (seriesDefaults.rendererOptions.barMargin) for the bars from the other graph to be overlaid next to them.
You can use jqPlot to create:
That graph has the scale, background and grid-lines you desire set to be visible. Note that the graph has an extra bar in it. This is needed to provide enough background and grid-lines for the last bar provided by the other graph.
You can also use jqPlot to create the second graph:
This graph has the scale and grid-lines set in jqPlot to not be visible.
seriesDefaults.axes.xaxis.tickOptions.show = false;
seriesDefaults.axes.yaxis.tickOptions.show = false;
etc.
The background is set to be transparent. Note that you are going to need to offset the position of this graph somewhat to the right when positioning the <div> relative to the first graph.
Overlaid, you end up with:
You then use a blank <div> with the same background color as the background color of your webpage and overlay that to cover the extra bar on the first graph, but leaving enough of the first graph's background and grid-lines to extend a bit past the last bar of the second graph.
You will end up with:
You can see a working solution at at JSFiddle using jqPlot 1.0.8r1250.
Comparing the original request vs. the final version of the graph produced using this method you can see that they are very close:
Between the two the most noticeable difference is the larger space between the Y-axis in the jqPlot version. Unfortunately, there does not appear to be an option to reduce that amount for stacked bar charts.
Note that the lack of a border on the right of the graph this code produces is intentional because it did not exist in the original request. Personally, I prefer having a border on the right side of the graph. If you change the CSS a bit, that is easy to obtain:
My preferred version of the graph includes a border on the left and balances the whitespace:
You can see a working JSFiddle of this version.
All-in-all it is not that difficult. It would, of course, be easier if jqPlot supported multiple sets of bars. Hopefully it will at some point. However, the last release was 2013-03-27 and there does not appear to have been any development work after that time. Prior to that there were releases every few months. But, jqPlot is released under the GPL and MIT licenses so anyone could continue the work.
$(document).ready(function () {
//Numbers derived from desired image
//var s1 = [10, 29, 35, 48, 0];
//var s2 = [34, 24, 15, 20, 0];
//var s3 = [18, 19, 26, 52, 0];
//Scale to get 30 max on plot
var s1 = [2, 5.8, 7, 9.6, 0];
var s2 = [6.8, 4.8, 3, 4, 0];
var s3 = [13.6, 8.8, 3, 7.8, 0];
plot4 = $.jqplot('chart4', [s1, s2, s3], {
// Tell the plot to stack the bars.
stackSeries: true,
captureRightClick: true,
seriesColors: ["#1B95D9", "#A5BC4E", "#E48701"],
seriesDefaults: {
shadow: false,
renderer: $.jqplot.BarRenderer,
rendererOptions: {
// jqPlot does not actually obey these except barWidth.
barPadding: 0,
barMargin: 66,
barWidth: 38,
// Highlight bars when mouse button pressed.
// Disables default highlighting on mouse over.
highlightMouseDown: false
},
title: {
text: '', // title for the plot,
show: false,
},
markerOptions: {
show: false, // wether to show data point markers.
},
pointLabels: {
show: false
}
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
tickOptions: {
show: false
},
lastPropertyConvenience: 0
},
yaxis: {
// Don't pad out the bottom of the data range. By default,
// axes scaled as if data extended 10% above and below the
// actual range to prevent data points right on grid boundaries.
// Don't want to do that here.
padMin: 0
}
},
legend: {
show: false,
location: 'e',
placement: 'outside'
},
grid: {
drawGridLines: true, // wether to draw lines across the grid or not.
shadow: false, // no shadow
borderWidth: 1,
background: 'white', // CSS color spec for background color of grid.
lastPropertyConvenience: 0
},
lastPropertyConvenience: 0
});
});
$(document).ready(function () {
//Numbers derived from desired image
//var s1 = [10, 29, 35, 48, 0];
//var s2 = [34, 24, 15, 20, 0];
//var s3 = [18, 19, 26, 52, 0];
//Scale to get 30 max on plot
var s1 = [2, 5.8, 7, 9.6, 0];
var s2 = [6.8, 4.8, 3, 4, 0];
var s3 = [3.6, 3.8, 5.2, 10.4, 0];
plot4 = $.jqplot('chart5', [s1, s2, s3], {
// Tell the plot to stack the bars.
stackSeries: true,
captureRightClick: true,
seriesColors: ["#754DE9", "#666666", "#000000"],
seriesDefaults: {
shadow: false,
renderer: $.jqplot.BarRenderer,
rendererOptions: {
// jqPlot does not obey these options except barWidth.
show: true,
barPadding: 0,
barMargin: 66,
barWidth: 38,
// Highlight bars when mouse button pressed.
// Disables default highlighting on mouse over.
highlightMouseDown: false
},
title: {
text: '', // title for the plot,
show: false,
},
markerOptions: {
show: false, // wether to show data point markers.
},
pointLabels: {
show: false
}
},
axesDefaults: {
//show: false
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
tickOptions: {
show: false
},
lastPropertyConvenience: 0
},
yaxis: {
show: false,
// Don't pad out the bottom of the data range. By default,
// axes scaled as if data extended 10% above and below the
// actual range to prevent data points right on grid boundaries.
// Don't want to do that here.
padMin: 0,
tickOptions: {
show: false
},
}
},
legend: {
show: false,
location: 'e',
placement: 'outside'
},
grid: {
drawGridLines: false, // wether to draw lines across the grid or not.
shadow: false, // no shadow
borderWidth: 10,
background: 'transparent', // CSS color for background color of grid.
gridLineColor: 'transparent', // *Color of the grid lines.
borderColor: 'transparent', // CSS color for border around grid.
lastPropertyConvenience: 0
},
lastPropertyConvenience: 0
});
});
#cover1 {
padding:0;
margin: 0;
background-color: white;
left: 451px;
width: 88px;
/* Uncomment the next three lines to have a border on the right of the graph and
balanced whitespace:*/
/*
border-left: 2px solid #CCCCCC;
left:476px;
width: 62px;
*/
}
#chart4 .jqplot-xaxis-tick {
visibility: hidden;
}
#chart5 .jqplot-xaxis-tick {
visibility: hidden;
}
#chart4 .jqplot-yaxis-tick {
font: 9px arial
}
<link class="include" rel="stylesheet" type="text/css" href="http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.css" />
<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="http://cdn.jsdelivr.net/excanvas/r3/excanvas.js"></script><![endif]-->
<script class="include" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Main jqPlot -->
<script class="include" type="text/javascript" src="http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.js"></script>
<!-- Additional jqPlot plugins -->
<script class="include" type="text/javascript" src="http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.barRenderer.min.js"></script>
<script class="include" type="text/javascript" src="http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.categoryAxisRenderer.min.js"></script>
<div style="position:absolute; left:10px; top:10px;">
<div id="chart4" style="width:548px; height:185px;"></div>
<div id="chart5" style="width:536px; height:185px; top:-185px; left:53px;"></div>
<div id="cover1" style="position: relative; height: 152px; top:-361px;"></div>
</div>
The above code is based on that at the example page listed in the question.
Practical solution...
$(document).ready(function(){
var s1 = [2, 0, 0, 10,11,0, 6, 2, 0,10,11];
var s2 = [7, 0, 0, 4,11,0, 6, 2, 0,10,11];
var s3 = [4, 0, 0, 7,11,0, 6, 2, 0,10,11];
var s4 = [0, 20, 0, 0,0,0, 0, 0, 0,0,0];
plot3 = $.jqplot('chart3', [s1, s2, s3,s4], {
stackSeries: true,
captureRightClick: true,
seriesDefaults:{
renderer:$.jqplot.BarRenderer,
rendererOptions: {
barMargin: 30,
highlightMouseDown: true
},
pointLabels: {show: true}
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer
},
yaxis: {
padMin: 0
}
},
legend: {
show: true,
location: 'e',
placement: 'outside'
}
});
$('#chart3').bind('jqplotDataClick',
function (ev, seriesIndex, pointIndex, data) {
$('#info3').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
}
);
});
Image: