← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4631: Impl new chart resource

 

------------------------------------------------------------
revno: 4631
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-09-21 19:22:32 +0200
message:
  Impl new chart resource
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java
  dhis-2/dhis-services/dhis-service-reporting/pom.xml
  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/web/api/resources/ChartResource.java
  dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml


--
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-api/src/main/java/org/hisp/dhis/chart/ChartService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java	2011-07-21 03:29:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java	2011-09-21 17:22:32 +0000
@@ -49,6 +49,8 @@
     String ID = ChartService.class.getName();
 
     JFreeChart getJFreeChart( int id, I18nFormat format );
+    
+    JFreeChart getJFreeChart( Indicator indicator, OrganisationUnit unit, I18nFormat format );
 
     JFreeChart getJFreeChart( List<Indicator> indicators, List<DataElement> dataElements, List<Period> periods,
         List<OrganisationUnit> organisationUnits, String dimension, boolean regression, I18nFormat format );

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/pom.xml'
--- dhis-2/dhis-services/dhis-service-reporting/pom.xml	2011-09-16 16:54:03 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/pom.xml	2011-09-21 17:22:32 +0000
@@ -49,6 +49,10 @@
       <artifactId>jfreechart</artifactId>
     </dependency>
     <dependency>
+      <groupId>jfree</groupId>
+      <artifactId>jcommon</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
     </dependency>
@@ -56,14 +60,6 @@
     <!-- Other -->
     
     <dependency>
-      <groupId>jfree</groupId>
-      <artifactId>jfreechart</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>jfree</groupId>
-      <artifactId>jcommon</artifactId>
-    </dependency> 
-    <dependency>
       <groupId>jep</groupId>
       <artifactId>jep</artifactId>
     </dependency>

=== 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-09-07 13:14:46 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2011-09-21 17:22:32 +0000
@@ -82,6 +82,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.period.RelativePeriods;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
 import org.hisp.dhis.system.util.MathUtils;
@@ -234,6 +235,29 @@
 
         return getJFreeChart( chart, !chart.getHideSubtitle() );
     }
+    
+    public JFreeChart getJFreeChart( Indicator indicator, OrganisationUnit unit, I18nFormat format )
+    {
+        RelativePeriods relatives = new RelativePeriods();
+        relatives.setMonthsThisYear( true );
+        List<Period> periods = relatives.getRelativePeriods( 1, format, true );
+        
+        Chart chart = new Chart();
+        
+        chart.setTitle( indicator.getName() );
+        chart.setType( TYPE_LINE3D );
+        chart.setSize( SIZE_NORMAL );
+        chart.setDimension( Chart.DIMENSION_PERIOD_INDICATOR );
+        chart.setVerticalLabels( true );
+        chart.getIndicators().add( indicator );
+        chart.setPeriods( periods );
+        chart.setOrganisationUnit( unit );
+        chart.setFormat( format );
+        
+        chart.init();
+        
+        return getJFreeChart( chart, true );        
+    }
 
     public JFreeChart getJFreeChart( List<Indicator> indicators, List<DataElement> dataElements, List<Period> periods,
         List<OrganisationUnit> organisationUnits, String dimension, boolean regression, I18nFormat format )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java	2011-09-18 14:36:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java	2011-09-21 17:22:32 +0000
@@ -13,14 +13,19 @@
 
 import org.hisp.dhis.chart.Chart;
 import org.hisp.dhis.chart.ChartService;
+import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
+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.system.util.CodecUtils;
 import org.hisp.dhis.util.ContextUtils;
 import org.hisp.dhis.web.api.ResponseUtils;
 import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
 
-@Path( "/chart/{id}" )
+@Path( "/chart" )
 public class ChartResource
 {
     private ChartService chartService;
@@ -29,6 +34,20 @@
     {
         this.chartService = chartService;
     }
+    
+    private IndicatorService indicatorService;
+
+    public void setIndicatorService( IndicatorService indicatorService )
+    {
+        this.indicatorService = indicatorService;
+    }
+    
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
 
     private I18nManager i18nManager;
 
@@ -38,6 +57,7 @@
     }
 
     @GET
+    @Path( "/{id}" )
     @Produces( ContextUtils.CONTENT_TYPE_PNG )
     public Response getChart( @PathParam("id") Integer id )
         throws Exception
@@ -58,4 +78,36 @@
             }
         } ).build();
     }
+    
+    @GET
+    @Path( "/indicator/{indicator}/{orgUnit}" )
+    @Produces( ContextUtils.CONTENT_TYPE_PNG )
+    public Response getIndicatorChart( @PathParam("indicator") String id, @PathParam("orgUnit") String orgUnit )
+        throws Exception
+    {
+        final Indicator indicator = indicatorService.getIndicator( id );
+        
+        final OrganisationUnit unit = organisationUnitService.getOrganisationUnit( orgUnit );
+        
+        if ( indicator == null || unit == null )
+        {
+            return null;
+        }
+        
+        final I18nFormat format = i18nManager.getI18nFormat();
+        
+        final String filename = CodecUtils.filenameEncode( indicator.getName() + ".png" );
+        
+        final JFreeChart jFreeChart = chartService.getJFreeChart( indicator, unit, format );
+        
+        return ResponseUtils.response( true, filename, false ).entity( new StreamingOutput()
+        {
+            @Override
+            public void write( OutputStream out )
+                throws IOException, WebApplicationException
+            {
+                ChartUtilities.writeChartAsPNG( out, jFreeChart, 600, 400 );
+            }
+        } ).build();
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2011-09-17 04:39:45 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2011-09-21 17:22:32 +0000
@@ -40,6 +40,8 @@
   <bean id="org.hisp.dhis.web.api.resources.ChartResource" class="org.hisp.dhis.web.api.resources.ChartResource"
 	scope="prototype">
 	<property name="chartService" ref="org.hisp.dhis.chart.ChartService"/>
+	<property name="indicatorService" ref="org.hisp.dhis.indicator.IndicatorService"/>
+    <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
 	<property name="i18nManager" ref="org.hisp.dhis.i18n.I18nManager"/>
   </bean>