1 /*
2  * gog-line.c
3  *
4  * Copyright (C) 2003-2004 Emmanuel Pacaud (emmanuel.pacaud@univ-poitiers.fr)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #include <goffice/goffice-config.h>
23 #include "gog-line.h"
24 #include "gog-1.5d.h"
25 #include <goffice/graph/gog-series-lines.h>
26 #include <goffice/graph/gog-view.h>
27 #include <goffice/graph/gog-chart-map.h>
28 #include <goffice/graph/gog-renderer.h>
29 #include <goffice/graph/gog-axis.h>
30 #include <goffice/graph/gog-theme.h>
31 #include <goffice/data/go-data.h>
32 #include <goffice/math/go-math.h>
33 #include <goffice/utils/go-color.h>
34 #include <goffice/utils/go-marker.h>
35 #include <goffice/utils/go-persist.h>
36 #include <goffice/utils/go-style.h>
37 #include <goffice/utils/go-styled-object.h>
38 
39 #include <glib/gi18n-lib.h>
40 #include <gsf/gsf-impl-utils.h>
41 
42 struct _GogLinePlot {
43 	GogPlot1_5d	base;
44 	gboolean	default_style_has_markers;
45 };
46 
47 static GType gog_line_view_get_type (void);
48 
49 enum {
50 	GOG_LINE_PROP_0,
51 	GOG_LINE_PROP_DEFAULT_STYLE_HAS_MARKERS
52 };
53 
54 /*****************************************************************************/
55 
56 typedef GogSeriesElement GogLineSeriesElement;
57 typedef GogSeriesElementClass GogLineSeriesElementClass;
58 
59 #define GOG_TYPE_LINE_SERIES_ELEMENT	(gog_line_series_element_get_type ())
60 #define GOG_LINE_SERIES_ELEMENT(o)	(G_TYPE_CHECK_INSTANCE_CAST ((o), GOG_TYPE_LINE_SERIES_ELEMENT, GogLinelSeriesElement))
61 #define GOG_IS_LINE_SERIES_ELEMENT(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GOG_TYPE_LINE_SERIES_ELEMENT))
62 GType gog_line_series_element_get_type (void);
63 
64 static void
gog_line_series_element_init_style(GogStyledObject * gso,GOStyle * style)65 gog_line_series_element_init_style (GogStyledObject *gso, GOStyle *style)
66 {
67 	GogSeries const *series = GOG_SERIES (GOG_OBJECT (gso)->parent);
68 
69 	g_return_if_fail (series != NULL);
70 
71 	style->interesting_fields = GO_STYLE_MARKER;
72 	gog_theme_fillin_style (gog_object_get_theme (GOG_OBJECT (gso)),
73 		style, GOG_OBJECT (gso), GOG_SERIES_ELEMENT (gso)->index, GO_STYLE_MARKER);
74 }
75 
76 static void
gog_line_series_element_class_init(GogLineSeriesElementClass * klass)77 gog_line_series_element_class_init (GogLineSeriesElementClass *klass)
78 {
79 	GogStyledObjectClass *style_klass = (GogStyledObjectClass *) klass;
80 	style_klass->init_style	    	= gog_line_series_element_init_style;
81 }
82 
83 GSF_DYNAMIC_CLASS (GogLineSeriesElement, gog_line_series_element,
84 	gog_line_series_element_class_init, NULL,
85 	GOG_TYPE_SERIES_ELEMENT)
86 
87 /******************************************************************************/
88 
89 typedef GogView		GogLineSeriesView;
90 typedef GogViewClass	GogLineSeriesViewClass;
91 
92 #define GOG_TYPE_LINE_SERIES_VIEW	(gog_line_series_view_get_type ())
93 #define GOG_LINE_SERIES_VIEW(o)	(G_TYPE_CHECK_INSTANCE_CAST ((o), GOG_TYPE_LINE_SERIES_VIEW, GogLineSeriesView))
94 #define GOG_IS_LINE_SERIES_VIEW(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GOG_TYPE_LINE_SERIES_VIEW))
95 
96 static void
gog_line_series_view_render(GogView * view,GogViewAllocation const * bbox)97 gog_line_series_view_render (GogView *view, GogViewAllocation const *bbox)
98 {
99 	GSList *ptr;
100 	for (ptr = view->children ; ptr != NULL ; ptr = ptr->next)
101 		gog_view_render	(ptr->data, bbox);
102 }
103 
104 static void
gog_line_series_view_size_allocate(GogView * view,GogViewAllocation const * allocation)105 gog_line_series_view_size_allocate (GogView *view, GogViewAllocation const *allocation)
106 {
107 	GSList *ptr;
108 
109 	for (ptr = view->children; ptr != NULL; ptr = ptr->next)
110 		gog_view_size_allocate (GOG_VIEW (ptr->data), allocation);
111 }
112 
113 static void
gog_line_series_view_class_init(GogLineSeriesViewClass * gview_klass)114 gog_line_series_view_class_init (GogLineSeriesViewClass *gview_klass)
115 {
116 	GogViewClass *view_klass = GOG_VIEW_CLASS (gview_klass);
117 	view_klass->render = gog_line_series_view_render;
118 	view_klass->size_allocate = gog_line_series_view_size_allocate;
119 	view_klass->build_toolkit = NULL;
120 }
121 
122 GSF_DYNAMIC_CLASS (GogLineSeriesView, gog_line_series_view,
123 	gog_line_series_view_class_init, NULL,
124 	GOG_TYPE_VIEW)
125 
126 /*****************************************************************************/
127 
128 typedef struct {
129 	GogSeries1_5d base;
130 	double	clamped_derivs[2]; /* start and slopes for clamped cubic splines */
131 	GogDataset	  *interpolation_props;
132 	double *x;
133 } GogAreaSeries;
134 
135 /****************************************************************************/
136 
137 typedef struct {
138 	GogObject base;
139 	GogAreaSeries *series;
140 	GogDatasetElement *derivs;
141 } GogLineInterpolationClamps;
142 
143 typedef GogObjectClass GogLineInterpolationClampsClass;
144 
145 #define GOG_TYPE_LINE_INTERPOLATION_CLAMPS	(gog_line_interpolation_clamps_get_type ())
146 #define GOG_LINE_INTERPOLATION_CLAMPS(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GOG_TYPE_LINE_INTERPOLATION_CLAMPS, GogLineInterpolationClamps))
147 #define GOG_IS_LINE_INTERPOLATION_CLAMPS(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GOG_TYPE_LINE_INTERPOLATION_CLAMPS))
148 GType gog_line_interpolation_clamps_get_type (void);
149 
150 static GObjectClass *interp_parent_klass;
151 
152 static void
gog_line_interpolation_clamps_dataset_dims(GogDataset const * set,int * first,int * last)153 gog_line_interpolation_clamps_dataset_dims (GogDataset const *set, int *first, int *last)
154 {
155 	*first = 0;
156 	*last = 1;
157 }
158 
159 static GogDatasetElement *
gog_line_interpolation_clamps_dataset_get_elem(GogDataset const * set,int dim_i)160 gog_line_interpolation_clamps_dataset_get_elem (GogDataset const *set, int dim_i)
161 {
162 	GogLineInterpolationClamps *clamps = GOG_LINE_INTERPOLATION_CLAMPS (set);
163 	g_return_val_if_fail (2 > dim_i, NULL);
164 	g_return_val_if_fail (dim_i >= 0, NULL);
165 	return clamps->derivs + dim_i;
166 }
167 
168 static void
gog_line_interpolation_clamps_dataset_dim_changed(GogDataset * set,int dim_i)169 gog_line_interpolation_clamps_dataset_dim_changed (GogDataset *set, int dim_i)
170 {
171 	GogLineInterpolationClamps *clamps = GOG_LINE_INTERPOLATION_CLAMPS (set);
172 	clamps->series->clamped_derivs[dim_i] = (GO_IS_DATA ((clamps->derivs + dim_i)->data))?
173 		go_data_get_scalar_value ((clamps->derivs + dim_i)->data): 0.;
174 	gog_object_request_update (GOG_OBJECT (clamps->series));
175 }
176 
177 static void
gog_line_interpolation_clamps_dataset_init(GogDatasetClass * iface)178 gog_line_interpolation_clamps_dataset_init (GogDatasetClass *iface)
179 {
180 	iface->get_elem	   = gog_line_interpolation_clamps_dataset_get_elem;
181 	iface->dims	   = gog_line_interpolation_clamps_dataset_dims;
182 	iface->dim_changed = gog_line_interpolation_clamps_dataset_dim_changed;
183 }
184 
185 static void
gog_line_interpolation_clamps_finalize(GObject * obj)186 gog_line_interpolation_clamps_finalize (GObject *obj)
187 {
188 	GogLineInterpolationClamps *clamps = GOG_LINE_INTERPOLATION_CLAMPS (obj);
189 	if (clamps->derivs != NULL) {
190 		gog_dataset_finalize (GOG_DATASET (obj));
191 		g_free (clamps->derivs);
192 		clamps->derivs = NULL;
193 	}
194 	(*interp_parent_klass->finalize) (obj);
195 }
196 
197 static void
gog_line_interpolation_clamps_class_init(GObjectClass * klass)198 gog_line_interpolation_clamps_class_init (GObjectClass *klass)
199 {
200 	interp_parent_klass = g_type_class_peek_parent (klass);
201 	klass->finalize	    = gog_line_interpolation_clamps_finalize;
202 }
203 
204 static void
gog_line_interpolation_clamps_init(GogLineInterpolationClamps * clamps)205 gog_line_interpolation_clamps_init (GogLineInterpolationClamps *clamps)
206 {
207 	clamps->derivs = g_new0 (GogDatasetElement, 2);
208 }
209 
210 GSF_CLASS_FULL (GogLineInterpolationClamps, gog_line_interpolation_clamps,
211 		NULL, NULL, gog_line_interpolation_clamps_class_init, NULL,
212 		gog_line_interpolation_clamps_init, GOG_TYPE_OBJECT, 0,
213 		GSF_INTERFACE (gog_line_interpolation_clamps_dataset_init, GOG_TYPE_DATASET))
214 
215 /*****************************************************************************/
216 
217 enum {
218 	SERIES_PROP_0,
219 	SERIES_PROP_CLAMP0,
220 	SERIES_PROP_CLAMP1
221 };
222 
223 typedef GogSeries1_5dClass GogAreaSeriesClass;
224 
225 static GogStyledObjectClass *area_series_parent_klass;
226 
227 GType gog_area_series_get_type (void);
228 #define GOG_TYPE_AREA_SERIES	(gog_area_series_get_type ())
229 #define GOG_AREA_SERIES(o)	(G_TYPE_CHECK_INSTANCE_CAST ((o), GOG_TYPE_AREA_SERIES, GogAreaSeries))
230 #define GOG_IS_AREA_SERIES(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GOG_TYPE_AREA_SERIES))
231 
232 static GogDataset *
gog_area_series_get_interpolation_params(GogSeries const * series)233 gog_area_series_get_interpolation_params (GogSeries const *series)
234 {
235 	GogAreaSeries *aseries = GOG_AREA_SERIES (series);
236 	g_return_val_if_fail (aseries, NULL);
237 	return aseries->interpolation_props;
238 }
239 
240 static void
gog_area_series_update(GogObject * obj)241 gog_area_series_update (GogObject *obj)
242 {
243 	GogAreaSeries *series = GOG_AREA_SERIES (obj);
244 	GogSeries *base_series = GOG_SERIES (obj);
245 	unsigned i, nb = base_series->num_elements;
246 	GSList *ptr;
247 	(GOG_OBJECT_CLASS (area_series_parent_klass))->update (obj);
248 	if (nb != base_series->num_elements) {
249 		nb = base_series->num_elements;
250 		g_free (series->x);
251 		series->x = g_new (double, nb);
252 		for (i = 0; i < nb; i++)
253 			series->x[i] = i + 1;
254 	}
255 	/* update children */
256 	for (ptr = obj->children; ptr != NULL; ptr = ptr->next)
257 		if (!GOG_IS_SERIES_LINES (ptr->data))
258 			gog_object_request_update (GOG_OBJECT (ptr->data));
259 }
260 
261 static unsigned
gog_area_series_get_xy_data(GogSeries const * series,double const ** x,double const ** y)262 gog_area_series_get_xy_data (GogSeries const *series,
263 					double const **x, double const **y)
264 {
265 	GogAreaSeries *area_ser = GOG_AREA_SERIES (series);
266 	*x = area_ser->x;
267 	*y = go_data_get_values (series->values[1].data);
268 	return series->num_elements;
269 }
270 
271 static void
gog_area_series_set_property(GObject * obj,guint param_id,GValue const * value,GParamSpec * pspec)272 gog_area_series_set_property (GObject *obj, guint param_id,
273 			    GValue const *value, GParamSpec *pspec)
274 {
275 	GogAreaSeries *series=  GOG_AREA_SERIES (obj);
276 
277 	switch (param_id) {
278 	case SERIES_PROP_CLAMP0:
279 		gog_dataset_set_dim (series->interpolation_props, 0,
280 				     go_data_scalar_val_new (g_value_get_double (value)), NULL);
281 		break;
282 	case SERIES_PROP_CLAMP1:
283 		gog_dataset_set_dim (series->interpolation_props, 1,
284 				     go_data_scalar_val_new (g_value_get_double (value)), NULL);
285 		break;
286 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
287 		 break;
288 	}
289 }
290 
291 static void
gog_area_series_get_property(GObject * obj,guint param_id,GValue * value,GParamSpec * pspec)292 gog_area_series_get_property (GObject *obj, guint param_id,
293 			  GValue *value, GParamSpec *pspec)
294 {
295 	GogAreaSeries *series=  GOG_AREA_SERIES (obj);
296 
297 	switch (param_id) {
298 	case SERIES_PROP_CLAMP0:
299 		g_value_set_double (value, series->clamped_derivs[0]);
300 		break;
301 	case SERIES_PROP_CLAMP1:
302 		g_value_set_double (value, series->clamped_derivs[1]);
303 		break;
304 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
305 		 break;
306 	}
307 }
308 
309 static void
gog_area_series_finalize(GObject * obj)310 gog_area_series_finalize (GObject *obj)
311 {
312 	GogAreaSeries *series = GOG_AREA_SERIES (obj);
313 
314 	if (series->interpolation_props != NULL) {
315 		g_object_unref (series->interpolation_props);
316 		series->interpolation_props = NULL;
317 	}
318 
319 	g_free (series->x);
320 	series->x = NULL;
321 
322 	G_OBJECT_CLASS (area_series_parent_klass)->finalize (obj);
323 }
324 
325 static void
gog_area_series_class_init(GogStyledObjectClass * gso_klass)326 gog_area_series_class_init (GogStyledObjectClass *gso_klass)
327 {
328 	GObjectClass *obj_klass = (GObjectClass *)gso_klass;
329 	GogObjectClass *gog_klass = (GogObjectClass *)gso_klass;
330 	GogSeriesClass *series_klass = (GogSeriesClass*) gso_klass;
331 	area_series_parent_klass = g_type_class_peek_parent (gso_klass);
332 	obj_klass->finalize = gog_area_series_finalize;
333 	obj_klass->set_property = gog_area_series_set_property;
334 	obj_klass->get_property = gog_area_series_get_property;
335 	gog_klass->view_type = gog_line_series_view_get_type ();
336 	series_klass->has_interpolation = TRUE;
337 	series_klass->get_interpolation_params = gog_area_series_get_interpolation_params;
338 	gog_klass->update = gog_area_series_update;
339 	series_klass->get_xy_data = gog_area_series_get_xy_data;
340 	g_object_class_install_property (obj_klass, SERIES_PROP_CLAMP0,
341 		g_param_spec_double ("clamp0",
342 			_("Clamp at start"),
343 			_("Slope at start of the interpolated curve when using clamped spline interpolation"),
344 			-G_MAXDOUBLE, G_MAXDOUBLE, 0.,
345 			GSF_PARAM_STATIC | G_PARAM_READWRITE | GO_PARAM_PERSISTENT));
346 	g_object_class_install_property (obj_klass, SERIES_PROP_CLAMP1,
347 		g_param_spec_double ("clamp1",
348 			_("Clamp at end"),
349 			_("Slope at end of the interpolated curve when using clamped spline interpolation"),
350 			-G_MAXDOUBLE, G_MAXDOUBLE, 0.,
351 			GSF_PARAM_STATIC | G_PARAM_READWRITE | GO_PARAM_PERSISTENT));
352 }
353 
354 static void
gog_area_series_init(GogAreaSeries * series)355 gog_area_series_init (GogAreaSeries *series)
356 {
357 	g_object_set_data (G_OBJECT (series), "no-bezier-interpolation", GINT_TO_POINTER (1));
358 	series->interpolation_props = g_object_new (GOG_TYPE_LINE_INTERPOLATION_CLAMPS, NULL);
359 	GOG_LINE_INTERPOLATION_CLAMPS (series->interpolation_props)->series = series;
360 	gog_dataset_set_dim (series->interpolation_props, 0, go_data_scalar_val_new (0.), NULL);
361 	gog_dataset_set_dim (series->interpolation_props, 1, go_data_scalar_val_new (0.), NULL);
362 }
363 
364 GSF_DYNAMIC_CLASS (GogAreaSeries, gog_area_series,
365 	gog_area_series_class_init, gog_area_series_init,
366 	GOG_SERIES1_5D_TYPE)
367 
368 /*****************************************************************************/
369 
370 typedef  GogAreaSeries GogLineSeries;
371 
372 typedef GogSeries1_5dClass	GogLineSeriesClass;
373 
374 static GogStyledObjectClass *line_series_parent_klass;
375 
376 GType gog_line_series_get_type (void);
377 #define GOG_TYPE_LINE_SERIES	(gog_line_series_get_type ())
378 #define GOG_LINE_SERIES(o)	(G_TYPE_CHECK_INSTANCE_CAST ((o), GOG_TYPE_LINE_SERIES, GogLineSeries))
379 #define GOG_IS_LINE_SERIES(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), GOG_TYPE_LINE_SERIES))
380 
381 static void
gog_line_series_init_style(GogStyledObject * gso,GOStyle * style)382 gog_line_series_init_style (GogStyledObject *gso, GOStyle *style)
383 {
384 	GogSeries *series = GOG_SERIES (gso);
385 	GogLinePlot const *plot;
386 
387 	line_series_parent_klass->init_style (gso, style);
388 	if (series->plot == NULL)
389 		return;
390 
391 	plot = GOG_LINE_PLOT (series->plot);
392 
393 	if (!plot->default_style_has_markers) {
394 		style->disable_theming |= GO_STYLE_MARKER;
395 		if (style->marker.auto_shape) {
396 			GOMarker *m = go_marker_dup (style->marker.mark);
397 			go_marker_set_shape (m, GO_MARKER_NONE);
398 			go_style_set_marker (style, m);
399 		}
400 	}
401 }
402 
403 static void
gog_line_series_class_init(GogStyledObjectClass * gso_klass)404 gog_line_series_class_init (GogStyledObjectClass *gso_klass)
405 {
406 	line_series_parent_klass = g_type_class_peek_parent (gso_klass);
407 	gso_klass->init_style = gog_line_series_init_style;
408 }
409 
GSF_DYNAMIC_CLASS(GogLineSeries,gog_line_series,gog_line_series_class_init,NULL,GOG_TYPE_AREA_SERIES)410 GSF_DYNAMIC_CLASS (GogLineSeries, gog_line_series,
411 	gog_line_series_class_init, NULL,
412 	GOG_TYPE_AREA_SERIES)
413 
414 static void
415 child_added_cb (GogLinePlot *plot, GogObject *obj)
416 {
417 	/* we only accept regression curves for not stacked plots */
418 	if (GOG_IS_SERIES (obj) && plot->base.type == GOG_1_5D_NORMAL)
419 		(GOG_SERIES (obj))->acceptable_children =
420 					GOG_SERIES_ACCEPT_TREND_LINE;
421 }
422 
423 static char const *
gog_line_plot_type_name(G_GNUC_UNUSED GogObject const * item)424 gog_line_plot_type_name (G_GNUC_UNUSED GogObject const *item)
425 {
426 	/* xgettext : the base for how to name bar/col plot objects
427 	 * eg The 2nd line plot in a chart will be called
428 	 * 	PlotLine2
429 	 */
430 	return N_("PlotLine");
431 }
432 
433 static void
gog_line_update_stacked_and_percentage(GogPlot1_5d * model,double ** vals,GogErrorBar ** errors,unsigned const * lengths)434 gog_line_update_stacked_and_percentage (GogPlot1_5d *model,
435 					double **vals, GogErrorBar **errors, unsigned const *lengths)
436 {
437 	unsigned i, j;
438 	double abs_sum, minima, maxima, sum, tmp, errplus, errminus;
439 
440 	for (i = model->num_elements ; i-- > 0 ; ) {
441 		abs_sum = sum = 0.;
442 		minima =  DBL_MAX;
443 		maxima = -DBL_MAX;
444 		for (j = 0 ; j < model->num_series ; j++) {
445 			if (i >= lengths[j])
446 				continue;
447 			tmp = vals[j][i];
448 			if (!go_finite (tmp))
449 				continue;
450 			if (gog_error_bar_is_visible (errors[j])) {
451 				gog_error_bar_get_bounds (errors[j], i, &errminus, &errplus);
452 				errminus = errminus > 0. ? errminus: 0.;
453 				errplus = errplus > 0. ? errplus : 0.;
454 			} else
455 				errplus = errminus = 0.;
456 			sum += tmp;
457 			abs_sum += fabs (tmp);
458 			if (minima > sum - errminus)
459 				minima = sum - errminus;
460 			if (maxima < sum + errplus)
461 				maxima = sum + errplus;
462 		}
463 		if ((model->type == GOG_1_5D_AS_PERCENTAGE) &&
464 		    (go_sub_epsilon (abs_sum) > 0.)) {
465 			if (model->minima > minima / abs_sum)
466 				model->minima = minima / abs_sum;
467 			if (model->maxima < maxima / abs_sum)
468 				model->maxima = maxima / abs_sum;
469 		} else {
470 			if (model->minima > minima)
471 				model->minima = minima;
472 			if (model->maxima < maxima)
473 				model->maxima = maxima;
474 		}
475 	}
476 }
477 
478 static void
gog_line_set_property(GObject * obj,guint param_id,GValue const * value,GParamSpec * pspec)479 gog_line_set_property (GObject *obj, guint param_id,
480 		       GValue const *value, GParamSpec *pspec)
481 {
482 	GogLinePlot *line = GOG_LINE_PLOT (obj);
483 	switch (param_id) {
484 	case GOG_LINE_PROP_DEFAULT_STYLE_HAS_MARKERS:
485 		line->default_style_has_markers = g_value_get_boolean (value);
486 		break;
487 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
488 		 break;
489 	}
490 }
491 static void
gog_line_get_property(GObject * obj,guint param_id,GValue * value,GParamSpec * pspec)492 gog_line_get_property (GObject *obj, guint param_id,
493 		       GValue *value, GParamSpec *pspec)
494 {
495 	GogLinePlot const *line = GOG_LINE_PLOT (obj);
496 	switch (param_id) {
497 	case GOG_LINE_PROP_DEFAULT_STYLE_HAS_MARKERS:
498 		g_value_set_boolean (value, line->default_style_has_markers);
499 		break;
500 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
501 		 break;
502 	}
503 }
504 
505 static void
gog_line_plot_class_init(GogPlot1_5dClass * gog_plot_1_5d_klass)506 gog_line_plot_class_init (GogPlot1_5dClass *gog_plot_1_5d_klass)
507 {
508 	GObjectClass *gobject_klass = (GObjectClass *) gog_plot_1_5d_klass;
509 	GogObjectClass *gog_klass = (GogObjectClass *) gog_plot_1_5d_klass;
510 	GogPlotClass *plot_klass = (GogPlotClass *) gog_plot_1_5d_klass;
511 
512 	gobject_klass->set_property = gog_line_set_property;
513 	gobject_klass->get_property = gog_line_get_property;
514 
515 	g_object_class_install_property (gobject_klass, GOG_LINE_PROP_DEFAULT_STYLE_HAS_MARKERS,
516 		g_param_spec_boolean ("default-style-has-markers",
517 			_("Default markers"),
518 			_("Should the default style of a series include markers"),
519 			TRUE,
520 			GSF_PARAM_STATIC | G_PARAM_READWRITE | GO_PARAM_PERSISTENT));
521 
522 	gog_klass->type_name	= gog_line_plot_type_name;
523 	gog_klass->view_type	= gog_line_view_get_type ();
524 
525 	plot_klass->desc.series.style_fields = GO_STYLE_LINE | GO_STYLE_MARKER;
526 	plot_klass->series_type = gog_line_series_get_type ();
527 
528 	gog_plot_1_5d_klass->update_stacked_and_percentage =
529 		gog_line_update_stacked_and_percentage;
530 }
531 
532 static void
gog_line_plot_init(GogLinePlot * plot)533 gog_line_plot_init (GogLinePlot *plot)
534 {
535 	plot->default_style_has_markers = TRUE;
536 	g_signal_connect (G_OBJECT (plot), "child-added",
537 					G_CALLBACK (child_added_cb), NULL);
538 	GOG_PLOT1_5D (plot)->support_drop_lines = TRUE;
539 }
540 
GSF_DYNAMIC_CLASS(GogLinePlot,gog_line_plot,gog_line_plot_class_init,gog_line_plot_init,GOG_PLOT1_5D_TYPE)541 GSF_DYNAMIC_CLASS (GogLinePlot, gog_line_plot,
542 	gog_line_plot_class_init, gog_line_plot_init,
543 	GOG_PLOT1_5D_TYPE)
544 
545 /*****************************************************************************/
546 
547 static GogObjectClass *gog_area_plot_parent_klass;
548 
549 enum {
550 	AREA_PROP_FILL_0,
551 	AREA_PROP_FILL_BEFORE_GRID
552 };
553 
554 static void
gog_area_plot_set_property(GObject * obj,guint param_id,GValue const * value,GParamSpec * pspec)555 gog_area_plot_set_property (GObject *obj, guint param_id,
556 		     GValue const *value, GParamSpec *pspec)
557 {
558 	GogPlot *plot = GOG_PLOT (obj);
559 	switch (param_id) {
560 	case AREA_PROP_FILL_BEFORE_GRID:
561 		plot->rendering_order = (g_value_get_boolean (value))?
562 					 GOG_PLOT_RENDERING_BEFORE_GRID:
563 					 GOG_PLOT_RENDERING_LAST;
564 		gog_object_emit_changed (GOG_OBJECT (obj), FALSE);
565 		break;
566 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
567 		 break;
568 	}
569 }
570 
571 static void
gog_area_plot_get_property(GObject * obj,guint param_id,GValue * value,GParamSpec * pspec)572 gog_area_plot_get_property (GObject *obj, guint param_id,
573 		     GValue *value, GParamSpec *pspec)
574 {
575 	GogPlot *plot = GOG_PLOT (obj);
576 
577 	switch (param_id) {
578 	case AREA_PROP_FILL_BEFORE_GRID:
579 		g_value_set_boolean (value, plot->rendering_order == GOG_PLOT_RENDERING_BEFORE_GRID);
580 		break;
581 	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
582 		 break;
583 	}
584 }
585 
586 #ifdef GOFFICE_WITH_GTK
587 static void
display_before_grid_cb(GtkToggleButton * btn,GObject * obj)588 display_before_grid_cb (GtkToggleButton *btn, GObject *obj)
589 {
590 	g_object_set (obj, "before-grid", gtk_toggle_button_get_active (btn), NULL);
591 }
592 #endif
593 
594 static void
gog_area_plot_populate_editor(GogObject * obj,GOEditor * editor,GogDataAllocator * dalloc,GOCmdContext * cc)595 gog_area_plot_populate_editor (GogObject *obj,
596 			     GOEditor *editor,
597                              GogDataAllocator *dalloc,
598                              GOCmdContext *cc)
599 {
600 #ifdef GOFFICE_WITH_GTK
601 	GtkBuilder *gui =
602 		go_gtk_builder_load ("res:go:plot_barcol/gog-area-prefs.ui",
603 				    GETTEXT_PACKAGE, cc);
604 	if (gui != NULL) {
605 		GtkWidget *w = go_gtk_builder_get_widget (gui, "before-grid");
606 		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w),
607 				(GOG_PLOT (obj))->rendering_order == GOG_PLOT_RENDERING_BEFORE_GRID);
608 		g_signal_connect (G_OBJECT (w),
609 			"toggled",
610 			G_CALLBACK (display_before_grid_cb), obj);
611 		w = go_gtk_builder_get_widget (gui, "gog-area-prefs");
612 		go_editor_add_page (editor, w, _("Properties"));
613 		g_object_unref (gui);
614 	}
615 
616 #endif
617 	gog_area_plot_parent_klass->populate_editor (obj, editor, dalloc, cc);
618 };
619 
620 static char const *
gog_area_plot_type_name(G_GNUC_UNUSED GogObject const * item)621 gog_area_plot_type_name (G_GNUC_UNUSED GogObject const *item)
622 {
623 	/* xgettext : the base for how to name bar/col plot objects
624 	 * eg The 2nd line plot in a chart will be called
625 	 * 	PlotArea2
626 	 */
627 	return N_("PlotArea");
628 }
629 
630 static void
gog_area_plot_class_init(GObjectClass * gobject_klass)631 gog_area_plot_class_init (GObjectClass *gobject_klass)
632 {
633 	GogObjectClass *gog_klass = (GogObjectClass *) gobject_klass;
634 	GogPlotClass   *plot_klass = (GogPlotClass *) gobject_klass;
635 	gog_area_plot_parent_klass = g_type_class_peek_parent (gobject_klass);
636 
637 	gobject_klass->set_property = gog_area_plot_set_property;
638 	gobject_klass->get_property = gog_area_plot_get_property;
639 	g_object_class_install_property (gobject_klass, AREA_PROP_FILL_BEFORE_GRID,
640 		g_param_spec_boolean ("before-grid",
641 			_("Displayed under the grids"),
642 			_("Should the plot be displayed before the grids"),
643 			FALSE,
644 			GSF_PARAM_STATIC | G_PARAM_READWRITE | GO_PARAM_PERSISTENT));
645 
646 	plot_klass->desc.series.style_fields = GO_STYLE_OUTLINE | GO_STYLE_FILL
647 										   | GO_STYLE_INTERPOLATION;
648 	plot_klass->series_type = gog_area_series_get_type ();
649 
650 	gog_klass->populate_editor = gog_area_plot_populate_editor;
651 	gog_klass->type_name	= gog_area_plot_type_name;
652 }
653 
654 static void
gog_area_plot_init(GogPlot * plot)655 gog_area_plot_init (GogPlot *plot)
656 {
657 	plot->rendering_order = GOG_PLOT_RENDERING_BEFORE_AXIS;
658 	GOG_PLOT1_5D (plot)->support_drop_lines = TRUE;
659 }
660 
661 GSF_DYNAMIC_CLASS (GogAreaPlot, gog_area_plot,
662 	gog_area_plot_class_init, gog_area_plot_init,
663 	GOG_TYPE_LINE_PLOT)
664 
665 /*****************************************************************************/
666 
667 typedef struct {
668 	double 		x;
669 	double 		y;
670 	double		plus;
671 	double 		minus;
672 } ErrorBarData;
673 
674 typedef struct {
675 	double x;
676 	double y;
677 } Point;
678 
679 typedef GogPlotView		GogLineView;
680 typedef GogPlotViewClass	GogLineViewClass;
681 
682 static int
gog_line_view_get_data_at_point(GogPlotView * view,double x,double y,GogSeries ** series)683 gog_line_view_get_data_at_point (GogPlotView *view, double x, double y, GogSeries **series)
684 {
685 	GogPlot1_5d const *model = GOG_PLOT1_5D (((GogView *) view)->model);
686 	GogPlot1_5dType const type = model->type;
687 	GogChart *chart = GOG_CHART (view->base.model->parent);
688 	GogChartMap *chart_map;
689 	GogAxisMap *x_map, *y_map;
690 	int i = -1, j, dist, max_dist = 0, line_dist = 0;
691 	GOStyle *style;
692 	GogViewAllocation const *area;
693 	double xc, yc, value, y_zero;
694 	GSList *ptr;
695 	GogSeriesElement *gse;
696 	GList *overrides; /* not const because we call g_list_* which have no const equivalent */
697 	gssize num_series = model->num_series, num_elements = model->num_elements, *lengths;
698 	double **vals, **yvals, *xvals, sum, abs_sum;
699 	GogSeries **pseries;
700 	gboolean is_null;
701 	GOLineInterpolation *interpolations;
702 
703 	if (g_slist_length (model->base.series) < 1)
704 		return -1;
705 	area = gog_chart_view_get_plot_area (view->base.parent);
706 	chart_map = gog_chart_map_new (chart, area,
707 				       GOG_PLOT (model)->axis[GOG_AXIS_X],
708 				       GOG_PLOT (model)->axis[GOG_AXIS_Y],
709 				       NULL, FALSE);
710 	if (!gog_chart_map_is_valid (chart_map)) {
711 		gog_chart_map_free (chart_map);
712 		return -1;
713 	}
714 
715 	x_map = gog_chart_map_get_axis_map (chart_map, 0);
716 	y_map = gog_chart_map_get_axis_map (chart_map, 1);
717 	y_zero = gog_axis_map_get_baseline (y_map);
718 
719 	vals    = g_alloca (num_series * sizeof (double *));
720 	yvals   = g_alloca (num_series * sizeof (double *));
721 	lengths = g_alloca (num_series * sizeof (gssize));
722 	pseries = g_alloca (num_series * sizeof (GogSeries *));
723 	interpolations	= g_alloca (num_series * sizeof (GOLineInterpolation));
724 	xvals = g_new (double, num_elements);
725 	j = 0;
726 	for (ptr = model->base.series ; ptr != NULL ; ptr = ptr->next) {
727 		pseries[j] = ptr->data;
728 
729 		if (!gog_series_is_valid (pseries[j])) {
730 			vals[j] = NULL;
731 			lengths[j] = 0;
732 			continue;
733 		}
734 
735 		vals[j] = go_data_get_values (pseries[j]->values[1].data);
736 		yvals[j] = g_new (double, num_elements);
737 		lengths[j] = go_data_get_vector_size (pseries[j]->values[1].data);
738 		interpolations[j] = pseries[j]->interpolation;
739 		if (interpolations[j] == GO_LINE_INTERPOLATION_CLOSED_SPLINE)
740 			interpolations[j] = GO_LINE_INTERPOLATION_SPLINE;
741 		j++;
742 	}
743 	for (i = 0; i < num_elements; i++) {
744 		xvals[i] = gog_axis_map_to_view (x_map, i + 1);
745 		sum = abs_sum = 0.0;
746 		if (type == GOG_1_5D_AS_PERCENTAGE) {
747 			for (j = 0; j < num_series; j++)
748 				if (vals[j]  && i < lengths[j] && gog_axis_map_finite (y_map, vals[j][i]))
749 					abs_sum += fabs (vals[j][i]);
750 			is_null = (go_sub_epsilon (abs_sum) <= 0.);
751 		} else
752 			is_null = TRUE;
753 
754 		for (j = 0; j < num_series; j++) {
755 			if (i >= lengths[j])
756 				continue;
757 
758 			if (vals[j] && gog_axis_map_finite (y_map, vals[j][i])) {
759 				value = vals[j][i];
760 				sum += value;
761 			} else
762 				value = go_nan;
763 
764 			switch (type) {
765 				case GOG_1_5D_NORMAL:
766 					yvals[j][i] = gog_axis_map_finite (y_map, value)?
767 								  value: go_nan;
768 					break;
769 
770 				case GOG_1_5D_STACKED:
771 					yvals[j][i] = gog_axis_map_finite (y_map, sum)?
772 								  sum: go_nan;
773 					break;
774 
775 				case GOG_1_5D_AS_PERCENTAGE:
776 					if (!go_finite (value))
777 						yvals[j][i] = go_nan;
778 					else if (is_null)
779 						yvals[j][i] = y_zero;
780 					else
781 					yvals[j][i] = gog_axis_map_finite (y_map, sum)?
782 						 		  sum  / abs_sum:
783 						 		  go_nan;
784 					break;
785 			}
786 		}
787 	}
788 	if (GOG_IS_PLOT_AREA (model)) {
789 		cairo_surface_t *surface;
790 		cairo_t *cr;
791 		GOPath *path;
792 		int start, end, step;
793 
794 		surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1 , 1);
795 		cr = cairo_create (surface);
796 		i = -1;
797 		if (type == GOG_1_5D_NORMAL) {
798 			start = num_series - 1;
799 			end = -1;
800 			step = -1;
801 		} else {
802 			start = 0;
803 			end = num_series;
804 			step = 1;
805 		}
806 		for (j = start; j != end; j += step) {
807 			if (lengths[j] <= 0)
808 				continue;
809 			path = gog_chart_map_make_path (chart_map, NULL, yvals[j], lengths[j],
810 				                            interpolations[j], FALSE,
811 				                            &GOG_AREA_SERIES (pseries[j])->clamped_derivs);
812 
813 			go_path_line_to (path, gog_axis_map_to_view (x_map, lengths[j]), y_zero);
814 			go_path_line_to (path, gog_axis_map_to_view (x_map, 1), y_zero);
815 			go_path_close (path);
816 			go_path_to_cairo (path, GO_PATH_DIRECTION_FORWARD, cr);
817 			go_path_free (path);
818 			if (cairo_in_fill (cr, x, y)) {
819 				*series = pseries[j];
820 				i = gog_axis_map_from_view (x_map, x) - 1;
821 				break;
822 			}
823 			cairo_new_path (cr);
824 		}
825 		cairo_surface_destroy (surface);
826 		cairo_destroy (cr);
827 	} else { /* GogLinePlot */
828 		for (j = num_series -1; j >= 0; j--) {
829 			if (lengths[j] == 0)
830 				continue;
831 			style = go_styled_object_get_style (GO_STYLED_OBJECT (pseries[j]));
832 			if (go_style_is_line_visible (style))
833 				line_dist = ceil (style->line.width / 2);
834 			if (go_style_is_marker_visible (style))
835 				max_dist = (go_marker_get_size (style->marker.mark) + 1) / 2;
836 			else if (go_style_is_line_visible (style))
837 				max_dist = line_dist;
838 			else
839 				max_dist = 0;
840 			overrides = g_list_last ((GList *) gog_series_get_overrides (GOG_SERIES (pseries[j])));
841 			for (i = lengths[j] - 1; i >= 0; i--) {
842 				xc = xvals[i];
843 				yc = gog_axis_map_to_view (y_map, yvals[j][i]);
844 				if (!go_finite (xc) || !go_finite (yc))
845 					continue;
846 				xc = fabs (xc - x);
847 				yc = fabs (yc - y);
848 				dist = MAX (xc, yc);
849 				gse = NULL;
850 				while (overrides &&
851 				       GOG_SERIES_ELEMENT (overrides->data)->index > (unsigned) i)
852 					overrides = g_list_previous (overrides);
853 				if (overrides &&
854 				    GOG_SERIES_ELEMENT (overrides->data)->index == (unsigned) i) {
855 					gse = GOG_SERIES_ELEMENT (overrides->data);
856 					overrides = g_list_previous (overrides);
857 					style = go_styled_object_get_style (GO_STYLED_OBJECT (gse));
858 					if (go_style_is_marker_visible (style)) {
859 						if (dist <= (go_marker_get_size (style->marker.mark) + 1) / 2) {
860 							*series = pseries[j];
861 							break;
862 						}
863 					} else if (dist <= line_dist) {
864 						*series = pseries[j];
865 						break;
866 					}
867 				}
868 				if (gse == NULL && dist <= max_dist) {
869 					*series = pseries[j];
870 					break;
871 				}
872 			}
873 			if (i >= 0)
874 				break;
875 		}
876 		for (j = 0; j < num_series; j++)
877 			g_free (yvals[j]);
878 	}
879 
880 	gog_chart_map_free (chart_map);
881 	return i;
882 }
883 
884 static void
gog_line_view_render(GogView * view,GogViewAllocation const * bbox)885 gog_line_view_render (GogView *view, GogViewAllocation const *bbox)
886 {
887 	GogPlot1_5d const *model = GOG_PLOT1_5D (view->model);
888 	GogPlot1_5dType const type = model->type;
889 	GogSeries1_5d const **series;
890 	GogSeries const *base_series;
891 	GogChart *chart = GOG_CHART (view->model->parent);
892 	GogChartMap *chart_map;
893 	GogViewAllocation const *area;
894 	unsigned i, j;
895 	unsigned num_elements = model->num_elements;
896 	unsigned num_series = model->num_series;
897 	GSList *ptr;
898 	double plus, minus;
899 
900 	double **vals, **yvals;
901 	ErrorBarData **error_data;
902 	GOStyle **styles;
903 	unsigned *lengths;
904 	GOPath **drop_paths;
905 	Point **points = NULL;
906 	GogErrorBar **errors;
907 	GogObjectRole const *role = NULL;
908 	GogSeriesLines **lines;
909 	GOLineInterpolation *interpolations;
910 
911 	double y_zero, y_top, y_bottom, drop_lines_y_min, drop_lines_y_max;
912 	double abs_sum, sum, value, x, y = 0.;
913 	gboolean is_null, is_area_plot;
914 
915 	GogAxisMap *x_map, *y_map;
916 
917 	GogSeriesElement *gse;
918 	GList const *overrides;
919 
920 	is_area_plot = GOG_IS_PLOT_AREA (model);
921 
922 	if (num_elements <= 0 || num_series <= 0)
923 		return;
924 
925 	area = gog_chart_view_get_plot_area (view->parent);
926 	chart_map = gog_chart_map_new (chart, area,
927 				       GOG_PLOT (model)->axis[GOG_AXIS_X],
928 				       GOG_PLOT (model)->axis[GOG_AXIS_Y],
929 				       NULL, FALSE);
930 	if (!gog_chart_map_is_valid (chart_map)) {
931 		gog_chart_map_free (chart_map);
932 		return;
933 	}
934 
935 	x_map = gog_chart_map_get_axis_map (chart_map, 0);
936 	y_map = gog_chart_map_get_axis_map (chart_map, 1);
937 
938 	/* Draw drop lines from point to axis start. See comment in
939 	 * GogXYPlotView::render */
940 
941 	gog_axis_map_get_extents (y_map, &drop_lines_y_min, &drop_lines_y_max);
942 	drop_lines_y_min = gog_axis_map_to_view (y_map, drop_lines_y_min);
943 	drop_lines_y_max = gog_axis_map_to_view (y_map, drop_lines_y_max);
944 	gog_axis_map_get_extents (y_map, &y_bottom, &y_top);
945 	y_bottom = gog_axis_map_to_view (y_map, y_bottom);
946 	y_top = gog_axis_map_to_view (y_map, y_top);
947 	y_zero = gog_axis_map_get_baseline (y_map);
948 
949 	vals    = g_alloca (num_series * sizeof (double *));
950 	yvals   = g_alloca (num_series * sizeof (double *));
951 	error_data = g_alloca (num_series * sizeof (ErrorBarData *));
952 	lengths = g_alloca (num_series * sizeof (unsigned));
953 	styles  = g_alloca (num_series * sizeof (GOStyle *));
954 	series  = g_alloca (num_series * sizeof (GogSeries *));
955 	interpolations	= g_alloca (num_series * sizeof (GOLineInterpolation));
956 	if (!is_area_plot)
957 		points  = g_alloca (num_series * sizeof (Point *));
958 	errors	= g_alloca (num_series * sizeof (GogErrorBar *));
959 	lines	= g_alloca (num_series * sizeof (GogSeriesLines *));
960 	drop_paths = g_alloca (num_series * sizeof (GOPath *));
961 
962 	i = 0;
963 	for (ptr = model->base.series ; ptr != NULL && i < num_series ; ptr = ptr->next) {
964 		series[i] = ptr->data;
965 		base_series = GOG_SERIES (ptr->data);
966 
967 		if (!gog_series_is_valid (base_series)) {
968 			vals[i] = NULL;
969 			lengths[i] = 0;
970 			continue;
971 		}
972 
973 		vals[i] = go_data_get_values (base_series->values[1].data);
974 		yvals[i] = g_new (double, num_elements);
975 		lengths[i] = go_data_get_vector_size (base_series->values[1].data);
976 		styles[i] = GOG_STYLED_OBJECT (base_series)->style;
977 		interpolations[i] = base_series->interpolation;
978 		if (interpolations[i] == GO_LINE_INTERPOLATION_CLOSED_SPLINE)
979 			interpolations[i] = GO_LINE_INTERPOLATION_SPLINE;
980 
981 		if (!is_area_plot)
982 			points[i] = g_malloc (sizeof (Point) * (type == GOG_1_5D_NORMAL? lengths[i]: num_elements));
983 
984 		errors[i] = series[i]->errors;
985 		if (gog_error_bar_is_visible (series[i]->errors))
986 			error_data[i] = g_malloc (sizeof (ErrorBarData) * num_elements);
987 		else
988 			error_data[i] = NULL;
989 		if (series[i]->has_drop_lines) {
990 			if (!role)
991 				role = gog_object_find_role_by_name (
992 							GOG_OBJECT (series[i]), "Drop lines");
993 			lines[i] = GOG_SERIES_LINES (
994 					gog_object_get_child_by_role (GOG_OBJECT (series[i]), role));
995 			drop_paths [i] = go_path_new ();
996 		} else
997 			lines[i] = NULL;
998 		i++;
999 	}
1000 	if (ptr != NULL || i != num_series) {
1001 		g_warning ("Wrong series number in bar/col plot");
1002 		num_series = i;
1003 	}
1004 
1005 	for (j = 0; j < num_elements; j++) {
1006 		sum = abs_sum = 0.0;
1007 		if (type == GOG_1_5D_AS_PERCENTAGE) {
1008 			for (i = 0; i < num_series; i++)
1009 				if (vals[i]  && j < lengths[i] && gog_axis_map_finite (y_map, vals[i][j]))
1010 					abs_sum += fabs (vals[i][j]);
1011 			is_null = (go_sub_epsilon (abs_sum) <= 0.);
1012 		} else
1013 			is_null = TRUE;
1014 
1015 		for (i = 0; i < num_series; i++) {
1016 			if (type == GOG_1_5D_NORMAL && j >= lengths[i])
1017 				continue;
1018 
1019 			if (vals[i] && gog_axis_map_finite (y_map, (j < lengths[i])? vals[i][j]: go_nan)) {
1020 				value = vals[i][j];
1021 				if (gog_error_bar_is_visible (errors[i])) {
1022 					gog_error_bar_get_bounds (errors[i], j, &minus, &plus);
1023 				}
1024 			} else if (type == GOG_1_5D_NORMAL && !is_area_plot) {
1025 				value = go_nan;
1026 				minus = -1.0;
1027 				plus = -1.;
1028 			} else {
1029 				value = 0.0;
1030 				minus = -1.0;
1031 				plus = -1.;
1032 			}
1033 
1034 			x = gog_axis_map_to_view (x_map, j + 1);
1035 			sum += value;
1036 
1037 			if (gog_error_bar_is_visible (errors[i]))
1038 				error_data[i][j].x = j + 1;
1039 
1040 			switch (type) {
1041 				case GOG_1_5D_NORMAL :
1042 					y = gog_axis_map_finite (y_map, value) ?
1043 						gog_axis_map_to_view (y_map, value) :
1044 					((is_area_plot)? y_zero: go_nan);
1045 					if (gog_error_bar_is_visible (errors[i])) {
1046 						error_data[i][j].y = value;
1047 						error_data[i][j].minus = minus;
1048 						error_data[i][j].plus = plus;
1049 					}
1050 					yvals[i][j] = isnan (y)? go_nan: value;
1051 					break;
1052 
1053 				case GOG_1_5D_STACKED :
1054 					y = gog_axis_map_finite (y_map, sum) ?
1055 						gog_axis_map_to_view (y_map, sum) :
1056 						y_zero;
1057 					if (gog_error_bar_is_visible (errors[i])) {
1058 						error_data[i][j].y = sum;
1059 						error_data[i][j].minus = minus;
1060 						error_data[i][j].plus = plus;
1061 					}
1062 					yvals[i][j] = sum;
1063 					break;
1064 
1065 				case GOG_1_5D_AS_PERCENTAGE :
1066 					y = is_null ?
1067 						y_zero :
1068 						(gog_axis_map_finite (y_map, sum) ?
1069 						 gog_axis_map_to_view (y_map, sum  / abs_sum) :
1070 						 y_zero);
1071 					if (gog_error_bar_is_visible (errors[i])) {
1072 						error_data[i][j].y = is_null ? 0. : sum / abs_sum;
1073 						error_data[i][j].minus = is_null ? -1. : minus / abs_sum;
1074 						error_data[i][j].plus = is_null ? -1. : plus / abs_sum;
1075 					}
1076 					yvals[i][j] = sum / abs_sum;
1077 					break;
1078 			}
1079 			if (!is_area_plot){
1080 				points[i][j].x = x;
1081 				points[i][j].y = y;
1082 			}
1083 			if (lines[i] && go_finite (y)) {
1084 				double y_target;
1085 				GogAxis *axis = GOG_PLOT (model)->axis[GOG_AXIS_Y];
1086 				GogAxisPosition pos = gog_axis_base_get_clamped_position (GOG_AXIS_BASE (axis));
1087 				switch (pos) {
1088 				case GOG_AXIS_AT_LOW:
1089 					y_target = gog_axis_map_is_inverted (y_map)? y_top: y_bottom;
1090 					break;
1091 				case GOG_AXIS_CROSS: {
1092 					GogChartMap *c_map;
1093 					GogAxisMap *a_map;
1094 					c_map = gog_chart_map_new (chart, area, axis,
1095 							gog_axis_base_get_crossed_axis (GOG_AXIS_BASE (axis)),
1096 							NULL, FALSE);
1097 					a_map = gog_chart_map_get_axis_map (c_map, 1);
1098 					y_target = gog_axis_map_to_view (a_map, gog_axis_base_get_cross_location (GOG_AXIS_BASE (axis)));
1099 					gog_chart_map_free (c_map);
1100 					break;
1101 				}
1102 				case GOG_AXIS_AT_HIGH:
1103 					y_target = gog_axis_map_is_inverted (y_map)? y_bottom: y_top;
1104 					break;
1105 				default:
1106 					/* this should not occur */
1107 					y_target = gog_axis_map_to_view (y_map, 0);
1108 					break;
1109 				}
1110 				go_path_move_to (drop_paths[i], x, y);
1111 				go_path_line_to (drop_paths[i], x, y_target);
1112 			}
1113 
1114 		}
1115 	}
1116 
1117 	gog_renderer_push_clip_rectangle (view->renderer, view->allocation.x, view->allocation.y,
1118 					  view->allocation.w, view->allocation.h);
1119 
1120 	if (is_area_plot && type != GOG_1_5D_NORMAL) {
1121 		GOPath **paths = g_alloca (num_series * sizeof (GOPath *)), *close_path, *seg;
1122 		unsigned step;
1123 		unsigned k, m;
1124 		for (i = 0; i < num_series; i++) {
1125 			if (lengths[i] == 0)
1126 				continue;
1127 			paths[i] = gog_chart_map_make_path (chart_map, NULL, yvals[i], lengths[i],
1128 				                            interpolations[i], type != GOG_1_5D_NORMAL && !is_area_plot,
1129 				                            &GOG_AREA_SERIES (series[i])->clamped_derivs);
1130 			close_path = NULL;
1131 			gog_renderer_push_style (view->renderer, styles[i]);
1132 			j = i;
1133 			k = 0;
1134 			while (j > 0) {
1135 				j--;
1136 				if (lengths[j] == 0)
1137 					continue;
1138 				if (lengths[j] == lengths[i]) {
1139 					k = lengths[i];
1140 					close_path = paths[j];
1141 				} else
1142 					j++;
1143 				break;
1144 			}
1145 			while (j > 0 && k < lengths[i] - 1) {
1146 				j--;
1147 				if (lengths[j] > k + 1) {
1148 					m = MIN (lengths[i], lengths[j]) - 1;
1149 					switch (interpolations[j]) {
1150 					case GO_LINE_INTERPOLATION_STEP_START:
1151 					case GO_LINE_INTERPOLATION_STEP_END:
1152 						step = 2;
1153 						break;
1154 					case GO_LINE_INTERPOLATION_STEP_CENTER_X:
1155 					case GO_LINE_INTERPOLATION_STEP_CENTER_Y:
1156 						step = 3;
1157 						break;
1158 					default:
1159 						step = 1;
1160 						break;
1161 					}
1162 					seg = go_path_copy_restricted (paths[j], k * step, m * step);
1163 					k = m;
1164 					close_path = go_path_append (close_path, seg);
1165 					go_path_free (seg);
1166 				}
1167 			}
1168 			k++;
1169 			if (k < lengths[i]) {
1170 				if (close_path == NULL)
1171 					close_path = go_path_new ();
1172 				go_path_move_to (close_path, gog_axis_map_to_view (x_map, k), y_zero);
1173 				go_path_line_to (close_path, gog_axis_map_to_view (x_map, lengths[i]), y_zero);
1174 			}
1175 			gog_renderer_fill_serie (view->renderer, paths[i], close_path);
1176 			gog_renderer_stroke_serie (view->renderer, close_path);
1177 			if (close_path != paths[j])
1178 				go_path_free (close_path);
1179 			gog_renderer_stroke_serie (view->renderer, paths[i]);
1180 			gog_renderer_pop_style (view->renderer);
1181 		}
1182 		for (i = 0; i < num_series; i++) {
1183 			if (lengths[i] == 0)
1184 				continue;
1185 			go_path_free (paths[i]);
1186 		}
1187 	} else {
1188 		GOPath *path;
1189 		for (i = 0; i < num_series; i++) {
1190 			if (lengths[i] == 0)
1191 				continue;
1192 			path = gog_chart_map_make_path (chart_map, NULL, yvals[i], lengths[i],
1193 				                            interpolations[i], type != GOG_1_5D_NORMAL && !is_area_plot,
1194 				                            &GOG_AREA_SERIES (series[i])->clamped_derivs);
1195 
1196 			gog_renderer_push_style (view->renderer, styles[i]);
1197 
1198 			if (!is_area_plot) {
1199 				gog_renderer_stroke_serie (view->renderer, path);
1200 				go_path_free (path);
1201 			} else {
1202 				GOPath *close_path = go_path_new ();
1203 				go_path_move_to (close_path, gog_axis_map_to_view (x_map, 1), y_zero);
1204 				go_path_line_to (close_path, gog_axis_map_to_view (x_map, lengths[i]), y_zero);
1205 				gog_renderer_fill_serie (view->renderer, path, close_path);
1206 				gog_renderer_stroke_serie (view->renderer, close_path);
1207 				go_path_free (close_path);
1208 				gog_renderer_stroke_serie (view->renderer, path);
1209 				go_path_free (path);
1210 			}
1211 			gog_renderer_pop_style (view->renderer);
1212 		}
1213 }
1214 
1215 	/*Now draw drop lines */
1216 	for (i = 0; i < num_series; i++)
1217 		if (lines[i] != NULL) {
1218 			gog_renderer_push_style (view->renderer,
1219 				go_styled_object_get_style (GO_STYLED_OBJECT (lines[i])));
1220 			gog_series_lines_stroke (lines[i], view->renderer, bbox, drop_paths[i], FALSE);
1221 			gog_renderer_pop_style (view->renderer);
1222 			go_path_free (drop_paths[i]);
1223 		}
1224 
1225 	/*Now draw error bars */
1226 	for (i = 0; i < num_series; i++)
1227 		if (gog_error_bar_is_visible (errors[i]))
1228 			for (j = 0; j < lengths[i]; j++)
1229 				gog_error_bar_render (errors[i], view->renderer, chart_map,
1230 						      error_data[i][j].x, error_data[i][j].y,
1231 						      error_data[i][j].minus, error_data[i][j].plus,
1232 						      GOG_ERROR_BAR_DIRECTION_VERTICAL);
1233 
1234 	gog_renderer_pop_clip (view->renderer);
1235 
1236 	/*Now draw markers*/
1237 	if (!is_area_plot) {
1238 		double x_margin_min, x_margin_max, y_margin_min, y_margin_max, margin;
1239 
1240 		margin = gog_renderer_line_size (view->renderer, 1.0);
1241 		x_margin_min = view->allocation.x - margin;
1242 		x_margin_max = view->allocation.x + view->allocation.w + margin;
1243 		y_margin_min = view->allocation.y - margin;
1244 		y_margin_max = view->allocation.y + view->allocation.h + margin;
1245 
1246 		ptr = model->base.series;
1247 		for (i = 0; i < num_series; i++) {
1248 			overrides = gog_series_get_overrides (GOG_SERIES (ptr->data));
1249 			if (lengths[i] == 0)
1250 				continue;
1251 
1252 			gog_renderer_push_style (view->renderer, styles[i]);
1253 
1254 			for (j = 0; j < lengths[i]; j++) {
1255 				x = points[i][j].x;
1256 				y = points[i][j].y;
1257 				if (isnan (y)) {
1258 					if ((overrides != NULL) &&
1259 						(GOG_SERIES_ELEMENT (overrides->data)->index == j))
1260 						overrides = overrides->next;
1261 					continue;
1262 				}
1263 				gse = NULL;
1264 				if ((overrides != NULL) &&
1265 					(GOG_SERIES_ELEMENT (overrides->data)->index == j)) {
1266 						gse = GOG_SERIES_ELEMENT (overrides->data);
1267 						overrides = overrides->next;
1268 						gog_renderer_push_style (view->renderer,
1269 							go_styled_object_get_style (
1270 								GO_STYLED_OBJECT (gse)));
1271 				}
1272 				if (x_margin_min <= x && x <= x_margin_max &&
1273 				    y_margin_min <= y && y <= y_margin_max)
1274 					gog_renderer_draw_marker (view->renderer, x, y);
1275 				if (gse)
1276 					gog_renderer_pop_style (view->renderer);
1277 			}
1278 			gog_renderer_pop_style (view->renderer);
1279 			ptr = ptr->next;
1280 		}
1281 	}
1282 
1283 	for (i = 0; i < num_series; i++) {
1284 		if (!is_area_plot)
1285 			g_free (points[i]);
1286 		g_free (error_data[i]);
1287 		g_free (yvals[i]);
1288 	}
1289 
1290 	/* Now render children */
1291 	for (ptr = view->children ; ptr != NULL ; ptr = ptr->next)
1292 		gog_view_render	(ptr->data, bbox);
1293 
1294 	gog_chart_map_free (chart_map);
1295 }
1296 
1297 static GogViewClass *line_view_parent_klass;
1298 
1299 static void
gog_line_view_size_allocate(GogView * view,GogViewAllocation const * allocation)1300 gog_line_view_size_allocate (GogView *view, GogViewAllocation const *allocation)
1301 {
1302 	GSList *ptr;
1303 	for (ptr = view->children; ptr != NULL; ptr = ptr->next)
1304 		gog_view_size_allocate (GOG_VIEW (ptr->data), allocation);
1305 	(line_view_parent_klass->size_allocate) (view, allocation);
1306 }
1307 
1308 static void
gog_line_view_class_init(GogViewClass * view_klass)1309 gog_line_view_class_init (GogViewClass *view_klass)
1310 {
1311 	line_view_parent_klass = (GogViewClass*) g_type_class_peek_parent (view_klass);
1312 	view_klass->render	  = gog_line_view_render;
1313 	view_klass->size_allocate = gog_line_view_size_allocate;
1314 	((GogPlotViewClass *) view_klass)->get_data_at_point = gog_line_view_get_data_at_point;
1315 }
1316 
1317 GSF_DYNAMIC_CLASS (GogLineView, gog_line_view,
1318 	gog_line_view_class_init, NULL,
1319 	GOG_TYPE_PLOT_VIEW)
1320