@billboard.js/react

中文文档
1.1.1发布于 2023/11/30MITTypeScriptESM + CJS零依赖近 30 天下载 3062

React component for billboard.js

原始使用文档
文档目录5 节
  1. Installation
  2. How to use
  3. Basic Usage
  4. Using props passed to the component
  5. TypeScript Interfaces

@billboard.js/react

React component for billboard.js

Installation

# Install billboard.js together if you don't have it already
$ npm install billboard.js @billboard.js/react

How to use

ℹ️ The component will create a <div> element and append it to the parent element and bindto option is not supported in this case.

Basic Usage

import React, {useEffect, useRef} from "react";

// import billboard.js
import bb, {line} from "billboard.js";
import "billboard.js/dist/billboard.css";  // default css

// import react wrapper
import BillboardJS, {IChart} from "@billboard.js/react";
// const BillboardJS = require("@billboard.js/react");  // for CJS

function App() {
    // to get the instance, create ref and pass it to the component
    const chartComponent = useRef<IChart>(null);
    const options = {
        data: {
            columns: [
                ["data1", 300, 350, 300]
            ],
            type: line()
        }
    };

    useEffect(() => {
        // get the instance from ref
        const chart = chartComponent.current?.instance;

        // call APIs
        if (chart) {
            chart.load( ... );
        }
    }, []);

    return <BillboardJS 
        bb={bb}
        options={options}
        ref={chartComponent}

        /* The values will be specified to the bound element as inlined styles */
        style={{
            width: "500px",
            ...
        }}

        /* When class name doesn't contains `bb`,
           then you also need to update the default CSS to be rendered correctly. */
        className={"bb my-classname"}
    />;
}

Using props passed to the component

When the options are passed to the "chart" component.

// index.tsx
import App from "./App.tsx";

const options = {
    data: {
        columns: [
            ["data1", 300, 350, 300]
        ],
        type: "bar"
    }
};

<App {...options} />

// or
// <App data={
//     columns: [
//         ["data1", 300, 350, 300]
//     ],
//     type: "bar"
// } />

// App.tsx
import * as Chart from "billboard.js";
import "billboard.js/dist/billboard.css";  // default css
import BillboardJS, {IChart, IChartOptions} from "../src/index";

export function App(props: IChartOptions) {
    const chartComponent = useRef<IChart>(null);

    // when chart "type" is passed from props, chart types need to be initialized separately.
    // in this scenario, can't be "tree-shaken" only used chart type modules.
    Chart[props.data.type]();

    useEffect(() => {
        const chart = chartComponent.current?.instance;

        if (chart) {
            chart.load( ... );
        }
    }, []);

    return <BillboardJS 
        bb={Chart.bb} 
        options={props} 
        ref={chartComponent}
     />;
}

TypeScript Interfaces

// chart options
interface IChartOptions;

// @billboard.js/react props
interface IProp extends Pick<HTMLProps<HTMLDivElement>, "className" | "style"> {
    bb: typeof bb;
    options: ChartOptions;
}

// Chart instance
interface IChart {
    instance: Chart;
}

版本与发布记录

@billboard.js/react 共发布 4 个版本,首次发布于 2022/09/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.1latest2023/11/302 年前
1.1.02023/04/073 年前
1.0.12023/02/063 年前
1.0.02022/09/014 年前

关于 @billboard.js/react 的常见问题

@billboard.js/react 是什么?

React component for billboard.js

如何安装 @billboard.js/react?

在项目目录运行 npm install @billboard.js/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:billboard.js(>=3.0.0)、react(>=16.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@billboard.js/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@billboard.js/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@billboard.js/react 可以商用吗?

@billboard.js/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@billboard.js/react 的下载量有多少?

根据 npm 官方下载统计,@billboard.js/react 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,062 次,日均约 102 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@billboard.js/react 还在维护吗?

最新版本 1.1.1 发布于 2023/11/30(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。