Two Y axis Barchart in ChartJS

huangapple go评论64阅读模式
英文:

Two Y axis Barchart in ChartJS

问题

I have created a two Y-axis barchart, the data and axis display correctly, however, the two axes are both positioned to the left of the plot. I put the position for the two Y axes but I don't know why it doesn't work. Here is the chart component in Codepen.

https://codepen.io/benmhamed/pen/mdzJKaq?editors=0010

yAxes: [
{
id: "y-axis-1",
position: "left",
ticks: {
maxTicksLimit: 5,
callback: (value) => formatValue(value)/1000 + "K (units)",
},
},
{
id: "y-axis-2",
position: "right",
ticks: {
maxTicksLimit: 5,
callback: (value) => formatValue(value) + "M ($)",
},
],

英文:

I have create a two Y-axis barchart, the data and axis display correctly, however, the two axis are both positioned to the left of the plot. I put the position for the two Y axes but I don't know why it doesn't work. Here is the chart component in Codepen.

https://codepen.io/benmhamed/pen/mdzJKaq?editors=0010

 yAxes: [
                    {
                        id: "y-axis-1",
                        position: "left",
                        ticks: {
                            maxTicksLimit: 5,
                            callback: (value) => formatValue(value)/1000 + "K (units)",
                        },
                    },
                    {
                        id: "y-axis-2",
                        position: "right",
                        ticks: {
                            maxTicksLimit: 5,
                            callback: (value) => formatValue(value) + "M ($)",
                        },
                    },

答案1

得分: 0

Your axes configuration seems to follow the format from an older version of charts.js. The main change has to be that in the options, scales is an object (not an array), and each individual axis will appear under its id as a key:

{
   options:{
      scales:{
         "y-axis-1": {
         // ...........
         },
         "y-axis-2": {
         // ...........
         },
         x: {
         // ...........
         }
      }
   }
}

Here's a simplified version of your code for illustration purposes, non-react, with function that were not defined excluded, with invented data and category x axis rather than time:

const ctx = document.getElementById('myChart'),
    container = ctx.parentElement;

new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['q','w','e','r'],
        datasets: [
            {
                label: 'Sales (in units)',
                data: [1100, 2200, 3300, 4400],
                yAxisID: "y-axis-1",
                //backgroundColor: tailwindConfig().theme.colors.blue[400],
                //hoverBackgroundColor: tailwindConfig().theme.colors.blue[500],
                barPercentage: 0.66,
                categoryPercentage: 0.66,
            },
            {
                label: 'Spend (million USD)',
                data: [3, 2, 4, 1],
                yAxisID: "y-axis-2",
                //backgroundColor: tailwindConfig().theme.colors.indigo[500],
                //hoverBackgroundColor: tailwindConfig().theme.colors.indigo[600],
                barPercentage: 0.66,
                categoryPercentage: 0.66,
            }
        ]
    },
    options: {
        layout: {
            padding: {
                top: 12,
                bottom: 16,
                left: 70,
                right: 70
            },
        },
        scales: {
            "y-axis-1": {
                //id: "y-axis-1",
                type: "linear",
                position: "left",
                ticks: {
                    maxTicksLimit: 5,
                    callback: (value) => /*formatValue*/(value)/1000 + "K (units)",
                },
            },
            "y-axis-2": {
                //id: "y-axis-2",
                type: "linear",
                position: "right",
                ticks: {
                    maxTicksLimit: 5,
                    callback: (value) =>  /*formatValue*/(value) + "M ($)",
                },
            },
            x: {
                /*  type: 'time',
                 time: {
                     parser: 'MM-DD-YYYY',
                     unit: 'month',
                     displayFormats: {
                         month: 'MMM YY',
                     },
                 }, */
                grid: {
                    display: false,
                    drawBorder: false,
                },
                position: 'bottom'
            },
        },
        plugins: {
            legend: {
                display: true,
            },
            tooltip: {
                callbacks: {
                    title: () => false, // Disable tooltip title
                    label: (context) => /*formatValue*/(context.parsed.y),
                },
            },
        },
        interaction: {
            intersect: false,
            mode: 'nearest',
        },
        animation: {
            duration: 500
        },
        maintainAspectRatio: false,
        resizeDelay: 200,
    },
});

