results-graph.component.ts 8.40 KiB
import { Component, ViewChild, AfterContentInit } from "@angular/core";
import { Observer } from "jalhyd";
import { VarResults, GraphType } from "../../results/var-results";
import { GraphTypeSelectComponent } from "./graph-type.component";
import { ApplicationSetupService } from "../../services/app-setup/app-setup.service";
import { I18nService } from "../../services/internationalisation/internationalisation.service";
@Component({
    selector: "results-graph",
    templateUrl: "./results-graph.component.html",
    styleUrls: [
        "./results-graph.component.scss"
export class ResultsGraphComponent implements AfterContentInit, Observer {
    private _results: VarResults;
    @ViewChild(GraphTypeSelectComponent)
    private _graphTypeComponent: GraphTypeSelectComponent;
     * config du graphe
    public graph_type: string;
    public graph_data = {};
    public graph_options = {
        responsive: true,
        maintainAspectRatio: true,
        animation: {
            duration: 0
        legend: {
            display: false
        title: {
            display: true,
            text: ""
    /** tracks the fullscreen state */
    public get isFullscreen() {
        return (document["fullscreenElement"] !== null);
    public constructor(
        private appSetup: ApplicationSetupService,
        private intlService: I18nService
    ) {
        // limit display precision according to app preferences
        const nDigits = this.appSetup.displayDigits;
    public set results(r: VarResults) {
        this._results = r;
        if (this._results && this._graphTypeComponent) {
            this._graphTypeComponent.selectedValue = r.graphType;
    public get availableChartAxis() {
        if (this._results) {
            return this._results.getAvailableChartAxis();
    public get chartX() {
        if (this._results) {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return this._results.chartX; } } public set chartX(X) { if (X !== this.chartX) { this._results.chartX = X; // refresh chart this.updateView(); } } public get chartY() { if (this._results) { return this._results.chartY; } } public set chartY(Y) { if (Y !== this.chartY) { this._results.chartY = Y; // refresh chart this.updateView(); } } /** * Returns a human readable description of any param / result symbol */ public getChartAxisLabel(symbol: string) { // 1. calculated param ? if (this._results.calculatedParameter.symbol === symbol) { return this._results.calculatedParameterHeader; } else // 2. variated param ? if (this._results.variatedParameter.symbol === symbol) { return this._results.variableParamHeader; } else { // 3. Result element ? return this.intlService.getExtraResLabel(symbol); } } public get uitextSelectX() { return this.intlService.localizeText("INFO_PARAMFIELD_GRAPH_SELECT_X_AXIS"); } public get uitextSelectY() { return this.intlService.localizeText("INFO_PARAMFIELD_GRAPH_SELECT_Y_AXIS"); } public updateView() { // (re)generate chart switch (this._graphTypeComponent.selectedValue) { case GraphType.Histogram: this.graph_type = "bar"; this.generateBarGraph(); break; case GraphType.HistoLine: this.graph_type = "line"; this.generateLineGraph(); break; default: this.graph_type = "scatter"; this.generateScatterGraph(); break; } }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
public ngAfterContentInit() { this._graphTypeComponent.addObserver(this); } /** * génère les données d'un graphe de type "line" */ private generateLineGraph() { const labs = []; const dat = []; const xSeries = this._results.getValuesSeries(this.chartX); const ySeries = this._results.getValuesSeries(this.chartY); // both series are supposed to be the same length for (let i = 0; i < xSeries.length; i++) { labs.push(xSeries[i]); dat.push(ySeries[i]); } // @see bug #137 this.graph_options.title.text = this.chartY + " = f ( " + this.chartX + " )"; this.graph_data = { labels: labs, datasets: [{ label: "", data: dat }] }; } /** * génère les données d'un graphe de type "bar" */ private generateBarGraph() { const labs = []; const dat = []; const xSeries = this._results.getValuesSeries(this.chartX); const ySeries = this._results.getValuesSeries(this.chartY); // both series are supposed to be the same length for (let i = 0; i < xSeries.length; i++) { labs.push(xSeries[i]); dat.push(ySeries[i]); } // @see bug #137 this.graph_options.title.text = this.chartY + " = f ( " + this.chartX + " )"; const nDigits = this.appSetup.displayDigits; this.graph_options["scales"] = { xAxes: [{ gridLines: { offsetGridLines: true }, ticks: { precision: nDigits, callback: function(value, index, values) { return Number(value).toFixed(nDigits); } } }] }; this.graph_options["tooltips"] = { callbacks: { label: function(tooltipItem, data) { return Number(tooltipItem.yLabel).toFixed(nDigits); } } };