简体   繁体   English

使用 react-chartjs-2 的折线图图例边框样式

[英]Line chart legend border style using react-chartjs-2

Is it possible to make the legend box of dashed line in line chart to have no dashed border?是否可以让折线图中的虚线图例框没有虚线边框?

I read about generateLegend() and legendCallback but I can't understand how it works in react-chartjs-2 together with React functional component.我阅读了 generateLegend() 和 legendCallback,但我不明白它在 react-chartjs-2 和 React 功能组件中是如何工作的。 Also, I'm not sure if they can be used to change the border style of legend.另外,我不确定它们是否可以用来改变图例的边框样式。

This is my sample code:这是我的示例代码:

import { Line } from "react-chartjs-2";

const LineChart = () => {
    const data = {
        labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
        datasets: [
            {
                label: "Line 1", data: [10, 10, 20, 20, 30, 30]
            },
            {
                label: 'Line 2',
                borderDash: [15, 5],
                data: [20, 20, 20, 20, 20, 20],
            }],
    };

    const options = {
        legend: {
            position: legendPosition,
            align: legendAlign,
        }
    };

    return <Line data={data} options={options} />
};

This is sample of desired Line 1(red) and Line 2(grey dahsed)这是所需的第 1 行(红色)和第 2 行(灰色虚线)的示例在此处输入图像描述

You have to use borderWidth:0 so, please remove borderDash: [15, 5] and add borderWidth: 0, .您必须使用borderWidth:0所以,请删除borderDash: [15, 5]并添加borderWidth: 0, Or you can completely replace data with below:或者您可以使用以下内容完全替换数据:

const data = {
    labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
    datasets: [
        {
            borderWidth: 0,
            label: "Line 1", data: [10, 10, 20, 20, 30, 30]
        },
        {
            label: 'Line 2',
            borderWidth: 0,
            data: [20, 20, 20, 20, 20, 20],
        }],
};

Update更新

You have to use borderWidth: 0 in data.datasets and this is mandatory for you requirement.您必须在 data.datasets 中使用borderWidth: 0 ,这对您的要求是强制性的。 Moreover, you can pass options={options} into Line to have more configuration for legend like legend background-color or legend font-color or legend boxWidth and so on.此外,您可以将options={options}传递给Line以对图例进行更多配置,例如legend background-colorlegend font-colorlegend boxWidth等。

Here is the Code Sandbox这是代码沙箱

Here is the output picture这是output图片

在此处输入图像描述

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

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