简体   繁体   English

如何从 ChartJS 的回调中返回 JSX?

[英]How to return JSX from a callback in ChartJS?

I'm using react-chartjs-2 in a React app and I would like to put JSX code into the Y axis.我在 React 应用程序中使用react-chartjs-2 ,我想将 JSX 代码放入 Y 轴。 When I try this it only displays [Object object] .当我尝试这样做时,它只显示[Object object]

const chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            y: {
                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, ticks) {
                        return <p>This is JSX</p>;
                    }
                }
            }
        }
    }
});

How can I create a Chart with JSX elements?如何使用 JSX 元素创建图表?

Short answer : You can't简短回答:你不能

Explanation:解释:
Since chart.js is a canvas based library it does not use dom elements to display things on the chart.由于 chart.js 是一个基于 canvas 的库,它不使用 dom 元素在图表上显示内容。 For this you will need to use a svg library like recharts.为此,您需要使用 svg 库,例如 recharts。

So by default you are bound to what the callback enables you to do.因此,默认情况下,您必须受回调允许您执行的操作的约束。 If you want more customization you will need to write a custom plugin that uses the canvas API to draw the things you want on the canvas itself.如果你想要更多的定制,你需要编写一个自定义插件,使用 canvas API 在 canvas 本身上绘制你想要的东西。

https://www.w3schools.com/html/html5_canvas.asp https://www.chartjs.org/docs/4.2.0/developers/plugins.html https://www.w3schools.com/html/html5_canvas.asp https://www.chartjs.org/docs/4.2.0/developers/plugins.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM