summaryrefslogtreecommitdiff
path: root/src/plot.c
blob: 3e1b8a7f826b3430fcb2e7542832c05d1bd22bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 * plot.c
 *
 *
 * Authors:
 *  Richard Hult <rhult@hem.passagen.se>
 *  Ricardo Markiewicz <rmarkie@fi.uba.ar>
 *  Andres de Barbara <adebarbara@fi.uba.ar>
 *  Marc Lorber <lorber.marc@wanadoo.fr>
 *  Bernhard Schuster <bernhard@ahoi.io>
 *  Guido Trentalancia <guido@trentalancia.com>
 *
 * Web page: https://ahoi.io/project/oregano
 *
 * Copyright (C) 1999-2001  Richard Hult
 * Copyright (C) 2003,2006  Ricardo Markiewicz
 * Copyright (C) 2009-2012  Marc Lorber
 * Copyright (C) 2013       Bernhard Schuster
 * Copyright (C) 2017       Guido Trentalancia
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <math.h>
#include <string.h>
#include <glib/gi18n.h>

#include "file.h"
#include "oregano.h"
#include "schematic.h"
#include "dialogs.h"
#include "plot.h"

#include "gplot.h"
#include "gplotfunction.h"
#include "gplotlines.h"
#include "plot-add-function.h"

#define PLOT_PADDING_X 50
#define PLOT_PADDING_Y 40
#define PLOT_WIDTH 400
#define PLOT_HEIGHT 300
#define DEBUG_PLOT 0

#define GET_Y_POINT(y_val, y_min, factor) (PLOT_HEIGHT - ((y_val) - (y_min)) * (factor))
#define GET_Y_VALUE(y_point, y_min, factor) ((y_min) + (PLOT_HEIGHT - (y_point)) / (factor))
#define GET_X_POINT(x_val, x_min, factor) (((x_val) - (x_min)) * (factor))
#define GET_X_VALUE(x_point, x_min, factor) ((x_min) + (x_point) / (factor))

#define PLOT_AXIS_COLOR "black"
#define PLOT_CURVE_COLOR "medium sea green"
#include "debug.h"

static guint next_color = 0;
static guint next_pulse = 0;
static guint n_curve_colors = 7;
static char *plot_curve_colors[] = {"white", "green", "red",     "yellow",
                                    "cyan",  "pink",  "orange1", 0};

typedef struct
{
	GtkWidget *window;
	GtkWidget *canvas;
	GtkWidget *coord; // shows the coordinates of the mouse
	GtkWidget *combo_box;

	GtkWidget *plot;

	gboolean show_cursor;

	OreganoEngine *sim;
	SimulationData *current;

	gboolean logx;
	gboolean logy;

	gchar *title;
	gchar *xtitle;
	gchar *ytitle;

	gint width;  // width of the plot, excluding
	gint height; // axes, titles and padding etc

	gdouble plot_min;
	gdouble plot_max;
	gdouble x_min;
	gdouble x_max;

	gdouble padding_x; // padding around the plot. Note that
	gdouble padding_y; // titles, axes etc live here

	gint selected; // the currently selected plot in the clist
	gint prev_selected;
} Plot;

static GtkWidget *plot_window_create (Plot *plot);
static void destroy_window (GtkWidget *widget, Plot *plot);
static gint delete_event_cb (GtkWidget *widget, GdkEvent *event, Plot *plot);
static void plot_canvas_movement (GtkWidget *, GdkEventMotion *, Plot *);
static void add_function (GtkMenuItem *menuitem, Plot *plot);
static void close_window (GtkMenuItem *menuitem, Plot *plot);

static gchar *get_variable_units (gchar *str)
{
	gchar *tmp;

	if (str == NULL)
		return g_strdup ("##");

	if (!strcmp (str, _ ("voltage"))) {
		tmp = g_strdup ("V");
	} else if (!strcmp (str, "db")) {
		tmp = g_strdup ("db");
	} else if (!strcmp (str, _ ("time"))) {
		tmp = g_strdup ("s");
	} else if (!strcmp (str, _ ("frequency"))) {
		tmp = g_strdup ("Hz");
	} else if (!strcmp (str, _ ("current"))) {
		tmp = g_strdup ("A");
	} else if (!strcmp (str, _ ("psd"))) {
		tmp = g_strdup ("V^2 / Hz");
	} else {
		tmp = g_strdup ("##");
	}
	return tmp;
}

static gint delete_event_cb (GtkWidget *widget, GdkEvent *event, Plot *plot)
{
	plot->window = NULL;
	g_object_unref (plot->sim);
	g_free (plot->ytitle);
	g_free (plot);
	plot = NULL;
	return FALSE;
}

