[gnumeric] ODF: handle regression line bounds



commit 09b2379009f63743fa95e6015aab4594bc03e76a
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Fri Aug 27 14:20:36 2010 -0600

    ODF: handle regression line bounds
    
    2010-08-28  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* openoffice-read.c (odf_store_data): new
    	(od_series_regression): handle upper and lower bounds of
    	  regression line
    	* openoffice-write.c (odf_write_data_element): new
    	(odf_write_data_attribute): new
    	(odf_write_standard_series): use the above and write upper and
    	  lower bounds of regression line

 plugins/openoffice/ChangeLog          |   10 ++
 plugins/openoffice/openoffice-read.c  |   32 +++++
 plugins/openoffice/openoffice-write.c |  200 +++++++++++++++++++--------------
 3 files changed, 156 insertions(+), 86 deletions(-)
---
diff --git a/plugins/openoffice/ChangeLog b/plugins/openoffice/ChangeLog
index aa03b70..00a22ba 100644
--- a/plugins/openoffice/ChangeLog
+++ b/plugins/openoffice/ChangeLog
@@ -1,3 +1,13 @@
+2010-08-28  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* openoffice-read.c (odf_store_data): new
+	(od_series_regression): handle upper and lower bounds of 
+	  regression line
+	* openoffice-write.c (odf_write_data_element): new
+	(odf_write_data_attribute): new
+	(odf_write_standard_series): use the above and write upper and
+	  lower bounds of regression line
+	
 2010-08-27  Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* openoffice-read.c (oo_prop_list_free): use go_slist_free_custom
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index 3b605ce..bf40304 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -4902,16 +4902,45 @@ od_series_reg_equation (GsfXMLIn *xin, xmlChar const **attrs)
 }
 
 static void
+odf_store_data (OOParseState *state, gchar const *str, GogObject *obj, int dim)
+{
+	if (str != NULL) {
+		GnmParsePos pp;
+		GnmRangeRef ref;
+		char const *ptr = oo_rangeref_parse 
+			(&ref, CXML2C (str),
+			 parse_pos_init (&pp, state->pos.wb, NULL, 0, 0));
+		if (ptr != CXML2C (str)) {
+			GnmValue *v = value_new_cellrange (&ref.a, &ref.b, 0, 0);
+			GnmExprTop const *texpr = gnm_expr_top_new_constant (v);
+			if (NULL != texpr) {
+				gog_dataset_set_dim (GOG_DATASET (obj), dim,
+						     gnm_go_data_scalar_new_expr 
+						     (state->pos.sheet, texpr),
+						     NULL);
+			}
+		}
+	}
+}
+
+static void
 od_series_regression (GsfXMLIn *xin, xmlChar const **attrs)
 {
 	OOParseState *state = (OOParseState *)xin->user_state;
 	char const *style_name = NULL;
+	gchar const *lower_bd = NULL;
+	gchar const *upper_bd = NULL;
 
 	state->chart.regression = NULL;
 
 	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
 		if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), OO_NS_CHART, "style-name"))
 			style_name = CXML2C (attrs[1]);
+		else if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), OO_GNUM_NS_EXT, "lower-bound"))
+			lower_bd = CXML2C (attrs[1]);
+		else if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), OO_GNUM_NS_EXT, "upper-bound"))
+			upper_bd = CXML2C (attrs[1]);
+
 	if (style_name != NULL) {
 		GSList *l;
 		OOChartStyle *chart_style = g_hash_table_lookup
@@ -4958,6 +4987,9 @@ od_series_regression (GsfXMLIn *xin, xmlChar const **attrs)
 			odf_apply_style_props (chart_style->style_props, style);
 			g_object_unref (style);			
 		}
+
+		odf_store_data (state, lower_bd, regression , 0);
+		odf_store_data (state, upper_bd, regression , 1);
 	}	
 }
 
diff --git a/plugins/openoffice/openoffice-write.c b/plugins/openoffice/openoffice-write.c
index c0ac081..1a40063 100644
--- a/plugins/openoffice/openoffice-write.c
+++ b/plugins/openoffice/openoffice-write.c
@@ -3684,6 +3684,38 @@ odf_write_drop_line (GnmOOExport *state, GogObject const *series, char const *dr
 }
 
 
