score:224
For ChartJs 2.x only a couple changes need to be made (it looks like you have tried to combine 2.x options with the multi-axes options from my fork?),
- The
yAxes
field needs to be in ascales
object - the yAxis is referenced by id not name.
- For the scale steps/size you just need to wrap these options in a
ticks
object. - No need for
scalePositionLeft
this is covered byposition
Example:
var canvas = document.getElementById('chart');
new Chart(canvas, {
type: 'line',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [{
label: 'A',
yAxisID: 'A',
data: [100, 96, 84, 76, 69]
}, {
label: 'B',
yAxisID: 'B',
data: [1, 1, 1, 1, 0]
}]
},
options: {
scales: {
yAxes: [{
id: 'A',
type: 'linear',
position: 'left',
}, {
id: 'B',
type: 'linear',
position: 'right',
ticks: {
max: 1,
min: 0
}
}]
}
}
});
score:0
This solution is working in react.
// "chart.js": "^2.9.4"
// "react-chartjs-2": "^2.11.1"
const lineChartData = {
data: {
labels: ['1', '2', '3', '4', '5', '6'],
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
fill: false,
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgba(255, 99, 132, 0.2)',
},
{
label: '# of Votes',
data: [19, 9, 2, 1, 1, 21],
fill: false,
backgroundColor: 'rgb(122, 99, 255)',
borderColor: 'rgba(102, 99, 255, 0.2)',
}
],
},
options: {
scales: {
yAxes: [
{
type: 'linear',
display: true,
position: 'left',
ticks: {
beginAtZero: true,
},
},
{
type: 'linear',
display: true,
position: 'left',
ticks: {
beginAtZero: true,
},
},
],
},
}
}
<Line
data={lineChartData.data}
options={lineChartData.options}
height={30}
width={80}
options={{ maintainAspectRatio: true }}
/>
score:21
The accepted answer no longer works as of 3.5, and the cause is listed as part of the breaking changes for 3.X (See 3.x Migration Guide)
The updated code below changes the scales
property, from scales: {yScales: [...]}
to scales: {[id]: {[options]}}
, and also adds fill: true,
(Was changed at 3.X from defaulting to true
) and tension: 0.4
(The example provided before does have smooth curves, and seems like it was an undocumented "breaking" change)
var canvas = document.getElementById('myChart');
new Chart(canvas, {
type: 'line',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [{
label: 'A',
yAxisID: 'A',
data: [100, 96, 84, 76, 69],
fill: true,
tension: 0.4
}, {
label: 'B',
yAxisID: 'B',
data: [1, 1, 1, 1, 0],
fill: true,
tension: 0.4
}]
},
options: {
scales: {
A: {
type: 'linear',
position: 'left',
},
B: {
type: 'linear',
position: 'right',
ticks: {
max: 1,
min: 0
}
}
}
}
});
Source: stackoverflow.com
Related Query
- How to use two Y axes in Chart.js v2?
- How to use same data / labels on two y axes in Chart.js
- How can I make two of my lines in Chart JS thicker
- How to use two datasets in chart.js doughnut chart?
- How to use PrimeNg Chart of Width and Height?
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- ChartJS: two axes in horizontalBar chart
- How to map json array to two different array for chart
- How do I use my chart.js line chart with handlebars?
- ChartJS : How to display two "y axis" scales on a chart
- How to use segment property to color line / border color based on value in chart js?
- How to use JSON data in creating a chart with chartjs?
- How do I remove cartesian axes from chart js?
- How to use set the color for each bar in a bar chart using chartjs?
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to use Real time chart in react?
- Chart.js two y axes line chart tooltip value incorrect for 2nd y axis
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- how to add second axes to chart JS
- How do I display two datasets on a single chart with chartjs
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- How to use forEach loop on my array to plot x and y variables on my chart (Chart.js)
- How to print a chart rendered by code
- 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?
- How to use plugins in chartjs and laravel chart consoletvs/chartsjs
- How do I destroy/update Chart Data in this chart.js code example?
- How to create a chart.js scatter chart with data from two lists
- How to use php variables in a chart js chart
- Angular chart js how to use formatter with Doughnut chart
- How to use a Data Array within a Chart JS dataset?
More Query from same tag
- Querying data from Firebase for Chart.js using AngularJS
- Chart.js tooltipTemplate not working
- Is there a way to adjust only the bottom padding of a chart's title in Chart.js?
- How to add a vertical line on the end of the chart.js scatter graph
- Custom background limits for Doughnut (Gauge)
- Unable to pass on data from json file to labels and datasets in chart.js
- Is it Possible to get the base 64 of graph without showing it on page / canvas (Graph.Js)
- Chart options in angularJs (NodeRed Charts)
- Vue ChartKick - different colours for bar chart
- Chart.js chart doesn't render when using Angular 2
- ChartJS dynamic line chart ghosting back to old data when hovered on
- Custom data in label on ChartJS pie chart
- Piechart doesn't show up in Canvas
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- Chart.js with Node.js
- Display dataset values on bar ChartJs 2.1.6
- Highlight external element on ChartJS hover
- chart.js: set specific numbers in y-axis
- is there anything wrong with this If Condition in Javascript code?
- Dynamically updating time data in chart.js freezes the chart
- Last value not showing in bar graph of charts.js-library
- Chartjs background color multiple Dataset
- how to change chart JS title text on change input field?
- Loading data into chart on html page
- Chartjs - Stacked bar chart blocking other values
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- How we embed google analytics chart from google analytic with angular 4?
- vue-chartjs not working properly in flexbox and when resize chart goes blank
- Add a click event to the bar chart of chartjs
- ChartJS remove the legends at the top of the chart