A problem occurred when using birt chart to create a chart:
After a fixed length and width are set for the chart, the chart may be too rough or too dense when presented. It seriously affects the chart presentation. If you can dynamically adjust the length and width of the chart based on the data volume.
For example, barNum = 5;
Set chart. width = 50 * barNum;
Content:
I. Introduction to chart Creation
Ii. Set chart size self-adaptation
I. Creating charts
The process is the same as that for creating a normal birt report. For more information, see birt Chinese Reference Manual. chm.
1: Create the template file chartResizingTest. rptdesign and configure the data source ClassicModels.
2: Create a test dataset to count the user ID, user name, and order quantity. Specify the maxID and minID parameters to limit the user ID range.
Select CLASSICMODELS. MERs. CUSTOMERNUMBER, CLASSICMODELS. MERs. CUSTOMERNAME, <br/> count (*) orderNum <br/> from CLASSICMODELS. MERs, CLASSICMODELS. ORDERS <br/> where CLASSICMODELS. MERs. CUSTOMERNUMBER = CLASSICMODELS. ORDERS. CUSTOMERNUMBER <br/> and CLASSICMODELS. MERs. CUSTOMERNUMBER> =? And CLASSICMODELS. CUSTOMERS. CUSTOMERNUMBER <=? <Br/> group by CLASSICMODELS. MERs. CUSTOMERNUMBER, CLASSICMODELS. CUSTOMERS. CUSTOMERNAME
3: Insert a statistical table and a column chart.
After completion, the Report Template looks like figure1
However, such a chart has an obvious defect. The chart length and width are fixed and cannot adapt to changes in the data volume. As a result, the generated chart cannot be viewed. For example, you have set Width: 300 points; Height: 200 points; when the data volume is small, the bar chart is very rough (ugly); when the data volume is large, the bar chart is very dense (not readable ).
If you can dynamically adjust the chart size based on the amount of data, the above problem can be well solved. Fortunately, the support provided by BIRT is still very powerful. It provides JS and Java interfaces for dynamic settings. For more information, see the following section.
Ii. dynamically adjust the chart size
Rewrite the chart onRender event using JavaScript, and dynamically adjust the chart Size Based on the queried data volume.
1: // obtain the data volume of the data set bound to the chart, and input the variable var barcnt;
/** <Br/> * called after populating the series dataset. <br/> * @ Param series <br/> * @ Param dataset <br/> * @ Param ICSC <br/> * ichartscriptcontext <br/> */</P> <p> function afterdatasetfilled (series, dataset, ICSC) <br/>{< br/> importpackage (packages. java. io); <br/> importpackage (packages.org. eclipse. birt. chart. model. type. impl); </P> <p> If (series. getclass () = lineseriesimpl) {<br/> series. getlineattributes (). setthickness (5); <br/>}< br/> If (series. getclass () = barseriesimpl) {<br/> If (series. getseriesidentifier () = "mybar") {<br/> var list = dataset. getvalues (); <br/> barcnt = List. length; <br/>}< br/>}
// "Mybar" is the custom name of the chart data area.
2: Get the value of the variable barcnt, and overwrite and set the chart size.
/** <Br/> * Called before generation of chart model to GeneratedChartState. <br/> * @ param chart <br/> * Chart <br/> * @ param icsc <br/> * IChartScriptContext <br/> */< /p> <p> function beforeGeneration (chart, icsc) <br/>{< br/> chart. getBlock (). getBounds (). setWidth (50 * barcnt); <br/> chart. getBlock (). getBounds (). setHeight (400); <br/>}
OK. Based on the data size of the data set bound to the chart, the chart width is dynamically set to 50 * barcnt. For the complete birt template, see "Birt chart dynamic size"
References:
1: birt Chinese Reference Manual. chm -- birt basic tutorials http://download.csdn.net/source/3318430
2: Birt chart dynamic size template source file: http://download.csdn.net/source/3392797
3: Birt chart width eclipse forum post: http://www.eclipse.org/forums/index.php/m/367583/#msg_367583