Alien-libhisto

 view release on metacpan or  search on metacpan

bundled/tools/src/cmd_plot.c  view on Meta::CPAN

            continue;
        }

        double val_scaled = log_scale ? log10(content + 1.0) : content;
        double frac_total = val_scaled / max_scaled;
        double bar_len_exact = frac_total * (double)bar_max_chars;

        char color_ansi[32] = "";
        if (use_color) {
            histo_palette_sample_ansi_fg(palette, frac_total, color_ansi, sizeof(color_ansi));
            fprintf(out, "%s", color_ansi);
        }

        if (strcmp(style, "ascii") == 0) {
            int full_chars = (int)bar_len_exact;
            for (int k = 0; k < full_chars; ++k) {
                fputc('#', out);
            }
            if (show_errors && err > 0.0 && full_chars < bar_max_chars) {
                fprintf(out, " ±%.2g", err);
            }
        } else if (strcmp(style, "shaded") == 0) {
            int full_chars = (int)bar_len_exact;
            for (int k = 0; k < full_chars; ++k) {
                fprintf(out, "â–ˆ");
            }
        } else { /* blocks with 1/8th sub-character precision */
            int full_chars = (int)bar_len_exact;
            int remainder_eighths = (int)((bar_len_exact - (double)full_chars) * 8.0);
            if (remainder_eighths < 0) remainder_eighths = 0;
            if (remainder_eighths > 8) remainder_eighths = 8;

            for (int k = 0; k < full_chars; ++k) {
                fprintf(out, "â–ˆ");
            }
            if (remainder_eighths > 0 && full_chars < bar_max_chars) {
                fprintf(out, "%s", UNICODE_BLOCKS[remainder_eighths]);
            }
            if (show_errors && err > 0.0) {
                if (use_color) fprintf(out, "\033[0m\033[90m");
                fprintf(out, " ╎±%.2g╎", err);
            }
        }

        if (use_color) {
            fprintf(out, "\033[0m");
        }
        fprintf(out, "\n");
    }

    /* Print out-of-range footer */
    double uflow = histo_underflow(h);
    double oflow = histo_overflow(h);
    uint64_t n_nan = histo_nan_count(h);

    char uflow_str[32], oflow_str[32], tot_str[32];
    snprintf(uflow_str, sizeof(uflow_str), (uflow == floor(uflow) && uflow >= 0.0 && uflow < 1e12) ? "%.0f" : "%.4g", uflow);
    snprintf(oflow_str, sizeof(oflow_str), (oflow == floor(oflow) && oflow >= 0.0 && oflow < 1e12) ? "%.0f" : "%.4g", oflow);
    snprintf(tot_str, sizeof(tot_str), (total_w == floor(total_w) && total_w >= 0.0 && total_w < 1e12) ? "%.0f" : "%.4g", total_w);

    fprintf(out, " Underflow: %s │ In-Range: %s │ Overflow: %s │ Non-Finite/NaN: %llu\n",
           uflow_str, tot_str, oflow_str, (unsigned long long)n_nan);
}

static void render_histogram_sparkline(const histo_t *h, const char *style, bool use_color, histo_palette_t palette, bool log_scale, bool show_stats, FILE *out) {
    if (!h || !out) return;

    uint32_t nbins = histo_nbins(h);
    double max_content = 0.0;
    for (uint32_t i = 0; i < nbins; ++i) {
        double c = 0.0;
        histo_bin_content(h, i, &c);
        if (c > max_content) max_content = c;
    }

    double max_scaled = (log_scale && max_content > 0.0) ? log10(max_content + 1.0) : max_content;
    bool is_ascii = (strcmp(style, "ascii") == 0);

    for (uint32_t i = 0; i < nbins; ++i) {
        double content = 0.0;
        histo_bin_content(h, i, &content);

        if (content <= 0.0 || max_scaled <= 0.0) {
            fputc(' ', out);
            continue;
        }

        double val_scaled = log_scale ? log10(content + 1.0) : content;
        double frac = val_scaled / max_scaled;
        int level = (int)(frac * 8.0 + 0.5);
        if (level < 1) level = 1;
        if (level > 8) level = 8;

        if (use_color) {
            char color_ansi[32] = "";
            histo_palette_sample_ansi_fg(palette, frac, color_ansi, sizeof(color_ansi));
            fprintf(out, "%s", color_ansi);
        }

        if (is_ascii) {
            fprintf(out, "%s", SPARKLINE_ASCII[level]);
        } else {
            fprintf(out, "%s", SPARKLINE_UNICODE[level]);
        }

        if (use_color) {
            fprintf(out, "\033[0m");
        }
    }

    if (show_stats) {
        double mean = 0.0, sdev = 0.0, rmin = 0.0, rmax = 0.0;
        histo_mean(h, &mean);
        histo_std_dev(h, &sdev);
        histo_range(h, &rmin, &rmax);
        uint64_t entries = histo_num_entries(h);

        fprintf(out, "  [N=%llu, range=[%.2g, %.2g), μ=%.2g, σ=%.2g]",
               (unsigned long long)entries, rmin, rmax, mean, sdev);
    }
    fprintf(out, "\n");



( run in 0.735 second using v1.01-cache-2.11-cpan-60884a8df76 )