cancel
Showing results for 
Search instead for 
Did you mean: 

CR2008 - Set chart titles during run time

Former Member
0 Kudos
217

Hello community!

Is there a way to change the chart titles during runtime with the fullversion?

I have the following code to change the text of the "FieldHeadingObject" and "TextObject" and this is working.


foreach (CrystalDecisions.CrystalReports.Engine.ReportObject repObject in sec.ReportObjects)
{
   if (repObject.Kind == ReportObjectKind.TextObject ||
       repObject.Kind == ReportObjectKind.FieldHeadingObject)
     {
        ((CrystalDecisions.CrystalReports.Engine.TextObject)repObject).Text = "my new Text";
     }
}

How can I change the titles of the chart object?

I know already that there is the class "ChartTextOptionsClass"

Does someone know how I could access the TextOptions in the ChartObject so that the new Titles are displayed in the report?

Thank you

Alex

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello

I found the following code to access the properties.


CrystalDecisions.ReportAppServer.Controllers.ReportObjectController crRasObjectControler;
crRasObjectControler = report.ReportClientDocument.ReportDefController.ReportObjectController;

foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject crRasReportObject
               in crRasObjectControler.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindChart))
             {
                crRasReportObject.ChartStyle.TextOptions.DataTitle = "my Data Title";
                crRasReportObject.ChartStyle.TextOptions.GroupTitle = "my Group Title";
                crRasReportObject.ChartStyle.TextOptions.Title = "my Title";
             }

In the debuger, I see that the values are changed. How can those values be displayed?

Do I have to save/change/modify the chart object?

How can I make this?

Thank you for your help

Alex

Edited by: Alexander Frwirth on Jul 6, 2010 11:49 AM

former_member183750
Active Contributor
0 Kudos

You'll have to do a bit of cloning... The code below should give you a good start:


        CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
        ISCDReportClientDocument rcd;
        rpt.Load(path);
        rcd = rpt.ReportClientDocument;

        CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject oldChart;
        CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject newChart;
        
        oldChart = (ChartObject)rcd.ReportDefController.ReportObjectController.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindChart)[0];
        newChart = (ChartObject)oldChart.Clone(true);
        newChart.ChartStyle.TextOptions.Title = "My New Chart Title";
        rcd.ReportDefController.ModifyChartObject(oldChart, newChart);

        CrystalReportViewer1.ReportSource = rpt;

Ludek

Follow us on Twitter http://twitter.com/SAPCRNetSup

Edited by: Ludek Uher on Jul 6, 2010 12:15 PM

Answers (3)

Answers (3)

Former Member
0 Kudos

I removed the chart titles and replaced those with text boxes.

The content of the text boces could be modified without to much effort.

Thank you Ludek

Former Member
0 Kudos

Hello Ludek!

I tried your proposals but without success.

The chart style is "crChartStyleTypeBar" and I access the first chart in the report if any reports exist.

I have one thought about it. All my charts have a cross tab as datasource. Could it be that the cross tab is processed to late (when the report is shown) and this causes the error in the chart data source?

Other solution would be to supress the chart titles and replace them with text fields as those could be accessed without problems. It is not very clean but it should be working. I think I will give this a try.

Thank you

Alex

Former Member
0 Kudos

Hello Ludek!

Thank you for the example code. I modified them to meet my requirements.


void LoadReportLanguageResources(ReportClass report)
{

   foreach ( CrystalDecisions.CrystalReports.Engine.Section sec in report.ReportDefinition.Sections)
   {
      foreach (CrystalDecisions.CrystalReports.Engine.ReportObject repObject in sec.ReportObjects)
      {
         if (repObject.Kind == ReportObjectKind.TextObject ||
              repObject.Kind == ReportObjectKind.FieldHeadingObject)
         {
            ((CrystalDecisions.CrystalReports.Engine.TextObject)repObject).Text = "test";
         }
      }
   }

   CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
   CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
   report.Load();
   rptClientDoc = report.ReportClientDocument;

   CrystalDecisions.ReportAppServer.ReportDefModel.ReportObjects myCharts;
   myCharts = rptClientDoc.ReportDefController.ReportObjectController.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindChart);

   foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject oldObject in myCharts)
   {
      if (oldObject.Kind == CrReportObjectKindEnum.crReportObjectKindChart)
      {
         CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject oldChart;
         CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject newChart;
         oldChart = (CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject)oldObject;
         newChart = (CrystalDecisions.ReportAppServer.ReportDefModel.ChartObject)oldChart.Clone(true);

         newChart.ChartStyle.TextOptions.Title = "My new Title";
         newChart.ChartStyle.TextOptions.DataTitle = "My new data Title";
         newChart.ChartStyle.TextOptions.SeriesTitle = "My new series Title";

         rptClientDoc.ReportDefController.ModifyChartObject(oldChart, newChart);
      }
   }
}

But I am hvaing problems with the method modifyChartObject(oldChart, newChart). When the method is called i get an COMException with a message like "The data fields of the diagramm are not valid" (German message: "Die Datenfelder des Diagramms sind ungültig"); ErrorCode -2147213287;

LoadReportLanguageResources is called via the following functions - I left out the outermost functions where the reportingList is loaded and the parameter is built.


//Checks the list with the report items and call the main load method
void IReportView.LoadHusReport(IList<HusRepItem> reportingList, BDEA01RangeParameter parameter)
{
   if ( reportingList.Count > 0)
   {
      reportClass = new repHusReport();
      reportClass.SetDataSource(reportingList);
      LoadReport(reportClass, "Hus Station Report", parameter);
   }
}

//
void LoadReport(CrystalDecisions.CrystalReports.Engine.ReportClass myReport, string title, BDEA01Parameter parameter)
{
   //check the parameters
   //set the parameters for the report (start and end time of the displayed report range)

   myReport.SummaryInfo.ReportTitle = title;
   
   if(reportViewer == null)
   {
      crCrystalReportViewer = new CrystalReportViewer();
      reportViewer.Child = crCrystalReportViewer;
   }

   LoadReportLanguageResources(myReport);
   crCrystalReportViewer.ReportSource = myReport;

}

Is there something I have forgotten?

I checked the datafields of the old and the new chart and both have 0 entries before the ModifyChartObject method is called.

If the method is not called, the report is functioning and all values/diagrams are displayed correctly.

Thank you Ludek for your help.

Alex

Edited by: Alexander Frwirth on Jul 7, 2010 10:13 AM

former_member183750
Active Contributor
0 Kudos

Try commenting out u201CReport.Load()u201D as this is used for loading the reportdefinition of Strongly Typed reports and since you've already called SetDataSource() on the report, the reportdefinition should already be loaded.

I only tested with a single bar chart and only changed the title. I donu2019t know how many charts you have or what types of charts.

So, let's see if simplyfing this will help; one chart (bar), no subreports, change just the title, then keep increasing the complexity / charts.

Also, try to put a breakpoint at ModifyChartObject(oldchart,newchart) and see if it fails on a specific chart in the report. If so, whatu2019s special about this chart? Is it suppressed? Is it possibly missing one of the properties being set (ie the series or data titles)?

Oh, and try hard-coding the chart index if it fails on the first chart just to check if its all charts in the report. If the method fails for all charts in the report, start with just setting the Title property and add properties from there.

Ludek