Third party JS in Angular2 typescript

Dynde

I'm new to angular and typescript, so this is probably really basic.

I'm trying to make an angular2 component with a chart (using Chart.js) in the template.

I realize there is a chart directive being developed, that specifically uses Chart.JS, but I would like to understand how to do this, as it will undoubtedly come up in an instance where a directive isn't available.

So far I've tried to do this simple thing in the template:

<canvas id="chart"></canvas>
<script> 
$(function () {
//instantiate chart on $("#chart")
});
</script>

But this javascript doesn't even run when the template has been loaded by angular2.

How would I go about this?

Dynde

Okay - with the help of @Pogrindis I think I found a usable, not too complex solution.

By simply adding the typing definition for chart.js from here and referencing it in a custom directive I finally have this:

chart.directive.ts

/// <reference path="../../typings/chartjs/chart.d.ts" />

import {Directive, ElementRef, Renderer, Input} from 'angular2/core';
@Directive({
    selector: '[chart]'
})
export class ChartDirective {
    constructor(el: ElementRef, renderer: Renderer) {
        //el.nativeElement.style.backgroundColor = 'yellow';
        var data = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                {
                    label: "My First dataset",
                    fillColor: "rgba(220,220,220,0.2)",
                    strokeColor: "rgba(220,220,220,1)",
                    pointColor: "rgba(220,220,220,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: [65, 59, 80, 81, 56, 55, 40]
                },
                {
                    label: "My Second dataset",
                    fillColor: "rgba(151,187,205,0.2)",
                    strokeColor: "rgba(151,187,205,1)",
                    pointColor: "rgba(151,187,205,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(151,187,205,1)",
                    data: [28, 48, 40, 19, 86, 27, 90]
                }
            ]
        };
        var ctx: any = el.nativeElement.getContext("2d");
        var lineChart = new Chart(ctx);
        ////var lineChartOptions = areaChartOptions;
        ////lineChartOptions.datasetFill = false;
        lineChart.Line(data);
    }
}

app.component.ts

import {Component} from 'angular2/core';
import {ChartDirective} from './chart.directive';

@Component({
    directives: [ChartDirective],
    selector: 'chart-graph', 
    templateUrl: '/js/app/template.html'
})

export class AppComponent { }

and template.html:

<canvas id="myChart" chart width="400" height="400"></canvas>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Import third party js files to angular typescript project

typescript declare third party modules

Augment third party library Typescript

Modifying Typescript third party interface

Using a third-party cordova plugin in Ionic 2 with TypeScript

Angular2 jasmine unit test component with third party directive

Angular2 Observable - how to wrap a third party ajax call

How to inject a third party library to an angular.js service

Mocking third party library (ioredis) in typescript tests

Generating Typescript Typings for third party JQuery Plugins?

using third party library in angular

Angular 2 and masonry grid third party library

Wrapping third party JS libraries in ReactJS

Handling third party dependencies in nest.js

Disabling compression for third-party JS libraries

Using third party js libraries with Jint

node.js - PM2 log uncaught exceptions to third-party service (as Logentries)

Third party Library is not working when navigating in Angular

Use third party library in a angular factory

Loading third party library with Angular-cli

How to make my TypeScript object available to a third party Javascript

In TypeScript, how do I modify a third party interface?

creating typescript .d.ts for third-party library

Typescript - Importing third-party modules - Name autocomplete VSCode

How to merge 2 third party components in VueJS

Extending third party zend framework 2 modules

Use third party library (parse.com) in Angular 2

add third-party js library to Create React App

SyntaxError: Unexpected reserved word => prettier/third-party.js

TOP Ranking

HotTag

Archive