score:14
A Bit of Hackery :-)
Chart.js calculates the top and bottom coordinates of the chart area and then uses this to draw the chart (points, bars, lines...). So if we manage to set / override the bottom coordinate of this chart area we can lift up / push down the x axis.
This bottom coordinate is in <<myChart instace>>.scale.endPoint
. So if we overwrite this before the chart is drawn we have control over where the x axis is drawn.
To do this we create a new chart type and override the draw function, like this.
Chart.types.Bar.extend({
name: "BarAlt",
draw: function(){
this.scale.endPoint = 200;
Chart.types.Bar.prototype.draw.apply(this, arguments);
}
});
Instead of passing in a hard coded value (200) we could configure this via options. So
Chart.types.Bar.extend({
name: "BarAlt",
draw: function(){
this.scale.endPoint = this.options.endPoint;
Chart.types.Bar.prototype.draw.apply(this, arguments);
}
});
and
ctx = document.getElementById("chart_a").getContext("2d");
mychart1 = new Chart(ctx).BarAlt(data, {
endPoint: 200
});
In fact, if we size both charts to be of the same height and width, we could even set this option to be same as the endPoint from the chart with the larger labels, thus aligning the x axis', like so
#chart_a,
#chart_b {
width: 320px;
height: 350px;
}
and assuming chart_b had the large labels and we drew that first (as myChart2)
ctx = document.getElementById("chart_a").getContext("2d");
mychart1 = new Chart(ctx).BarAlt(data, {
endPoint: mychart2.scale.endPoint
});
(and we don't have to use BarAlt for myChart2 because we don't need to control it's x axis position)
Now, all seems well and good, except for the fact that the bars seem to be animating from the original postion, which is a bit ugly. So we put in the easiest fix we can think of - turn off the animation!
ctx = document.getElementById("chart_a").getContext("2d");
mychart1 = new Chart(ctx).BarAlt(data, {
endPoint: mychart2.scale.endPoint,
animation: false
});
Working fiddle - http://jsfiddle.net/2kmf10hg/
I took the liberty of removing the 2nd set of charts.
Source: stackoverflow.com
Related Query
- Chartjs - keeping 2 graphs aligned side by side
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- Side effects from Chartjs for only *some* clients
- How to add ChartJS code in Html2Pdf to view image
- ChartJS changing graphs based upon Drop Down selection
- Keeping scales in sync across multiple graphs or dynamically changing scales
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- chartJS label on each side of the chart
- Django show Graphs with ChartJS
- Chartjs not working with d3 from csv source
- Chartjs graphs are not respecting height of container
- How to hide the labels of graphs that are not toggled in ChartJS
- Graphs sharing datasets in chartjs
- How to align labels at same side chartjs React
- ChartJS have xAxes labels match data source
- Updating Chartjs to 2.5 with custom code
- How can I move chartJs legend left side and change the width and Hight of the chart?
- Calling data from outside of Chartjs code
- VueJS + Chartjs - Chart only renders after code change
- How to show label at right side of Y axis same as left side of Y Axis ChartJS
- How to display multiple graphs in real time with chartjs
- How to keep side bars in Chartjs bar graph from clipping?
- ChartJS 2.0 differences in code help needed
- chartJS doesn't show graphs on iphone/ipad
- How can I put my label on the right hand side of my chart in Chartjs
- My Chartjs is not updating after setstate full code attached Reactjs Update
- Boxplot chartjs make min and max dynamic by leaving some space on both side
- chartjs - bar graphs size not increasing
- Chartjs doc examples are lightning fast but same code is slow when reproducing
- 'require is not defined' error when attempting to use chartjs in javascript code
More Query from same tag
- Uncaught ReferenceError : require is not defined - Chart.js
- Chart.js custom tooltip events
- How can I make two of my lines in Chart JS thicker
- Is there a way to draw floating rectangle using chart.js
- Using Chart.js with SvelteKit
- How do I fetch data from 2 tables and display into 1 chartjs
- Cannot read property 'labels' of undefined
- chart.js uncaught reference error while updating
- How to make integer scale in Chartjs
- How to get rid of large left and right margins in chart.js donut chart?
- Chart.js: Create custom major ticks on log x-Axis?
- Show center tick value radar chart- Chart.js
- react-chartjs-2 TypeError: undefined is not an object (evaluating 'nextDatasets.map')
- Chart.js Time series - x axis that change depending on timestamp
- Create data in data with Chartjs
- dyamically constructing javascript object
- Styling background (fill) with ChartJs and React
- How to filter dropdown based on the choice of another dropdown in JS?
- I am getting TS2339: Property 'chart' does not exist on type 'Window'?
- Pie Chart shows up only when I zoom in-out
- dynamically update the scale type of chartjs graph
- ChartJS v2: how to remove strange line in Line Chart
- How do you remove the data value from the mouse-over result in radar chart?
- PHP MySQL: count views per month for jQuery charts
- how to change the label color and set y-axis values 1k intervals and hide y-axis?
- ChartJS fails to render one of the lines in cartesian chart following update after change to max Y axis label value
- chartjs - show hide specific dataset with click on element outside graph
- generate multiple charts via loop for chart js
- JavaScript Chart.JS - issue keeping charts in two rows, instead everything in stacked into one column
- How to customize Title position with ChartJS