1.3.9 • Published 4 months ago

solid-chartjs v1.3.9

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

solid-chartjs

version downloads telegram chat

The solid-chartjs library is a SolidJS wrapper around the Chart.js library, allowing you to easily create interactive charts in your SolidJS applications.

Quick start

Installation:

pnpm add solid-chartjs chart.js @solid-primitives/refs
# or
yarn add solid-chartjs chart.js @solid-primitives/refs
# or
npm i solid-chartjs chart.js @solid-primitives/refs

Usage:

import { onMount } from 'solid-js'
import { Chart, Title, Tooltip, Legend, Colors } from 'chart.js'
import { Line } from 'solid-chartjs'

const MyChart = () => {
    /**
     * You must register optional elements before using the chart,
     * otherwise you will have the most primitive UI
     */
    onMount(() => {
        Chart.register(Title, Tooltip, Legend, Colors)
    })

    const chartData = {
        labels: ['January', 'February', 'March', 'April', 'May'],
        datasets: [
            {
                label: 'Sales',
                data: [50, 60, 70, 80, 90],
            },
        ],
    }
    const chartOptions = {
        responsive: true,
        maintainAspectRatio: false,
    }

    return (
        <div>
            <Line data={chartData} options={chartOptions} width={500} height={500} />
        </div>
    )
}

If you don't want to import and register the controllers, elements, scales, and plugins you want to use, you can use the following solution:

!NOTE\ It is considered to better use the tree-shakable way, to decrease the bundle size.

import 'chart.js/auto'
import { DefaultChart } from 'solid-chartjs'

<DefaultChart type="line" data={data} options={options} />

Chart Props

PropDescriptionType
widthThe width of the chart canvas in pixels.number | undefined
heightThe height of the chart canvas in pixels.number | undefined
fallbackA fallback element to display when chart fails.JSX.Element
typeThe type of the chart.keyof ChartTypeRegistry
dataThe chart data object.ChartData | undefined
optionsThe chart options object.ChartOptions | undefined
pluginsThe chart plugins object.Plugin[] | undefined

Examples

Check out /dev folder and run the SolidJs application to see how it works.

You can also use the DefaultChart components:

!NOTE\ DefaultChart is a wrapper around Chart component, so you can use all the props from Chart component. DefaultChart component does not have its registrable elements registered by default, so you need to register them yourself unless you use chart.js/auto.

import { onMount } from 'solid-js'
import {
    Chart,
    LineController,
    CategoryScale,
    PointElement,
    LineElement,
    LinearScale,
} from 'chart.js'
import { DefaultChart } from 'solid-chartjs'

const MyChart = () => {
    onMount(() => {
        Chart.register(LineController, CategoryScale, PointElement, LineElement, LinearScale)
    })

    // ... your data and options objects

    return <DefaultChart type="line" data={data} options={options} width={400} height={300} />
}

Usage of fallback prop:

const fallback = () => {
    return (
        <div>
            <p>Chart is not available</p>
        </div>
    )
}

<DefaultChart type="bar" data={chartData} options={chartOptions} fallback={fallback} />

Credits

Contributing

Contribution Guidelines

Please read our Contribution Guidelines before you contribute.

Code and Commit Standards

Please read and follow the standards for this repo

These standards are laid out to ensure that the code base is as maintainable as possible.

License

MIT

1.0.0-develop.1

4 months ago

1.3.9

4 months ago

1.3.8

12 months ago

1.3.7

12 months ago

1.3.6

12 months ago

1.3.5

12 months ago

1.3.4

12 months ago

1.3.3

12 months ago

1.3.2

12 months ago

1.3.1

12 months ago

1.3.0

12 months ago

1.2.4

12 months ago

1.2.3

12 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.2.0

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago