score:5
Accepted answer
It's a margin issue. Your effective height is height - margin.top - margin.bottom
. I've fixed the example accordingly (with readability improvement).
To use months in ordinal scale you must initialize the scale with, yeah, months. Then use the scale to map an ordinal value to a x-scale value, e.g. x0Scale(d.Month)
.
function createGroupchart()
{
var width = 500;
var height = 180;
var margin = {
'top' : 5,
'right' : 20,
'bottom' : 20,
'left' : 50
};
var colors = [
["TotalTicket", "#377EB8"],
["TotalOrders", "#4DAF4A"]
];
var dataset = [
{"Month": "Jan", "TotalOrders": 7324, "TotalTicket": 23954, "Year": "2013", "Avg": "10%"},
{"Month": "Feb", "TotalOrders": 3738, "TotalTicket": 12319, "Year": "2013", "Avg": "12%"},
{"Month": "March", "TotalOrders": 4466, "TotalTicket": 15524, "Year": "2013", "Avg": "14%"},
{"Month": "May", "TotalOrders": 100, "TotalTicket": 200, "Year": "2013", "Avg": "50%"},
{"Month": "Oct", "TotalOrders": 109, "TotalTicket": 2000, "Year": "2013", "Avg": "9%" }
];
var x0Scale = d3.scale.ordinal()
.domain(dataset.map(function(d)
{
return d.Month;
}))
.rangeRoundBands([margin.left, width - margin.right], 0.1);
var x1Scale = d3.scale.ordinal()
.domain([0, d3.max(dataset, function(d)
{
return d.TotalOrders;
})])
.rangeRoundBands([margin.left, width - margin.right], 0.1);
var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d)
{
return (d.TotalTicket + 10000);
})])
.range([height - margin.top - margin.bottom, 0]);
var xAxis = d3.svg.axis()
.scale(x0Scale)
.tickSize(5)
.tickSubdivide(true)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.tickSize(5)
.orient("left")
.tickSubdivide(true);
var commaFormat = d3.format(',');
// SVG element
var svg = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("margin", margin);
// Draw X Axis values
svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0,' + (height - margin.bottom - margin.top) + ')')
.call(xAxis);
// Draw Y Axis values
svg.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(' + margin.left + ',0)')
.call(yAxis);
// Graph Bars
var sets = svg.selectAll(".set")
.data(dataset)
.enter().append("g")
.attr("class", "set")
.attr("transform", function(d)
{
return "translate(" + x0Scale(d.Month) + ",0)"
});
sets.append("rect")
.attr("class", "TotalTicket")
.attr("width", x0Scale.rangeBand() / 2)
.attr("y", function(d)
{
return yScale(d.TotalTicket);
})
.attr("x", x0Scale.rangeBand() / 2)
.attr("height", function(d)
{
return height - margin.top - margin.bottom - yScale(d.TotalTicket);
})
.attr("fill", colors[0][1]);
sets.append("rect")
.attr("class", "TotalOrders")
.attr("width", x0Scale.rangeBand() / 2)
.attr("y", function(d)
{
return yScale(d.TotalOrders);
})
.attr("height", function(d)
{
return height - margin.top - margin.bottom - yScale(d.TotalOrders);
})
.attr("fill", colors[1][1])
var legend = svg.append("g")
.attr("class", "legend")
.attr("height", 100)
.attr("width", 100)
.attr('transform', 'translate(-30,20)');
var legendRect = legend.selectAll('rect').data(colors);
legendRect.enter()
.append("rect")
.attr("x", width - 65)
.attr("width", 10)
.attr("height", 10);
legendRect
.attr("y", function(d, i)
{
return i * 20;
})
.style("fill", function(d)
{
return d[1];
});
var legendText = legend.selectAll('text').data(colors);
legendText.enter()
.append("text")
.attr("x", width - 52);
legendText
.attr("y", function(d, i)
{
return i * 20 + 9;
})
.text(function(d)
{
return d[0];
});
svg.selectAll("text")
.data(dataset, function(d)
{
return d.Avg;
})
.enter()
.append("text")
.text(function(d)
{
return d.Avg;
})
.attr("class", "bartext")
.attr("text-anchor", "middle")
.attr("x", function(d)
{
return x0Scale(d.Month) + x0Scale.rangeBand() / 2;
})
.attr("y", function(d)
{
return yScale(d.TotalTicket) - 10;
})
.attr("font-family", "sans-serif")
.attr("font-size", "15px")
.attr("fill", "black");
}
createGroupchart()
svg .axis path, .axis line {
fill : none;
stroke : #000;
shape-rendering : crispEdges;
}
svg .x.axis path {
display : none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id='chart'></div>
Source: stackoverflow.com
Related Query
- X axis not displaying on bar chart using d3.js
- d3.js axis labels not displaying in responsive bar chart
- Right Axis not displaying on Dual Axis Grouped Bar and Line Chart D3
- X axis not displaying on Multi Series Line Chart using d3.js
- Make simple bar chart using C3 with separate columns on the x axis
- Y axis label not displaying large numbers - Multi-Bar Chart
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- Stacked Bar Chart using d3.js stack layout; the bar sections not always in correct place
- D3.js bar chart - axis and labels not working / transitioning
- D3.js V5 - Why is my bar chart axis not scaling?
- D3 Multi Line Chart Not Displaying Properly With X Axis Value
- Why does my bar chart using scaleBand not line up with my tick marks?
- c3 chart not displaying changes on axis
- after making bar chart inside SVG using D3 , it is not fitting the entire SVG
- X axis label not displayed in bar chart with scroll and zoom feature - d3js, Brushing
- D3 bar chart axis not scaling correctly
- dc.js Stacked Bar Chart having only one column and with elastic X Axis is not rendered properly
- Pie chart is not displaying as Responsive using d3.js
- How can I make a bar chart starting from the 0 point of the y axis and not from the bottom of the svg?
- Create Stacked Bar chart for Timeseries X axis using C3.js
- Stacked Bar chart not displaying correctly
- First 2 entries on stacked bar chart not displaying in dc.js / d3.js
- D3 stacked bar chart not able to see full x axis lables
- D3.js bar chart not selecting or binding "date" data to Y axis label "text" elements on first ajax request
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- Hide all text on y axis dc.js bar chart
- dc.js barChart first and last bar not displaying fully
- d3 bar chart y axis ticks and grid lines above max value
- Making a grouped bar chart using d3.js
- DC.js bar chart with date axis
More Query from same tag
- Reading DOT files in javascript/d3
- D3.js draw diamonds and connecting links in a decision tree
- How to prevent a SVG marker (arrow head) to inherit path's stroke width?
- Find tag name of svg element using D3
- Exporting svg to png or other image with styling, using JavaScript
- d3.js dendrogram : binding old and new properties to the svg element
- How to visualise the relationship between two nodes in Neo4j using d3.js?
- Text Inside SVG Circle in D3 Doesn't Show
- D3 fill shape with image using pattern
- D3 Javascript - these lines won't move
- Is there a way to change C3 legend item tiles to be icons?
- Make D3 graphs responsive
- How to draw straight line in d3.js (horizontally and vertically)
- D3 styling of text element
- Dimplejs Vertical 100% Bar Decimal
- Focus on form field after animation
- How to update a single data point in d3 without touching other elements?
- Reading csv data file with c3.js
- Lost flow chart lines after upgrading from 3.5.5 to 4.2.8
- D3 Force Directed Map conditionally setting fill color of a nodes
- Add lines to SVG donut
- D3 svg click event and circle click event overlapping
- Updating child elements of data joins in d3.js
- D3: d3.behavior.drag
- How to modify a d3 force layout with voronoi polygons to trigger events on grouped elements?
- Adding a variable number to data
- Can I create a WordPress view to create a JSON file to be used by javascript in a page?
- Run an java script after controller has been loaded in CodeIgniter
- D3 Zoom behavior
- How to render a directive only after data is loaded from tsv in angular js