Alien-libhisto

 view release on metacpan or  search on metacpan

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

    histo2d_axis_x(h, &ax);
    histo2d_axis_y(h, &ay);

    double max_content = 0.0;
    for (uint32_t ix = 0; ix < nx; ++ix) {
        for (uint32_t iy = 0; iy < ny; ++iy) {
            double c = 0.0;
            histo2d_bin_content(h, ix, iy, &c);
            if (c > max_content) max_content = c;
        }
    }
    if (max_content <= 0.0) max_content = 1.0;

    if (title && *title) {
        fprintf(out, "\n── %s ──\n", title);
    } else {
        fprintf(out, "\n── 2D Histogram Heatmap (%u x %u bins, Entries: %llu, Weight: %.4g) ──\n",
               nx, ny, (unsigned long long)histo2d_num_entries(h), histo2d_total_weight(h));
    }

    /* Render Y rows from top (ny-1) to bottom (0) */
    for (int iy = (int)ny - 1; iy >= 0; --iy) {
        double ymin_b, ymax_b, dummy;
        histo2d_bin_bounds(h, 0, (uint32_t)iy, &dummy, &dummy, &ymin_b, &ymax_b);
        fprintf(out, "%7.2f │ ", 0.5 * (ymin_b + ymax_b));

        for (uint32_t ix = 0; ix < nx; ++ix) {
            double c = 0.0;
            histo2d_bin_content(h, ix, (uint32_t)iy, &c);
            double frac = (c > 0.0) ? (c / max_content) : 0.0;
            if (use_color) {
                char bg[32];
                histo_palette_sample_ansi_bg(palette, frac, bg, sizeof(bg));
                fprintf(out, "%s  \033[0m", bg);
            } else {
                static const char density[] = " .:-=+*#%@";
                int idx = (int)(frac * 9.0);
                if (idx < 0) idx = 0;
                if (idx > 9) idx = 9;
                fprintf(out, "%c ", density[idx]);
            }
        }
        fprintf(out, " │\n");
    }

    /* X Axis line and ticks */
    fprintf(out, "        â””");
    for (uint32_t ix = 0; ix < nx; ++ix) fprintf(out, "──");
    fprintf(out, "─┘\n");

    fprintf(out, "         ");
    fprintf(out, "%-7.2f", ax.min);
    int mid_pad = (int)(nx * 2) - 14;
    if (mid_pad > 0) {
        for (int p = 0; p < mid_pad / 2; ++p) fputc(' ', out);
        fprintf(out, "%-7.2f", 0.5 * (ax.min + ax.max));
        for (int p = 0; p < (mid_pad - mid_pad / 2); ++p) fputc(' ', out);
    }
    fprintf(out, "%7.2f\n", ax.max);

    /* Color bar legend */
    if (use_color) {
        fprintf(out, "\n  Intensity: 0.00 ");
        for (int step = 0; step <= 20; ++step) {
            double frac = (double)step / 20.0;
            char bg[32];
            histo_palette_sample_ansi_bg(palette, frac, bg, sizeof(bg));
            fprintf(out, "%s \033[0m", bg);
        }
        fprintf(out, " %.2e [%s]\n\n", max_content, histo_palette_name(palette));
    } else {
        fprintf(out, "\n  Density scale: [ .:-=+*#%%@ ] (0.00 -> %.2e)\n\n", max_content);
    }
}

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

    uint32_t nbins = histo_nbins(h);
    double total_w = histo_total_weight(h);
    uint64_t n_fills = histo_num_entries(h);

    double max_content = 0.0;
    int max_bounds_len = 0;
    int max_count_len = 0;
    int max_err_len = 0;

    for (uint32_t i = 0; i < nbins; ++i) {
        double lower = 0.0, upper = 0.0, content = 0.0, err = 0.0;
        histo_bin_bounds(h, i, &lower, &upper);
        histo_bin_content(h, i, &content);
        if (content > max_content) max_content = content;

        char b_tmp[64];
        int blen = snprintf(b_tmp, sizeof(b_tmp), "[%6.2f, %6.2f)", lower, upper);
        if (blen > max_bounds_len) max_bounds_len = blen;

        char c_tmp[64];
        int clen;
        if (content == floor(content) && content >= 0.0 && content < 1e12) {
            clen = snprintf(c_tmp, sizeof(c_tmp), "%.0f", content);
        } else {
            clen = snprintf(c_tmp, sizeof(c_tmp), "%.4g", content);
        }
        if (clen > max_count_len) max_count_len = clen;

        if (show_errors) {
            histo_bin_error(h, i, &err);
            if (err > 0.0) {
                char e_num[32];
                int nlen = snprintf(e_num, sizeof(e_num), "%.2g", err);
                int elen = (strcmp(style, "ascii") == 0) ? (nlen + 3) : (nlen + 4);
                if (elen > max_err_len) max_err_len = elen;
            }
        }
    }

    if (max_bounds_len < 16) max_bounds_len = 16;
    if (max_count_len < 6) max_count_len = 6;

    /* Print Title & Header */



( run in 0.535 second using v1.01-cache-2.11-cpan-02e1dfdd527 )