private SGSeriesCollection GetSeries(Dictionary <string, object> file) { SGSeriesCollection series_collection = new SGSeriesCollection(); JObject all_series = (JObject)jo_parsed_file["series"]; foreach (var series in all_series) { SGSeries parsed_series = new SGSeries(); parsed_series.Type = 0; parsed_series.ID = 1; parsed_series.Status = 4; parsed_series.Name = series.Key; JObject values = (JObject)series.Value; List <Object> val_range = new List <Object>(); List <Object> key_range = new List <Object>(); List <Object> val_range_copy = new List <object>(); int last_value_index = -1; foreach (var data in values) { try { val_range.Add((Double)data.Value); key_range.Add((String)data.Key); val_range_copy.Add((Double)data.Value); last_value_index += 1; } catch (System.ArgumentException e) { val_range.Add("none"); val_range_copy.Add("none"); } } parsed_series.Values = val_range; parsed_series.EndsAt = (Double)val_range.OfType <Double>().Last(); parsed_series.CategoryEndsAt = Convert.ToDouble(key_range[last_value_index]); parsed_series.CategoryStartsAt = Convert.ToDouble(key_range.First()); parsed_series.StartsAt = (Double)val_range.OfType <Double>().First(); parsed_series.Trend = (string)jo_parsed_file["trends"][series.Key]; parsed_series.ValuesIncludingNones = val_range_copy; // add series to collection series_collection.Add(parsed_series); } return(series_collection); }
/*************************************************************************** * Series are dealt with here. This is a very error prone method. **************************************************************************/ private SGSeriesCollection GetXLSeries(Chart chart) { SGSeriesCollection sg_series_collection = new SGSeriesCollection(); IEnumerator series_enumerator = ((SeriesCollection) chart.SeriesCollection(Type.Missing)).GetEnumerator(); int ctr = 0; // iterate through all the boxes in the chart area. while (series_enumerator.MoveNext()) { SGSeries sg_series = new SGSeries(); Series xl_series = (Series)series_enumerator.Current; sg_series.ID = ctr; /////////////////////////////////////// // \internal find if there is a type (line, bar, etc.) ////////////////////////////////////// try { sg_series.Type = xl_series.Type; } catch { sg_series.Type = -1; log.Warn("Couldn't get the type of the series." + "\n\tThis must be fixed!"); continue; } /////////////////////////////////////// // find if there is a name ////////////////////////////////////// if (!String.IsNullOrEmpty(xl_series.Name)) { sg_series.Name = xl_series.Name; } else { log.Warn("Couldn't find the name of the series." + "\n\tSetting it to: \"none\"."); sg_series.Name = "none"; } /////////////////////////////////////// // find if there are values ////////////////////////////////////// try { List <object> vals = new List <object>(); //vals.AddRange(((Array)xl_series.Values).Cast<object>()); foreach (object c in (Array)xl_series.Values) { if (c != null && c.ToString().Length != 0) { vals.Add(c); } else { vals.Add(""); } } sg_series.Values = vals; } catch { log.Warn("Couldn't find the values for the series." + "\n\tSetting it to: \"none\"."); continue; } sg_series_collection.Add(sg_series); log.Debug(sg_series.ToString() + "."); ctr++; } log.Debug(ctr + " legal series found and added to the list."); return(sg_series_collection); }
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(); }