classdef ProfileStackReportImage < coder.profile.ProfileReportItem
    % PROFILERESTACKPORTIMAGE is an entry in the profiling report 
    % containing an image

    % Copyright 2021 The MathWorks, Inc.

    properties
        StackUsages;
        XLabels;
        MaxUsage;
        Intro;
    end
    
    methods
        function obj = ProfileStackReportImage(name, intro, usages, names, maxUsage)
            obj = obj@coder.profile.ProfileReportItem(name);
            obj.StackUsages = usages;
            obj.XLabels = names;
            obj.MaxUsage = maxUsage;
            obj.Intro = intro;
        end
        
        function build(this, report, location)
            f = figure('visible','off');
            bar(this.StackUsages, 'b');
            xticklabels(this.XLabels);
            xlabel('Tasks');
            ylabel('Max stack usage (Bytes)');
            if (double(this.MaxUsage) / max(this.StackUsages)) < 20
                line(xlim, [this.MaxUsage this.MaxUsage], 'Color', 'r', 'LineWidth', 2);
            end
            htmlDir = fileparts(location);
            imagePath = fullfile(htmlDir, 'maxStackUsage.png');
            saveas(f, imagePath);
            saveas(f, fullfile(htmlDir, 'maxStackUsage.fig'));
            close(f);
            html = sprintf('<p>%s</p><p><img src="%s" /></p>', this.Intro, imagePath);
            report.addCustomizedSection(this.SectionName, html, true, true, true);
        end
    end
    
end
