Reputation: 295
i want to visualize some live data in the ngx-chart-gauge.
I build this up with this demo:
https://swimlane.gitbooks.io/ngx-charts/content/v/6.0.2/charts/gauge.html
I get my livedata from a Service and want to show the data in the ngx-chart-gauge. I get the data from the Service ClassdataproviderService, which saves the objects in the array 'single'.
This is my ngx-chart-gauge component:
import {Component, OnInit, OnChanges, AfterViewInit, AfterViewChecked, AfterContentChecked, AfterContentInit, NgModule} from '@angular/core';
import {NgxChartsModule} from '@swimlane/ngx-charts';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {BrowserModule} from '@angular/platform-browser';
import {TempdatagiaService} from '../paho/tempdatagia.service';
import {ClassdataproviderService} from '../mqtt/classdataprovider.service';
import * as d3 from 'd3';
@Component({
selector: 'app-mqtt',
templateUrl: './mqtt.component.html',
styleUrls: ['./mqtt.component.css']
})
export class MqttComponent implements OnChanges {
view: any[] = [700, 400];
data: any[];
multi: any[];
autoScale = true;
constructor(private classdataproviderServie: ClassdataproviderService) {
}
ngOnChanges() {
this.data = this.classdataproviderServie.single;
}
colorScheme = {
domain: ['#5AA454', '#A10A28', '#C7B42C']
};
onSelect(event) {
console.log(event);
}
}
This ist the ngx-chart-gauge.html
<ngx-charts-gauge
[view]="view"
[scheme]="colorScheme"
[results]="data"
[min]="0"
[max]="100"
[angleSpan]="240"
[startAngle]="-120"
[units]="'Temperature'"
[bigSegments]="10"
[smallSegments]="5"
(select)="onSelect($event)">
</ngx-charts-gauge>
And this is my Service, where i get the data from:
import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {Paho} from '../../../node_modules/ng2-mqtt/mqttws31';
@Injectable()
export class ClassdataproviderService {
public name: string;
public value: string;
single = [
{
name: '1',
value: '15'
},
{
name: '2',
value: '20'
}/*,
{
name: '3',
value: '73'
}*/
];
// Create a client instance
client: any;
packet: any;
constructor() {
// this.client = new Paho.MQTT.Client('broker.mqttdashboard.com', 8000, 'clientId-FUp9rr6sZS');
this.client = new Paho.MQTT.Client('wpsdemo.gia.rwth-aachen.de', 8080, 'Steffen');
this.onMessage();
this.onConnectionLost();
// connect the client
this.client.connect({onSuccess: this.onConnected.bind(this)});
}
// called when the client connects
onConnected() {
console.log('Connected');
this.client.subscribe('node/m1/temperature');
//this.sendMessage('HelloWorld');
}
sendMessage(message: string) {
const packet = new Paho.MQTT.Message(message);
packet.destinationName = 'node/m1/temperature';
this.client.send(packet);
}
// called when a message arrives
onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived : ' + message.payloadString);
this.single.push({name: 'test', value: message.payloadString});
console.log(this.single);
};
}
// called when the client loses its connection
onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
When i try to inject the data in the gauge with OnInit, the gauge is not updating. To solve this i tried OnChanges, but now i get the ERROR TypeError: Cannot read property 'big' of undefined...
I dont know if its possible to auto update the gauge automatically without OnChanges or how to avoid the ERR with the OnChanges lifecycle hook?
Upvotes: 2
Views: 5516
Reputation: 139
you trying to modify data is not recognized by change detection.
this.data = this.classdataproviderServie.single;
instead try spreading data over, that will detect the changes. It worked for me.
this.data = [...this.classdataproviderServie.single];
Upvotes: 1
Reputation: 295
Now i tried out many solutions and the most effective is by using an observable, to observe my Service where i get the data from. The Service, which injects the data in the ngx-chart-component, withe the observable, looks like:
import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {Paho} from '../../../node_modules/ng2-mqtt/mqttws31';
import 'rxjs/add/observable/interval';
import {AddDataService} from './datawindow/addmqtt/formmqtt/addmqtt.service';
@Injectable()
export class ClassdataproviderService {
public name: string;
public value: string;
single = [
{
name: '',
value: ''
}
];
// Create a client instance
client: any;
packet: any;
constructor() {
this.addDataService.componentMethodCalled$.subscribe(
() => {
this.client = new Paho.MQTT.Client('wpsdemo.gia.rwth-aachen.de', 8080, 'Steffen');
this.onMessage();
this.onConnectionLost();
// connect the client
this.client.connect({onSuccess: this.onConnected.bind(this)});
}}
// called when the client connects
onConnected() {
console.log('Connected');
this.client.subscribe('node/m1/temperature');
//this.sendMessage('HelloWorld');
}
sendMessage(message: string) {
const packet = new Paho.MQTT.Message(message);
packet.destinationName = 'node/m1/temperature';
this.client.send(packet);
}
// called when a message arrives
onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived : ' + message.payloadString);
this.single.push({name: 'test', value: message.payloadString});
console.log(this.single);
};
}
// called when the client loses its connection
onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
Now the single array will be updatetd every time i get new data and it works perfectly with ngx-charts. And use ngOnInit as lifecyclehook in the gauge-component.
Upvotes: 2