sparkline: Add "reversed" property to process data back to front (Guido Gunther)

this way we don't have to reverse the list in place in python code so
it's more obvious which set is which when having multiple lines (see
follow up patch).
This commit is contained in:
Cole Robinson 2008-10-06 12:56:11 -04:00
parent d1d6cdd8df
commit 0e0f4b8da0
3 changed files with 62 additions and 11 deletions

View File

@ -55,8 +55,8 @@ enum {
enum {
PROP_0,
PROP_NUMDATAPOINTS,
PROP_DATAARRAY
PROP_DATAARRAY,
PROP_REVERSED,
};
static gpointer parent_class;
@ -68,6 +68,7 @@ typedef struct _GtkCellRendererSparklinePrivate GtkCellRendererSparklinePrivate;
struct _GtkCellRendererSparklinePrivate
{
gboolean filled;
gboolean reversed;
GValueArray *data_array;
};
@ -137,6 +138,14 @@ static void gtk_cell_renderer_sparkline_class_init (GtkCellRendererSparklineClas
G_PARAM_READABLE | G_PARAM_WRITABLE),
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (object_class,
PROP_REVERSED,
g_param_spec_boolean ("reversed",
"Reversed",
"process data from back to front",
FALSE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_type_class_add_private (object_class, sizeof (GtkCellRendererSparklinePrivate));
}
@ -165,6 +174,10 @@ static void gtk_cell_renderer_sparkline_get_property (GObject *object,
g_value_set_boxed(value, priv->data_array);
break;
case PROP_REVERSED:
g_value_set_boolean(value, priv->reversed);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
@ -188,6 +201,10 @@ gtk_cell_renderer_sparkline_set_property (GObject *object,
priv->data_array = g_value_array_copy(g_value_get_boxed(value));
break;
case PROP_REVERSED:
priv->reversed = g_value_get_boolean(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
@ -225,12 +242,21 @@ static void gtk_cell_renderer_sparkline_get_size (GtkCellRenderer *cell,
}
}
static double get_y (GdkRectangle *cell_area,
static double get_y (GtkCellRendererSparklinePrivate *priv,
GdkRectangle *cell_area,
GValueArray *data,
int index)
{
int n;
double baseline_y = cell_area->y + cell_area->height;
GValue *val = g_value_array_get_nth(data, index);
GValue *val;
if (priv->reversed)
n = data->n_values - 1 - index;
else
n = index;
val = g_value_array_get_nth(data, n);
return baseline_y - (cell_area->height * g_value_get_double(val));
}
@ -261,7 +287,7 @@ gtk_cell_renderer_sparkline_render (GtkCellRenderer *cell,
points = g_new(GdkPoint, data->n_values);
for (index=0;index<data->n_values;index++) {
double cx = ((double)index * pixels_per_point);
double cy = get_y (cell_area, data, index);
double cy = get_y (priv, cell_area, data, index);
points[index].x = cx + cell_area->x;
points[index].y = cy;

View File

@ -43,6 +43,7 @@ enum {
PROP_0,
PROP_DATAARRAY,
PROP_FILLED,
PROP_REVERSED,
};
static gpointer parent_class;
@ -53,6 +54,7 @@ typedef struct _GtkSparklinePrivate GtkSparklinePrivate;
struct _GtkSparklinePrivate
{
gboolean filled;
gboolean reversed;
GValueArray *data_array;
};
@ -89,6 +91,7 @@ static void gtk_sparkline_init (GtkSparkline *sparkline)
priv = GTK_SPARKLINE_GET_PRIVATE (sparkline);
priv->filled = TRUE;
priv->reversed = FALSE;
priv->data_array = g_value_array_new(0);
g_signal_connect (G_OBJECT (sparkline), "expose_event",
@ -131,6 +134,13 @@ static void gtk_sparkline_class_init (GtkSparklineClass *class)
"fill space under sparcline",
TRUE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (object_class,
PROP_REVERSED,
g_param_spec_boolean ("reversed",
"Reversed",
"process data from back to front",
FALSE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_type_class_add_private (object_class, sizeof (GtkSparklinePrivate));
}
@ -162,6 +172,10 @@ static void gtk_sparkline_get_property (GObject *object,
g_value_set_boolean(value, priv->filled);
break;
case PROP_REVERSED:
g_value_set_boolean(value, priv->reversed);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
@ -190,6 +204,10 @@ gtk_sparkline_set_property (GObject *object,
priv->filled = g_value_get_boolean(value);
break;
case PROP_REVERSED:
priv->reversed = g_value_get_boolean(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
@ -214,10 +232,17 @@ static void gtk_sparkline_size_request(GtkWidget *widget,
static double get_y (GtkAllocation *cell_area,
GValueArray *data,
int index)
int index, int reversed)
{
double baseline_y = cell_area->height;
GValue *val = g_value_array_get_nth(data, index);
int n;
if (reversed)
n = data->n_values - 1 - index;
else
n = index;
GValue *val = g_value_array_get_nth(data, n);
return baseline_y - ((cell_area->height-1) * g_value_get_double(val));
}
@ -245,7 +270,7 @@ gtk_sparkline_expose (GtkWidget *widget,
points = g_new(GdkPoint, data->n_values);
for (index=0;index<data->n_values;index++) {
double cx = ((double)index * pixels_per_point);
double cy = get_y (cell_area, data, index);
double cy = get_y (cell_area, data, index, priv->reversed);
points[index].x = cx;
points[index].y = cy;
}

View File

@ -182,12 +182,15 @@ class vmmDetails(gobject.GObject):
self.choose_cd = None
self.cpu_usage_graph = sparkline.Sparkline()
self.cpu_usage_graph.set_property("reversed", True)
self.window.get_widget("graph-table").attach(self.cpu_usage_graph, 1, 2, 0, 1)
self.memory_usage_graph = sparkline.Sparkline()
self.memory_usage_graph.set_property("reversed", True)
self.window.get_widget("graph-table").attach(self.memory_usage_graph, 1, 2, 1, 2)
self.network_traffic_graph = sparkline.Sparkline()
self.network_traffic_graph.set_property("reversed", True)
self.window.get_widget("graph-table").attach(self.network_traffic_graph, 1, 2, 3, 4)
@ -806,15 +809,12 @@ class vmmDetails(gobject.GObject):
history_len = self.config.get_stats_history_length()
cpu_vector = self.vm.cpu_time_vector()
cpu_vector.reverse()
self.cpu_usage_graph.set_property("data_array", cpu_vector)
memory_vector = self.vm.current_memory_vector()
memory_vector.reverse()
self.memory_usage_graph.set_property("data_array", memory_vector)
network_vector = self.vm.network_traffic_vector()
network_vector.reverse()
self.network_traffic_graph.set_property("data_array", network_vector)
def refresh_config_cpu(self):