<commit_before>import { SvgContext } from "../svg/base/SvgContext";
import { copy, isValuesInObjectKeys, hasValuesWithKeys, filterKeys, melt } from "../utils/functions";
import { throwError } from "../utils/error";
import { Subscription } from 'rxjs';
import { calculateWidth } from "../utils/screen";

import StreamingStrategy from './enums/StreamingStrategy';
import Injector from '../MyInjector';
import Datasource from "../datasources/Datasource";
import WebsocketDatasource from "../datasources/WebsocketDatasource";
import Config from "../Config";
import SvgStrategy from "../svg/base/SvgStrategy";
import Globals from '../Globals';

abstract class Chart {

    private context: SvgContext;
    protected config: Config;
    protected data: any;
    private events: Map<string, any>;
    private subscription: Subscription;
    private _injector: Injector = new Injector();
    private strategy: SvgStrategy;

    protected streamingStrategy: StreamingStrategy;

    constructor(Class: { new (...args: any[]): SvgStrategy }, data: any, userConfig: any, defaults: any) {

        this.config = this.loadConfigFromUser(userConfig, defaults);
        this._injector = new Injector();

        this._instantiateInjections(Class);

        this.data = data;
        this.events = new Map();
        this.config.put('pivotVars', []);
    }

    private _instantiateInjections(Class: { new (...args: any[]): SvgStrategy }) {
        this._injector.mapValue('Config', this.config);

        //Instantiate SvgStrategy
        this.strategy = this._injector.instantiate(Class);
        this.strategy.initialize();

        this._injector.mapValue('Strategy', this.strategy);
        //Instantiate SvgContext
        this.context = this._injector.instantiate(SvgContext);
    }

    public annotations(annotations: any): Chart {
        this.config.put('annotations', annotations);
        return this;
    }

    public draw(data: [{}] = this.data, events: Map<string, any> = this.events) { //TODO: SPLIT DATA INTO SMALL CHUNKS (stream-like). 
        this.context.draw(copy(data), this.events);
        this.data = data;
    }

    /**
     *
     * Configure a datasources for the current instance. Datasources allows data to be catached from many sources, such as websockets endpoints, HTTP urls, etc.
     * @param {Datasource} ds A datasource
     *
     * @memberOf Chart
     */
    public datasource(ds: WebsocketDatasource) {
        let annotations = this.config.get('annotations'),
            eventKeys: Array<string> = [];

        if (annotations) {
            annotations.forEach((a: any) => eventKeys.push(a.event))
        }

        let subscription: Subscription = ds.subscription().subscribe(
            (data: any) => {
                // Event detection
                for (let e of eventKeys) {
                    if (e in data) {
                        this.events.set(e, data[e]);
                        return;
                    }
                }
                // Wide data to narrow and draw
                let pivotVars = this.config.get('pivotVars');
                let keys = Object.keys(data);
                let varsInDatum = keys.filter((k) => pivotVars.indexOf(k) != -1);
                let ids = keys.filter((k) => pivotVars.indexOf(k) == -1);
                
                if(varsInDatum.length < 2) {
                    this.keepDrawing(data);<commit_msg>Allow variables to be used as events<commit_after>import { SvgContext } from "../svg/base/SvgContext";
import { copy, isValuesInObjectKeys, hasValuesWithKeys, filterKeys, melt } from "../utils/functions";
import { throwError } from "../utils/error";
import { Subscription } from 'rxjs';
import { calculateWidth } from "../utils/screen";

import StreamingStrategy from './enums/StreamingStrategy';
import Injector from '../MyInjector';
import Datasource from "../datasources/Datasource";
import WebsocketDatasource from "../datasources/WebsocketDatasource";
import Config from "../Config";
import SvgStrategy from "../svg/base/SvgStrategy";
import Globals from '../Globals';

abstract class Chart {

    private context: SvgContext;
    protected config: Config;
    protected data: any;
    private events: Map<string, any>;
    private subscription: Subscription;
    private _injector: Injector = new Injector();
    private strategy: SvgStrategy;

    protected streamingStrategy: StreamingStrategy;

    constructor(Class: { new (...args: any[]): SvgStrategy }, data: any, userConfig: any, defaults: any) {

        this.config = this.loadConfigFromUser(userConfig, defaults);
        this._injector = new Injector();

        this._instantiateInjections(Class);

        this.data = data;
        this.events = new Map();
        this.config.put('pivotVars', []);
    }

    private _instantiateInjections(Class: { new (...args: any[]): SvgStrategy }) {
        this._injector.mapValue('Config', this.config);

        //Instantiate SvgStrategy
        this.strategy = this._injector.instantiate(Class);
        this.strategy.initialize();

        this._injector.mapValue('Strategy', this.strategy);
        //Instantiate SvgContext
        this.context = this._injector.instantiate(SvgContext);
    }

    public annotations(annotations: any): Chart {
        this.config.put('annotations', annotations);
        return this;
    }

    public draw(data: [{}] = this.data, events: Map<string, any> = this.events) { //TODO: SPLIT DATA INTO SMALL CHUNKS (stream-like). 
        this.context.draw(copy(data), this.events);
        this.data = data;
    }

    /**
     *
     * Configure a datasources for the current instance. Datasources allows data to be catached from many sources, such as websockets endpoints, HTTP urls, etc.
     * @param {Datasource} ds A datasource
     *
     * @memberOf Chart
     */
    public datasource(ds: WebsocketDatasource) {
        let annotations = this.config.get('annotations'),
            eventKeys: Array<string> = [];

        if (annotations) {
            annotations.forEach((a: any) => eventKeys.push(a.event))
        }

        let subscription: Subscription = ds.subscription().subscribe(
            (data: any) => {
                // Event detection
                for (let e of eventKeys) {
                    if (e in data) {
                        this.events.set(e, data[e]);
                    }
                }
                // Wide data to narrow and draw
                let pivotVars = this.config.get('pivotVars');
                let keys = Object.keys(data);
                let varsInDatum = keys.filter((k) => pivotVars.indexOf(k) != -1);
                let ids = keys.filter((k) => pivotVars.indexOf(k) == -1);
                
                if(varsInDatum.length < 2) {
                    this.keepDrawing(data);
{'fraction_non_alphanumeric': 0.12413691787865433, 'fraction_numerical': 0.0008814455707360071, 'mean_word_length': 2.393818544366899, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087646', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1856, 'n_words': 482}