2

enter image description here

How to turn off the scale with additional marks from 0 to 45000 which I highlighted in the screenshot? I have tried everything possible. it seems that it is possible to update chartjs to the latest version, but I'm interested in the possibility of correcting the behavior in version 3.7.1 there is also a problem that at the point at the very top where 100% the label with a value of 100 of this point is cut off, until I also found how to fix it.

Options:

export const chartOptions: ICustomChartOptions = {
  responsive: true,
  layout: {},
  elements: {
    bar: {
      borderRadius: 4,
    },
    line: {
      borderWidth: 3,
    },
    point: {
      backgroundColor: '#FFFFFF',
      borderWidth: 2,
      radius: 4,
    },
  },
  clip: false,
  scales: {
    x: {
      grid: {
        display: false,
      },
      ticks: {
        font: {
          size: 10,
        },
      },
      min: 0,
      max: 100000,
    },
    y: {
      position: 'left',
      grid: {
        drawBorder: false,
      },
      ticks: {
        callback: function (value) {
          return `${value}%`;
        },
        stepSize: 25,
        font: {
          size: 10,
        },
      },
      min: 0,
      max: 100,
      beginAtZero: true,
    },
  },
  plugins: {},
};

UPD: This is data for chart

export const chartData: ChartData<'line'> = {
  labels: chartLabels,
  datasets: [
    {
      type: 'line',
      // yAxisID: 'line',
      data: [100, 20, 49, 10, 97],
      order: 1,
      datalabels: {
        anchor: 'start',
        align: 'end',
        color: color['Purple/Purple 80'],
        formatter(value) {
          return `${value}%`;
        },
      },
    },
    {
      type: 'bar' as 'line',
      yAxisID: 'bar',
      data: [22000, 17500, 12000, 10000, 44000],
      order: 2,
      datalabels: {
        align: 'end',
      },
    },
  ],
};

UPD 2 for display values, i use ChartDataLabels

Rafael Shepard
  • 214
  • 3
  • 15

1 Answers1

1

Without the data, one can't be sure how the second axis comes to be, but I'll suppose that it is the axis for the type: "bar" part of a mixed chart. Therefore, the data might be something on the lines of (I'll spare the typings, as this is too imprecise for that):

const data = {
   labels: [....],
   datasets: [
       {
          type: "line",
          label: "....",
          data: [.....]
       },
       {
          type: "bar",
          label: "....",
          data: [.....]
       },
   ]
}

In this case, you'll have to set the y axes ids for the two parts and, in options set display: false for the axis you don't want to show:

const data = {
   labels: [....],
   datasets: [
       {
          type: "line",
          label: "....",
          data: [.....],
          yAxisID: "y"
       },
       {
          type: "bar",
          label: "....",
          data: [.....],
          yAxisID: "y2"
       }
   ]
};

const chartOptions = {
   //....... other options
   scales:{
      x: { 
         // .... options for the x axis
      },
      y: { 
         // .... options for the y axis 
      },
      y2:{
         display: false,
         // ... other y2 options, if any
      }
   }
}

Here's a snippet with a minimal version of the code doing that:

const data = {
    labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    datasets: [
        {
            label: 'lines',
            data: [100, 20, 40, 50, 90, 44, 29],
            type: 'line',
            order: 1,
            yAxisID: 'y'
        },
        {
            label: 'bars',
            data: [10009, 2000, 4000, 5000, 9000, 4400, 2900],
            type: 'bar',
            order: 0,
            yAxisID: 'y2'
        }
    ]
};

const options = {
    scales:{
        y: {
            beginAtZero: false,
            //grace: "2%"
        },
        y2: {
            display: false,
        }
    },
    layout: {
        padding: {top: 10} // to fit the label
    }
}
new Chart(document.getElementById('chart1'), {data, options});
<div style="height: 100%; width:100%">
<canvas id="chart1"  style="height: 90vh; width:95vw"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

As for fitting the label, again, we don't know how you added them. You may try:

  • options.layout.padding.top docs link (see the example above),
  • options.scales.y.grace docs link (commented in the example above)
  • or simply increasing the maximum (max) of the y scale according to data.
kikon
  • 3,670
  • 3
  • 5
  • 20
  • Sorry, i add data in description – Rafael Shepard Jun 20 '23 at 09:02
  • It works! i just added different yAxisID to line 'y' and bar 'y2' in data and in options added y2: { display : false } – Rafael Shepard Jun 20 '23 at 09:09
  • unfortunately the numbers are still clipped which are close to 100% layout.padding.top not works – Rafael Shepard Jun 20 '23 at 09:16
  • Have you tried a large padding? or setting scales.y.max=105? Labels (by datalabels plugin) being cut is an old problem, there are already a few questions with this issue here on SO, for instance [this one](https://stackoverflow.com/questions/50214709/chart-js-data-labels-getting-cut) or [this one](https://stackoverflow.com/questions/68528881/datalabels-of-chart-js-can-not-display-full-values) – kikon Jun 20 '23 at 09:31
  • it worked for me to add Scales.y.max = 105, but an extra horizontal line appeared at 105%. While I'm looking for myself whether it is possible to remove it, but I will not refuse help. – Rafael Shepard Jun 21 '23 at 09:44
  • I set the data from your update and tested the `layout` method, which is the recommended one since it has no side-effects, and it seems to work, see [this fiddle](https://jsfiddle.net/qsg3pe50/); please let me know if there is some reason that this is not convenient for you and I'll try to see if the other methods can be used without the extra grid line – kikon Jun 21 '23 at 14:03
  • In your jsfiddle version of chart-data-labels is 2.2.0, but i have 2.0.0 padding.top = 30 not works for me, screenshot: https://ibb.co/grLKTRY – Rafael Shepard Jun 22 '23 at 12:27
  • if I write scales.y.max = 106, then an extra line graph appears at the very top https://ibb.co/3zJVRST – Rafael Shepard Jun 22 '23 at 12:33
  • The `layout` solution works with datalabels 2.0.0 also, see [this fiddle](https://jsfiddle.net/okaprbjd/1/). I recommend you check it againg - maybe you misplaced or repeated/doubled the `layout` property? – kikon Jun 22 '23 at 14:06
  • If you still want to use `max: 105`, I think the top line can be disabled by setting for the y scale `ticks:{includeBounds: false}`, see [this fiddle](https://jsfiddle.net/okaprbjd/2/) – kikon Jun 22 '23 at 14:06
  • please, check codesandbox, padding.top not works for me, 100% value is clipped: https://codesandbox.io/s/chartjs-bugs-3-7-1-px3t74?file=/src/settings.ts – Rafael Shepard Jun 23 '23 at 08:23
  • I don't know why, but all of a sudden in codesandbox everything worked as it should, but in my project the number is still being cut off. I checked the versions of react, chartjs, chart data labels , react chartjs 2 in codesandbox and y locally in my project - all versions match – Rafael Shepard Jun 23 '23 at 11:13
  • 1
    Oh god, forgive me friend :) I found this setting and completely forgot that I experimented with it: ChartJS.defaults.set('plugins.datalabels', { color: color['Primary/Primary 80'], clip: true, }); clip = true fixed my problem! – Rafael Shepard Jun 23 '23 at 11:31
  • 1
    clip = false * sorry ;) – Rafael Shepard Jun 23 '23 at 12:01