[goffice] Implement support of percent in bar/col data labels.



commit 03e781c3ef0f8fcb94ebb8d238594d46bff6090d
Author: Jean Brefort <jean brefort normalesup org>
Date:   Wed Nov 9 15:56:23 2011 +0100

    Implement support of percent in bar/col data labels.

 ChangeLog                         |   16 ++++++++++++++++
 goffice/graph/gog-plot-impl.h     |    2 +-
 goffice/graph/gog-series-impl.h   |    2 --
 goffice/graph/gog-series-labels.c |   16 ++++++++--------
 goffice/graph/gog-series.c        |   14 +-------------
 goffice/graph/gog-series.h        |    2 --
 plugins/plot_barcol/gog-1.5d.c    |   33 +++++++++++++++++++++++++++++++++
 plugins/plot_barcol/gog-1.5d.h    |    2 ++
 plugins/plot_barcol/gog-barcol.c  |    1 +
 9 files changed, 62 insertions(+), 26 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3a09bce..9ab5684 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2011-11-09  Jean Brefort  <jean brefort normalesup org>
 
+	* goffice/graph/gog-plot-impl.h: fixed macro name.
+	* goffice/graph/gog-series-impl.h: removed the get_percent_value member.
+	* goffice/graph/gog-series-labels.c (gog_data_label_update),
+	(gog_series_labels_update): really implement percent as label.
+	* goffice/graph/gog-series.c (role_series_labels_post_add),
+	(gog_series_get_interpolation_params): remove
+	gog_series_get_data_as_percent.
+	* goffice/graph/gog-series.h: ditto.
+	* plugins/plot_barcol/gog-1.5d.c (gog_plot1_5d_update),
+	(_gog_plot1_5d_get_percent_value): implement percent as labels for bar/col
+	plots.
+	* plugins/plot_barcol/gog-1.5d.h: ditto.
+	* plugins/plot_barcol/gog-barcol.c (gog_barcol_plot_class_init): ditto.
+
+2011-11-09  Jean Brefort  <jean brefort normalesup org>
+
 	* goffice/component/go-component.c (go_component_set_font): new function.
 	* goffice/component/go-component.h: ditto.
 	* goffice/utils/go-image.c (go_image_finalize),
