12

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?

10
  • What are you trying to do ? And what has this got to do with typescript ? Your question and code shows nothing about typescript.. Commented Jan 7, 2016 at 10:41
  • 1
    You need to write interfaces in typescript for the chart.js library, but there is probably some already made, how far have you gotten though ? Commented Jan 7, 2016 at 10:44
  • 2
    No @Dynde, the .ts file will need to be strongly typed, and js libs are just not, so you use an interface : This might be useful for you: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/chartjs/… on another note, there are 'hacks' like saying chartLib: any so the any will circumvent the strong typing but that defeats the purpose really. Commented Jan 7, 2016 at 10:49
  • 1
    Okay - thanks. I'll try and read some more about typescript interfaces. I guess I just don't really understand how that interface knows the source of chart.js - I only just started typescript Commented Jan 7, 2016 at 10:53
  • 2
    @Dynde an interface doesn't know! That's why you define the interface, and ensure you got it right! :) That library I sent you have a lot of 3rd party definitions for TS ! Commented Jan 7, 2016 at 10:54

1 Answer 1

11

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for you question, this was exactly what I was looking for. I have a general idea why you need DefinitelyTyped, however I'm slightly confused how to implement it. 1. git clone DefinitelyTyped, 2. then referenced it <script src="DefinitelyTyped/chartjs/chart.d.ts"></script> in the index.html? could elaborate just a little more on your answer. Everything else I understand, even for the Angular2!
Okay this was the trick /// <reference path="../DefinitelyTyped/chartjs/chart.d.ts" /> I didn't know that was a command or line in Angular2, I thought it was a comment out line for some reason. Regardless, make sure that is working this answer will get you up an running on Chart.js and Angular2! Thanks @dynde
@Dynde Which version of charts.js did you use with the typings you have mentioned in your answer?
@Anmol Oh, darn, it's been so long, I honestly don't know, and the project has been abandoned, so I can't dig it up - sorry.
@Dynde No worries. I figured it out. The typings referred here are valid for 1x versions. These are not valid for 2x versions. Typings for 2x version are still awaited. The issue is open on Github. github.com/chartjs/Chart.js/issues/1572

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.