// Call this to close the plot window
static void destroy_window (GtkWidget *widget, Plot *plot)
{
	gtk_widget_destroy (plot->canvas);
	gtk_widget_destroy (plot->coord);
	gtk_widget_destroy (plot->combo_box);
	gtk_widget_destroy (plot->window);
	g_object_unref (plot->sim);
	plot->window = NULL;
	g_free (plot->title);
	g_free (plot);
	plot = NULL;
}

static void on_plot_selected (GtkCellRendererToggle *cell_renderer, gchar *path, Plot *plot)
{
	GPlotFunction *f;
	GtkTreeIter iter;
	GtkTreeModel *model;
	GtkTreeView *treeview;
	gboolean visible = FALSE;

	treeview = GTK_TREE_VIEW (g_object_get_data (G_OBJECT (plot->window), "clist"));

	model = gtk_tree_view_get_model (treeview);
	if (!gtk_tree_model_get_iter_from_string (model, &iter, path))
		return;

	gtk_tree_model_get (model, &iter, 0, &visible, 4, &f, -1);
	visible = !visible;
	gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, visible, -1);

	g_object_set (G_OBJECT (f), "visible", visible, NULL);

	gtk_widget_queue_draw (plot->plot);
}

static GPlotFunction *create_plot_function_from_simulation_data (guint i, SimulationData *current)
{
	GPlotFunction *f;
	double *X;
	double *Y;
	guint len, j;
	gdouble width = 1;
	GraphicType graphic_type = FUNCTIONAL_CURVE;
	gdouble shift_step = 0;

	len = current->data[i]->len;
	X = g_new0 (double, len);
	Y = g_new0 (double, len);

	for (j = 0; j < len; j++) {
		Y[j] = g_array_index (current->data[i], double, j);
		X[j] = g_array_index (current->data[0], double, j);
	}
	if (current->type == ANALYSIS_TYPE_FOURIER) {
		graphic_type = FREQUENCY_PULSE;
		next_pulse++;
		width = 5.0;
		shift_step = X[1] / 20;
		NG_DEBUG ("shift_step = %lf\n", shift_step);
	} else {
		next_pulse = 0;
		width = 1.0;
	}

	f = g_plot_lines_new (X, Y, len);
	g_object_set (G_OBJECT (f), "color", plot_curve_colors[(next_color++) % n_curve_colors], NULL);
	g_object_set (G_OBJECT (f), "graph-type", graphic_type, NULL);
	g_object_set (G_OBJECT (f), "shift", shift_step * next_pulse, NULL);
	g_object_set (G_OBJECT (f), "width", width, NULL);
	NG_DEBUG ("plot: create_plot_function_from_simulation_data: shift = %lf\n", 0.1 * next_pulse);

	return f;
}

