Code Monkey home page Code Monkey logo

ngx-trend's Introduction

Angular Trend

ngx-trend


npm circleci codecov


This is a port of the react-trend library by unsplash.

Demo

https://ngx-trend.vercel.app

Features

  • Simple. Integrate in seconds.
  • Scalable. Uses SVG for sharp, scalable graphs. Will fill the parent container, or you can provide a fixed size.
  • Beautiful. Built-in gradient support, and customizable smoothing.
  • Animatable. Support for on-mount animations where the trend graph draws from left to right using Angular's native Animations library
  • Tiny. gzips to <4kb.

Installation

npm install ngx-trend

Dependencies

Latest version available for each version of Angular

ngx-trend Angular
3.4.3 6.x 7.x
4.0.2 8.x
5.0.1 9.x
6.1.1 10.x 11.x
7.0.0 12.x
current >= 13.x

Quickstart

// app.module.ts
import { NgModule } from '@angular/core';
import { TrendModule } from 'ngx-trend';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
  imports: [BrowserAnimationsModule, TrendModule],
})
export class AppModule {}

// your.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'your-component',
  template: ` <ngx-trend [data]="[0, 10, 5, 22, 3.6, 11]"></ngx-trend> `,
})
export class YourComponent {}

// That's it!
// You can, of course, customize it. Check out the API Reference below.
// Be sure to check out `autoDraw`, `gradient`, and `smoothing`.

API Reference

SVG Props

By default, all properties not recognized by React Trend will be delegated to the SVG. The line inherits these properties if none of its own override them.

This means that, among other properties, you can use:

  • stroke to set a solid color,
  • strokeWidth to change the default line thickness,
  • strokeOpacity to create a transparent line,
  • strokeLinecap/strokeLinejoin to control the edges of your line,
  • strokeDasharray to create a dashed line, and
  • strokeDashoffset to control where the dashes start.

autoDraw

Type Required Default
boolean โœ• false

Allow the line to draw itself on mount. Set to true to enable, and customize using autoDrawDuration and autoDrawEasing.

NOTE: This property uses strokeDasharray and strokeDashoffset under the hood to perform the animation. Because of this, any values you provide for those properties will be ignored.

Example
<ngx-trend
  [data]="data"
  [autoDraw]="true"
  [autoDrawDuration]="3000"
  autoDrawEasing="ease-in"
></ngx-trend>

autoDrawDuration

Type Required Default
number โœ• 2000

The amount of time, in milliseconds, that the autoDraw animation should span.

This prop has no effect if autoDraw isn't set to true.

Example
<ngx-trend
  [data]="data"
  autoDraw="true"
  autoDrawDuration="3000"
  autoDrawEasing="ease-in"
></ngx-trend>

autoDrawEasing

Type Required Default
string โœ• ease

The easing function to use for the autoDraw animation. Accepts any transition timing function within the CSS spec (eg. linear, ease, ease-in, cubic-bezier...).

This prop has no effect if autoDraw isn't set to true.

Example
<ngx-trend
  [data]="data"
  [autoDraw]="true"
  [autoDrawDuration]="3000"
  autoDrawEasing="ease-in"
></ngx-trend>

data

Type Required Default
number[] object[] โœ“ undefined

The data accepted by React Trend is incredibly simple: An array of y-axis values to graph.

React Trend takes care of normalization, so don't worry about ensuring the data is in a specific range.

This does mean that all data points will be evenly-spaced. If you have irregularly-spaced data, it will not be properly represented.

As of v1.2.0, you may supply an array of data objects with a value property.

Example
<ngx-trend [data]="[120, 149, 193.4, 200, 92]"></ngx-trend>
<ngx-trend [data]="[{ value: 4 }, { value: 6 }, { value: 8 }]"></ngx-trend>

gradient

Type Required Default
string[] โœ• undefined

React Trend supports vertical gradients. It accepts an array of 2+ color values, and will fade evenly between them from the bottom up.

color can be specified as any SVG-supported format (named, rgb, hex, etc).

Example
<ngx-trend [gradient]="['#0FF', '#F0F', '#FF0']"></ngx-trend>

height

Type Required Default
number โœ• undefined

Set an explicit height for your SVG. By default it ensures a 1:4 aspect ratio with the width, and the width expands to fill the container.

Note that in most cases it is sufficient to leave this blank, and just control the size of the parent container.

Example
<ngx-trend [width]="200" [height]="200"></ngx-trend>

padding

Type Required Default
number โœ• 8

If you set a very large strokeWidth on your line, you may notice that it gets "cropped" towards the edges. This is because SVGs don't support overflow.

By increasing this number, you expand the space around the line, so that very thick lines aren't cropped.

In most cases you don't need to touch this value.

Example
<ngx-trend strokeWidth="20" [padding]="18"></ngx-trend>

