Hello, since some time ago I’ve been using libgoffice to make charts in Gnome applications, instead of using Cairo directly.
This library was separated from Gnumeric , so you could make the same charts that it paints.
The results are very good, but the unique issue that I had was that there is very little documentation about the topic.
Here are some advices to make a GObject “painter of charts”:
- GObject attributes:
GtkWidget chartWidget; // Widget that will contain the components
GogPlot *plot; // Plot
GogLegend *legend; // Legend
- Initialize:
libgoffice_init ();
go_plugins_init (NULL, NULL, NULL, NULL, TRUE, GO_PLUGIN_LOADER_MODULE_TYPE);
self->priv->chartWidget = g_object_ref_sink (go_graph_widget_new (NULL));
- Paint one chart of example:
// Get the embedded graph
graph = go_graph_widget_get_graph (GO_GRAPH_WIDGET (self->priv->chartWidget));
// Get the chart created by the widget initialization
chart = go_graph_widget_get_chart (GO_GRAPH_WIDGET (self->priv->chartWidget));
// Create plot and add to the chart giving its name [1]
self->priv->plot = (GogPlot *) gog_plot_new_by_name (”GogBarColPlot”);
g_object_set (G_OBJECT (self->priv->plot),
“horizontal”, FALSE,
“type”, “stacked”,
“overlap_percentage”, 100,
“guru-hints”, “backplane”,
NULL);
gog_object_add_by_name (GOG_OBJECT (chart), “Plot”, GOG_OBJECT (self->priv->plot));
// Create a series for the plot and populate it with data.
// gchar **legends -> go_data_vector_str_new
// gdouble *values -> go_data_vector_val_new
GogSeries *series;
GOData *data;
GError *error;
// New serie. Each serie is a set of values, one for each legend. We need to set its name to see it in the legend.
// Every time that we need a serie, we have to do gog_plot_new_series, but only adding data, not the legends.
series = gog_plot_new_series (self->priv->plot);
gog_object_set_name (GOG_OBJECT (series), "My first serie", NULL);
data = go_data_vector_str_new ((char const * const *) legends, size, g_free);
gog_series_set_dim (series, 0, data, &error);
data = go_data_vector_val_new (values, size, g_free);
gog_series_set_dim (series, 1, data, &error);
// Add another serie of data
series = gog_plot_new_series (self->priv->plot);
gog_object_set_name (GOG_OBJECT (series), "My second serie", NULL);
data = go_data_vector_val_new (values, size, g_free);
gog_series_set_dim (series, 1, data, &error);
// Add a legend to the chart and get it to be able to clear in the future
gog_object_add_by_name (GOG_OBJECT (chart), "Legend", GOG_OBJECT (self->priv->legend));
self->priv->legend = (GogLegend *) gog_object_get_child_by_name (GOG_OBJECT (chart), "Legend");
- Clean the chart: (i.e. clean components from the widget, to paint another)
gog_object_clear_parent (GOG_OBJECT (self->priv->plot));
g_object_unref (self->priv->plot);
gog_object_clear_parent (GOG_OBJECT (self->priv->legend));
g_object_unref (self->priv->legend);
- Dispose:
libgoffice_shutdown ();
[1]: All the plot names and their properties can be queried at /usr/lib/goffice/version/plugins/plot_type/plot-types.xml
I hope that this help to anybody
