Mix chart, labels.slice is not a function error
See original GitHub issueVersions
react-chartjs-2 version: 2.7.0 react: 16.2.0
Error Description
hi, i m trying to apply the mix example which has two y-axes, however it gives the below error. labels.slice is not a function
Code
const data = {
labels: [
'January',
'February',
'March',
'April',
'May',
'June',
'July'
],
datasets: [
{
label: 'Sales',
type: 'line',
data: [
51,
65,
40,
49,
60,
37,
40
],
fill: false,
borderColor: '#EC932F',
backgroundColor: '#EC932F',
pointBorderColor: '#EC932F',
pointBackgroundColor: '#EC932F',
pointHoverBackgroundColor: '#EC932F',
pointHoverBorderColor: '#EC932F',
yAxisID: 'y-axis-2'
}, {
type: 'bar',
label: 'Visitor',
data: [
200,
185,
590,
621,
250,
400,
95
],
fill: false,
backgroundColor: '#71B37C',
borderColor: '#71B37C',
hoverBackgroundColor: '#71B37C',
hoverBorderColor: '#71B37C',
yAxisID: 'y-axis-1'
}
]
};
const options = {
responsive: true,
tooltips: {
mode: 'label'
},
elements: {
line: {
fill: false
}
},
scales: {
xAxes: [
{
display: true,
gridLines: {
display: false
},
labels: {
show: true
}
}
],
yAxes: [
{
type: 'linear',
display: true,
position: 'left',
id: 'y-axis-1',
gridLines: {
display: false
},
labels: {
show: true
}
}, {
type: 'linear',
display: true,
position: 'right',
id: 'y-axis-2',
gridLines: {
display: false
},
labels: {
show: true
}
}
]
}
};
<Line data={data} options={options}/>
Is it a bug or do i implement something wrong?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:22
- Comments:18
Top Results From Across the Web
how to solve ERROR TypeError: labels.slice is not a function?
chart.js expects the labels property to be an array. But your labels is a Set object, which can't use slice . One way...
Read more >error typeerror: labels.slice is not a function - You.com | The AI ...
1 Answer. chart.js expects the labels property to be an array. But your labels is a Set object, which can't use slice. One...
Read more >Mix chart, labels.slice is not a function error - Bountysource
hi, i m trying to apply the mix example which has two y-axes, however it gives the below error. labels.slice is not a...
Read more >TypeError: slice is not a function in JavaScript | bobbyhadz
The "slice is not a function" error occurs when the slice() method is called on a value that is not of type string...
Read more >highcharts.chart is not a function
Highcharts basic graph; highcharts with data label curve chart. Change the data in the ... In Highchart OS11 events: { load: function ()...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After fiddling with this for a little while, I found a way to fix the example. It is very easy and I don’t understand why it is not posted anywhere, but now it is. Instead of defining your labels in data, you just have to define them in scales.xAxes like so:
` import React from ‘react’; import {Bar} from ‘react-chartjs-2’;
const data = { // labels: [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’], datasets: [{ label: ‘Sales’, type:‘line’, data: [51, 65, 40, 49, 60, 37, 40], fill: false, borderColor: ‘#EC932F’, backgroundColor: ‘#EC932F’, pointBorderColor: ‘#EC932F’, pointBackgroundColor: ‘#EC932F’, pointHoverBackgroundColor: ‘#EC932F’, pointHoverBorderColor: ‘#EC932F’, yAxisID: ‘y-axis-2’ },{ type: ‘bar’, label: ‘Visitor’, data: [200, 185, 590, 621, 250, 400, 95], fill: false, backgroundColor: ‘#71B37C’, borderColor: ‘#71B37C’, hoverBackgroundColor: ‘#71B37C’, hoverBorderColor: ‘#71B37C’, yAxisID: ‘y-axis-1’ }] };
const options = { responsive: true, labels: [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’], tooltips: { mode: ‘label’ }, elements: { line: { fill: false } }, scales: {
} };
const plugins = [{ afterDraw: (chartInstance, easing) => { const ctx = chartInstance.chart.ctx; ctx.fillText(“This text drawn by a plugin”, 100, 100); } }];
class Mix extends React.Component { render() { return ( <div>
Mixed data Example
<Bar data={data} options={options} plugins={plugins} /> </div> ); } }export default Mix; `
that is because ‘options.scales.xAxes.labels’ rewrite ‘data.labels’,so you also delete the labels in options and keep the labeled in data to makesure that this chart can changed when xAxes’s data change! I don’t know why it cover,maybe it is a bug…