<commit_before>/*
 *  Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { createElement as s } from 'react';

import { DIV } from 'sdi/components/elements';
import { PiechartConfig, PiechartPiece } from 'sdi/source';
import { getAlias } from '../../app';

interface NotNullProperties {
    [key: string]: any;
}


const deg2rad = (d: number) => d * Math.PI / 180;

const polar2cartesian = (R: number, theta: number) => {
    const x = R * Math.cos(deg2rad(theta));
    const y = R * Math.sin(deg2rad(theta));
    return [x, y];
};


const arcString: (a: number, b: number, c: number, d: number, e: number) => string =
    (centerX, centerY, radius, start, stop) => {

        const large = (stop - start) > 180;
        const sweepFlag = 1;
        const largeArcFlag = large ? 1 : 0;
        const [sx, sy] = polar2cartesian(radius, start);
        const [ex, ey] = polar2cartesian(radius, stop);

        return `
    M ${centerX + sx} ${centerY + sy} 
    A ${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${centerX + ex} ${centerY + ey}
    L ${centerX} ${centerY} Z `;
    };


const normData = (d: number[]): number[] => {
    const sum = Math.max(d.reduce((acc, v) => acc + v, 0), 1);
    return d.map(v => v * 360 / sum);
};


const log = (n: number) => { return (n > 0 ? Math.log(n) : 0); };

const pie = (size: number, props: NotNullProperties, row: PiechartConfig) => {
    let offset = 0;
    const columns = row.options.columns;
    const maxRadius = size * 0.5;
    const minRadius = size * 0.2;
    const radiusInterval = maxRadius - minRadius;
    const c = size * 0.5;

    if (columns.length === 1) {
        return [
            s('circle', {
                cx: c,
                cy: c,
                r: maxRadius,
                fill: columns[0].color,
            }),
        ];
    }

    const rawData = columns.map((c) => {
        const v = props[c.propName];
        if (row.options.scale === 'log') {
            return log(v);
        }
        return v;
    });
    const radius = (v: number) => {
        if (row.options.radius === 'dynamic' && interval > 0) {
            return ((v - min) * radiusInterval / interval) + minRadius;
        }
        return maxRadius;
    };
    const data = normData(rawData);
    const [min, max] = data.reduce((acc, v) => {
        return [Math.min(acc[0], v), Math.max(acc[1], v)];
    }, [Number.MAX_VALUE, Number.MIN_VALUE]);
    const interval = max - min;
    return data.map((v, idx) => {
        const color = columns[idx].color;<commit_msg>[sdi] fixed piechart fails when prop is a number in its string repr<commit_after>/*
 *  Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { createElement as s } from 'react';

import { DIV } from 'sdi/components/elements';
import { PiechartConfig, PiechartPiece } from 'sdi/source';
import { getAlias } from '../../app';

interface NotNullProperties {
    [key: string]: any;
}


const deg2rad = (d: number) => d * Math.PI / 180;

const polar2cartesian = (R: number, theta: number) => {
    const x = R * Math.cos(deg2rad(theta));
    const y = R * Math.sin(deg2rad(theta));
    return [x, y];
};


const arcString: (a: number, b: number, c: number, d: number, e: number) => string =
    (centerX, centerY, radius, start, stop) => {

        const large = (stop - start) > 180;
        const sweepFlag = 1;
        const largeArcFlag = large ? 1 : 0;
        const [sx, sy] = polar2cartesian(radius, start);
        const [ex, ey] = polar2cartesian(radius, stop);

        return `
    M ${centerX + sx} ${centerY + sy} 
    A ${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${centerX + ex} ${centerY + ey}
    L ${centerX} ${centerY} Z `;
    };


const normData = (d: number[]): number[] => {
    const sum = Math.max(d.reduce((acc, v) => acc + v, 0), 1);
    return d.map(v => v * 360 / sum);
};


const log = (n: number) => { return (n > 0 ? Math.log(n) : 0); };

const pie = (size: number, props: NotNullProperties, row: PiechartConfig) => {
    let offset = 0;
    const columns = row.options.columns;
    const maxRadius = size * 0.5;
    const minRadius = size * 0.2;
    const radiusInterval = maxRadius - minRadius;
    const c = size * 0.5;

    if (columns.length === 1) {
        return [
            s('circle', {
                cx: c,
                cy: c,
                r: maxRadius,
                fill: columns[0].color,
            }),
        ];
    }

    const rawData = columns.map((c) => {
        const prop = props[c.propName];
        const v = typeof prop === 'number' ? prop : parseFloat(prop);
        if (row.options.scale === 'log') {
            return log(v);
        }
        return v;
    });
    const radius = (v: number) => {
        if (row.options.radius === 'dynamic' && interval > 0) {
            return ((v - min) * radiusInterval / interval) + minRadius;
        }
        return maxRadius;
    };
    const data = normData(rawData);
    const [min, max] = data.reduce((acc, v) => {
        return [Math.min(acc[0], v), Math.max(acc[1], v)];
    }, [Number.MAX_VALUE, Number.MIN_VALUE]);
    const interval = max - min;
    return data.map((v, idx) => {
        const color = columns[idx].color;
{'fraction_non_alphanumeric': 0.1448254266478785, 'fraction_numerical': 0.012525442304681384, 'mean_word_length': 2.430719656283566, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1872073', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1989, 'n_words': 680}