<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import { format, time } from 'd3';

// Helpers
import { getTimeFormat } from 'utils/widgets/WidgetHelper';

// Global functions
// eslint-disable-next-line no-confusing-arrow
const getRoundNumber = number => Math.abs(number % 1) > 0 ? format('.2f')(number) : number;
const getSINumber = format('.2s');

class VegaChartTooltip extends React.Component {

  getParsedX() {
    const { item } = this.props;
    if (!item) return null;

    const { x } = item;

    if (x.type === 'number') {
      return getRoundNumber(x.value);
    } else if (x.type === 'date') {
      const date = new Date(x.value);
      return time.format(getTimeFormat(x.range))(date);
    }

    return x.value;
  }

  getParsedY() {
    const { item } = this.props;
    if (!item) return null;

    const { y } = item;
    if (!y) return null;

    if (y.type === 'number') {
      return getSINumber(y.value);
    } else if (y.type === 'date') {
      const date = new Date(y.value);
      return time.format(getTimeFormat(y.range))(date);
    }

    return y.value;
  }


  render() {
    return (
      <div className="c-chart-tooltip">
        { this.props.item.x.label && (
          <div className="labels">
            <span>{this.props.item.y && this.props.item.y.label}</span>
            <span>{this.props.item.x.label}</span>
          </div>
        )}
        <div className="values">
          <span>{this.getParsedY()}</span>
          <span>{this.getParsedX()}</span>
        </div>
      </div>
    );
  }
}

const columnType = PropTypes.shape({
  type: PropTypes.oneOf(['number', 'string', 'date']),
  label: PropTypes.string,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object]),
  // Range needed to properly display the dates
  // Either a timestamp or a date object
  range: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.object]))
});

VegaChartTooltip.propTypes = {
  item: PropTypes.shape({
    x: columnType,
    y: columnType
  })
};

export default VegaChartTooltip;<commit_msg>Improve the resilience of the layout for the 1d_* charts<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import { format, time } from 'd3';

// Helpers
import { getTimeFormat } from 'utils/widgets/WidgetHelper';

// Global functions
// eslint-disable-next-line no-confusing-arrow
const getRoundNumber = number => Math.abs(number % 1) > 0 ? format('.2f')(number) : number;
const getSINumber = format('.2s');

class VegaChartTooltip extends React.Component {

  getParsedX() {
    const { item } = this.props;
    if (!item) return null;

    const { x } = item;

    if (x.type === 'number') {
      return getRoundNumber(x.value);
    } else if (x.type === 'date') {
      const date = new Date(x.value);
      return time.format(getTimeFormat(x.range))(date);
    }

    return x.value;
  }

  getParsedY() {
    const { item } = this.props;
    if (!item) return null;

    const { y } = item;
    if (!y) return null;

    if (y.type === 'number') {
      return getSINumber(y.value);
    } else if (y.type === 'date') {
      const date = new Date(y.value);
      return time.format(getTimeFormat(y.range))(date);
    }

    return y.value;
  }


  render() {
    return (
      <div className="c-chart-tooltip">
        { this.props.item.x.label && (
          <div className="labels">
            { this.props.item.y.label && <span>{this.props.item.y.label}</span> }
            <span>{this.props.item.x.label}</span>
          </div>
        )}
        <div className="values">
          { this.props.item.y.value && <span>{this.getParsedY()}</span> }
          <span>{this.getParsedX()}</span>
        </div>
      </div>
    );
  }
}

const columnType = PropTypes.shape({
  type: PropTypes.oneOf(['number', 'string', 'date']),
  label: PropTypes.string,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object]),
  // Range needed to properly display the dates
  // Either a timestamp or a date object
  range: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.object]))
});

VegaChartTooltip.propTypes = {
  item: PropTypes.shape({
    x: columnType,
    y: columnType
  })
};

export default VegaChartTooltip;
{'fraction_non_alphanumeric': 0.16, 'fraction_numerical': 0.0025581395348837207, 'mean_word_length': 2.7894273127753304, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, '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': '3816730', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1405, 'n_words': 317}