static void analysis_selected (GtkWidget *combo_box, Plot *plot)
{
	int i;
	const gchar *ca;
	GtkTreeView *list;
	GtkTreeModel *model;
	GtkTreeIter parent_nodes, parent_functions;
	GList *analysis;
	SimulationData *sdat;

	list = GTK_TREE_VIEW (g_object_get_data (G_OBJECT (plot->window), "clist"));
	if (gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box)) == -1) {
		// Simulation failed?
		g_warning (_ ("The simulation produced no data!!\n"));
		return;
	}
	ca = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (combo_box));

	plot->current = NULL;
	analysis = oregano_engine_get_results (plot->sim);
	for (; analysis; analysis = analysis->next) {
		sdat = SIM_DATA (analysis->data);
		gchar *analysis_name = oregano_engine_get_analysis_name (sdat);
		if (!strcmp (ca, analysis_name)) {
			plot->current = sdat;
			g_free(analysis_name);
			break;
		}
		g_free(analysis_name);
	}

	if (plot->current == NULL) {
		// Simulation failed?
		g_warning (_ ("The simulation produced no data!!\n"));
		return;
	}

	g_free (plot->xtitle);
	plot->xtitle = get_variable_units (plot->current->var_units[0]);
	g_free (plot->ytitle);
	plot->ytitle = get_variable_units (plot->current->var_units[1]);

	g_free (plot->title);
	gchar *analysis_name = oregano_engine_get_analysis_name (plot->current);
	plot->title =
	    g_strdup_printf (_ ("Plot - %s"), analysis_name);
	g_free(analysis_name);

	//  Set the variable names in the list
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
	gtk_tree_store_clear (GTK_TREE_STORE (model));

	// Create root nodes
	gtk_tree_store_append (GTK_TREE_STORE (model), &parent_nodes, NULL);
	gtk_tree_store_set (GTK_TREE_STORE (model), &parent_nodes, 0, FALSE, 1, _ ("Nodes"), 2, FALSE,
	                    3, "white", -1);

	gtk_tree_store_append (GTK_TREE_STORE (model), &parent_functions, NULL);
	gtk_tree_store_set (GTK_TREE_STORE (model), &parent_functions, 0, NULL, 1, _ ("Functions"), 2,
	                    FALSE, 3, "white", -1);

	g_plot_set_axis_labels (GPLOT (plot->plot), plot->xtitle, plot->ytitle);
	g_plot_clear (GPLOT (plot->plot));

	for (i = 1; i < plot->current->n_variables; i++) {
		GtkTreeIter iter;
		GPlotFunction *f;
		if (plot->current->type != ANALYSIS_TYPE_DC_TRANSFER) {
			//FIXME extra axis scaling for current/voltage
			//FIXME or extra plot window for current/voltage
			//FIXME or extra analysis for current/voltage
			gchar *color;

			f = create_plot_function_from_simulation_data (i, plot->current);
			g_object_set (G_OBJECT (f), "visible", FALSE, NULL);
			g_object_get (G_OBJECT (f), "color", &color, NULL);

			g_plot_add_function (GPLOT (plot->plot), f);
			gtk_tree_store_append (GTK_TREE_STORE (model), &iter, &parent_nodes);
			gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, FALSE, 1,
			                    plot->current->var_names[i], 2, TRUE, 3, color, 4, f, -1);
		} else {
			gchar *color;

			f = create_plot_function_from_simulation_data (i, plot->current);
			g_object_set (G_OBJECT (f), "visible", FALSE, NULL);
			g_object_get (G_OBJECT (f), "color", &color, NULL);

			g_plot_add_function (GPLOT (plot->plot), f);
			gtk_tree_store_append (GTK_TREE_STORE (model), &iter, &parent_nodes);
			gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, FALSE, 1,
			                    plot->current->var_names[i], 2, TRUE, 3, color, 4, f, -1);
		}
	}
	gtk_widget_queue_draw (plot->plot);
}

static void plot_canvas_movement (GtkWidget *w, GdkEventMotion *event, Plot *plot)
{
	gchar *coordstr;
	gdouble x, y;

	x = event->x;
	y = event->y;

	g_plot_window_to_device (GPLOT (plot->plot), &x, &y);

	coordstr = g_strdup_printf ("(%g, %g)", x, y);

	gtk_entry_set_text (GTK_ENTRY (plot->coord), coordstr);

	g_free (coordstr);
}

