question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how can I put a scale on the right side for mixed chart type with chartjs 3? also how to reduce scale size

See original GitHub issue

I am trying to migrate a react app to chartjs3.

I have a mixed, bar chart, linechart: image

2 issues with this chart:

  1. how can I place the bar scale to the right side
  2. how can I set the maximum of the bar chart to only take 10% of the chart height instead of 100% of the chart height?

code :

`import React, { useState } from ‘react’; import { Line, defaults } from ‘react-chartjs-2’;

const LineChart = () => {

// default values are just for testing

const [dataClose, setDataClose] = useState([21.02, 7.92, 1.73, 3.65, 4.13, 1.36, 3.56, 5.02, 1.85, 1.17, 6.29, 24.07])
const [labels, setLabels] = useState(['2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020'])
const [dataVolume, SetDataVolume] = useState([41000, 28000, 62000, 43000, 36000, 48000, 41000, 28000, 62000, 43000, 36000, 48000])

return (

    <div style={{ backgroundColor: "lightblue" }}>
        <Line
            data={{
                labels: labels,
                datasets: [
                    // first dataset
                    {
                        // yAxisID:'yLine',
                        pointRadius: 3,
                        pointHoverRadius: 4,
                        lineTension: 0.3,
                        label: '',
                        data: dataClose,
                        backgroundColor: 'black',
                        borderColor: 'black',
                        borderWidth: 3,  //line thickness
                    },
                    {
                        yAxisID: 'yBar',
                        type: 'bar',
                        data: dataVolume,
                        label: labels,
                        position: 'right'
                    }
                ],
            }}
            height={200}
            width={600}
            options={{
                // maintainAspectRatio: false, //removes scroll bars
                scales: {
                    y: [
                        {
                            id: 'yLine',
                            display:false,
                            ticks: {
                                // beginAtZero: true, //sets the scale minimum to 0
                            },
                        },
                        {
                            id:'yBar',
                            ticks: {
                                // beginAtZero: true, //sets the scale minimum to 0
                            },
                        },
                    ],
                },
            }}
        />
    </div>
)

}

export default LineChart`

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
AndyyTaylorcommented, Jul 9, 2021

nvm got it still had the keys nested in yAxes which is incorrect, this works

scales: {
  price: {
    type: 'linear',
    position: 'left',
  },
  equity: {
    type: 'linear',
    position: 'right',
  },
}
0reactions
nomikeepercommented, Aug 16, 2022

For me, setting the options as follows:

        {
          scales: {
            screenY: {
              type: 'linear',
              display: true,
              position: 'right'
            }
          }
        }

worked for me.

The current option is set for mixed charts too ( more accurately, I’m using bar and line mixed charts).

The code I’m using for the mixed chart is:


      <Chart type='bar' 
        data={data} 
        options={{
          responsive: true,
          scales: {
            screenY: {
              type: 'linear',
              display: true,
              position: 'right'
            }
          }
        }}
      />

One thing I realized is that the property names are quite different than the charts, so you have to find the options you are looking for one by one I guess.

Hope this helps you out.

@afshinmoshrefi

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mixed Chart Types - Chart.js
With Chart.js, it is possible to create mixed charts that are a combination of two or more different chart types. A common example...
Read more >
Axes | Chart.js
The default scaleId 's for carterian charts are 'x' and 'y' . For radial charts: 'r' . Each dataset is mapped to a...
Read more >
Bar Chart | Chart.js
A bar chart provides a way of showing data values represented as vertical bars. It is sometimes used to show trend data, and...
Read more >
Cartesian Axes - Chart.js
Cartesian axes are used for line, bar, and bubble charts. ... const config = { type: 'line', data, options: { scales: { x:...
Read more >
Line Chart - Chart.js
A line chart is a way of plotting data points on a line. ... Clipping can also be configured per side: clip: {left:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found