score:2
Fist of all in your barChartData.datasets[]
you are using id
but you should use yAxisID
instead. giving id
there has no impact on the chart. here is the doc for that: http://www.chartjs.org/docs/latest/charts/line.html#dataset-properties
Now onto what you're asking, you can add a click handler to the legend to toggle between axes on click, here is an example of that:
a fork codepen off of yours with new code: https://codepen.io/anon/pen/ooZMwG?editors=0010
set options.legend.onClick
to this function:
function(e, legendItem) {
var index = legendItem.datasetIndex;
var ci = this.chart;
var axisIds = []
var yAxes = ci.options.scales.yAxes;
for (var i = 0; i < yAxes.length; i++) { // loop over the registered yAxes
yAxes[i].gridLines.color = "rgba(0,0,0,0.1)"; // reset all scale gridlines to default
axisIds.push(yAxes[i].id) // get all yAxes ids
}
var meta = ci.getDatasetMeta(index);
var currentAxisId = meta.yAxisID
var currentAxisIdIndex = axisIds.indexOf(currentAxisId);
var newAxisIdIndex;
// basically get the next axis id in the array, if the last one, get the first (circular)
if (currentAxisIdIndex == axisIds.length - 1) newAxisIdIndex = 0;
else newAxisIdIndex = currentAxisIdIndex + 1;
var newAxisId = axisIds[newAxisIdIndex];
meta.yAxisID = newAxisId // set the new axis id to the next one in the array
axis = ci.scales[newAxisId];
axis.options.gridLines.color = "rgba(0,0,0,1)"; // change lines color of the new axes
ci.update(); // update chart
}
**UPDATE: **
based on your new codepen, here are a few things to help:
I have added a new update to my answer to add offfset to all Y Axes in the chart. Use that instead of what you have in the pen.
then update your changeLeftRightY
to the following and make sure your legend HTML container has the id: legend
changeLeftRightY = function(e, index) {
//console.log("-- " + data.datasets[index].yAxisID)
var ci = e.view.ch;
var axisIds = []
var yAxes = ci.options.scales.yAxes;
for (var i = 0; i < yAxes.length; i++) { // loop over the registered yAxes
yAxes[i].gridLines.color = "rgba(0,0,0,0.1)"; // reset all scale gridlines to default
axisIds.push(yAxes[i].id) // get all yAxes ids
}
var meta = ci.getDatasetMeta(index);
var currentAxisId = meta.yAxisID
var currentAxisIdIndex = axisIds.indexOf(currentAxisId);
var newAxisIdIndex;
// basically get the next axis id in the array, if the last one, get the first (circular)
if (currentAxisIdIndex == axisIds.length - 1) newAxisIdIndex = 0;
else newAxisIdIndex = currentAxisIdIndex + 1;
var newAxisId = axisIds[newAxisIdIndex];
meta.yAxisID = newAxisId // set the new axis id to the next one in the array
axis = ci.scales[newAxisId];
axis.options.gridLines.color = "rgba(0,0,0,1)"; // change lines color of the new axes
document.getElementById('legend').innerHTML = ci.generateLegend();
ci.update()
}
Here is a codepen with the code result *I have made some changes to some of your code to make it easier to read.
If that codepen is changed or deleted, here is the full code
// set default no fill beneath the line
Chart.defaults.global.elements.line.fill = false;
// Define a plugin to provide data labels
Chart.pluginService.register({
beforeUpdate: function(chart){
console.log("beforeUpdate");
// get custom defined offset
var offset = chart.options.customOffset;
// exisit gracefully if offset not defined or less than 0
if(!offset || offset < 0) return;
var yAxes = chart.options.scales.yAxes;
for(var i=0; i<yAxes.length; i++){
var axis = yAxes[i];
var datasets = chart.data.datasets;
var max = Number.MIN_VALUE;
var min = Number.MAX_VALUE;
var datasetsOnAxis = [];
for(var j=0; j<datasets.length; j++){ // add datasets for this axes to datasetsOnAxis
var dataset = datasets[j];
var meta = chart.getDatasetMeta(j);
if (meta.yAxisID === axis.id) datasetsOnAxis.push(dataset);
}
for(var k=0; k<datasetsOnAxis.length; k++){
var dataset = datasetsOnAxis[k]
var newMax = Math.max.apply(null, dataset.data);
var newMin = Math.min.apply(null, dataset.data);
max = newMax > max ? newMax : max;
min = newMin > min ? min : newMin;
}
axis.ticks.max = max + offset;
axis.ticks.min = min - offset;
}
}
});
var barChartData = {
labels: [1510332261000, 1510332473000, 1510332489000, 1510332726000, 1510332777000, 1510332778000, 1510332958000, 1510333050000, 1510333131000, 1510333389000, 1510333476000, 1510333493000, 1510333588000, 1510333604000, 1510333664000, 1510333668000, 1510333758000, 1510333801000, 1510333820000, 1510333821000, 1510333878000, 1510333928000],
datasets: [{
type: 'line',
label: 'a (F)',
yAxisID: "y-axis-0",
backgroundColor: "rgba(217,83,79,0.75)",
data: [70, 72, 73, 73, 75, 50, 50, 40, 40, 45, 70, 73, 70, 73, 73, 73, 74, 73, 73, 73]
}, {
type: 'line',
label: 'b (V)',
yAxisID: "y-axis-0",
backgroundColor: "rgba(92,184,92,0.75)",
data: [12.9, 17.9, 15.9, 17.9, 17.9, 17.9, 15.9, 17.9, 15.8, 17.8, 16.8, 17.8, 17.9, 17.9, 17.8, 17.8, 19.8, 17.9, 17.8, 20.8]
}, {
type: 'line',
label: 'c (%)',
yAxisID: "y-axis-0",
backgroundColor: "rgba(51,51,51,0.5)",
data: [30, 30, 50, 30, 20, 10, 30, 40, 30, 50, 30, 70, 30, 50, 30, 80, 90, 30, 30, 30]
}, {
type: 'line',
label: 'd (AH)',
yAxisID: "y-axis-1",
backgroundColor: "rgba(151,187,205,0.5)",
data: [10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6, 10.6]
}]
};
var options = {
title: {
display: true,
text: "Chart.js"
},
legendCallback: function (chart) {
console.log('legendCallback');
var legendHtml = [];
legendHtml.push('<div id="legend">');
for (var i = 0; i < chart.data.datasets.length; i++) {
var meta = ch.getDatasetMeta(i);
var yAxis = meta.yAxisID =='y-axis-0' ? "Left Axis" : "Right Axis"
if (chart.data.datasets[i].label) {
var datasetIndex = chart.legend.legendItems[i].datasetIndex
var color = chart.data.datasets[i].backgroundColor;
var label = chart.data.datasets[i].label;
legendHtml.push(
'<div class="lgnd-item-container">'+
'<input class="lgnd-ckeckbox" type="checkbox" checked '+
'onclick="updateDataset(event, '+datasetIndex+')"/>'+
'<div class="lgnd-square" style="background-color:'+color+'"></div>'+
'<div class="lgnd-label">'+label+' '+yAxis+'</div>'+
'<input class="lgnd-btn" type="button" value="Change" onclick="changeLeftRightY(event,'+datasetIndex +')"></input>'+
'</div>');
}
}
legendHtml.push('</div>');
return legendHtml.join("");
},
legend:{
display:false
},
customOffset: 2,
responsive: true,
tooltips: {
mode: 'label',
position: 'nearest'
},
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
position: "left",
id: "y-axis-0",
}, {
position: "right",
id: "y-axis-1",
}]
}
}
// Show/hide chart by click legend
updateDataset = function (e, datasetIndex) {
var index = datasetIndex;
var ci = e.view.ch;
var meta = ci.getDatasetMeta(index);
// See controller.isDatasetVisible comment
meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
// We hid a dataset ... rerender the chart
ci.update();
};
var ctx = document.getElementById("myChart");
// allocate and initialize a chart
window.ch = new Chart(ctx, {
type: 'line',
data: barChartData,
options: options
});
document.getElementById("chartLegend").innerHTML = ch.generateLegend();
changeLeftRightY = function(e, index) {
//console.log("-- " + data.datasets[index].yAxisID)
var ci = e.view.ch;
var axisIds = []
var yAxes = ci.options.scales.yAxes;
for (var i = 0; i < yAxes.length; i++) { // loop over the registered yAxes
yAxes[i].gridLines.color = "rgba(0,0,0,0.1)"; // reset all scale gridlines to default
axisIds.push(yAxes[i].id) // get all yAxes ids
}
var meta = ci.getDatasetMeta(index);
var currentAxisId = meta.yAxisID
var currentAxisIdIndex = axisIds.indexOf(currentAxisId);
var newAxisIdIndex;
// basically get the next axis id in the array, if the last one, get the first (circular)
if (currentAxisIdIndex == axisIds.length - 1) newAxisIdIndex = 0;
else newAxisIdIndex = currentAxisIdIndex + 1;
var newAxisId = axisIds[newAxisIdIndex];
meta.yAxisID = newAxisId // set the new axis id to the next one in the array
axis = ci.scales[newAxisId];
axis.options.gridLines.color = "rgba(0,0,0,1)"; // change lines color of the new axes
document.getElementById('legend').innerHTML = ci.generateLegend();
ci.update()
}
CSS:
.myChartDiv {
max-width: 600px;
max-height: 400px;
}
.lgnd-square {
float: left;
height: 20px;
width:20px;
overflow:hidden;
margin: 5px;
}
.lgnd-ckeckbox{
float:left;
margin: 10px;
}
.lgnd-btn{
float: left;
margin: 5px;
}
.lgnd-item-container{
width: 300px;
height: 40px;
}
.lgnd-label{
float:left;
margin: 5px;
}
HTML:
<html>
<body>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"></canvas>
</div>
<div id="chartLegend"></div>
</body>
</html>
Source: stackoverflow.com
Related Query
- Chart JS, Choose different Y-axis for different datasets
- Chart js different background for y axis
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- show label in tooltip but not in x axis for chartjs line chart
- chart.js Line chart with different background colors for each section
- Chart JS - set start of week for x axis time series
- Different color for each column in angular-chartjs bar chart
- Can I specify a different font size for each row of text in Chart Title?
- ChartJS - Line Chart with different size datasets
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- chart js - Apply different color for each x-axes label
- Vue ChartKick - different colours for bar chart
- Multiple bubble chart datasets for chartjs
- How to map json array to two different array for chart
- chartjs custom y axis values, different text for each one
- How to show different product name for every bar in chart js
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- How can I have different values for the chart and the tooltip in chart.js?
- create different labels for different data chart js
- Chart Js reduce text size for label on X axis
- chart.JS i want to put different color for each Y axis value grid line color
- Charts.js - Bar chart different colors for value intervals not working
- Linear x axis for bar chart chartjs
- Chart.js two y axes line chart tooltip value incorrect for 2nd y axis
- React with chart js, labelling problems in multiple datasets for doughnut chart
- How do I get a different label for each bar in a bar chart in ChartJS?
- How do you set x and y axis and Title for a line chart using charts.js?
- Chart JS tick options not working for y axis
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
- getting additional value fields from data source for dx.chartjs doughnut chart
More Query from same tag
- Move tooltip further from data point for Chart.js?
- Range grouping data and label in Chartjs
- Resizing chart before downloading as image
- Chart.js Bar graph will not start at zero as minimum value
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- Why Chart.js's tooltip appears in wrong position when mouse hover?
- Unable to hide X-Axis in horizontal stacked chart.js
- Python list does not work in django with ChartJS
- Updating Chart Data on Charts.js with Ng2-Charts
- Chartjs real time graph x axis movement
- How to customize chart js Bar chart shape?
- How to get dataset from MySQL using Flask?
- ChartJS: Bar chart with axis ticks wider than categories
- How to set the xAxes min and max values of time cartesian chart in Chart.js
- Undefined values when try to Insert array in component
- Angular: chart.js chart is not displaying
- How to generate multi bar chart in Chart.js with php & sql
- How can I get the label onclick event by multiple graphs?
- How do you set x and y axis and Title for a line chart using charts.js?
- Building Chart.js master - generated .js gives errors
- chartjs creating data array dynamically
- Chart.js multiTooltip labels
- chart.js line for only part of the labels
- No chart display for Chart.js and JSON data
- Chartjs blocks jumping to a section in ASP.NET MVC 5
- How to format the left legend on chartjs
- How can I get access to the index of the highlighted dataset in Chart.js
- How do I increase the space between the Legend and the Chart when using angular-chart.js?
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Y-axis label in chartjs 2.0.0