static GtkWidget *plot_window_create (Plot *plot)
{
	GtkTreeView *list;
	GtkTreeStore *tree_model;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;

	GtkWidget *outer_table, *window, *button, *plot_scrolled;
	GtkWidget *vbox, *menubar, *menu, *menuitem, *combo_box, *w;
	GtkBuilder *gui;
	GError *perror = NULL;
	gchar *msg;
	GtkAccelGroup *accel_group;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), _ ("Oregano - Plot"));
	gtk_container_set_border_width (GTK_CONTAINER (window), 0);
	g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (delete_event_cb), plot);
	accel_group = gtk_accel_group_new ();
	gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

	if ((gui = gtk_builder_new ()) == NULL) {
		oregano_error (_ ("Could not create plot window."));
		return NULL;
	}
	gtk_builder_set_translation_domain (gui, NULL);

	if (gtk_builder_add_from_file (gui, OREGANO_UIDIR "/plot-window.ui", &perror) <= 0) {
		msg = perror->message;
		oregano_error_with_title (_ ("Could not create plot window."), msg);
		g_error_free (perror);
		return NULL;
	}

	outer_table = GTK_WIDGET (gtk_builder_get_object (gui, "plot_grid"));
	plot_scrolled = plot->canvas = GTK_WIDGET (gtk_builder_get_object (gui, "plot_scrolled"));
	plot->coord = GTK_WIDGET (gtk_builder_get_object (gui, "pos_label"));

	plot->plot = g_plot_new ();

	gtk_container_add (GTK_CONTAINER (plot_scrolled), plot->plot);
	gtk_widget_set_size_request (plot_scrolled, 600, 400);

	g_signal_connect (G_OBJECT (plot->plot), "motion_notify_event",
	                  G_CALLBACK (plot_canvas_movement), plot);

	// Creation of the menubar
	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	menubar = gtk_menu_bar_new ();
	gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, TRUE, 0);
	gtk_widget_show (menubar);

	// Creation of the menu
	menu = gtk_menu_new ();
	//add Add Function menuitem
	menuitem = gtk_menu_item_new_with_label (_ ("Add Function"));
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (menuitem, "activate", G_CALLBACK (add_function), plot);
	gtk_widget_show (menuitem);
	//add separator
	menuitem = gtk_separator_menu_item_new ();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	gtk_widget_show (menuitem);
	menuitem = gtk_menu_item_new_with_label (_ ("Close"));
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (menuitem, "activate", G_CALLBACK (close_window), plot);
	gtk_widget_show (menuitem);

	// Installation of the menu in the menubar
	menuitem = gtk_menu_item_new_with_label (_ ("File"));
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
	gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
	gtk_widget_show (menuitem);

	g_object_ref (outer_table);
	gtk_widget_unparent (outer_table);
	gtk_container_add (GTK_CONTAINER (vbox), outer_table);
	gtk_widget_show (vbox);

	g_object_ref (vbox);
	gtk_widget_unparent (vbox);
	gtk_container_add (GTK_CONTAINER (window), vbox);

	button = GTK_WIDGET (gtk_builder_get_object (gui, "close_button"));
	g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (destroy_window), plot);

	list = GTK_TREE_VIEW (gtk_builder_get_object (gui, "variable_list"));
	tree_model = gtk_tree_store_new (5, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_BOOLEAN,
	                                 G_TYPE_STRING, G_TYPE_POINTER);

	// One Column with 2 CellRenderer. First the Toggle and next a Text
	column = gtk_tree_view_column_new ();

	cell = gtk_cell_renderer_toggle_new ();
	g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (on_plot_selected), plot);
	gtk_tree_view_column_pack_start (column, cell, FALSE);
	gtk_tree_view_column_set_attributes (column, cell, "active", 0, "visible", 2, NULL);

	cell = gtk_cell_renderer_text_new ();
	gtk_tree_view_column_pack_start (column, cell, FALSE);
	gtk_tree_view_column_set_attributes (column, cell, "text", 1, NULL);

	cell = gtk_cell_renderer_text_new ();
	gtk_tree_view_column_pack_start (column, cell, FALSE);
	gtk_cell_renderer_set_fixed_size (cell, 20, 20);
	gtk_tree_view_column_set_attributes (column, cell, "background", 3, NULL);

	gtk_tree_view_append_column (list, column);
	gtk_tree_view_set_model (list, GTK_TREE_MODEL (tree_model));

	g_object_set_data (G_OBJECT (window), "clist", list);

	gtk_widget_realize (GTK_WIDGET (window));

	w = GTK_WIDGET (gtk_builder_get_object (gui, "analysis_combobox"));
	gtk_widget_destroy (w);

	w = GTK_WIDGET (gtk_builder_get_object (gui, "grid1"));
	combo_box = gtk_combo_box_text_new_with_entry ();

	gtk_grid_attach (GTK_GRID (w), combo_box, 0, 0, 1, 1);

	plot->combo_box = combo_box;
	plot->window = window;
	gtk_widget_show_all (window);

	return window;
}

int plot_show (OreganoEngine *engine)
{
	GList *analysis = NULL;
	GList *analysis_backup = NULL;
	Plot *plot;
	gchar *str = NULL;
	gchar *s_current = NULL;
	SimulationData *first = NULL;
	SimulationData *sdat = NULL;
	gboolean abort = TRUE;

	g_return_val_if_fail (engine != NULL, FALSE);

	// Get the analysis we have
	analysis = analysis_backup = oregano_engine_get_results (engine);

	for (; analysis; analysis = analysis->next) {
		sdat = SIM_DATA (analysis->data);
		str = oregano_engine_get_analysis_name (sdat);
		if (sdat->type != ANALYSIS_TYPE_OP_POINT)
			abort = FALSE;
	}

	if (abort)
		return FALSE;

	plot = g_new0 (Plot, 1);

	g_object_ref (engine);
	plot->sim = engine;
	plot->window = plot_window_create (plot);

	plot->logx = FALSE;
	plot->logy = FALSE;

	plot->padding_x = PLOT_PADDING_X;
	plot->padding_y = PLOT_PADDING_Y;

	plot->show_cursor = TRUE;

	next_color = 0;

	for (; analysis_backup; analysis_backup = analysis_backup->next) {
		sdat = SIM_DATA (analysis_backup->data);
		str = oregano_engine_get_analysis_name (sdat);
		if (sdat->type == ANALYSIS_TYPE_OP_POINT) {
			continue;
		}

		if (s_current == NULL) {
			s_current = str;
			first = sdat;
		}

		gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (plot->combo_box), str);
		gtk_combo_box_set_active (GTK_COMBO_BOX (plot->combo_box), 0);
	}

	g_signal_connect (G_OBJECT (plot->combo_box), "changed", G_CALLBACK (analysis_selected), plot);

	plot->title = g_strdup_printf (_ ("Plot - %s"), s_current);
	plot->xtitle = get_variable_units (first ? first->var_units[0] : "##");
	plot->ytitle = get_variable_units (first ? first->var_units[1] : "!!");

	g_free (s_current);

	analysis_selected ((plot->combo_box), plot);

	return TRUE;
}

