orbisgis / orbisgis-official-plugins

A set of optional gui components provided as optionnal features of OrbisGIS
1 stars 3 forks source link

ChartService #32

Closed ebocher closed 7 years ago

ebocher commented 7 years ago

The idea is to offer a way to display a JfreeChart object onto a docking element.

The user , developer access to a factory that permits to create a chart from any point in OrbisGIS (Groovy, menu on a table or on a layer in the TOC).

The syntax could be

ChartService chart =  Services.getService(ChartService.class);
chart.createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, String query)

The implementation will be

@Component(service = {ChartService.class})
public class ChartService {

    private Connection con;

    /**
     * Create a bar chart
     * @param title
     * @param categoryAxisLabel
     * @param valueAxisLabel
     * @param query
     * @throws SQLException 
     */
    public void createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, String query) throws SQLException{
        JDBCCategoryDataset dataset = new JDBCCategoryDataset(con, query);
        JFreeChart chart = ChartFactory.createBarChart(title, categoryAxisLabel, valueAxisLabel, dataset);
        ChartPanel chartPanel = new ChartPanel(chart);     
        //Create the Docking element from the chartPanel
        ???
    }

    @Reference
    public void setDataSource(DataSource ds) throws SQLException {
        con = ds.getConnection();
    }

Any comments are welcome.

nicolas-f commented 7 years ago

Good idea. Please use the existing mechanism of editable element as done for all editors in OrbisGIS. Use the same template as the SQL editor:

Action to open a new editor: https://github.com/orbisgis/orbisgis/blob/master/bundles/sqlconsole/src/main/java/org/orbisgis/sqlconsole/MainWindowMenu.java

Implementation of editor factory: https://github.com/orbisgis/orbisgis/blob/master/bundles/sqlconsole/src/main/java/org/orbisgis/sqlconsole/SQLConsoleFactory.java

Implementation of the editor itself: https://github.com/orbisgis/orbisgis/blob/master/bundles/sqlconsole/src/main/java/org/orbisgis/sqlconsole/SQLConsole.java

ebocher commented 7 years ago

I understand how to create the editor factory. The main problem is to build a ChartService that exposes several methods :

ChartService chart = Services.getService(ChartService.class); chart.createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, String query)

ChartService chart = Services.getService(ChartService.class); chart.createXYChart(String title, String categoryAxisLabel, String valueAxisLabel, String query)

...

ebocher commented 7 years ago

Forgot to said thanks for comments.

nicolas-f commented 7 years ago

Services static class is a thing of the past.

An implementation of EditableElement will store all the data related to the chart. Then this EditableElement feed an instance of EditorManager:

editorManager.openEditable(new ChartElement(title, label, valueLabel ...));

As all OSGi services, in order to get the instance of editor manager you have multiple choice:

    private EditorManager editorManager;
    @Reference
    public void setEditorManager(EditorManager editorManager) {
        this.editorManager = editorManager;
    }

    public void unsetEditorManager(EditorManager editorManager) {
        this.editorManager = null;
}

OR

BundleContext thisBundle = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<?> serviceReference = thisBundle .getServiceReference(EditorManager.class.getName());
// serviceReference  may be null if not available
if(serviceReference != null) {
    EditorManager editorManager= (EditorManager ) thisBundle .
        getService(serviceReference );
}

https://osgi.org/javadoc/r4v43/core/org/osgi/framework/FrameworkUtil.html

ebocher commented 7 years ago

Started here https://github.com/ebocher/orbisgis-view-plugins/tree/chartService

nicolas-f commented 7 years ago

Open the connection only when used by the application into a try with resources. Store the datasource.

ebocher commented 7 years ago

Done