Why are some react-chartjs-2 options being ignored? - reactjs

I have this simple line chart that I am trying to change the gridlines from their default gray, however the options object appears to be selectively working. Maybe I am missing something basic but I have been fiddling with it for hours and cannot get it to work, any help is appreciated! I am using "react-chartjs-2".
Below is my render() method where I pass the data and options.
render() {
const data = {
labels: this.state.realTimes,
datasets: [
{
label: "Price",
data: this.state.Prices,
fill: false,
backgroundColor: "rgb(255, 255, 255)",
borderColor: "rgba(255, 255, 255)"
}
]
};
const options = {
scales: {
yAxis: {
beginAtZero: true, //this works
gridLines: {
color: "rgba(171,171,171,1)", //this doesn't
lineWidth: 0.5
}
}
}
};
return (
<div>
{this.state.loading ? (
this.loading()
) : (
<Line data={data} options={options} width={600} height={160} />
)}
</div>
);
}

Change scales.yAxis.gridLines to scales.yAxis.grid and it will work.
see Grid Line Configuration from Chart.js documentation or the Grid Configuration samples page.
Please take a look at below runnable JavaScript code and see how it works.
Please take a look at
new Chart('line-chart', {
type: 'line',
data: {
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
datasets: [{
label: 'My Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.2
}]
},
options: {
scales: {
yAxis: {
beginAtZero: true,
grid: {
color: 'rgb(0 ,0 ,255)',
lineWidth: 0.5
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js"></script>
<canvas id="line-chart" height="100"></canvas>

React chart.js is not up to date with chart.js so you can't use v3. Please use the v2 syntax since that is what react-chartjs uses.
https://www.chartjs.org/docs/2.9.4/axes/styling.html#grid-line-configuration
If you change yAxes: {gridLines: {color: 'red'}} to yAxes: [{gridLines: { color: 'red'}}] it should work as intended

Related

Give radius to bars in bar graph

Apply border radius to the bars of Bar_Chart using react-chartjs-2 module so please suggest changes in this code only.
i tried radius in data component but it didn't work .
import { Bar } from 'react-chartjs-2';
<Bar
data={{
labels :['2020-10-26', '2020-10-27', '2020-10-28', '2020-10-29'],
datasets: [
{
label: 'r',
data: [130,220,310,420],
backgroundColor: 'rgb(255, 144, 18)',
},
{
label: 'm',
data: [100,200,149,330],
backgroundColor: '#66CCFF',
},
{
label: 'u',
data: [90,150,30,240],
backgroundColor: '#00FF99',
},
],
}}
options={{
responsive: true,
cornerRadius:19,
// plugins: {
legend: {
cursor: "pointer",
display:true,
position:'bottom',
labels: {
usePointStyle: true,
boxWidth:10,
}
},
// },
scales: {
xAxes: [{
gridLines: {
display:false
}
}]}
}}
margin={{
top: 5,
right: 30,
left: 20,
bottom: 5,
}}
/>
Want my bars to be round from above like this only visuals are example not code
https://codepen.io/jordanwillis/pen/jBoppb
You have to apply borderRadius parameter in datasets as per below code
datasets: [
{
label: 'r',
data: [130,220,310,420],
backgroundColor: 'rgb(255, 144, 18)',
borderRadius : 50 // You can apply any value as per your requirement
},
{
label: 'm',
data: [100,200,149,330],
backgroundColor: '#66CCFF',
borderRadius : 50
},
{
label: 'u',
data: [90,150,30,240],
backgroundColor: '#00FF99',
borderRadius : 50
}]
Try borderRadius in options object instead

Chart.js Line graph y-axis not starting from 0

This is my current line graph. The x-axis is just a mock data so hope u guys can overlook the bad data used. As you can see on the y-axis, it is starting from 60 which is not what i want. I have tried beginAtZero = true but its not working. I would also like to remove the label weight but by simply removing not putting the labels, it would say undefined.
Below is the code for the line graph.
import React from 'react'
import {Line} from 'react-chartjs-2'
function LineChart(datasets) {
const dataset = datasets.data
return (
<div>
<Line
data={{
labels: dataset.map(data=> data.created_at),
datasets: [{
label: 'Weight',
data: dataset.map(data => data.weight),
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderWidth: 1,
borderColor: 'rgb(255, 99, 132)',
}]
}}
height = {200}
options = {{
resposive: true,
maintainAspectRatio: true,
scales: {
yAxes: [{
ticks: {
min: 0,
beginAtZero: true,
}
}]
}
}}
/>
</div>
)
}
export default LineChart
It seems that you're using Chart.js version 3, where the scales option needs to be defined differently (see Chart.js documentation).
options = {{
resposive: true,
maintainAspectRatio: true,
scales: {
y: {
beginAtZero: true
}
}
}}

How to show data value on top of bar in react-chartjs-2?

I made bar chart in react-chartjs-2. I want to show data value of each bar on the top of the bar. For that I used the plugin calls chartjs-plugin-datalabels But it's not showing anything.
is it possible to show each value of bar chart ?
I want to show data value somethings like this.
Here is my code.
import React, { Component } from "react";
import "chartjs-plugin-datalabels";
import { Bar } from "react-chartjs-2";
export default class Button extends Component {
render() {
const dataBar = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
backgroundColor: "#EC932F",
borderColor: "rgba(255,99,132,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(255,99,132,0.4)",
hoverBorderColor: "rgba(255,99,132,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};
const options = {
plugins: {
datalabels: {
display: true,
color: "black"
}
},
legend: {
display: false
}
};
return (
<div>
<Bar data={dataBar} options={options} width={100} height={50} />
</div>
);
}
}
Any help would be appreciated.
import React, { Component } from "react";
import Chart from "chart.js/auto";
import ChartDataLabels from "chartjs-plugin-datalabels";
import { Bar } from "react-chartjs-2";
export default class Button extends Component {
componentDidMount() {
Chart.register(ChartDataLabels);
}
render() {
const dataBar = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
backgroundColor: "#EC932F",
borderColor: "rgba(255,99,132,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(255,99,132,0.4)",
hoverBorderColor: "rgba(255,99,132,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};
const options = {
plugins: {
datalabels: {
display: true,
color: "black",
formatter: Math.round,
anchor: "end",
offset: -20,
align: "start"
}
},
legend: {
display: false
}
};
return (
<div>
<Bar data={dataBar} options={options} width={100} height={50} />
</div>
);
}
}
Here you can see in componentDidMount we have registered chart data label plugin for displaying data.
And another thing is in the options added few more parameters as per below field
anchor: "end",
offset: -20,
align: "start"
This is used for displaying data in the top.
The react-chartjs-2 package holds a plugin property which can be set on the components.
Change the import from:
import "chartjs-plugin-datalabels";
to:
import ChartDataLabels from 'chartjs-plugin-datalabels';
On your component, add the plugin variable which holds the imported value.
from:
<Line data={data} options={options} />
to:
<Line data={data} plugins={[ChartDataLabels]} options={options} />
See this original stackoverflow answer to the similar question
use this "chartjs-plugin-datalabels": "^0.5.0" version
it works for me
Here is working example
https://codesandbox.io/s/barchart-knycb
const options2 = {
plugins: {
datalabels: {
anchor: "end",
align: "start",
formatter:(value,context)=>{
const datasetArray = []
context.chart.data.datasets.forEach((dataset)=>{
if(dataset.data[context.dataIndex] != undefined){
datasetArray.push(dataset.data[context.dataIndex])
}
})
function totalSum(total, datapoint){
return total + datapoint;
}
let sum = datasetArray.reduce(totalSum,0)
if(context.datasetIndex == datasetArray.length-1)
return sum
else
return ''
},
},
title: {
display: true,
text: 'Chart.js Bar Chart - Stacked',
},
},
responsive: true,
scales: {
x: {
stacked: true,
ticks: {
maxRotation: 70,
minRotation: 70
}
},
y: {
stacked: true,
},
},
};
const data1 = {
labels,
datasets: [
{
label: 'Dataset One',
data: [1,2,3,4,5,6,7],
backgroundColor: 'rgba(255, 99, 132, 0.5)',
},
{
label: 'Dataset Two',
data: [1,8,3,4,9,6,7],
backgroundColor: 'rgba(53, 162, 235, 0.5)',
},
],
};

Chart.js Yaxis custom horizontal line and label

I work with Chart.js and I would like to create a graph like the one in attachment. The idea is to write the values ​​of the Y axis on the horizontal line. Can you help me ??
You can use the gridLines and ticks styling options to get the intended effect. Here is the relevant Chart.js documentation.
Specifically, for the Y axis, use the following settings:
gridLines: {
drawBorder: false,
color: '#aaa',
zeroLineColor: '#aaa',
tickMarkLength: 10,
offsetGridLines: true,
},
ticks: {
padding: 10,
mirror: true
}
The mirror and offsetGridLines settings are the most important here for creating the appearance of the grid lines extending below the axis labels.
This configuration creates the following effect:
Here's a runnable interactive example:
const ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First dataset',
backgroundColor: '#5595eb',
data: [10, 30, 39, 20, 25, 34, 0],
}, {
label: 'My Second dataset',
backgroundColor: '#433a93',
data: [18, 33, 22, 19, 11, 39, 30],
}]
},
options: {
scales: {
xAxes: [{
stacked: true,
gridLines: {
display: false
}
}],
yAxes: [{
stacked: true,
gridLines: {
drawBorder: false,
color: '#aaa',
zeroLineColor: '#aaa',
tickMarkLength: 10,
offsetGridLines: true,
},
ticks: {
padding: 10,
mirror: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<body>
<canvas id="myChart" width="600" height="400"></canvas>
</body>
Maybe this plugin can help: chartjs-plugin-annotation
The plugin annotations like a horizontal line on the specific x/y axis.

Is it possible to set up 0 as a minimum number for bar chart? react-chart.js-2

I'm trying to make a bar chart using react-chart.js-2. I just noticed that all bar chart example start from the minimum number of the data, not 0.
The example below, the bar chart starts from 40, not 0. What I want to do is to make a bar chart starting from 0, not the minimum number of the data.
Is it possible to make it using react-chart.js2?
Here is the code(mostly code from the official example )
import React from 'react';
import {Bar} from 'react-chartjs-2';
import 'chartjs-plugin-datalabels';
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'My First dataset',
backgroundColor: 'rgba(255,99,132,0.2)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1,
hoverBackgroundColor: 'rgba(255,99,132,0.4)',
hoverBorderColor: 'rgba(255,99,132,1)',
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};
export default React.createClass({
displayName: 'BarExample',
render() {
return (
<div>
<h2>Bar Example (custom size)</h2>
<Bar
data={data}
width={150}
height={100}
/>
</div>
);
}
});
Here is a demo.
To start the y-axis from 0, you would have to set the beginAtZero property to true for y-axis ticks, in your chart options, like so :
options={{
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}}
see - working demo
You need to add this piece of code to the dataset object
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
}
}]
}
}
You can read more in the docs Axes Range setting

Resources