angular - 如何在 Angular 2+ 包装器中向 plotly.js 添加数据点

标签 angular plotly

有人能说一下如何在 Angular 2+ 包装器 ( https://github.com/plotly/angular-plotly.js ) 中为 plotly.js 使用 Plotly.js 的扩展跟踪功能吗?我无法从 url 中给出的文档中弄清楚这一点。

相关问题:Plotly update data

**** 解决方案 ****
下面我更改了我的原始邮政编码以显示有效的解决方案。方法 addPoint 和 addPoint2(向现有系列添加新的单个数据点) 方法 addRandomLine() 添加新轨迹(即全新的数据系列):

<button (click)="addRandomLine()" class="button">Add random line</button>
<button (click)="addPoint()" class="button">Add point to 1. series</button>
<button (click)="addPoint2()" class="button">Add point with extendTrace to 1. series</button>

<plotly-plot 
    #chart
    id="plotlyChart"
    [data]="data" 
    [layout]="layout">
</plotly-plot>


import { Component, ViewChild } from '@angular/core';
import { PlotlyService } from '../../shared/plotly.service';

export class LinearChartsComponent {

@ViewChild('chart') el: any;

    public data: any[] = [
        { x: [1, 2, 3, 4], y: [10, 15, 13, 17], type: 'bar',  mode: 'markers', name: 'Bar' },
        { x: [2, 3, 4, 1], y: [16, 5, 11, 9], type: 'scattergl', mode: 'lines', name: 'Lines' },
        { x: [1, 2, 3, 4], y: [12, 9, 15, 12], type: 'markers', mode: 'lines+markers', name: 'Scatter + Lines' },
    ];

constructor(public plotly: PlotlyService) {
}


    public layout: any = {
        title: 'Adding Names to Line and Scatter Plot',
    };

    public addRandomLine() {
        const line: any = { x: [], y: [], mode: 'lines', name: 'Line ' + this.data.length };

        line.x = [1, 2, 3, 4].map(i => Math.round(Math.random() * 10));
        line.y = [1, 2, 3, 4].map(i => Math.round(Math.random() * 20));
        line.mode = ['markers', 'lines', 'lines+markers'][Math.floor(Math.random() * 3)] as any;

        this.data.push(line);
    }


// adds point with relayout-method
addPoint() {

    const line: any = this.data[1];
    line.x.push(line.x.length + 1);
    line.y.push(Math.round(Math.random() * 20));
    this.data[1] = line;

    const update = {
        title: 'New Title',
        data: this.data
    }
    this.plotly.getPlotly().relayout(this.el.plotEl.nativeElement, update);

}

// adds point with extendTraces-method
addPoint2() {
    this.plotly.getPlotly().extendTraces(
        this.el.plotEl.nativeElement,
        { y: [[Math.random()*10]] }, [1]
    );
    this.plotly.getPlotly().extendTraces(
        this.el.plotEl.nativeElement,
        { x: [[this.data[1].x.length + 1]] }, [1]
    );
}

}

最佳答案

OP 通过编辑原始问题提供了解决方案。
我不会撤销这些编辑并在此处发布完整的解决方案,而是添加 OP 的 plotly-service该解决方案所依赖的。

import { Injectable } from '@angular/core';
import { Plotly } from './plotly.interface';


@Injectable({
    providedIn: 'root'
})
export class PlotlyService {
    protected static instances: Plotly.PlotlyHTMLElement[] = [];
    protected static _plotly?: any = undefined;

    public static setPlotly(plotly: any) {
        PlotlyService._plotly = plotly;
    }

    public static insert(instance: Plotly.PlotlyHTMLElement) {
        const index = PlotlyService.instances.indexOf(instance);
        if (index === -1) {
            PlotlyService.instances.push(instance);
        }
        return instance;
    }

    public static remove(div: Plotly.PlotlyHTMLElement) {
        const index = PlotlyService.instances.indexOf(div);
        if (index >= 0) {
            PlotlyService.instances.splice(index, 1);
        }
    }

    public getInstanceByDivId(id: string): Plotly.PlotlyHTMLElement | undefined {
        for (const instance of PlotlyService.instances) {
            if (instance && instance.id === id) {
                return instance;
            }
        }
        return undefined;
    }

    public getPlotly() {
        if (typeof PlotlyService._plotly === 'undefined') {
            throw new Error(`Peer dependency plotly.js isn't installed`);
        }

        return PlotlyService._plotly;
    }

    protected waitFor(fn: () => boolean): Promise<void> {
        return new Promise((resolve) => {
            const localFn = () => {
                fn() ? resolve() : setTimeout(localFn, 10);
            };

            localFn();
        });
    }

    public async newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
        await this.waitFor(() => this.getPlotly() !== 'waiting');
        return this.getPlotly().newPlot(div, data, layout, config).then(() => PlotlyService.insert(div as any)) as Promise<any>;
    }

    public plot(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
        return this.getPlotly().plot(div, data, layout, config) as Promise<any>;
    }

    public update(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
        return this.getPlotly().react(div, data, layout, config) as Promise<any>;
    }

    public resize(div: Plotly.PlotlyHTMLElement): void {
        return this.getPlotly().Plots.resize(div);
    }
}

关于angular - 如何在 Angular 2+ 包装器中向 plotly.js 添加数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54573717/

相关文章:

尽管在 dom 中,Angular Material2 sidenav 并未显示

angular - Angular 谷歌地图上缺少卫星 View 切换

angular - 为什么使用 Angular2 构建的应用程序如此沉重?

Plotly - 反转图例单击选择?

javascript - Plotlyjs 方形曲线

python - 如何在Python Dash中的两个dcc组件之间留出空间?

javascript - Plotly 禁用 "band behavior"缩放选择

javascript - 如何使用@ngrx/store 有效地重置状态?

angular - 如何找到添加到 leafletLayer 的多个标记的边界

python - 带有基于另一列的标记的 Pandas 线图