score:9
Shifting x axis Labels Vertically
The easiest way to do 1. is by adding spaces to your x labels. You can extend your chart type and override your initialize function to do this (increase 30 to something larger if your labels are long to start with anyway)
initialize: function(data){
data.labels.forEach(function(item, index) {
data.labels[index] += Array(Math.max(30 - item.length, 0)).join(" ");
})
Chart.types.Bar.prototype.initialize.apply(this, arguments);
},
Edit : As pointed out in the comments, this causes a horizontal shift as well and the label ends no longer align with the x axis markers.
Since both the x axis and the x labels are drawn in a single function and you have no other variables you can mess around with (safely) this means you'll have to change the actual scale draw function.
Look for a ctx.translate towards the end of the draw function and change it to
ctx.translate(xPos, (isRotated) ? this.endPoint + 22 : this.endPoint + 18);
You'll also have to adjust the endpoint (which drives the y limits) a bit so that the additional y offset doesn't cause the labels to overflow the chart (look for the line adjusting this in the draw override for 2.).
Leaving a gap on the Right Side
To do 2, you override your draw function (in your extended chart) and change xScalePaddingRight. However since this doesn't affect your horizontal grid lines you have to overlay a filled rectangle once your draw is complete. Your complete draw function would look like this
draw: function(){
// this line is for 1.
if (!this.scale.done) {
this.scale.endPoint -= 20
// we should do this only once
this.scale.done = true;
}
var xScalePaddingRight = 120
this.scale.xScalePaddingRight = xScalePaddingRight
Chart.types.Bar.prototype.draw.apply(this, arguments);
this.chart.ctx.fillStyle="#FFF";
this.chart.ctx.fillRect(this.chart.canvas.width - xScalePaddingRight, 0, xScalePaddingRight, this.chart.canvas.height);
}
Original fiddle - https://jsfiddle.net/gvdmxc5t/
Fiddle with modified Scale draw function - https://jsfiddle.net/xgc6a77a/ (I turned off animation in this one so that the endpoint is shifted only once, but you could just hard code it, or add some extra code so that it's done only once)
score:2
use this for chartjs 2.0
scales: {
xAxes: [{
barPercentage: 0.9,
categoryPercentage: 0.55
}]
score:5
The 'tickMarkLength' option extends the grid lines outside the chart and pushes the ticks down.
xAxes: [
{
gridLines: {
tickMarkLength: 15
},
}
]
Source: stackoverflow.com
Related Query
- Chart.js - Increase spacing between legend and chart
- Chart.JS spacing and padding
- chartjs - top and bottom padding of a chart area
- Increase padding between legend and chart with react-chartjs-2
- React chartjs-2 - Increase spacing between legend and chart
- Chart.js - Increase spacing between bottom legend and chart
- How can I reduce the spacing between my legend and chart proper in my Chart.JS bar chart, and increase it in the graph area?
- Chartjs: I only want left and right padding in pie chart
- chart.js line chart and "correct" spacing between points? (i.e. horizontal position based on percent of width, not fixed)
- How to increase the spacing between labels and the chart
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart.js adds unwanted padding on top and below chart with padding set to 0
- In Chart.js set chart title, name of x axis and y axis?
- Chart.js: bar chart first bar and last bar not displaying in full
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- chartjs datalabels change font and color of text displaying inside pie chart
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to add second Y-axis for Bar and Line chart in Chart.js?
- Chart.js Mixed Bar and Line chart with different scales
- Chart.js bar chart : Grid color and hide label
- Problem for display a chart with Chart.js and Angular
- How to use PrimeNg Chart of Width and Height?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Timeseries chart - formatting and missing data values
- Draw a horizontal and vertical line on mouse hover in chart js
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- Can Chart.js combines Line Chart and Bar Chart in one canvas
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Creating a grouped and stacked chart in Chart.js
More Query from same tag
- Showing Percentage and Total In stacked Bar Chart of chart.js
- How to use chart.js in angular
- How to Draw Gantt chart using chart js or other libraries
- Use multiple datasets with Javascript Chart
- Use JSON file with chart.js
- Chartjs scaleLabel position
- Gantt Chart Timeline Chart js 3
- Bubble Chart in Chartjs not working
- Chart bundle js is not showing dynamic data
- Chart.js Legend not showing
- chart.js onAnimationProgress progress percentage
- Chart.js dataset label issue
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- React-Chartjs-2 and Chartjs v3:Option Property
- Chart.js - x-axis and tooltip label shows "undefined" in Chrome when browser language is NOT English
- Border behind grid line
- Color by point in ChartJS.Blazor
- How to set vertical lines for new day on x-axis in ChartJS v3.x
- Annotation plugin not working with Chart.js 2.8.0
- How to access data outside the onclick function
- How to display data values on bar graph (chart.js)?
- impossible to remove scale from radar chart (chart.js)
- How to add an empty data point to a linechart on Chart.js?
- Chart.js hiding lable
- Duplicate of legends are Produced in ng2 chart
- How to use an Aurelia JSPM plugin with WebPack
- Save data from REST API Get method and use it one more time
- Chart.js data/label scale
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs