<commit_before>import * as d3selection from 'd3-selection'
import * as d3scale from 'd3-scale'

import {TimeLine, class_name} from './TimeLine'


export class TimeLineLegend {
    /**
     * legend for time line chart
     * @param context
     * @param config
     *      data: {
     *          label: {
     *              text: ''
     *          },
     *          info:{
     *              text: ''
     *          },
     *          class_styles: [
     *              {class_name:'unknown', color: '#bdbdbd'},
     *          ]
     *      },
     *      options: {
     *          size: {
     *              height: 50,
     *              width: 1800
     *          },
     *          start_point: {
     *              x: 250
     *          }
     *      }
     */
    constructor(context, config){
        this.context = context;
        this.config = {};

        this.color_scale = null;
        this.color_domain = [];
        this.color_range = [];

        // merge config
        Object.assign(this.config, TimeLineLegend.default);


        if(config.hasOwnProperty('options')){
            let options = this.config.options;
            let user_options = config.options;

            if(user_options.hasOwnProperty("size")) {
                Object.keys(user_options.size).forEach(function (key) {
                    options.size[key] = user_options.size[key];
                });
            }

            if(user_options.hasOwnProperty("start_point")) {
                Object.keys(user_options.start_point).forEach(function (key) {
                    options.start_point[key] = user_options.start_point[key];
                });
            }
        }

        if(config.hasOwnProperty('data')){
            let data = this.config.data;
            let user_data = config.data;

            if(user_data.hasOwnProperty("class_styles")) {
                data.class_styles = user_data.class_styles;

                let color_domain = this.color_domain;
                let color_range = this.color_range;
                data.class_styles.forEach(function(a_class){
                    color_domain.push(a_class.class_name);
                    color_range.push(a_class.color);
                });
                this.color_scale = d3scale.scaleOrdinal()
                    .domain(color_domain)
                    .range(color_range);
            }

            if(user_data.hasOwnProperty("label")) {
                Object.keys(user_data.label).forEach(function (key) {
                    data.label[key] = user_data.label[key];
                });
            }

            if(user_data.hasOwnProperty("info")) {
                Object.keys(user_data.info).forEach(function (key) {
                    data.info[key] = user_data.info[key];
                });
            }
        }

        this.drawLegend();
    }

    drawLegend() {
        let context = this.context;
        let data = this.config.data;
        let options = this.config.options;

        let color_domain = this.color_domain;
        let color_scale = this.color_scale;

        let legend_svg = d3selection.select(context)
            .append("svg")
            .classed(class_name, true)
            .attr('width', options.size.width)<commit_msg>use mergeConfig in TimeLineLegend.<commit_after>import * as d3selection from 'd3-selection'
import * as d3scale from 'd3-scale'

import {TimeLine, class_name} from './TimeLine'
import {mergeConfig} from './config'


export class TimeLineLegend {
    /**
     * legend for time line chart
     * @param context
     * @param config
     *      data: {
     *          label: {
     *              text: ''
     *          },
     *          info:{
     *              text: ''
     *          },
     *          class_styles: [
     *              {class_name:'unknown', color: '#bdbdbd'},
     *          ]
     *      },
     *      options: {
     *          size: {
     *              height: 50,
     *              width: 1800
     *          },
     *          start_point: {
     *              x: 250
     *          }
     *      }
     */
    constructor(context, config){
        this.context = context;

        this.color_domain = [];
        this.color_range = [];

        this.config = mergeConfig(TimeLineLegend.default, config);

        let color_domain = this.color_domain;
        let color_range = this.color_range;
        this.config.data.class_styles.forEach(function(a_class){
            color_domain.push(a_class.class_name);
            color_range.push(a_class.color);
        });
        this.color_scale = d3scale.scaleOrdinal()
            .domain(color_domain)
            .range(color_range);

        this.drawLegend();
    }

    drawLegend() {
        let context = this.context;
        let data = this.config.data;
        let options = this.config.options;

        let color_domain = this.color_domain;
        let color_scale = this.color_scale;

        let legend_svg = d3selection.select(context)
            .append("svg")
            .classed(class_name, true)
            .attr('width', options.size.width)
{'fraction_non_alphanumeric': 0.1140625, 'fraction_numerical': 0.005859375, 'mean_word_length': 1.4110169491525424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5653465', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1411, 'n_words': 231}