﻿/*
    1.0     Release
    2.0     Rewrote the script in jsx for the newer versions of Illustrator.
*/

var sversion = '2.0',
    scriptName = 'dViz: Radiating Bar Graph' + ' ' + sversion

var grayColor = new RGBColor();
var blackColor = new RGBColor();

grayColor.red = 133;
grayColor.green = 133;
grayColor.blue = 133;

blackColor.red = 0;
blackColor.green = 0;
blackColor.blue = 0;

if (version.split(".")[0] >= 15) {
    if (app.documents.length > 0) {
        if (app.activeDocument != null) {
            doc = app.activeDocument;
            var dlg = new Window('dialog', scriptName, undefined, { "resizeable": true, "closeButton":true, "independent":true } ); 
            
            var values = [];
            var max_value = 10;
            var width = 10;
            var height = 100;
            var space_between = 0;
            
            dlg.inputGroup = dlg.add('group', undefined, 'Input Fields:');
            dlg.inputGroup.spacing = 0;
            (dlg.inputGroup.addutton = dlg.inputGroup.add('button', [0,0,25,20], '+')).helpTip = "Add another value input field";

            function addValueInputField() {
                values.push(0);
                dlg.inputGroup["valueInput_"+values.length] = dlg.inputGroup.add('edittext', [0,0,50,20], 0);
            }
            
            dlg.inputGroup.addutton.onClick = function addValueField() {
                addValueInputField();
                dlg.layout.layout(true);
                dlg.inputGroup.layout.layout(true);
            }
            addValueInputField();
            dlg.inputGroup.orientation = 'row';
            
            dlg.optionGroup = dlg.add('group', undefined, 'Options Group');
            dlg.optionGroup.add('statictext', undefined, 'Max Value:');
            dlg.optionGroup.maxValueInput = dlg.optionGroup.add('edittext',  [0,0,50,20], max_value);
            dlg.optionGroup.add('statictext', undefined, 'Height:');
            dlg.optionGroup.heightInput = dlg.optionGroup.add('edittext',  [0,0,50,20], height);            
            dlg.optionGroup.orientation = 'row';

            dlg.graphicsOptionsGroup = dlg.add('group', undefined, 'Options Group');
            dlg.graphicsOptionsGroup.add('statictext', undefined, 'Width:');
            dlg.graphicsOptionsGroup.widthInput = dlg.graphicsOptionsGroup.add('edittext',  [0,0,50,20], width);
            dlg.graphicsOptionsGroup.add('statictext', undefined, 'Radius:');
            dlg.graphicsOptionsGroup.radiusInput = dlg.graphicsOptionsGroup.add('edittext',  [0,0,50,20], space_between);
            dlg.graphicsOptionsGroup.orientation = 'row';
            
            dlg.moreGraphicsOptionsGroup = dlg.add('group', undefined, 'Options Group');
            (dlg.moreGraphicsOptionsGroup.clipMax = dlg.moreGraphicsOptionsGroup.add('checkbox', undefined, "Clip Bars > Its Max Value")).helpTip = "Clip Bars > Its Max Value";
            (dlg.moreGraphicsOptionsGroup.allowAboveMax = dlg.moreGraphicsOptionsGroup.add('checkbox', undefined, "Allow Input Higher Than Max Value")).helpTip = "Allow Input Higher Than Max Value";
            (dlg.moreGraphicsOptionsGroup.drawMax = dlg.moreGraphicsOptionsGroup.add('checkbox', undefined, "Draw Max Value")).helpTip = "Draw Max Value";
            dlg.moreGraphicsOptionsGroup.orientation = 'column';

            dlg.buttonGroup = dlg.add('group', undefined, 'Button Group');
            dlg.buttonGroup.cancelBtn = dlg.buttonGroup.add('button', undefined, 'Cancel', {name:'cancel'});
            dlg.buttonGroup.cancelBtn.onClick = function() {dlg.close();};
            dlg.buttonGroup.okBtn = dlg.buttonGroup.add('button', undefined, 'OK', {name:'ok'});
            dlg.buttonGroup.okBtn.onClick = function() {
                var inval = 0;
                for(var key in dlg.inputGroup) {
                    if (key.split("_")[0]==="valueInput") {
                        if (isNaN(dlg.inputGroup[key].text)) {
                            inval++; break;
                        } else if (dlg.moreGraphicsOptionsGroup.allowAboveMax.value) {
                            if (parseFloat(dlg.inputGroup[key].text) > max_value) {
                                inval++; break;
                            }
                        } else{
                            values[parseInt(key.split("_")[1])-1] = parseFloat(dlg.inputGroup[key].text);
                        }
                    }
                }
            
                if (inval) {
                    Window.alert("Invalid values!");
                } else {
                    // Create the graph
                    var centerPoint = doc.activeView.centerPoint;
                    var offset = 0; 
                    
                    //$.writeln(centerPoint[1] + " " +  centerPoint[0]);
                    
                    width = parseFloat (dlg.graphicsOptionsGroup.widthInput.text);
                    height = parseFloat (dlg.optionGroup.heightInput.text);
                    radius = parseFloat(dlg.graphicsOptionsGroup.radiusInput.text);
                    max_value = parseFloat(dlg.optionGroup.maxValueInput.text);
                                        
                    for (var i in values) {
                        var t = values[i]*1/max_value;
                        
                        if (dlg.moreGraphicsOptionsGroup.drawMax.value) {
                            var angle = i*360/values.length;    
                            var bg = doc.pathItems.rectangle(centerPoint[1]-radius*Math.cos(angle*Math.PI/180), centerPoint[0]+radius*Math.sin(angle*Math.PI/180), width, height);                            
                            
                            bg.stroked = false;
                            bg.fillColor = grayColor;
                            
                            bg.rotate(angle, true, false, false, false, Transformation.TOP  )
                        }
                    
                        if (dlg.moreGraphicsOptionsGroup.clipMax.value) {
                            if (values[i]>max_value) {
                                t = 1;
                            }   
                        }
                    
                        var ft = doc.pathItems.rectangle(centerPoint[1]-radius*Math.cos(angle*Math.PI/180), centerPoint[0]+radius*Math.sin(angle*Math.PI/180), width, height*t);
                        ft.stroked = false;
                        ft.fillColor = blackColor;
                        
                        ft.rotate(i*360/values.length, true, false, false, false, Transformation.TOP  )
                        
                        offset+=width;
                        offset+=space_between;
                    }
                    $.writeln(values);
                    dlg.close();
                }
            }
            dlg.buttonGroup.orientation = 'row';
            dlg.show();
        } else { Window.alert("You must have an active document to run this script."); }
    } else { Window.alert("You must open at least one document to run this script."); }
} else { Window.alert("Your version of Adobe illustrator is too old to run this script."); }
