Ejemplo n.º 1
0
 public static void CleanGraph(StatisticalGraph sg)
 {
     log.Debug("Attempting cleaning of graph: "
               + sg.Prologue.GetGraphName().ToUpper());
     new TextboxCleaner().Clean(sg);
     new SeriesCleaner().Clean(sg);
     new CategoryAxisCleaner().Clean(sg);
 }
Ejemplo n.º 2
0
        public void Clean(StatisticalGraph graph)
        {
            log.Debug("Applying category axis cleaning algorithms.");

            g  = graph;
            ca = graph.CategoryAxis;

            CleanNullsCategory();

            CleanPrimaryCategories();

            CleanSecondaryCategories();
        }
Ejemplo n.º 3
0
        private void RoundDecimals(StatisticalGraph graph)
        {
            int num_ser       = graph.Series.Count;
            int decimals2show = 2;

            for (int i = 0; i < num_ser; i++)
            {
                SGSeries curr = graph.Series[i];
                if (graph.ValueAxis.ScaleUnit > 0)
                {
                    for (int j = 0; j < curr.Values.Count; j++)
                    {
                        curr.Values[j] = Math.Round((Double)curr.Values[j], decimals2show);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Clean(StatisticalGraph graph)
        {
            log.Debug("Applying textbox cleaning algorithms.");

            g = graph;
            txtbox_collection = graph.Textboxes;
            removeEmpty(); //remove empty textboxes
            txtbox_collection.SortVertically();

            int ctr = 0;

            foreach (SGTextBox txtbx in txtbox_collection)
            {
                RecognizeFunction(txtbx, ctr);
                ctr++;
            }
        }
Ejemplo n.º 5
0
        private void ScaleValues(StatisticalGraph graph)
        {
            int num_ser = graph.Series.Count;

            // Scaling data by unit of visualization

            graph.ValueAxis.EndsAt   = Convert.ToDouble(graph.ValueAxis.EndsAt) / graph.ValueAxis.ScaleUnit;
            graph.ValueAxis.StartsAt = Convert.ToDouble(graph.ValueAxis.StartsAt) / graph.ValueAxis.ScaleUnit;
            graph.ValueAxis.Stepping = Convert.ToDouble(graph.ValueAxis.Stepping) / graph.ValueAxis.ScaleUnit;

            for (int i = 0; i < num_ser; i++)
            {
                SGSeries curr = graph.Series[i];
                if (graph.ValueAxis.ScaleUnit > 0)
                {
                    for (int j = 0; j < curr.Values.Count; j++)
                    {
                        curr.Values[j] = Convert.ToDouble(curr.Values[j]) / graph.ValueAxis.ScaleUnit;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /* This method builds and returns a list of Statisitcal Graph objects from
         * a (json) file.
         */
        public List <StatisticalGraph> BuildGraphList(string file, bool gif)
        {
            // A container for the graphs to be parsed
            List <StatisticalGraph> sg_collection = new List <StatisticalGraph>();

            StatisticalGraph graph = new StatisticalGraph();

            // Some excel flags we shouldn't need
            const int graphID   = 1;
            const int sheetName = 1;

            jo_parsed_file = JObject.Parse(File.ReadAllText(file));
            var parsed_file = jo_parsed_file.ToObject <Dictionary <string, object> >();

            // Prologue which holds metadata about graph, mainly name and size
            graph.Prologue = new GraphPrologue(file,
                                               "JSON",
                                               graphID,
                                               sheetName,
                                               GetGraphType(),
                                               GetGraphHeight(),
                                               GetGraphWidth());



            graph.MainTitle    = GetJSONMainTitle(parsed_file);
            graph.PlotArea     = GetJSONPlotArea(parsed_file);
            graph.Textboxes    = GetJSONTextBoxes(parsed_file);
            graph.ValueAxis    = GetJSONValueAxis(parsed_file);
            graph.CategoryAxis = GetJSONCategoryAxis(parsed_file);
            graph.Series       = GetSeries(parsed_file);


            sg_collection.Add(graph);

            return(sg_collection);
        }
Ejemplo n.º 7
0
        public void Clean(StatisticalGraph graph)
        {
            log.Debug("Applying series cleaning algorithms.");
            int  num_ser = graph.Series.Count;
            bool problem = true;

            for (int i = 0; i < num_ser; i++)
            {
                SGSeries curr = graph.Series[i];

                switch (GetSeriesNullState(curr))
                {
                case SeriesNulls.ALL_NULL:
                    curr.Status = (int)SeriesNulls.ALL_NULL;
                    graph.Series.RemoveAt(i);
                    i--;
                    num_ser--;
                    log.Error("Empty series. Why?");
                    break;

                case SeriesNulls.SOME_NULL:
                    curr.Status = (int)SeriesNulls.SOME_NULL;
                    CleanSomeNullValues(curr);
                    break;

                case SeriesNulls.FIRST_LAST_NULL:
                    curr.Status        = (int)SeriesNulls.FIRST_LAST_NULL;
                    graph.Series.Dirty = true;
                    curr.Values.RemoveAt(0);
                    curr.Values.RemoveAt(curr.Values.Count - 1);
                    log.Error("Empty first and last elements. Why?");
                    break;

                case SeriesNulls.FIRST_NULL:
                    curr.Status        = (int)SeriesNulls.FIRST_NULL;
                    graph.Series.Dirty = true;
                    curr.Values.RemoveAt(0);
                    log.Error("Empty first element. Why?");
                    break;

                case SeriesNulls.LAST_NULL:
                    curr.Status        = (int)SeriesNulls.LAST_NULL;
                    graph.Series.Dirty = true;
                    curr.Values.RemoveAt(curr.Values.Count - 1);
                    log.Error("Empty last element. Why?");
                    break;

                default:
                    curr.Status = (int)SeriesNulls.NO_NULL;
                    problem     = false;
                    break;
                }
            }

            //Scale Values ;)
            ScaleValues(graph);

            //Round to 2 decimals
            RoundDecimals(graph);

            if (!problem)
            {
                log.Debug("All series seem OK. Bravo!");
            }
        }
Ejemplo n.º 8
0
        public List <StatisticalGraph> BuildGraphList(string file, bool gif)
        {
            List <StatisticalGraph> sg_collection = new List <StatisticalGraph>();

            Excel.Workbook xl_workbook;

            try
            {
                xl_workbook = this.xl_app.Workbooks.Open(file, 0, false, 5,
                                                         "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false,
                                                         false);
            } catch
            {
                return(null); /*returning the null list; handled in iGraphMain*/
            }

            /** Loop over every worksheet and every ChartObject in the worksheet.
             * The ChartObject object acts as a container for a Chart object.
             * Properties and methods for the ChartObject object control the
             * appearance and size of the embedded chart on the worksheet,
             * see: http://msdn.microsoft.com/en-us/library/aa173258(office.11).aspx
             */
            foreach (Worksheet sheet in xl_workbook.Worksheets)
            {
                foreach (ChartObject curr_chart in
                         (ChartObjects)sheet.ChartObjects(Type.Missing))
                {
                    sg      = new StatisticalGraph();
                    problem = false;
                    Chart c = curr_chart.Chart; // c is the chart in ChartObject

                    if ((int)c.ChartType == 68 || (int)c.ChartType == 71)
                    {
                        log.Warn("iGraph does not process pie charts... Skipping.");
                        continue;
                    }

                    sg.Prologue = new GraphPrologue(file,
                                                    "MS Excel",
                                                    curr_chart.Index,
                                                    sheet.Index,
                                                    (int)c.ChartType,
                                                    curr_chart.Height,
                                                    curr_chart.Width);

                    log.Debug("Graph ID: " + sg.Prologue.GetGraphName());

                    sg.PlotArea = GetXLPlotArea(c);

                    sg.Textboxes = GetXLTextBoxes(c);

                    sg.Series = GetXLSeries(c);

                    sg.MainTitle = GetXLChartTitle(c);

                    sg.ValueAxis = GetXLValueAxis(c);

                    sg.CategoryAxis = GetXLCategoryAxis(c);

                    // add to the returned statgraph collection
                    if (!problem)
                    {
                        sg_collection.Add(sg);
                        // if flagged, export graph as gif in same argument directory
                        if (gif)
                        {
                            c.Export(sg.Prologue.GetGifName(), "GIF", false);
                            log.Info("Saved " + sg.Prologue.GetGifName());
                        }
                    }
                }
            }
            // Excel has to be closed.
            xl_app.Workbooks.Close();
            xl_app.Quit();

            // the return value if all goes well...
            return(sg_collection);
        }
Ejemplo n.º 9
0
        public bool Generate(StatisticalGraph graph)
        {
            g     = graph;
            error = false;
            FrenchGenerator  fr_t = new FrenchGenerator();
            EnglishGenerator en_t = new EnglishGenerator();

            #region NVelocity setup
            VelocityEngine velocity = new VelocityEngine();

            ExtendedProperties props = new ExtendedProperties();
            velocity.Init(props);

            //Template template;
            string strTemplate;

            // This nested if can be better...
            if (g.GraphLanguage == null || g.GraphLanguage.Length == 0)
            {
                if (g.Prologue.GetLanguageByFilename() != "French")
                {
                    g.GraphLanguage = IGraphConstants.LANG_ENG;
                }
                else
                {
                    g.GraphLanguage = IGraphConstants.LANG_FRA;
                }
            }

            if (g.GraphLanguage == IGraphConstants.LANG_ENG)
            {
                //template = velocity.GetTemplate(@"./English.nv");
                byte[] NVtemplate            = igl.Properties.Resources.NVenglish;
                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                strTemplate = enc.GetString(NVtemplate);
            }
            else
            {
                //template = velocity.GetTemplate(@"./French.nv");
                byte[] NVtemplate            = igl.Properties.Resources.NVfrench;
                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                strTemplate = enc.GetString(NVtemplate);
            }


            VelocityContext context = new VelocityContext();
            #endregion

            context.Put("graph", g);
            context.Put("french", fr_t);
            context.Put("english", en_t);
            context.Put("date", DateTime.Now);

            // run template matching
            StringWriter writer = new StringWriter();

            try
            {
                //setting Culture
                if (g.GraphLanguage == IGraphConstants.LANG_ENG)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-CA", false);
                }
                else
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA", false);
                }

                //template.Merge(context, writer);
                velocity.Evaluate(context, writer, "NVlocity", strTemplate);
                SaveDescription(writer.GetStringBuilder().ToString());
            } catch (Exception e)
            {
                log.Error("NVelocity error." + e.Message);
                error = true;
            }

            //Restoring culture
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", false);

            return(error);
        }
Ejemplo n.º 10
0
        public static void write(StatisticalGraph sg)
        {
            string xml_file_path = sg.Prologue.GetGraphOriginalDirectory();
            string xml_file_name = sg.Prologue.GetGraphName() + ".xml";

            XmlDocument   xmlDoc = new XmlDocument();
            XmlTextWriter w      = new XmlTextWriter(xml_file_path + xml_file_name,
                                                     System.Text.Encoding.UTF8);

            w.Formatting = Formatting.Indented;
            w.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

            // add information in a comment //
            Assembly executingAssembly = Assembly.GetExecutingAssembly();

            System.Version version = executingAssembly.GetName().Version;

            string comment = String.Format("Generated by iGraph, Version "
                                           + "{0}\n(c) 2005-{1}, Leo Ferres - University of Concepción, "
                                           + "email: [email protected].\nGenerated on: {2}, {3}",
                                           version.ToString(4), DateTime.Now.Year,
                                           DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString());

            xmlDoc.AppendChild(xmlDoc.CreateComment(comment));

            // construct XML
            XmlElement root  = xmlDoc.CreateElement("Scene");
            XmlElement graph = xmlDoc.CreateElement("Graph");
            XmlElement axis  = xmlDoc.CreateElement("Axis");

            // adding the top nodes
            xmlDoc.AppendChild(root);
            root.AppendChild(graph);
            graph.AppendChild(axis);

            graph.SetAttribute("type", sg.Prologue.GetGraphType().ToString());
            graph.SetAttribute("lang", sg.Prologue.GetLanguageByFilename());
            graph.SetAttribute("origin", sg.Prologue.GetOrigin());
            graph.SetAttribute("name", sg.Prologue.GetOriginalExcelFile());

            /*
             * add titles
             */

            XmlElement titles = xmlDoc.CreateElement("Titles");

            graph.AppendChild(titles);

            // main titles
            XmlElement main_title   = xmlDoc.CreateElement("MainTitle");
            XmlElement main_primary = xmlDoc.CreateElement("PrimaryTitle");

            main_title.AppendChild(main_primary);
            main_primary.InnerText = sg.MainTitle;
            titles.AppendChild(main_title);

            //// category axis titles
            XmlElement catAxisTitle    = xmlDoc.CreateElement("CategoryAxisTitle");
            XmlElement cataxis_primary = xmlDoc.CreateElement("PrimaryTitle");

            catAxisTitle.AppendChild(cataxis_primary);
            cataxis_primary.InnerText = sg.CategoryAxis.Title;
            titles.AppendChild(catAxisTitle);

            //// value axis titles
            XmlElement valAxisTitle    = xmlDoc.CreateElement("ValueAxisTitle");
            XmlElement valAxis_primary = xmlDoc.CreateElement("PrimaryTitle");

            valAxisTitle.AppendChild(valAxis_primary);
            valAxis_primary.InnerText = sg.ValueAxis.Title;
            titles.AppendChild(valAxisTitle);

            /*
             * add category axis
             */

            XmlElement category_axis = xmlDoc.CreateElement("CategoryAxis");

            category_axis.SetAttribute("crossesAt",
                                       sg.CategoryAxis.Origin.ToString());

            XmlElement primaryCats   = xmlDoc.CreateElement("PrimaryCategory");
            XmlElement secondaryCats = xmlDoc.CreateElement("SecondaryCategory");
            XmlElement categoryset   = xmlDoc.CreateElement("CategorySet");

            List <string> category_values = sg.CategoryAxis.PrimaryCategories;

            int counter = 0;

            foreach (string s in category_values)
            {
                XmlElement cat_element = xmlDoc.CreateElement("Category");
                cat_element.InnerText = s.ToString();
                cat_element.SetAttribute("id", counter.ToString());
                categoryset.AppendChild(cat_element);
                counter++;
            }

            primaryCats.AppendChild(categoryset);
            category_axis.AppendChild(primaryCats);
            axis.AppendChild(category_axis);


            if (sg.CategoryAxis.SecondaryCategories != null)
            {
                List <string> sec_category_values = sg.CategoryAxis.SecondaryCategories;
                XmlElement    seccategoryset      = xmlDoc.CreateElement("CategorySet");
                int           ctr = 0;
                foreach (string s in sec_category_values)
                {
                    XmlElement sec_cat_element = xmlDoc.CreateElement("Category");
                    sec_cat_element.InnerText = s.ToString();
                    sec_cat_element.SetAttribute("id", ctr.ToString());
                    seccategoryset.AppendChild(sec_cat_element);
                    ctr++;
                }
                secondaryCats.AppendChild(seccategoryset);
                category_axis.AppendChild(secondaryCats);
                axis.AppendChild(category_axis);
            }

            /*
             * Add category axis. I could've made the properties StartsAt, EndsAt and
             * Stepping in ValueAxis as strings, so I could have avoided the toString
             * method here, but it is probable I will need the "double" datatype for
             * certain calculations, while writing the object to xml is a one-time
             * pass. (I have to improve this argument...)
             */

            XmlElement valueAxis = xmlDoc.CreateElement("ValueAxis");

            valueAxis.SetAttribute("startsAt", sg.ValueAxis.StartsAt.ToString());
            valueAxis.SetAttribute("endAt", sg.ValueAxis.EndsAt.ToString());
            valueAxis.SetAttribute("step", sg.ValueAxis.Stepping.ToString());
            axis.AppendChild(valueAxis);

            /*
             * Add series
             */

            SGSeriesCollection graph_series = sg.Series;

            foreach (SGSeries s in graph_series)
            {
                XmlElement series_elem = xmlDoc.CreateElement("Series");
                XmlElement valueSet    = xmlDoc.CreateElement("ValueSet");
                series_elem.SetAttribute("id", s.ID.ToString());
                series_elem.SetAttribute("type", s.Type.ToString());
                series_elem.SetAttribute("name", s.Name);
                graph.AppendChild(series_elem);

                //add the values
                int vctr = 0;

                foreach (object val in s.Values)
                {
                    XmlElement value_elem = xmlDoc.CreateElement("value");
                    value_elem.SetAttribute("id", vctr.ToString());
                    if (val != null)
                    {
                        value_elem.InnerText = val.ToString();
                    }
                    else
                    {
                        value_elem.InnerText = "none";
                    }
                    valueSet.AppendChild(value_elem);
                    vctr++;
                }

                series_elem.AppendChild(valueSet);
            }

            /*
             * Add Boxes
             */
            XmlElement contentBoxes = xmlDoc.CreateElement("ContentBoxes");

            foreach (SGTextBox box in sg.Textboxes)
            {
                XmlElement bx = xmlDoc.CreateElement("box");
                bx.SetAttribute("id", box.ID.ToString());
                bx.SetAttribute("posX", box.Geometry.PosX.ToString());
                bx.SetAttribute("posY", box.Geometry.PosY.ToString());
                bx.SetAttribute("width", box.BoxSize.Width.ToString());
                bx.SetAttribute("height", box.BoxSize.Height.ToString());
                //bx.SetAttribute("fontBold", "??");
                //bx.SetAttribute("fontItalics", "??");
                //bx.SetAttribute("fontSize", "??");
                bx.InnerText = box.Text;
                contentBoxes.AppendChild(bx);
            }

            graph.AppendChild(contentBoxes);

            /*
             * Add the geometry of the whole graph and the plot area
             */

            XmlElement geom_elem     = xmlDoc.CreateElement("Geometry");
            XmlElement plotArea_elem = xmlDoc.CreateElement("PlotAreaGeometry");

            plotArea_elem.SetAttribute("height",
                                       sg.PlotArea.Geometry.Height.ToString());
            plotArea_elem.SetAttribute("width",
                                       sg.PlotArea.Geometry.Width.ToString());
            plotArea_elem.SetAttribute("posX", sg.PlotArea.Geometry.PosX.ToString());
            plotArea_elem.SetAttribute("posY", sg.PlotArea.Geometry.PosY.ToString());

            XmlElement graph_geom_elem = xmlDoc.CreateElement("GraphGeometry");

            graph_geom_elem.SetAttribute("height",
                                         sg.Prologue.GetGraphHeight().ToString());
            graph_geom_elem.SetAttribute("width",
                                         sg.Prologue.GetGraphWidth().ToString());

            geom_elem.AppendChild(graph_geom_elem);
            geom_elem.AppendChild(plotArea_elem);
            graph.AppendChild(geom_elem);

            XmlElement schema_elem = xmlDoc.CreateElement("VisualSchema");

            XmlElement slope_elem = xmlDoc.CreateElement("VisualSlope");

            double slope        = 0;
            double slope_degree = 0;
            double hg           = sg.Prologue.GetGraphHeight();
            double wd           = sg.Prologue.GetGraphWidth();
            double r            = sg.ValueAxis.EndsAt - sg.ValueAxis.StartsAt;
            int    C            = sg.CategoryAxis.PrimaryCategories.Count - 1;

            foreach (SGSeries s in graph_series)
            {
                XmlElement serieslope_elem = xmlDoc.CreateElement("Series");
                serieslope_elem.SetAttribute("id", s.ID.ToString());

                for (int i = 0; i < s.Values.Count - 1; i++)
                {
                    XmlElement slopevalue_elem = xmlDoc.CreateElement("value");
                    slopevalue_elem.SetAttribute("id", i.ToString());
                    slopevalue_elem.SetAttribute("from", i.ToString());
                    slopevalue_elem.SetAttribute("to", (i + 1).ToString());

                    if (s.Values[i] != null && s.Values[i + 1] != null)
                    {
                        double deltaY = (double)s.Values[i + 1] - (double)s.Values[i];

                        //CALCULAR Visual SLOPE!
                        slope        = Math.Atan(((hg / r) * deltaY) / (wd / C));
                        slope_degree = (180 / Math.PI) * slope;
                        slopevalue_elem.InnerText = slope.ToString();
                        slopevalue_elem.SetAttribute("degree", slope_degree.ToString());
                    }
                    else
                    {
                        slopevalue_elem.InnerText = "none";
                    }
                    serieslope_elem.AppendChild(slopevalue_elem);
                }
                slope_elem.AppendChild(serieslope_elem);
            }

            schema_elem.AppendChild(slope_elem);
            graph.AppendChild(schema_elem);

            // save xml
            xmlDoc.Save(w);
            w.Close();
        }