diff --git a/goffice/graph/gog-plot-impl.h b/goffice/graph/gog-plot-impl.h
index 994b8f3..ffa640f 100644
--- a/goffice/graph/gog-plot-impl.h
+++ b/goffice/graph/gog-plot-impl.h
@@ -82,7 +82,7 @@ typedef struct {
 
 #define GOG_PLOT_CLASS(k)		(G_TYPE_CHECK_CLASS_CAST ((k), GOG_TYPE_PLOT, GogPlotClass))
 #define GOG_IS_PLOT_CLASS(k)		(G_TYPE_CHECK_CLASS_TYPE ((k), GOG_TYPE_PLOT))
-#define GOG_PLOT_ITEM_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), GOG_TYPE_PLOT, GogPlotClass))
+#define GOG_PLOT_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), GOG_TYPE_PLOT, GogPlotClass))
 
 /* protected */
 
diff --git a/goffice/graph/gog-series-impl.h b/goffice/graph/gog-series-impl.h
index 1e1fd29..a67208d 100644
--- a/goffice/graph/gog-series-impl.h
+++ b/goffice/graph/gog-series-impl.h
@@ -104,8 +104,6 @@ typedef struct {
 	unsigned    (*get_xy_data) (GogSeries const *series,
 	                            double const **x, double const **y);
 	GogDataset *(*get_interpolation_params) (GogSeries const *series);
-	gboolean    (*get_data_as_percent) (GogSeries const *series,
-	                                    double const **pc);
 
 } GogSeriesClass;
 
diff --git a/goffice/graph/gog-series-labels.c b/goffice/graph/gog-series-labels.c
index 7a03ca1..76e6cad 100644
--- a/goffice/graph/gog-series-labels.c
+++ b/goffice/graph/gog-series-labels.c
@@ -794,12 +794,12 @@ gog_data_label_update (GogObject *obj)
 				break;
 			}
 			case 'p': {
-				double const *pc;
-				if (gog_series_get_data_as_percent (series, &pc)) {
+				double pc = gog_plot_get_percent_value (series->plot, series->index, lbl->index);
+				if (go_finite (pc)) {
 					/* Note to translators: a space might be needed before '%%' in some languages */
 					/* FIXME: should the number of digits be customizable? */
-					next = g_strdup_printf (_("%.1f%%"), pc[lbl->index]);
-					g_string_append (str, next); /* this one will be replaced by the legend entry */
+					next = g_strdup_printf (_("%.1f%%"), pc);
+					g_string_append (str, next);
 					g_free (next);
 				}
 				break;
@@ -1177,7 +1177,6 @@ gog_series_labels_update (GogObject *obj)
 	}
 	if (GOG_IS_SERIES (parent)) {
 		GogSeries *series = GOG_SERIES (parent);
-		double const *pc = NULL;
 		labels->n_elts = n = gog_series_num_elements (series);
 		labels->elements = g_new0 (GogSeriesLabelElt, n);
 		override = labels->overrides;
@@ -1233,10 +1232,11 @@ gog_series_labels_update (GogObject *obj)
 							break;
 						}
 						case 'p': {
-							if (pc || gog_series_get_data_as_percent (series, &pc)) {
-								/* Note to translators: a space might be needed before '%%' in some languages */
+							double pc = gog_plot_get_percent_value (series->plot, series->index, i);
+							if (go_finite (pc)) {
+								/* Note to translators: a space might be needed before '%%' in someo_is_finie languages */
 								/* FIXME: should the number of digits be customizable? */
-								next = g_strdup_printf (_("%.1f%%"), pc[i]);
+								next = g_strdup_printf (_("%.1f%%"), pc);
 								g_string_append (str, next); /* this one will be replaced by the legend entry */
 								g_free (next);
 							}
diff --git a/goffice/graph/gog-series.c b/goffice/graph/gog-series.c
index 1e26685..6d5d52d 100644
--- a/goffice/graph/gog-series.c
+++ b/goffice/graph/gog-series.c
@@ -327,7 +327,7 @@ role_series_labels_post_add (GogObject *parent, GogObject *child)
 	labels->format = (i != desc->num_dim)?
 		                            g_strdup_printf ("%%%u", i):
 		                            g_strdup ("");
-	labels->supports_percent = GOG_SERIES_GET_CLASS (series)->get_data_as_percent != NULL;
+	labels->supports_percent = GOG_PLOT_GET_CLASS (series->plot)->get_percent != NULL;
 }
 
 static void
@@ -1276,15 +1276,3 @@ gog_series_get_interpolation_params (GogSeries const *series)
 			series_klass->get_interpolation_params (series):
 			NULL;
 }
-
-gboolean
-gog_series_get_data_as_percent (GogSeries const *series, double const **pc)
-{
-	GogSeriesClass *series_klass;
-
-	g_return_val_if_fail (GOG_IS_SERIES (series), FALSE);
-	series_klass = GOG_SERIES_GET_CLASS (series);
-	return (series_klass->get_data_as_percent)?
-			series_klass->get_data_as_percent (series, pc):
-			FALSE;
-}
diff --git a/goffice/graph/gog-series.h b/goffice/graph/gog-series.h
index 0700306..51dceca 100644
--- a/goffice/graph/gog-series.h
+++ b/goffice/graph/gog-series.h
@@ -74,8 +74,6 @@ unsigned	  gog_series_get_xyz_data  	(GogSeries const *series,
 						 double const **x,
 						 double const **y,
 						 double const **z);
-gboolean	  gog_series_get_data_as_percent (GogSeries const *series,
-						  double const **pc);
 
 GogSeriesFillType gog_series_get_fill_type 	(GogSeries const *series);
 void 		  gog_series_set_fill_type 	(GogSeries *series, GogSeriesFillType fill_type);
diff --git a/plugins/plot_barcol/gog-1.5d.c b/plugins/plot_barcol/gog-1.5d.c
index 9284e68..5169e63 100644
--- a/plugins/plot_barcol/gog-1.5d.c
+++ b/plugins/plot_barcol/gog-1.5d.c
@@ -168,6 +168,8 @@ gog_plot1_5d_update (GogObject *obj)
 	model->minima =  DBL_MAX;
 	model->maxima = -DBL_MAX;
 	gog_plot_1_5d_clear_formats (model);
+	g_free (model->sums);
+	model->sums = 0;
 
 	num_elements = num_series = 0;
 	for (ptr = model->base.series ; ptr != NULL ; ptr = ptr->next) {
@@ -380,6 +382,37 @@ GSF_DYNAMIC_CLASS_ABSTRACT (GogPlot1_5d, gog_plot1_5d,
 	gog_plot1_5d_class_init, gog_plot1_5d_init,
 	GOG_TYPE_PLOT)
 
+double _gog_plot1_5d_get_percent_value (GogPlot *plot, unsigned series, unsigned index)
+{ 
+	GogPlot1_5d *model = GOG_PLOT1_5D (plot);
+	GogSeries1_5d const *ser = NULL, *cur;
+	GSList *ptr;
+	if (index >= model->num_elements)
+		return go_nan;
+	if (model->sums == NULL) {
+		unsigned i, j;
+		double *vals;
+		model->sums = g_new0 (double, model->num_elements);
+		for (i = 0, ptr = plot->series; i < model->num_series; i++, ptr = ptr->next) {
+			cur = ptr->data;
+			if (i == series)
+				ser = cur;
+			if (!gog_series_is_valid (GOG_SERIES (cur)))
+				continue;
+			vals = go_data_get_values (cur->base.values[1].data);
+			for (j = 0; j < cur->base.num_elements; j++)
+				model->sums[j] += vals[j];
+		}
+		
+	} else
+		for (ptr = plot->series ; ptr != NULL ; ptr = ptr->next)
+			if (0 == series--)
+				ser = ptr->data;
+	return (ser && gog_series_is_valid (GOG_SERIES (ser)) && index < ser->base.num_elements)?
+			go_data_get_vector_value (ser->base.values[1].data, index) / model->sums[index] * 100:
+			go_nan;
+}
+
 /*****************************************************************************/
 
 static gboolean
diff --git a/plugins/plot_barcol/gog-1.5d.h b/plugins/plot_barcol/gog-1.5d.h
index eb8146e..dac8ff1 100644
--- a/plugins/plot_barcol/gog-1.5d.h
+++ b/plugins/plot_barcol/gog-1.5d.h
@@ -48,6 +48,7 @@ typedef struct {
 	unsigned int support_lines : 1;
 	GOFormat const *fmt;
 	GODateConventions const *date_conv;
+	double  *sums; /* used to evaluate percent values */
 } GogPlot1_5d;
 
 typedef struct {
@@ -70,6 +71,7 @@ GType gog_plot1_5d_get_type (void);
 void  gog_plot1_5d_register_type (GTypeModule *module);
 
 GogAxis * gog_plot1_5d_get_index_axis (GogPlot1_5d *model);
+double _gog_plot1_5d_get_percent_value (GogPlot *plot, unsigned series, unsigned index);
 
 /***************************************************************************/
 
diff --git a/plugins/plot_barcol/gog-barcol.c b/plugins/plot_barcol/gog-barcol.c
index bb0183e..0380872 100644
--- a/plugins/plot_barcol/gog-barcol.c
+++ b/plugins/plot_barcol/gog-barcol.c
@@ -289,6 +289,7 @@ gog_barcol_plot_class_init (GogPlot1_5dClass *gog_plot_1_5d_klass)
 	plot_klass->desc.series.style_fields	= GO_STYLE_OUTLINE | GO_STYLE_FILL;
 	plot_klass->series_type = gog_barcol_series_get_type ();
 	plot_klass->axis_get_bounds   		= gog_barcol_axis_get_bounds;
+	plot_klass->get_percent			= _gog_plot1_5d_get_percent_value;
 
 	gog_plot_1_5d_klass->swap_x_and_y = gog_barcol_swap_x_and_y;
 	gog_plot_1_5d_klass->update_stacked_and_percentage =



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