forked from valor-software/ng2-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demo: Demo custom chart type (financial)
Closes valor-software#876
- Loading branch information
Showing
14 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "chartjs-chart-financial"] | ||
path = chartjs-chart-financial | ||
url = [email protected]:chartjs/chartjs-chart-financial.git |
Submodule chartjs-chart-financial
added at
de5d0d
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.chart { | ||
display: block; | ||
width: 1300px; | ||
height: 600px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<div> | ||
<markdown ngPreserveWhitespaces> | ||
|
||
This chart demonstrates how custom chart types can be plugged into `ng2-charts`. Check out the Typescript source | ||
code below. | ||
|
||
This custom chart type depends on a package called [`chartjs-chart-financial`](https://github.com/chartjs/chartjs-chart-financial) | ||
which is not yet on [npmjs.com](https://npmjs.com). It is included in the demo app as a git submodule directly from the github site of the package. | ||
|
||
This also demonstrates using a different date/time adapter than [`moment`](https://www.npmjs.com/package/moment). | ||
This demo uses [`luxon`](https://www.npmjs.com/package/luxon) instead, plugged into the `chart.js` module using | ||
the [`chartjs-adapter-luxon`](https://www.npmjs.com/package/chartjs-adapter-luxon) package. | ||
|
||
Developers who wish to run this locally must first clone the demo app, then run some npm commands: | ||
|
||
```bash | ||
git clone [email protected]:valor-software/ng2-charts.git | ||
npm install | ||
npm run install:financial | ||
npm run build:financial | ||
npm run build:lib | ||
``` | ||
|
||
Then you may run `npm start` to run the demo app locally on http://localhost:4200 | ||
</markdown> | ||
</div> | ||
<div class="chart"> | ||
<canvas baseChart [datasets]="financialChartData" [options]="financialChartOptions" | ||
[colors]="financialChartColors" [legend]="financialChartLegend" [chartType]="financialChartType" | ||
[plugins]="financialChartPlugins"> | ||
</canvas> | ||
</div> | ||
<div> | ||
<button mat-button mat-raised-button color="primary" (click)="update()">Toggle Chart Type</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { FinancialChartComponent } from './financial-chart.component'; | ||
import { ChartsModule } from 'ng2-charts'; | ||
|
||
describe('FinancialChartComponent', () => { | ||
let component: FinancialChartComponent; | ||
let fixture: ComponentFixture<FinancialChartComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [FinancialChartComponent], | ||
imports: [ | ||
ChartsModule, | ||
], | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(FinancialChartComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Component, OnInit, ViewChild } from '@angular/core'; | ||
import 'dist/chartjs-chart-financial/chartjs-chart-financial'; | ||
import * as luxon from 'luxon'; | ||
import 'chartjs-adapter-luxon'; | ||
import { ChartOptions } from 'chart.js'; | ||
import { Color, BaseChartDirective } from 'ng2-charts'; | ||
|
||
@Component({ | ||
selector: 'app-financial-chart', | ||
templateUrl: './financial-chart.component.html', | ||
styleUrls: ['./financial-chart.component.css'] | ||
}) | ||
export class FinancialChartComponent implements OnInit { | ||
barCount = 60; | ||
initialDateStr = '01 Apr 2017 00:00 Z'; | ||
|
||
public financialChartData = [ | ||
{ | ||
label: 'CHRT - Chart.js Corporation', | ||
data: this.getRandomData(this.initialDateStr, this.barCount) | ||
}, | ||
]; | ||
public financialChartOptions: ChartOptions = { | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
}; | ||
public financialChartColors: Color[] = [ | ||
{ | ||
borderColor: 'black', | ||
backgroundColor: 'rgba(255,0,0,0.3)', | ||
}, | ||
]; | ||
public financialChartLegend = true; | ||
public financialChartType = 'candlestick'; | ||
public financialChartPlugins = []; | ||
|
||
@ViewChild(BaseChartDirective) chart: BaseChartDirective; | ||
|
||
constructor() { } | ||
|
||
ngOnInit() { | ||
} | ||
|
||
randomNumber(min: number, max: number) { | ||
return Math.random() * (max - min) + min; | ||
} | ||
|
||
randomBar(date: luxon.DateTime, lastClose: number) { | ||
const open = this.randomNumber(lastClose * 0.95, lastClose * 1.05); | ||
const close = this.randomNumber(open * 0.95, open * 1.05); | ||
const high = this.randomNumber(Math.max(open, close), Math.max(open, close) * 1.1); | ||
const low = this.randomNumber(Math.min(open, close) * 0.9, Math.min(open, close)); | ||
return { | ||
t: date.valueOf(), | ||
o: open, | ||
h: high, | ||
l: low, | ||
c: close | ||
}; | ||
} | ||
|
||
getRandomData(dateStr: string, count: number) { | ||
let date = luxon.DateTime.fromRFC2822(dateStr); | ||
const data = [this.randomBar(date, 30)]; | ||
while (data.length < count) { | ||
date = date.plus({ days: 1 }); | ||
if (date.weekday <= 5) { | ||
data.push(this.randomBar(date, data[data.length - 1].c)); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
update() { | ||
// candlestick vs ohlc | ||
this.financialChartType = this.financialChartType === 'candlestick' ? 'ohlc' : 'candlestick'; | ||
} | ||
} |