static GPlotFunction *create_plot_function_from_data (SimulationFunction *func,
                                                      SimulationData *current)
{
	GPlotFunction *f;
	double *X;
	double *Y;
	double data;
	guint j, len;
	GraphicType graphic_type = FUNCTIONAL_CURVE;
	gdouble width = 1.0;

	len = current->data[func->first]->len;
	X = g_new0 (double, len);
	Y = g_new0 (double, len);

	for (j = 0; j < len; j++) {
		Y[j] = g_array_index (current->data[func->first], double, j);

		data = g_array_index (current->data[func->second], double, j);
		switch (func->type) {
		case FUNCTION_SUBTRACT:
			Y[j] -= data;
			break;
		case FUNCTION_DIVIDE:
			if (data < 0.000001f) {
				if (Y[j] < 0)
					Y[j] = -G_MAXDOUBLE;
				else
					Y[j] = G_MAXDOUBLE;
			} else {
				Y[j] /= data;
			}
		}

		X[j] = g_array_index (current->data[0], double, j);
	}
	if (current->type == ANALYSIS_TYPE_FOURIER) {
		graphic_type = FREQUENCY_PULSE;
		next_pulse++;
		width = 5.0;
	} else {
		next_pulse = 0;
		width = 1.0;
	}

	f = g_plot_lines_new (X, Y, len);
	g_object_set (G_OBJECT (f), "color", plot_curve_colors[(next_color++) % n_curve_colors],
	              "graph-type", graphic_type, "shift", 50.0 * next_pulse, "width", width, NULL);

	return f;
}

static void close_window (GtkMenuItem *menuitem, Plot *plot)
{
	destroy_window (GTK_WIDGET (menuitem), plot);
}

static void add_function (GtkMenuItem *menuitem, Plot *plot)
{
	GtkTreeView *tree;
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkTreePath *path;
	GList *lst;
	gchar *color;

	plot_add_function_show (plot->sim, plot->current);

	tree = GTK_TREE_VIEW (g_object_get_data (G_OBJECT (plot->window), "clist"));
	model = gtk_tree_view_get_model (tree);

	path = gtk_tree_path_new_from_string ("1");
	gtk_tree_model_get_iter (model, &iter, path);
	gtk_tree_path_free (path);

	gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);

	gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
	gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, FALSE, 1, _ ("Functions"), 2, FALSE, 3,
	                    "white", -1);

	lst = plot->current->functions;
	while (lst) {
		GPlotFunction *f;
		GtkTreeIter child;
		int i;
		gchar *str = NULL, *name1 = NULL, *name2 = NULL;
		SimulationFunction *func = (SimulationFunction *)lst->data;

		for (i = 1; i < plot->current->n_variables; i++) {
			if (func->first == i) {
				name1 = g_strdup (plot->current->var_names[i]);
			}
			if (func->second == i) {
				name2 = g_strdup (plot->current->var_names[i]);
			}
		}
		switch (func->type) {
		case FUNCTION_SUBTRACT:
			str = g_strdup_printf ("%s - %s", name1, name2);
			break;
		case FUNCTION_DIVIDE:
			str = g_strdup_printf ("%s(%s, %s)", _ ("TRANSFER"), name1, name2);
		}

		f = create_plot_function_from_data (func, plot->current);
		g_object_get (G_OBJECT (f), "color", &color, NULL);

		g_plot_add_function (GPLOT (plot->plot), f);

		gtk_tree_store_append (GTK_TREE_STORE (model), &child, &iter);
		gtk_tree_store_set (GTK_TREE_STORE (model), &child, 0, TRUE, 1, str, 2, TRUE, 3, color, 4,
		                    f, -1);

		lst = lst->next;
	}
	g_list_free_full (lst, g_object_unref);

	gtk_widget_queue_draw (plot->plot);
}