+static gboolean
+odf_write_data_element (GnmOOExport *state, GOData const *data, GnmParsePos *pp,
+			char const *element, char const *attribute)
+{
+	GnmExprTop const *texpr = gnm_go_data_get_expr (data);
+
+	if (NULL != texpr) {
+		char *str = gnm_expr_top_as_string (texpr, pp, state->conv);
+		gsf_xml_out_start_element (state->xml, element);
+		gsf_xml_out_add_cstr (state->xml, attribute,
+				      odf_strip_brackets (str));
+		g_free (str);
+		return TRUE;
+	}
+	return FALSE;
+}
+
+static void
+odf_write_data_attribute (GnmOOExport *state, GOData const *data, GnmParsePos *pp,
+			  char const *attribute)
+{
+	GnmExprTop const *texpr = gnm_go_data_get_expr (data);
+
+	if (NULL != texpr) {
+		char *str = gnm_expr_top_as_string (texpr, pp, state->conv);
+		gsf_xml_out_add_cstr (state->xml, attribute,
+				      odf_strip_brackets (str));
+		g_free (str);
+	}
+}
+
+
 static void
 odf_write_standard_series (GnmOOExport *state, GSList const *series)
 {
@@ -3693,98 +3725,94 @@ odf_write_standard_series (GnmOOExport *state, GSList const *series)
 
 	for (i = 1; NULL != series ; series = series->next, i++) {
 		GOData const *dat = gog_dataset_get_dim (GOG_DATASET (series->data), GOG_MS_DIM_VALUES);
-		if (NULL != dat) {
-			GnmExprTop const *texpr = gnm_go_data_get_expr (dat);
-			if (NULL != texpr) {
-				GogObjectRole const *role = 
-					gog_object_find_role_by_name 
-					(GOG_OBJECT (series->data), "Regression curve");
-
-				char *str = gnm_expr_top_as_string (texpr, &pp, state->conv);
-				GOData const *cat = gog_dataset_get_dim (GOG_DATASET (series->data), 
-									 GOG_MS_DIM_LABELS);
-				gsf_xml_out_start_element (state->xml, CHART "series");
-				gsf_xml_out_add_cstr (state->xml, CHART "values-cell-range-address",
-						      odf_strip_brackets (str));
-				g_free (str);
-				str = g_strdup_printf ("series%i", i);
-				gsf_xml_out_add_cstr (state->xml, CHART "style-name", str);
-				g_free (str);
-
-				odf_write_label_cell_address 
-					(state, gog_series_get_name (GOG_SERIES (series->data)));
-
-				if (NULL != cat) {
-					texpr = gnm_go_data_get_expr (cat);
-					if (NULL != texpr) {
-						str = gnm_expr_top_as_string (texpr, &pp, state->conv);
-						gsf_xml_out_start_element (state->xml, CHART "domain");
-						gsf_xml_out_add_cstr (state->xml, TABLE "cell-range-address",
-								      odf_strip_brackets (str));
-						gsf_xml_out_end_element (state->xml); /* </chart:domain> */
-						g_free (str);
+		if (NULL != dat && odf_write_data_element (state, dat, &pp, CHART "series",
+							   CHART "values-cell-range-address")) {
+			GogObjectRole const *role = 
+				gog_object_find_role_by_name 
+				(GOG_OBJECT (series->data), "Regression curve");
+			
+			GOData const *cat = gog_dataset_get_dim (GOG_DATASET (series->data), 
+								 GOG_MS_DIM_LABELS);
+			char *str = g_strdup_printf ("series%i", i);
+			gsf_xml_out_add_cstr (state->xml, CHART "style-name", str);
+			g_free (str);
+			
+			odf_write_label_cell_address 
+				(state, gog_series_get_name (GOG_SERIES (series->data)));
+			
+			if (NULL != cat && odf_write_data_element (state, cat, &pp, CHART "domain", 
+								   TABLE "cell-range-address"))
+				gsf_xml_out_end_element (state->xml); /* </chart:domain> */
+			
+			if (role != NULL) {
+				GSList *l, *regressions = gog_object_get_children 
+					(GOG_OBJECT (series->data), role);
+				for (l = regressions; l != NULL && l->data != NULL; l = l->next) {
+					GOData const *bd;
+					GogObject const *regression = l->data;
+					GogObject const *equation 
+						= gog_object_get_child_by_name (regression, "Equation");
+					str = odf_get_gog_style_name_from_obj 
+						(GOG_OBJECT (regression));
+					gsf_xml_out_start_element 
+						(state->xml, 
+						 (l == regressions) ? CHART "regression-curve" 
+						 : GNMSTYLE "regression-curve");
+					gsf_xml_out_add_cstr (state->xml, CHART "style-name", str);
+					
+					if (state->with_extension) {
+						/* Upper and lower bounds */
+						bd = gog_dataset_get_dim (GOG_DATASET (regression), 0);
+						if (bd != NULL)
+							odf_write_data_attribute 
+								(state, bd, &pp, GNMSTYLE "lower-bound");
+						bd = gog_dataset_get_dim (GOG_DATASET (regression), 1);
+						if (bd != NULL) 
+							odf_write_data_attribute 
+								(state, bd, &pp, GNMSTYLE "upper-bound");
 					}
-				}
-
-				
-				if (role != NULL) {
-					GSList *l, *regressions = gog_object_get_children 
-						(GOG_OBJECT (series->data), role);
-					for (l = regressions; l != NULL && l->data != NULL; l = l->next) {
-						GogObject const *regression = l->data;
-						GogObject const *equation 
-							= gog_object_get_child_by_name (regression, "Equation");
-						str = odf_get_gog_style_name_from_obj 
-							(GOG_OBJECT (regression));
+					if (equation != NULL) {
+						GObjectClass *klass = G_OBJECT_GET_CLASS (equation);
+						char const *eq_element, *eq_automatic, *eq_display, *eq_r;
+						if (get_gsf_odf_version () > 101) {
+							eq_element = CHART "equation";
+							eq_automatic = CHART "automatic-content";
+							eq_display = CHART "display-equation";
+							eq_r = CHART "display-r-square";
+						} else {
+							eq_element = GNMSTYLE "equation";
+							eq_automatic = GNMSTYLE "automatic-content";
+							eq_display = GNMSTYLE "display-equation";
+							eq_r = GNMSTYLE "display-r-square";
+						}
 						gsf_xml_out_start_element 
-							(state->xml, 
-							 (l == regressions) ? CHART "regression-curve" 
-							 : GNMSTYLE "regression-curve");
+							(state->xml, eq_element);
+						odf_add_bool (state->xml, eq_automatic, TRUE);
+						odf_write_plot_style_bool (state->xml, equation, klass,
+									   "show-eq", eq_display);
+						odf_write_plot_style_bool (state->xml, equation, klass,
+									   "show-r2", eq_r);
+						str = odf_get_gog_style_name_from_obj 
+							(GOG_OBJECT (equation));
 						gsf_xml_out_add_cstr (state->xml, CHART "style-name", str);
-						
-						if (equation != NULL) {
-							GObjectClass *klass = G_OBJECT_GET_CLASS (equation);
-							char const *eq_element, *eq_automatic, *eq_display, *eq_r;
-							if (get_gsf_odf_version () > 101) {
-								eq_element = CHART "equation";
-								eq_automatic = CHART "automatic-content";
-								eq_display = CHART "display-equation";
-								eq_r = CHART "display-r-square";
-							} else {
-								eq_element = GNMSTYLE "equation";
-								eq_automatic = GNMSTYLE "automatic-content";
-								eq_display = GNMSTYLE "display-equation";
-								eq_r = GNMSTYLE "display-r-square";
-							}
-							gsf_xml_out_start_element 
-								(state->xml, eq_element);
-							odf_add_bool (state->xml, eq_automatic, TRUE);
-							odf_write_plot_style_bool (state->xml, equation, klass,
-										   "show-eq", eq_display);
-							odf_write_plot_style_bool (state->xml, equation, klass,
-										   "show-r2", eq_r);
-							str = odf_get_gog_style_name_from_obj 
-								(GOG_OBJECT (equation));
-							gsf_xml_out_add_cstr (state->xml, CHART "style-name", str);
-							odf_write_gog_position (state, equation);
-							gsf_xml_out_end_element (state->xml); /* </chart:equation> */
-						}
-
-						gsf_xml_out_end_element (state->xml); /* </chart:regression-curve> */
-						g_free (str);
+						odf_write_gog_position (state, equation);
+						gsf_xml_out_end_element (state->xml); /* </chart:equation> */
 					}
+					
+					gsf_xml_out_end_element (state->xml); /* </chart:regression-curve> */
+					g_free (str);
 				}
-
-				if (state->with_extension) {
-					odf_write_drop_line (state, GOG_OBJECT (series->data), 
-							     "Horizontal drop lines", FALSE);
-					odf_write_drop_line (state, GOG_OBJECT (series->data), 
-							     "Vertical drop lines", TRUE);
-					odf_write_drop_line (state, GOG_OBJECT (series->data), 
-							     "Drop lines", TRUE);
-				}
-				gsf_xml_out_end_element (state->xml); /* </chart:series> */
 			}
+			
+			if (state->with_extension) {
+				odf_write_drop_line (state, GOG_OBJECT (series->data), 
+						     "Horizontal drop lines", FALSE);
+				odf_write_drop_line (state, GOG_OBJECT (series->data), 
+						     "Vertical drop lines", TRUE);
+				odf_write_drop_line (state, GOG_OBJECT (series->data), 
+						     "Drop lines", TRUE);
+			}
+			gsf_xml_out_end_element (state->xml); /* </chart:series> */
 		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]