Here's the translated code snippet you requested.

英文:

Your axes configuration seems to follow the format from an older version of charts.js. The main change has to be that the in the options, scales is an object (not an array), and each individual axis will appear under its id as a key:

{
   options:{
      scales:{
         "y-axis-1": {
         // ...........
         },
         "y-axis-2": {
         // ...........
         },
         x: {
         // ...........
         }
      }
   }
}

Here's a simplified version of your code for illustration purposes, non-react, with function that were not defined excluded, with invented data and category x axis rather than time:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const ctx = document.getElementById(&#39;myChart&#39;),
    container = ctx.parentElement;

new Chart(ctx, {
    type: &#39;bar&#39;,
    data: {
        labels: [&#39;q&#39;,&#39;w&#39;,&#39;e&#39;,&#39;r&#39;],
        datasets: [
            {
                label: &#39;Sales (in units)&#39;,
                data: [1100, 2200, 3300, 4400],
                yAxisID: &quot;y-axis-1&quot;,
                //backgroundColor: tailwindConfig().theme.colors.blue[400],
                //hoverBackgroundColor: tailwindConfig().theme.colors.blue[500],
                barPercentage: 0.66,
                categoryPercentage: 0.66,
            },
            {
                label: &#39;Spend (million USD)&#39;,
                data: [3, 2, 4, 1],
                yAxisID: &quot;y-axis-2&quot;,
                //backgroundColor: tailwindConfig().theme.colors.indigo[500],
                //hoverBackgroundColor: tailwindConfig().theme.colors.indigo[600],
                barPercentage: 0.66,
                categoryPercentage: 0.66,
            }
        ]
    },
    options: {
        layout: {
            padding: {
                top: 12,
                bottom: 16,
                left: 70,
                right: 70
            },
        },
        scales: {
            &quot;y-axis-1&quot;: {
                //id: &quot;y-axis-1&quot;,
                type: &quot;linear&quot;,
                position: &quot;left&quot;,
                ticks: {
                    maxTicksLimit: 5,
                    callback: (value) =&gt; /*formatValue*/(value)/1000 + &quot;K (units)&quot;,
                },
            },
            &quot;y-axis-2&quot;: {
                //id: &quot;y-axis-2&quot;,
                type: &quot;linear&quot;,
                position: &quot;right&quot;,
                ticks: {
                    maxTicksLimit: 5,
                    callback: (value) =&gt;  /*formatValue*/(value) + &quot;M ($)&quot;,
                },
            },
            x: {
                /*  type: &#39;time&#39;,
                 time: {
                     parser: &#39;MM-DD-YYYY&#39;,
                     unit: &#39;month&#39;,
                     displayFormats: {
                         month: &#39;MMM YY&#39;,
                     },
                 }, */
                grid: {
                    display: false,
                    drawBorder: false,
                },
                position: &#39;bottom&#39;
            },
        },
        plugins: {
            legend: {
                display: true,
            },
            tooltip: {
                callbacks: {
                    title: () =&gt; false, // Disable tooltip title
                    label: (context) =&gt; /*formatValue*/(context.parsed.y),
                },
            },
        },
        interaction: {
            intersect: false,
            mode: &#39;nearest&#39;
        },
        animation: {
            duration: 500
        },
        maintainAspectRatio: false,
        resizeDelay: 200,
    },
});

<!-- language: lang-html -->

&lt;div style=&quot;position: relative; min-height:300px; width: 90vw&quot; &gt;
    &lt;canvas id=&quot;myChart&quot;&gt;&lt;/canvas&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 07:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定