Search code examples
javachartscrystal-reports

CR4E Crystal Reports save modified ChartObject


I am attempting to programatically modify a chart within a Crystal Report using Java. The java then feeds the report to the viewer. Removing an item works, modifying does not.

//Experiment : Can we programatically modify a chart?
            ReportDefController rdc = reportClientDocument.getReportDefController();
            ReportObjectController roc = rdc.getReportObjectController();
            ReportObjects ros = roc.getReportObjectsByKind(ReportObjectKind.chart);

            logger.debug("There are " + ros.size() + " chart items");
            IChartObject ro = null;
            IChartObject ro_original = null;
            ISection iSection = null;
            for (int i = 0; i <ros.size(); i++){
                ro = (IChartObject)ros.get(i);
                ro_original = (IChartObject)ros.get(i);
                String rn = ro.getName();
                ChartStyle cs = (ChartStyle) ro.getChartStyle();
                cs.setEnableDataAxisAutoRange(false);
                cs.setEnableShowLegend(false);
                cs.setEnableDepthEffect(true);
                cs.setIsVertical(true);
                cs.setDataAxisMinValue(-2.0);
                cs.setDataAxisMaxValue(100.0);
                Double minVal = (Double)cs.getDataAxisMinValue();
                Double maxVal = (Double)cs.getDataAxisMaxValue();
                boolean d = cs.getEnableDepthEffect();
                boolean l = cs.getEnableShowLegend();
                boolean a = cs.getEnableDataAxisAutoRange();
                boolean v = cs.getIsVertical();
                ro.setChartStyle(cs);
                int sectionCode = ro.getSectionCode();
                iSection = rdc.getReportDefinition().getDetailArea().getSections().getSection(0);
                try
                {
                    //roc.modify((IChartObject)ros.get(i), ro);
                    rdc.modifyChartObject((IChartObject)ros.get(i), ro);
                    reportClientDocument.refreshReportDocument();
                    reportClientDocument.save();
                } catch (ReportSDKException e){
                    writer.println("Couldn't modify graph");
                    e.printStackTrace();
                }
                logger.debug("Chart named "+rn + " With Min Val " + minVal + " and Max Val " + maxVal +" with depth " + d + " and legend " + l + " autorange " + a + " Vertical " + v);
            }

I've tried the modify method of ReportObjectController and the modifychartobject method of ReportDefController, and have tried refreshReportDocument and save to attempt to get something to update, but nothing's happening. Logger is showing that the values are updating as you'd expect. Any ideas?


Solution

  • My mistake was in not cloning the object at...

    ro = (IChartObject)ros.get(i) 
    

    ...so it should read...

    ro = (IChartObject)ros.get(i).clone(false)
    

    ..so that..

    roc.modify((IChartObject)ros.get(i), ro)
    

    .. will now work. Hope this helps someone else having similar fun and games.