How to insert angular script? In simple HTML it works!

0

Well, my .html component looks like this:

<script>
    window.onload = function () {

    var chart = new CanvasJS.Chart("chartContainer", {
        animationEnabled: true,
        title:{
            text: "Monthly Expenses, 2016-17"
        },
        axisY :{
            includeZero: false,
            prefix: "$"
        },
        toolTip: {
            shared: true
        },
        legend: {
            fontSize: 13
        },
        data: [{
            type: "splineArea",
            showInLegend: true,
            name: "Salaries",
            yValueFormatString: "$#,##0",
            xValueFormatString: "MMM YYYY",
            dataPoints: [
                { x: new Date(2016, 2), y: 30000 },
                { x: new Date(2016, 3), y: 35000 },
                { x: new Date(2016, 4), y: 30000 },
                { x: new Date(2016, 5), y: 30400 },
                { x: new Date(2016, 6), y: 20900 },
                { x: new Date(2016, 7), y: 31000 },
                { x: new Date(2016, 8), y: 30200 },
                { x: new Date(2016, 9), y: 30000 },
                { x: new Date(2016, 10), y: 33000 },
                { x: new Date(2016, 11), y: 38000 },
                { x: new Date(2017, 0),  y: 38900 },
                { x: new Date(2017, 1),  y: 39000 }
            ]
         },
        {
            type: "splineArea", 
            showInLegend: true,
            name: "Office Cost",
            yValueFormatString: "$#,##0",
            dataPoints: [
                { x: new Date(2016, 2), y: 20100 },
                { x: new Date(2016, 3), y: 16000 },
                { x: new Date(2016, 4), y: 14000 },
                { x: new Date(2016, 5), y: 18000 },
                { x: new Date(2016, 6), y: 18000 },
                { x: new Date(2016, 7), y: 21000 },
                { x: new Date(2016, 8), y: 22000 },
                { x: new Date(2016, 9), y: 25000 },
                { x: new Date(2016, 10), y: 23000 },
                { x: new Date(2016, 11), y: 25000 },
                { x: new Date(2017, 0), y: 26000 },
                { x: new Date(2017, 1), y: 25000 }
            ]
         },
        {
            type: "splineArea", 
            showInLegend: true,
            name: "Entertainment",
            yValueFormatString: "$#,##0",     
            dataPoints: [
                { x: new Date(2016, 2), y: 10100 },
                { x: new Date(2016, 3), y: 6000 },
                { x: new Date(2016, 4), y: 3400 },
                { x: new Date(2016, 5), y: 4000 },
                { x: new Date(2016, 6), y: 9000 },
                { x: new Date(2016, 7), y: 3900 },
                { x: new Date(2016, 8), y: 4200 },
                { x: new Date(2016, 9), y: 5000 },
                { x: new Date(2016, 10), y: 14300 },
                { x: new Date(2016, 11), y: 12300 },
                { x: new Date(2017, 0), y: 8300 },
                { x: new Date(2017, 1), y: 6300 }
            ]
         },
        {
            type: "splineArea", 
            showInLegend: true,
            yValueFormatString: "$#,##0",      
            name: "Maintenance",
            dataPoints: [
                { x: new Date(2016, 2), y: 1700 },
                { x: new Date(2016, 3), y: 2600 },
                { x: new Date(2016, 4), y: 1000 },
                { x: new Date(2016, 5), y: 1400 },
                { x: new Date(2016, 6), y: 900 },
                { x: new Date(2016, 7), y: 1000 },
                { x: new Date(2016, 8), y: 1200 },
                { x: new Date(2016, 9), y: 5000 },
                { x: new Date(2016, 10), y: 1300 },
                { x: new Date(2016, 11), y: 2300 },
                { x: new Date(2017, 0), y: 2800 },
                { x: new Date(2017, 1), y: 1300 }
            ]
        }]
    });
    chart.render();

    }

<div class="container-fluid">
<div class="row mb-4">
    <div class="col-md-4 padding">
        <div class="mod shadow">
            <h6>Moderações - 5213</h6>     
                <!-- Tentativa Charts -->                        
                <div id="chartContainer" style="height: 250px; width: 200px;"></div>
                <script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script><!--FimCharts--></div></div><divclass="col-md-4 padding">
            <div class="modUser shadow">
                <h6>Moderações por Usuário - 2603</h6> 
            </div>
        </div>
        <div class="col-md-4 padding">
            <div class="modCat shadow">
                <h6>Moderações por Categoria - 2603</h6> 
            </div>
        </div>
</div>

<div class="row mb-4">
    <div class="col-md-12 padding">
        <div class="solMod shadow">
            <h6>Solicitação de Moderação - 3875</h6> 
        </div>
    </div>
</div>

<div class="row">   
    <div class="col-md-4 padding">
        <div class="solCad shadow">
            <h6>Solicitações de Cadastro - 1254</h6> 
        </div>
    </div>
    <div class="col-md-8 padding">
        <div class="solCadExpand shadow">
            <h6>Solicitações de Cadastro - 1254</h6>
            <!-- Charts -->

        </div>
    </div>
</div>

But the time does not display the graph. I tested it in plain html and it worked. But in angular no ..

    
asked by anonymous 12.09.2018 / 20:35

1 answer

2

Since you are using an external library, you should add it to angular-cli.json

  "scripts": [
    //caminho do seu JS 
    "../assets/scripts/canvasjs.min"
  ],

Or you can use npm install canvasjs --save importing as in TS below and importing the script into index.html <script src="canvasjs/dist/canvasjs.js">

And for good practice avoid using <script></script> in the angle. The canvasJS itself already supports the angular. Example in canvasJS documentation

/*app.component.ts*/
import { Component, OnInit } from '@angular/core';
import * as CanvasJS from './canvasjs.min';
//var CanvasJS = require('./canvasjs.min');

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})

export class AppComponent implements OnInit {
    ngOnInit() {
        let chart = new CanvasJS.Chart("chartContainer", {
        animationEnabled: true,
        exportEnabled: true,
        title: {
            text: "Basic Column Chart in Angular"
        },
        data: [{
            type: "column",
            dataPoints: [
                { y: 71, label: "Apple" },
                { y: 55, label: "Mango" },
                { y: 50, label: "Orange" },
                { y: 65, label: "Banana" },
                { y: 95, label: "Pineapple" },
                { y: 68, label: "Pears" },
                { y: 28, label: "Grapes" },
                { y: 34, label: "Lychee" },
                { y: 14, label: "Jackfruit" }
            ]
        }]
    });

    chart.render();
    }
}

.html

<div id="chartContainer" style="height: 370px; width: 100%;"></div>
    
14.09.2018 / 18:14