This article is going to describe how to add a Zingchart control into a page using Angular 6. Let's see that in simple steps.
We have used Visual Studio Code to develop the solution.
↑ Return to Top
You can find the following screen shows how to render a ZingChart gauge using an Angular component.
First of all, Let's install zingchart packages from npm
npm i zingchart
<!doctype html>
<
html
lang
=
"en"
>
head
meta
charset
"utf-8"
title
>BMI Calculator</
base
href
"/"
name
"viewport"
content
"width=device-width, initial-scale=1"
link
rel
"icon"
type
"image/x-icon"
"favicon.ico"
</
body
app-root
></
script
src
"https://cdn.zingchart.com/zingchart.min.js"
div
class
"container"
form
"form-row"
"form-group col-md-6"
label
for
"height"
>Height (cm)</
input
"number"
"form-control"
id
placeholder
"Height"
[(ngModel)]="height" (change)="onValueChange()"/>
"weight"
>Weight (kg)</
"Weight"
[(ngModel)]="weight" (change)="onValueChange()"/>
"form-group"
app-chart
import { Component, OnInit } from
'@angular/core'
;
import { ChartComponent } from
'./shared/app.chart'
@Component({
selector:
'app-root'
,
templateUrl:
'./app.component.html'
styleUrls: [
'./app.component.css'
],
providers:[ChartComponent]
})
export class AppComponent implements OnInit{
weight : number;
height : number;
bmi : string;
constructor(private chartComponent : ChartComponent){}
ngOnInit() {
this
.weight = 0;
.height = 0;
}
onValueChange() {
var
bmi = (
.weight / ((
.height / 100) * (
.height / 100)));
if
(isNaN(bmi) || bmi < 10)
bmi = 10;
else
(bmi > 40)
bmi = 40;
.bmi = bmi.toFixed(2);
.chartComponent.ChangeChartValue(
.bmi);
import { BrowserModule } from
'@angular/platform-browser'
import { NgModule } from
import {ChartComponent} from
import { FormsModule } from
'@angular/forms'
import { AppComponent } from
'./app.component'
@NgModule({
declarations: [
AppComponent,
ChartComponent
imports: [
BrowserModule,
FormsModule
providers: [],
bootstrap: [AppComponent]
export class AppModule { }
"{{chart.id}}"
import { Component, AfterViewInit } from
declare
zingchart: any;
'app-chart'
'./app.chart.html'
'./app.chart.scss'
]
export class ChartComponent implements AfterViewInit {
chart: Chart = {
id:
"chart-1"
data: {
"type"
:
"gauge"
"scale-r"
: {
"aperture"
: 200,
"values"
"10:40:1"
"center"
"size"
: 10,
"background-color"
"#000"
"border-color"
"none"
},
"guide"
"visible"
true
"item"
"tick"
"ring"
: 20,
"rules"
: [
{
"rule"
"%v >= 10 && %v <= 20"
"#f6f34a"
"%v >= 20 && %v <= 28"
"#7cfd45"
"%v >= 28 && %v <= 32"
"#f79333"
"%v >= 32 && %v <= 40"
"#f30c22"
"plot"
"csize"
"8%"
"90%"
"#000000"
"series"
: [ {
: [ 25 ] } ]
height: 170,
width: 170
};
ngAfterViewInit() {
zingchart.render(
.chart);
ChangeChartValue (value){
zingchart.exec(
'chart-1'
'setseriesvalues'
, {
plotindex : 0,
values : [Number(value)]
});
interface Chart {
id:string;
data : {};
width : number;