<commit_before>import { createSelector, createStructuredSelector } from 'reselect';
import { format } from 'd3-format';
import { formatNumber } from 'utils/format';
import isEmpty from 'lodash/isEmpty';
// get list data
const getData = (state) => state.data;
const getSettings = (state) => state.settings;
const getColors = (state) => state.colors;
const getIndicator = (state) => state.indicator;
const getLocationName = (state) => state.locationLabel;
const getSentences = (state) => state.sentences;

export const parseData = createSelector(
  [getData, getSettings],
  (totalData, settings) => {
    if (!totalData) return null;
    const { startYear, endYear } = settings;

    // TODO: return as object
    return [
      totalData.length &&
        Object.keys(totalData[0]).reduce((result, k) => {
          result[k] = totalData[0][k] / (endYear - startYear);
          return result;
        }, {}),
    ];
  }
);

export const parseConfig = createSelector(
  [parseData, getColors],
  (data, colors) => {
    if (!data || isEmpty(data)) return null;
    const {
      carbonFlux: { emissions, netCarbonFlux, removals },
    } = colors;

    const maxValue =
      Math.abs(
        Object.values(data[0]).reduce((a, b) => (data[a] > data[b] ? b : a))
      ) * 1.2;

    const { flux: netFluxData } = data[0];

    return {
      height: 250,
      stackOffset: 'sign',
      margin: {
        bottom: 40,
      },
      yAxis: {
        hide: true,
        type: 'category',
      },
      xAxis: {
        type: 'number',
        domain: [-maxValue, maxValue],
        allowDecimals: false,
        ticks: [-maxValue, -maxValue / 2, 0, maxValue / 2, maxValue],
        tickFormatter: (value) => format('.2r')(value * 1e-9),
        label: {
          value: 'GtCO\u2082e/year',
          fontSize: '14px',
          position: 'bottom',
        },
      },
      xKeys: {
        bars: {
          emissions: {
            fill: emissions,
            background: false,
            stackId: 2,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, width, height, offset } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={x + width + offset}
                      y={y}
                      textAnchor="start"
                      fill="#000"
                      fontSize={14}
                    >
                      EMISSIONS
                    </text>
                  </g>
                );
              },
            },
          },
          removals: {
            fill: removals,
            background: false,
            stackId: 2,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, offset, width, height } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={x + width - offset}
                      y={y}
                      textAnchor="end"
                      fill="#000"
                      fontSize={14}
                    >
                      REMOVALS
                    </text>
                  </g>
                );
              },
            },
          },
          flux: {
            fill: netCarbonFlux,
            background: false,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, width, height, offset } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={x + width - offset}
                      y={y}
                      textAnchor="end"
                      fill="#000"
                      fontSize={14}
                    >
                      {netFluxData > 0 ? 'NET EMISSIONS' : 'NET REMOVALS'}
                    </text>
                  </g>
                );
              },
            },
          },
        },
      },
      referenceLine: { x: 0, stroke: '#606060', strokeWidth: 2 },
      tooltip: [
        {
          label: 'Carbon flux (per year)',
        },
        {
          key: 'emissions',
          label: 'Emissions',
          unitFormat: (value) => `${format('.3s')(value)}tCO\u2082e`,
          color: emissions,<commit_msg>domain + labels position in carbon flux widget<commit_after>import { createSelector, createStructuredSelector } from 'reselect';
import { format } from 'd3-format';
import { formatNumber } from 'utils/format';
import isEmpty from 'lodash/isEmpty';
// get list data
const getData = (state) => state.data;
const getSettings = (state) => state.settings;
const getColors = (state) => state.colors;
const getIndicator = (state) => state.indicator;
const getLocationName = (state) => state.locationLabel;
const getSentences = (state) => state.sentences;

export const parseData = createSelector(
  [getData, getSettings],
  (totalData, settings) => {
    if (!totalData) return null;
    const { startYear, endYear } = settings;

    // TODO: return as object
    return [
      totalData.length &&
        Object.keys(totalData[0]).reduce((result, k) => {
          result[k] = totalData[0][k] / (endYear - startYear);
          return result;
        }, {}),
    ];
  }
);

export const parseConfig = createSelector(
  [parseData, getColors],
  (data, colors) => {
    if (!data || isEmpty(data)) return null;
    const {
      carbonFlux: { emissions, netCarbonFlux, removals },
    } = colors;

    const maxValue =
      Math.abs(
        Object.values(data[0]).reduce((a, b) => (data[a] > data[b] ? b : a))
      ) * 1.2;

    const {
      emissions: emissionsData,
      removals: removalsData,
      flux: netFluxData,
    } = data[0];

    return {
      height: 250,
      stackOffset: 'sign',
      margin: {
        bottom: 40,
      },
      yAxis: {
        hide: true,
        type: 'category',
      },
      xAxis: {
        type: 'number',
        domain: [-maxValue - 100000000, maxValue + 100000000],
        allowDecimals: false,
        ticks: [-maxValue, -maxValue / 2, 0, maxValue / 2, maxValue],
        tickFormatter: (value) => format('.2r')(value * 1e-9),
        label: {
          value: 'GtCO\u2082e/year',
          fontSize: '14px',
          position: 'bottom',
        },
      },
      xKeys: {
        bars: {
          emissions: {
            fill: emissions,
            background: false,
            stackId: 2,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, width, height, offset } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={
                        emissionsData > 0
                          ? x + width + offset
                          : x + width - offset
                      }
                      y={y}
                      textAnchor={emissionsData > 0 ? 'start' : 'end'}
                      fill="#000"
                      fontSize={14}
                    >
                      EMISSIONS
                    </text>
                  </g>
                );
              },
            },
          },
          removals: {
            fill: removals,
            background: false,
            stackId: 2,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, offset, width, height } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={
                        removalsData > 0
                          ? x + width + offset
                          : x + width - offset
                      }
                      y={y}
                      textAnchor={removalsData > 0 ? 'start' : 'end'}
                      fill="#000"
                      fontSize={14}
                    >
                      REMOVALS
                    </text>
                  </g>
                );
              },
            },
          },
          flux: {
            fill: netCarbonFlux,
            background: false,
            labelList: {
              content: (props) => {
                // eslint-disable-next-line react/prop-types
                const { x, y, width, height, offset } = props;
                return (
                  <g transform={`translate(0 ${height / 2 + offset})`}>
                    <text
                      x={
                        netFluxData > 0
                          ? x + width + offset
                          : x + width - offset
                      }
                      y={y}
                      textAnchor={netFluxData > 0 ? 'start' : 'end'}
                      fill="#000"
                      fontSize={14}
                    >
                      {netFluxData > 0 ? 'NET EMISSIONS' : 'NET REMOVALS'}
                    </text>
                  </g>
                );
              },
            },
          },
        },
      },
      referenceLine: { x: 0, stroke: '#606060', strokeWidth: 2 },
      tooltip: [
        {
          label: 'Carbon flux (per year)',
        },
        {
          key: 'emissions',
          label: 'Emissions',
          unitFormat: (value) => `${format('.3s')(value)}tCO\u2082e`,
          color: emissions,
{'fraction_non_alphanumeric': 0.12583256481196844, 'fraction_numerical': 0.014960549236602111, 'mean_word_length': 1.1535745807590467, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2362704', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2581, 'n_words': 602}