{"id":28089132,"url":"https://github.com/heofs/trendline","last_synced_at":"2025-05-13T12:54:00.766Z","repository":{"id":57379799,"uuid":"267522621","full_name":"heofs/trendline","owner":"heofs","description":"Generate a trend line using linear regression","archived":false,"fork":false,"pushed_at":"2023-10-08T10:19:27.000Z","size":333,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T21:40:07.341Z","etag":null,"topics":["hacktoberfest","javascript","linear-regression","node","npm","prediction","statistics","trendline"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/trendline","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heofs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-05-28T07:27:50.000Z","updated_at":"2025-04-12T10:24:42.000Z","dependencies_parsed_at":"2022-09-06T03:32:30.202Z","dependency_job_id":null,"html_url":"https://github.com/heofs/trendline","commit_stats":{"total_commits":8,"total_committers":2,"mean_commits":4.0,"dds":0.25,"last_synced_commit":"654fa567561615e53e7694baa9b13949d0fb3b28"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heofs%2Ftrendline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heofs%2Ftrendline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heofs%2Ftrendline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heofs%2Ftrendline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heofs","download_url":"https://codeload.github.com/heofs/trendline/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253948328,"owners_count":21988953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","javascript","linear-regression","node","npm","prediction","statistics","trendline"],"created_at":"2025-05-13T12:54:00.139Z","updated_at":"2025-05-13T12:54:00.748Z","avatar_url":"https://github.com/heofs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trendline.js\n\nA lightweight JavaScript library to generate a trend line using linear regression. Display the trendline using something like [D3.js](https://d3js.org/) or [Recharts](https://recharts.org/).\n\n## Example\n\n```js\nimport { createTrend } from 'trendline';\n// OR\n// const { createTrend } = require('trendline');\n\nconst data = [\n  { y: 2, x: 1 },\n  { y: 4, x: 2 },\n  { y: 5, x: 3 },\n  { y: 4, x: 4 },\n  { y: 5, x: 5 },\n];\n\n// Takes the following arguments (dataset, xKey, yKey)\nconst trend = createTrend(data, 'x', 'y');\n\nconsole.log(trend);\n// { slope: 0.6, yStart: 2.2, calcY: [Function: calcY] }\n```\n\n## The object returned from `createTrend`\n\nThe object has the following 3 properties.\n\n`slope`, a number representing how sharply the trend increases.\n\n`yStart`, a number showing where the trend line starts on the Y axis.\n\n`calcY`, a function to calculate what the Y value is based on the X value.\n\n### Illustrating `calcY`\n\n```js\nconst data = [\n  { y: 2, x: 1 },\n  { y: 4, x: 2 },\n  { y: 5, x: 3 },\n  { y: 4, x: 4 },\n  { y: 5, x: 5 },\n];\n\nconst trend = createTrend(data, 'x', 'y');\n\nconsole.log(trend.yStart); // 2.2\n\n// yStart is representing the Y value when X is 0\n// This will always returns true.\nconsole.log(trend.calcY(0) === trend.yStart); // True\n```\n\n## Example use with Recharts\n\n```jsx\nimport React from 'react';\nimport { createTrend } from 'trendline';\n\nimport { LineChart, Line, XAxis, YAxis } from 'recharts';\n\nconst Graph = () =\u003e {\n  const { weightData } = useAPI();\n\n  const weights = weightData.map((data) =\u003e data.weight);\n  const yMax = Math.max(...weights);\n  const yMin = Math.min(...weights);\n  const timestamps = weightData.map((data) =\u003e data.dateTime);\n  const xMax = Math.max(...timestamps);\n  const xMin = Math.min(...timestamps);\n\n  const trendData = () =\u003e {\n    const trend = createTrend(weightData, 'dateTime', 'weight');\n\n    return [\n      { weight: trend.calcY(xMin), dateTime: xMin },\n      { weight: trend.calcY(xMax), dateTime: xMax },\n    ];\n  };\n\n  return (\n    \u003cLineChart\n      data={weightData}\n      margin={{ top: 5, right: 30, bottom: 5, left: -20 }}\n      onMouseLeave={() =\u003e setMessage('')}\n    \u003e\n      \u003cXAxis\n        name=\"Time\"\n        type=\"number\"\n        dataKey=\"dateTime\"\n        domain={['dataMin', 'dataMax']}\n      /\u003e\n      \u003cYAxis\n        name=\"Weight\"\n        type=\"number\"\n        dataKey=\"weight\"\n        domain={[yMin, yMax]}\n      /\u003e\n      \u003cLine type=\"monotoneX\" dataKey=\"weight\" /\u003e\n\n      \u003cLine\n        data={trendData()}\n        dataKey=\"weight\"\n        stroke=\"red\"\n        strokeDasharray=\"3 3\"\n      /\u003e\n    \u003c/LineChart\u003e\n  );\n};\n```\n\n## Contributing\n\nI appreciate anyone who wants to contribute to the project.\nIf you find any improvements to be made to the library, feel free to raise a PR for it on GitHub.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheofs%2Ftrendline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheofs%2Ftrendline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheofs%2Ftrendline/lists"}