npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

polynomial-curve-fitting

v3.1.0

Published

A React component to interactively compile polyomial curves with D3 using least squares regression

Downloads

7

Readme

Polynomial Curve Fitting

License: MIT npm version

A React component to interactively compile polyomial curves with D3 using least squares regression

Preview

Installation

npm i polynomial-curve-fitting

Usage

Use the react component generating a random curve:

import React from 'react';
import PolynomialCurveFitting from 'polynomial-curve-fitting';

const App = () => <PolynomialCurveFitting></PolynomialCurveFitting>;

export default App;
import React from 'react';
import PolynomialCurveFitting from 'polynomial-curve-fitting';

const App = () => <PolynomialCurveFitting></PolynomialCurveFitting>;

export default App;

The following sections show:

  1. how to specify a curve
  2. how to provide graph settings
  3. how to get the curve as output

Specify a Curve

To provide initial information about the curve, there are four options:

  1. Use PropsBaseCurve and specify the following fields (all optional):

    const curve1 = {
      name: 'Fancy Polynomial',
      description: 'This polynomial is a random polynomial.',
      xAxis: { label: 'x Axis', min: 0, max: 10 },
      yAxis: { label: 'y Axis', min: 0, max: 10 },
    };
    import { PropsBaseCurve } from 'polynomial-curve-fitting/lib/types';
    const curve1: PropsBaseCurve = {
      name: 'Fancy Polynomial',
      description: 'This polynomial is a random polynomial.',
      xAxis: { label: 'x Axis', min: 0, max: 10 },
      yAxis: { label: 'y Axis', min: 0, max: 10 },
    };
  2. Use PropsCurvePoints to specify points for the least squares regression. The order of the polynomial will be one less than the number of provided points. The fields of PropsBaseCurve are still optional, but the points field is required:

    const curve2 = {
      points: [
        [0, 0],
        [1, 1],
      ],
    };
    import { PropsCurvePoints } from 'polynomial-curve-fitting/lib/types';
    const curve2: PropsCurvePoints = {
      points: [
        [0, 0],
        [1, 1],
      ],
    };
  3. Use PropsCurveOrder to specify the order of the polynomial. The points on the curve are randomly generated. The fields of PropsBaseCurve are still optional, but the polynomialOrder field is required:

    const curve3 = {
      polynomialOrder: 2,
    };
    import { PropsCurveOrder } from 'polynomial-curve-fitting/lib/types';
    const curve3: PropsCurveOrder = {
      polynomialOrder: 2,
    };
  4. Use PropsCurveCoefficients to specify the coefficients of the polynomial. When optionally specifying points, the x values are taken to create points on the curve (ideally the y values match). If no points have been specified, they are randomly generated. The fields of PropsBaseCurve are still optional, but the coefficients field is required:

    const curve4 = {
      coefficients: [-0.1, 1.25, 5],
      points: [
        [0, 5],
        [1, 6.15],
        [2, 7.1],
      ],
    };
    import { PropsCurveCoefficients } from 'polynomial-curve-fitting/lib/types';
    const curve4: PropsCurveCoefficients = {
      coefficients: [-0.1, 1.25, 5],
      points: [
        [0, 5],
        [1, 6.15],
        [2, 7.1],
      ],
    };

Provide the information about the curve as follows:

import PolynomialCurveFitting from 'polynomial-curve-fitting';
import React from 'react';

// const curve ...

const App = () => <PolynomialCurveFitting curve={curve}></PolynomialCurveFitting>;

export default App;
import PolynomialCurveFitting from 'polynomial-curve-fitting';
import React from 'react';

// const curve ...

const App = () => <PolynomialCurveFitting curve={curve}></PolynomialCurveFitting>;

export default App;

Provide Settings

In addition to the curve prop, there is also a settings prop that allows to set the style of the drawn graph (e.g, the size of the svg, font sizes, colors, and spacing). Have a look at the default props to see the default settings and what can be changed.

const App = () => <PolynomialCurveFitting settings={settings}></PolynomialCurveFitting>;

Get Curve

To get updates on the curve while changing it within the component, use a callback function as shown in the following example:

import PolynomialCurveFitting from 'polynomial-curve-fitting';
import React from 'react';