radius

Type Required Default
number โœ• 10

When using smoothing, you may wish to control the amount of curve around each point. For example, a 0 radius is equivalent to not having any smoothing at all, where an impossibly-large number like 10000 will ensure that each peak is as curved as it can possibly be.

This prop has no effect if smooth isn't set to true.

Example
<ngx-trend [smooth]="true" [radius]="20" [strokeWidth]="4"></ngx-trend>

smooth

Type Required Default
boolean โœ• false

Smooth allows the peaks to be 'rounded' out so that the line has no jagged edges.

By tweaking the radius prop, you can use this as a subtle prop to tone down the sharpness, or you can set a very high radius to create a snake-like line.

Example
<ngx-trend [smooth]="true" [radius]="20" [strokeWidth]="4"></ngx-trend>

width

Type Required Default
number โœ• undefined

Set an explicit width for your SVG. By default it ensures a 1:4 aspect ratio with the height, expanding to fill the width of the container.

Note that in most cases it is sufficient to leave this blank, and just control the width of the parent container.

Example
<ngx-trend [width]="200" [height]="200"></ngx-trend>

GitHub @scttcper ย ยทย  Twitter @scttcper

ngx-trend's People

Contributors

aguerot avatar damingerdai avatar dependabot[bot] avatar greenkeeper[bot] avatar renovate-bot avatar renovate[bot] avatar scttcper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ngx-trend's Issues

SVG disappears after routing with angular route

The current setup is a fixed side navigation with an ngx trend chart. The main components are routed.
If the route changes the svg disappears for some time till (maybe change detection runs).

If we look into the html code i found this line of code:
<path fill="none" class="ng-tns-c514-14 ng-trigger ng-trigger-pathAnimaiton" stroke="url('http://localhost:4200/plant-diagnosis/overview/cabinet/21#ngx-trend-vertical-gradient-7906924350227782')"

What i observed is that the svg comes visible again after this url changes to the right routing url which is after the data set is updated from the api (change detection).
Why is there a url used ?

The comp:

<ngx-trend autoDraw="true" autoDrawDuration="1000" autoDrawEasing="ease-out" smooth="true" [height]="60" [data]="systemCycleTimeData" [gradient]="['#2196F3']" radius="0.1" strokeWidth="2.7" strokeLinecap="round" > </ngx-trend>

updating data's values array

Hello Scott,

Any idea's on how to update [data]'s values? or would that require a complete tear-down of the component?

thanks in advance, marc

Dependency deprecation warning: travis-deploy-once (npm)

On registry https://registry.npmjs.org/, the "latest" version (v5.0.11) of dependency travis-deploy-once has the following deprecation notice:

We recommend to use Travis Build Stages instead

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about travis-deploy-once's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

gradient binding is not working

Setup

"ngx-trend": "^3.0.4"

I have this code in my component
<ngx-trend autoDraw="true" autoDrawDuration="1000" autoDrawEasing="ease-out" smooth="true" [data]="[7,5,3,1,0,0]" [gradient]="[ '#1feaea', '#ffd200', '#f72047' ]" strokeLinecap="round" > </ngx-trend>

Result

The gradient is not applied to the path thus trend line is invisible

Expected result

Gradient is applied correctly

Workaround

Remove [gradient] property from markup but the trendline is then black

My guess

The generated stroke reference in path is not formatted correctly as per bellow:
<path class="ng-tns-c25-26 ng-trigger ng-trigger-pathAnimaiton" fill="none" stroke="url('http://localhost:4300/#/dashboard#ngx-trend-vertical-gradient-4337036028564612" d="M 8,8 L 55.213284686572436,22.01199092810751 S 64.8,24.857142857142854 74.38671531342756,27.702294786178197 L 112.01328468657243,38.86913378525037 S 121.6,41.714285714285715 131.18671531342756,44.559437643321054 L 168.81328468657244,55.72627664239323 S 178.4,58.57142857142857 188.29168704874172,60.039259396065795 L 225.30831295125827,65.53216917536277 S 235.2,67 245.2,67 L 292,67" style=""></path>

missing ') at the end of stroke atribute here:

stroke="url('http://localhost:4300/#/dashboard#ngx-trend-vertical-gradient-4337036028564612"

svgHeight

Thanks for your job. Can you make svgHeight like input property. I want to set svgHeight='100%' but I can't do it.

[Feature Request] Support Boundaries provided through the component

Thanks for the awesome library. Im using it to draw multiple trends of similiar data.
Is it possible, to provide min / max (boundary) values to the library, so that charts are more meaningfull when displaying the same data.

In this example, the last chart looks like it has the same changes as the others, but in reality the range between min and max is a lot smaller.
image

I tried using the height, but this leads to other issues.

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.