← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5440: Web api: added chart resource for dynamic input

 

------------------------------------------------------------
revno: 5440
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-12-15 19:39:36 +0100
message:
  Web api: added chart resource for dynamic input
modified:
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2011-12-15 10:32:22 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2011-12-15 18:39:36 +0000
@@ -258,9 +258,8 @@
     public JFreeChart getJFreeOrganisationUnitChart( Indicator indicator, OrganisationUnit parent, boolean title,
                                                      I18nFormat format )
     {
-        RelativePeriods relatives = new RelativePeriods();
-        relatives.setThisYear( true );
-        List<Period> periods = periodService.reloadPeriods( relatives.getRelativePeriods( format, true ) );
+        List<Period> periods = periodService.reloadPeriods( 
+            new RelativePeriods().setThisYear( true ).getRelativePeriods( format, true ) );
 
         Chart chart = new Chart();
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2011-12-15 15:58:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2011-12-15 18:39:36 +0000
@@ -41,6 +41,10 @@
 import org.hisp.dhis.chart.Charts;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.i18n.I18nManagerException;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.util.ContextUtils;
 import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
@@ -68,6 +72,12 @@
     private ChartService chartService;
 
     @Autowired
+    private IndicatorService indicatorService;
+    
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+    
+    @Autowired
     private I18nManager i18nManager;
 
     //-------------------------------------------------------------------------------------------------------
@@ -108,29 +118,43 @@
     }
 
     @RequestMapping( value = {"/{uid}/data","/{uid}/data.png"}, method = RequestMethod.GET )
-    public void getChartPng( @PathVariable( "uid" ) String uid,
-                             @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
-                             @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
-                             HttpServletResponse response ) throws IOException, I18nManagerException
-    {
-        JFreeChart chart = chartService.getJFreeChart( uid, i18nManager.getI18nFormat() );
-
-        response.setContentType( ContextUtils.CONTENT_TYPE_PNG );
-        ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
-    }
-
-    @RequestMapping( value = "/{uid}/data.jpg", method = RequestMethod.GET )
-    public void getChartJpg( @PathVariable( "uid" ) String uid,
-                             @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
-                             @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
-                             HttpServletResponse response ) throws IOException, I18nManagerException
-    {
-        JFreeChart chart = chartService.getJFreeChart( uid, i18nManager.getI18nFormat() );
-
-        response.setContentType( ContextUtils.CONTENT_TYPE_JPG );
-        ChartUtilities.writeChartAsJPEG( response.getOutputStream(), chart, width, height );
-    }
-
+    public void getChart( @PathVariable( "uid" ) String uid,
+                             @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
+                             @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
+                             HttpServletResponse response ) throws IOException, I18nManagerException
+    {
+        JFreeChart chart = chartService.getJFreeChart( uid, i18nManager.getI18nFormat() );
+
+        response.setContentType( ContextUtils.CONTENT_TYPE_PNG );
+        ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
+    }
+
+    @RequestMapping( value = "/data", method = RequestMethod.GET )
+    public void getChart( @RequestParam( value = "in" ) String indicatorUid, 
+        @RequestParam( value = "ou" ) String organisationUnitUid,
+        @RequestParam( value = "periods", required = false ) boolean periods,
+        @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
+        @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
+        HttpServletResponse response ) throws Exception
+    {
+        Indicator indicator = indicatorService.getIndicator( indicatorUid );
+        OrganisationUnit unit = organisationUnitService.getOrganisationUnit( organisationUnitUid );
+        
+        JFreeChart chart = null;
+        
+        if ( periods )
+        {
+            chart = chartService.getJFreePeriodChart( indicator, unit, true, i18nManager.getI18nFormat() );
+        }
+        else
+        {
+            chart = chartService.getJFreeOrganisationUnitChart( indicator, unit, true, i18nManager.getI18nFormat() );
+        }
+        
+        response.setContentType( ContextUtils.CONTENT_TYPE_PNG );
+        ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
+    }
+    
     //-------------------------------------------------------------------------------------------------------
     // POST
     //-------------------------------------------------------------------------------------------------------