{"size": 1318, "ext": "js", "max_stars_repo_path": "src/chart/resize/setMargins.js", "max_stars_repo_name": "RhoInc/Webcharts", "max_stars_repo_stars_event_min_datetime": "2015-03-24T15:58:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-27T19:04:49.000Z", "max_issues_repo_path": "src/chart/resize/setMargins.js", "max_issues_repo_name": "RhoInc/Webcharts", "max_issues_repo_issues_event_min_datetime": "2015-09-11T12:54:16.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-28T16:58:24.000Z", "max_forks_repo_path": "src/chart/resize/setMargins.js", "max_forks_repo_name": "RhoInc/Webcharts", "max_forks_repo_forks_event_min_datetime": "2015-07-29T15:01:05.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-11T08:45:33.000Z"}
{"max_stars_count": 32, "max_issues_count": 208, "max_forks_count": 7, "avg_line_length": 41.1875, "max_line_length": 100, "alphanum_fraction": 0.6320182094}
import { max } from 'd3';

export default function setMargins() {
    let y_ticks = this.yAxis.tickFormat()
        ? this.y.domain().map(m => this.yAxis.tickFormat()(m))
        : this.y.domain();

    let max_y_text_length = max(y_ticks.map(m => String(m).length));
    if (this.config.y_format && this.config.y_format.indexOf('%') > -1) {
        max_y_text_length += 1;
    }
    max_y_text_length = Math.max(2, max_y_text_length);
    let x_label_on = this.config.x.label ? 1.5 : 0;
    let y_label_on = this.config.y.label ? 1.5 : 0.25;
    let font_size = parseInt(this.wrap.style('font-size'));
    let x_second = this.config.x2_interval ? 1 : 0;
    let y_margin = max_y_text_length * font_size * 0.5 + font_size * y_label_on * 1.5 || 8;
    let x_margin = font_size + font_size / 1.5 + font_size * x_label_on + font_size * x_second || 8;

    y_margin += 6;
    x_margin += 3;

    return {
        top: this.config.margin && this.config.margin.top ? this.config.margin.top : 8,
        right: this.config.margin && this.config.margin.right ? this.config.margin.right : 16,
        bottom: this.config.margin && this.config.margin.bottom
            ? this.config.margin.bottom
            : x_margin,
        left: this.config.margin && this.config.margin.left ? this.config.margin.left : y_margin
    };
}