const App = () => {
  const [curve, setCurve] = React.useState();
  return (
    <div>
      <PolynomialCurveFitting curveChange={value => setCurve(value)}></PolynomialCurveFitting>
      <pre>
        {JSON.stringify(curve, (_, v) => (v instanceof Array ? JSON.stringify(v, null) : v), 3)}
      </pre>
    </div>
  );
};

export default App;
import PolynomialCurveFitting from 'polynomial-curve-fitting';
import { CurveOut } from 'polynomial-curve-fitting/lib/types';
import React from 'react';

const App = () => {
  const [curve, setCurve] = React.useState<CurveOut>();
  return (
    <div>
      <PolynomialCurveFitting
        curveChange={(value: CurveOut) => setCurve(value)}
      ></PolynomialCurveFitting>
      <pre>
        {JSON.stringify(curve, (_, v) => (v instanceof Array ? JSON.stringify(v, null) : v), 3)}
      </pre>
    </div>
  );
};

export default App;

Set Translations

The internationalization prop accepts a PropsInternationalization object specifying translations. Depending on the current UI language change the i18n object in the following example to either ENGLISH or GERMAN. Have a look at the default props to see the default strings and what can be changed. Note that every string is optional.

import PolynomialCurveFitting from 'polynomial-curve-fitting';
import React from 'react';

const ENGLISH = {
  textSettings: { title: 'English Title' },
};

const GERMAN = {
  textSettings: {
    title: 'German Title',
    curveName: {
      label: 'German Label',
    },
  },
};

const App = () => {
  const [lang, setLang] = React.useState('en');
  const [i18n, setI18n] = React.useState(ENGLISH);

  const langChange = newLang => {
    setLang(newLang);
    newLang === 'en' && setI18n(ENGLISH);
    newLang === 'de' && setI18n(GERMAN);
  };

  return (
    <div>
      <select value={lang} onChange={e => langChange(e.target.value)}>
        <option value="en">EN</option>
        <option value="de">DE</option>
      </select>
      <PolynomialCurveFitting internationalization={i18n}></PolynomialCurveFitting>
    </div>
  );
};

export default App;
import PolynomialCurveFitting from 'polynomial-curve-fitting';
import { PropsInternationalization } from 'polynomial-curve-fitting/lib/types';
import React from 'react';

const ENGLISH: PropsInternationalization = {
  textSettings: { title: 'English Title' },
};

const GERMAN: PropsInternationalization = {
  textSettings: {
    title: 'German Title',
    curveName: {
      label: 'German Label',
    },
  },
};

const App = () => {
  const [lang, setLang] = React.useState<string>('en');
  const [i18n, setI18n] = React.useState<PropsInternationalization>(ENGLISH);

  const langChange = (newLang: string) => {
    setLang(newLang);
    newLang === 'en' && setI18n(ENGLISH);
    newLang === 'de' && setI18n(GERMAN);
  };

  return (
    <div>
      <select value={lang} onChange={e => langChange(e.target.value)}>
        <option value="en">EN</option>
        <option value="de">DE</option>
      </select>
      <PolynomialCurveFitting internationalization={i18n}></PolynomialCurveFitting>
    </div>
  );
};

export default App;

Development

The following steps show how to make changes to polynomial-curve-fitting and use the component in an pcf-example react app. The commands used below assume that the two projects are sibling directories.

Clone and Build polynomial-curve-fitting

Within the terminal, execute the following commands:

# clone the repository
git clone https://github.com/alexscheitlin/polynomial-curve-fitting.git

# install the dependencies
cd polynomial-curve-fitting
npm install

# continuously build the library
npm run build:watch

Create new React App pcf-example

Open a new terminal tab and execute the following commands:

# create new react app with typescript
npx create-react-app pcf-example --template typescript

# install this library as a dependency
cd pcf-example
npm install ../polynomial-curve-fitting

# link react of the library with the one of the example react app
cd ../polynomial-curve-fitting/
npm link ../pcf-example/node_modules/react

# only do this if the example react app uses material ui
# link material ui of the library with the one of the example react app
npm link ../pcf-example/node_modules/@material-ui/core

# start the example react app
cd ../pcf-example/
npm run start

Add the react component to the App.tsx file (see Usage)