GithubHelp home page GithubHelp logo

sherrbss / lightweight-charts-react-wrapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trash-and-fire/lightweight-charts-react-wrapper

0.0 0.0 0.0 8.22 MB

React wrapper for financial lightweight charts

License: Other

Shell 0.29% JavaScript 0.41% TypeScript 97.77% CSS 0.99% HTML 0.54%

lightweight-charts-react-wrapper's Introduction

lightweight-charts-react-wrapper

bundle-size

React.js component-based wrapper for Lightweight Charts to easily create interactive financial charts in React

At the link above, you can find codesandbox examples for any use case, including legend, loading historical data, multiple series on the same chart, moving average, and more. Feel free to ask questions and ask for more use cases in the issues tab.

Installing

npm install lightweight-charts lightweight-charts-react-wrapper

Usage

import {Chart, LineSeries} from "lightweight-charts-react-wrapper";

const data = [
    {time: '2019-04-11', value: 80.01},
    {time: '2019-04-12', value: 96.63},
    {time: '2019-04-13', value: 76.64},
    {time: '2019-04-14', value: 81.89},
    {time: '2019-04-15', value: 74.43},
    {time: '2019-04-16', value: 80.01},
    {time: '2019-04-17', value: 96.63},
    {time: '2019-04-18', value: 76.64},
    {time: '2019-04-19', value: 81.89},
    {time: '2019-04-20', value: 74.43},
];

export function App() {
    return (
        <Chart width={800} height={600}>
            <LineSeries data={data}/>
        </Chart>
    );
}

Getting reference to lightweight-chart objects

You can use the ref property to get a reference to a lightweight-chart api-instance from any component.

function App() {
    const ref = useRef(null);
    return (
        <>
            <Chart width={400} height={300} ref={ref}/>
            <button onClick={() => ref.current?.timeScale().fitContent()}>Fit Content</button>
        </>
    )
}

Components

Chart

<Chart> - main chart container and wrapping dom element. You can pass any option from ChartOptions as separate property.

Events:

Use the ref property to get a reference to a IChartApi instance

Series

Following types of series are supported:

  • <AreaSeries>
  • <BarSeries>
  • <BaselineSeries>
  • <CandlestickSeries>
  • <HistogramSeries>
  • <LineSeries>
  • <CustomSeries>

Series components should be nested inside a chart component.

You can pass any series option as separate property. List of available options corresponding to each type of series can be found here

Use the ref property to get reference to a ISeriesApi<SeriesType> instance.

Passing data

To pass a data to a series you can use the data property. Look here to find what shape of data you need for each series type.

By default data represents only the initial data. Any subsequent data update does not update series. If you want to change this behavior please add reactive={true} to your series component. In this case series will apply a new data if it is not reference equal to previous array.

Passing markers

To pass markers to a series you can use the markers property. Markers should be an array of SeriesMarker<Time>.

Custom series

You can pass an instance of a class that implements the ICustomSeriesPaneView interface as the value of the view property. All additional properties other than children, ref, reactive, and markers will be passed to the ICustomSeriesPaneView::update method

function BrushableSeries() {
    const [view] = useState(() => new BrushableAreaSeries())
    return <CustomSeries view={view} data={data} {...options}/>
}

Look the demo page for an example of custom series.

Price line

To draw price line add <PriceLine> component inside any series.

    <Chart width={600} height={300}>
        <LineSeries data={data}>
            <PriceLine
                title="minimum price"
                price={minimumPrice}
            />
            <PriceLine
                title="average price"
                price={avgPrice}
            />
            <PriceLine
                title="maximum price"
                price={maximumPrice}
            />
        </LineSeries>
    </Chart>

You can pass any options from PriceLineOptions as separate property. The price property is mandatory in dev mode.

Use the ref property to get reference to a IPriceLine instance.

Custom Series Primitives

You can implement your own series primitive using the <SeriesPrimitive> component.

export function VerticalLine() {
    const [view] = useState(() => new VertLine());
    
    return (
        <SeriesPrimitive
            view={view}
            time={Date.now()}
            showLabel={true}
            color={color}
            labelText={'Hello'}
        />
    );
}

The only mandatory property is view which should be an instance of the class that implements ISeriesPrimitive and additional method applyOptions(options: T): void. All additional properties other than view will be passed to the applyOptions method.

Any series primitive should be nested inside a <[Type]Series> component.

Look the demo page for an example of vertical lines.

Time scale

<TimeScale> - the component is a binding to the current time scale of the current chart. This component has to be nested inside a chart component and should not have duplicates. Each chart has only one time scale.

You can pass any option from TimeScaleOptions as separate property.

Events:

Use the ref property to get reference to a ITimeScaleApi instance.

Note: don't use ChartOptions['timeScale'] and <TimeScale> component at the same time. This can lead to uncontrolled overwriting of options.

Price scale

<PriceScale> - the component is a bindings to a certain price scale. This component has to be nested inside chart component and requires an id property. Two price scales with the same id within the same chart result in undefined behaviour.

You can pass any option from PriceScaleOptions as separate property.

Note: don't use ChartOptions['leftPriceScale']' or ChartOptions['rightPriceScale'] or ChartOptions['overlayPriceScale'] and <PriceScale> at the same time. This can lead to uncontrolled overwriting of options.

Licence

MIT

Review the license requirements for the required "attribution notice" in the Lightweight Chart Repository.

lightweight-charts-react-wrapper's People

Contributors

trash-and-fire avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.