#! ngraph
# fitrslt.nsc written by S. ISHIZAKA. 1999/04
#
# This script generates a legend box.
#
# Please edit the following X, Y, and WIDTH variables as you like.
#
# Description: _Fit result...,insert fitting results as legend-text,

X=5000      # top-left of the legend to be inserted.
Y=5000      # top-left of the legend to be inserted.

new iarray name=ID
for i in `object data -instances`
do
    if [ `get data:$i -field type` = "fit" ]
    then
	if [ "x${data::fit}" != "x" ]
	then
	    iarray:ID:push $i
	fi
    fi
done

if [ ${iarray:ID:num} != 0 ]
then
    DATALIST=${system:0:temp_file}
    echo ${iarray:ID:num} > "$DATALIST"
    for i in ${iarray:ID:@}
    do
	if [ "x`get data:$i -field fit`" != "x" ]
	then
            get data: -field -escape id source file array >> "$DATALIST"
	    get ${data::fit} -field -escape id type poly_dimension user_func %00 %01 %02 %03 %04 %05 %06 %07 %08 %09 >> "$DATALIST"
	fi
    done
    if [ `get menu -field configuration:use_dark_theme` = "true" ]
    then
	DARK_THEME="-d"
    else
	DARK_THEME=""
    fi
    SCRIPT=${system:0:temp_file}
    "${NGRAPHLIB}/fitrslt" $DARK_THEME -x ${X} -y ${Y} "$DATALIST" "$SCRIPT"
    if [ -s "$SCRIPT" ];
    then
	new shell
	shell::shell "$SCRIPT"
	del shell
	gra::clear
	gra::draw
	gra::flush
    fi
    system:0:unlink_temp_file "$SCRIPT"
    system:0:unlink_temp_file "$DATALIST"
else
    new dialog
    dialog::beep
    dialog::message "No fitting data."
    del dialog
fi
del iarray:ID
