score:9
I have finally pushed my port of multi axis feature to the master on this fork. As an extremely brief overview of what is happening i have added a collection of axes into the scale. Each axis is able to calculate where a value should be position in terms of y against it's own scale. The only thing that developers need to now do is say which axes group each dataset needs to belong. Each axis can also be told what colour it should be and what side of the graph it should appear on.
How to use it - within each of your datasets declare a new property called yAxesGroup
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1", //any with same group will appear in the same axis, also this can be anything as long as you always refer to it with the same name
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
},
to then give more options to this axis add an extra property to the data called yAxis
which is an array of axis options. By default each axis will show a scale on the left and in whatever colour is default or you have set as the overall font colour but here is where you can override them
var overlayData = {
labels: ["January", "Februarya", "March", "April", "May", "Jun", "July"],
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1",
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}, {
label: "My Second dataset",
type: "line",
yAxesGroup: "2",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [8, 38, 30, 29, 46, 67, 80]
}],
yAxes: [{
name: "1",
scalePositionLeft: false,
scaleFontColor: "rgba(151,137,200,0.8)"
}, {
name: "2",
scalePositionLeft: true,
scaleFontColor: "rgba(151,187,205,0.8)"
}]
};
now this chart will have 2 axis, one on the left and one on the right.
A few things to note, if any values are going to be below 0 you need to set the overall attribute of scaleBeginAtZero
to false
otherwise it gets a bit messed up. Also each axis will have it's own 0 base line so the 2 sides will not line up. this is as intended as i decided the reason you have two axis is because they are showing different scales so for everything to be sit in nicely 0 needs to at a different position for each axis.
I have gone through and tested it for regression aginst other features i have added and it all seems to play nicely but if you do use this and notice anything, odd, let me know.
Also i have not been able to write this as plugin for chartjs as so much is touched in the core that it does not make sense as such you would the built version from the fork and not just override an original version of chartjs.
Overall this is the effect it will produce
var overlayData = {
labels: ["January", "Februarya", "March", "April", "May", "Jun", "July"],
datasets: [{
label: "My First dataset",
type: "bar",
yAxesGroup: "1",
fillColor: "rgba(151,137,200,0.5)",
strokeColor: "rgba(151,137,200,0.8)",
highlightFill: "rgba(151,137,200,0.75)",
highlightStroke: "rgba(151,137,200,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}, {
label: "My Second dataset",
type: "line",
yAxesGroup: "2",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [8, 38, 30, 29, 46, 67, 80]
}],
yAxes: [{
name: "1",
scalePositionLeft: false,
scaleFontColor: "rgba(151,137,200,0.8)"
}, {
name: "2",
scalePositionLeft: true,
scaleFontColor: "rgba(151,187,205,0.8)"
}]
};
window.onload = function () {
window.myOverlayChart = new Chart(document.getElementById("canvas").getContext("2d")).Overlay(overlayData, {
populateSparseData: true,
overlayBars: false,
datasetFill: true,
});
}
<script src="https://raw.githack.com/leighquince/Chart.js/master/Chart.js"></script>
<canvas id="canvas" height="300" width="300"></canvas>
Source: stackoverflow.com
Related Query
- Chart.js with dual axis on bar and line graph
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chart.js Mixed Bar and Line chart with different scales
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Chart with Time axis only displaying first grid line and tick label (unitStepSize)
- Chart.js combined line and bar chart with differing data points
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- Google Charts, HighCharts or ChartJS Dual Axis Gantt Chart and Line Chart Visualization
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- ChartJS: Is this horizontal stacked bar and line graph multiaxis chart even possible?
- Chart.js with line chart and bar chart - bar chart not rendered although the max value of it is shown
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Obtain max value of y axis of line chart rendered with Chart.js
- Line chart with large number of labels on X axis
- Can Chart.js combines Line Chart and Bar Chart in one canvas
- Create a rounded bar graph with Angular and chartJS
- Can we draw a Line Chart with both solid and dotted line in it?
- Chart.js - Plot line graph with X , Y coordinates and connected by line
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How to show data values in top of bar chart and line chart in chart.js 3
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- to increase space between x axis and first horizontal bar in chart js
- Issue while plotting bar chart with custom x-axis with month and year in chart.js
- Combo Bar Line Chart with Chart.js
- Chart Js update legend boxes of graph with graph line style
- Chart.js Date and Time Bar Chart Not Rendering - Line Works Though
More Query from same tag
- How to change position from absolute to relative in charts in ChartJS
- "<%=value%>" in javascript not ASP.net
- Show chart.js animation only when user scroll on the specific DIV
- Displaying custom dataset properties in tooltip in chart.js
- Chart.js beginAtZero doesn't work
- Chart.js - add gradient instead of solid color - implementing solution
- How to update data from ajax call (chart.js)
- HTML Canvas height defaults to 1 after slideToggle
- destroy method implementation in pie chart chartjs
- How to compare one value to another in chartJS
- CHART.JS scriptable backgroupColor attribute
- Legend color not working with randomly generated background colors in chartjs pie chart
- Send data from php to chart with ajax
- Chart.js multiple doughnut charts above pie chart
- zoom and pan on charts in angular
- ChartJS "half donut" chart not displaying properly
- Chart.js bar chart label gets hidden on hover
- Input data in german data format combined with ChartsJs
- How to add an offset to a dataset in Chart js
- How to make lines in line charts from ng2-charts straight lines?
- Chart.js Globally Formatted Number Labels
- Move chart x axis label and borders
- Something similar to grace available in the latest chartJs for ChartJs 2.9.3
- How to use Chart.js in Nodejs?
- ChartJS - How can I skip certain minutes on the X-Axis
- ChartJS: Percentage labels
- real time scrolling based chart with chart.js
- Keeping scales in sync across multiple graphs or dynamically changing scales
- Legend option destroys pie chart in Chart.js
- Chart.js replace all data