<commit_before>// @flow
import type ChartData from '../chartdata/ChartData';
import type {ChartRow} from '../definitions';
import type {Scale} from '../definitions';

import * as d3Scale from 'd3-scale';
import * as array from 'd3-array';

import isContinuous from './isContinuous';
import isDate from './isDate';

export type ScaleConfig<Data> = {
    columns: Array<string>,
    data: Data,
    scaleType?: string,
    updateScale?: Scale => Scale,
    range: [number, number],
    stack?: boolean,
    zero?: boolean,
    stackedData: Array<Array<Array<number>>>
};


function min(data, columns, stackedData) {
    return stackedData
        ? array.min([].concat(...stackedData), d => d[0])
        : data.min(columns)
    ;
}

function max(data, columns, stackedData) {
    return stackedData
        ? array.max([].concat(...stackedData), d => d[1])
        : data.max(columns)
    ;
}

export default function createScale<Data: ChartData<ChartRow>>(config: ScaleConfig<Data>): Function {

    const {columns} = config;
    const {scaleType} = config;
    const {data} = config;
    const {range} = config;
    const {zero} = config;
    const {stackedData} = config;

    const continuousList = isContinuous(columns, data);

    // are any of the columns continuous.
    const continuous = continuousList.includes(true);
    const time = isDate(columns, data).includes(true);
    let domainArray;


    const scaleName = scaleType || (
        time
            ? 'scaleTime'
            : continuous
                ? 'scaleLinear'
                : 'scaleBand'
    );

    // if the size is greater than one we have multiple data types
    if(new Set(continuousList).size > 1) {
        throw new Error(`A scale cannot share continuous and non continuous data: ${columns.join(', ')}`);
    }

    if (continuous) {
        domainArray = [
            zero ? 0 : min(data, columns, stackedData),
            max(data, columns, stackedData)
        ];
    } else {
        // the domain of non-continuous data has to be an array of all unique values of columns
        domainArray = data.getUniqueValues(columns);
    }

    return d3Scale[scaleName]()
        .domain(domainArray)
        .range(range);
}
<commit_msg>amend: add errors for bad stacking config<commit_after>// @flow
import type ChartData from '../chartdata/ChartData';
import type {ChartRow} from '../definitions';
import type {Scale} from '../definitions';

import * as d3Scale from 'd3-scale';
import * as array from 'd3-array';

import isContinuous from './isContinuous';
import isDate from './isDate';

export type ScaleConfig<Data> = {
    columns: Array<string>,
    data: Data,
    scaleType?: string,
    updateScale?: Scale => Scale,
    range: [number, number],
    stack?: boolean,
    zero?: boolean,
    stackedData: Array<Array<Array<number>>>
};


function min(data, columns, stackedData) {
    return stackedData
        ? array.min([].concat(...stackedData), d => d[0])
        : data.min(columns)
    ;
}

function max(data, columns, stackedData) {
    return stackedData
        ? array.max([].concat(...stackedData), d => d[1])
        : data.max(columns)
    ;
}

export default function createScale<Data: ChartData<ChartRow>>(config: ScaleConfig<Data>): Function {

    const {columns} = config;
    const {scaleType} = config;
    const {data} = config;
    const {stack} = config;
    const {range} = config;
    const {zero} = config;
    const {stackedData} = config;

    if(stack && !stackedData) throw 'You cant stack a scale without stackedData';

    const continuousList = isContinuous(columns, data);

    // are any of the columns continuous.
    const continuous = continuousList.includes(true);
    const time = isDate(columns, data).includes(true);
    let domainArray;


    const scaleName = scaleType || (
        time
            ? 'scaleTime'
            : continuous
                ? 'scaleLinear'
                : 'scaleBand'
    );

    // if the size is greater than one we have multiple data types
    if(new Set(continuousList).size > 1) {
        throw new Error(`A scale cannot share continuous and non continuous data: ${columns.join(', ')}`);
    }

    if (continuous) {
        if(time && stack) throw 'Stacked columns must be numerical';
        domainArray = [
            zero ? 0 : min(data, columns, stackedData),
            max(data, columns, stackedData)
        ];
    } else {
        // the domain of non-continuous data has to be an array of all unique values of columns
        domainArray = data.getUniqueValues(columns);
    }

    return d3Scale[scaleName]()
        .domain(domainArray)
        .range(range);
}

{'fraction_non_alphanumeric': 0.1258050665521683, 'fraction_numerical': 0.0034349506225848005, 'mean_word_length': 2.6917591125198097, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, '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': '1646305', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1324, 'n_words': 407}