Custom bar chart in reporting

Hii all,

Created a report with a bar chart and placed a script in the bar chart. In the Preview tab, check that the x-axis 'ist_date' is not showing and the y-axis 'total_totalizer' data is showing.

Created store procedure for the fetch the data from database.

Bar chart configuration.
Defined data key : 'Monthly'

Configured the script for the custom bar chart.

def configureChart(data, chart):

    from org.jfree.data.category import DefaultCategoryDataset

    # Get the original dataset from the chart
    originalDataset = chart.getCategoryPlot().getDataset()
    
    # Create a new dataset
    filteredDataset = DefaultCategoryDataset()

    # Extract data from the original dataset
    for row in range(originalDataset.getRowCount()):
        for col in range(originalDataset.getColumnCount()):
            category = originalDataset.getColumnKey(col)  # Assume this is 'ist_date'
            series = originalDataset.getRowKey(row)  # Series name (e.g., 'total_totalizer')
            value = originalDataset.getValue(row, col)  # Data value

            # Add only 'total_totalizer' values to the new dataset
            if series == 'total_totalizer' and value is not None:
                # Scale the value by 1000
                filteredDataset.addValue(value * 1000, series, category)

    # Set the new dataset to the chart
    chart.getCategoryPlot().setDataset(filteredDataset)

When the preview tab the ist_date not displayed.

That looks like the date labels are too big, so the report is hiding them and showing ... instead. You would need to either decrease font size or format the date shown as the category to make it show less text.

1 Like

I would rotate the text of x-axis
After the for loop try this:

from org.jfree.chart.axis import CategoryAxis
from org.jfree.chart.plot import CategoryPlot
from java.awt import Font

...

#chart.getCategoryPlot().setDataset(filteredDataset) #replace this with:
plot = chart.getCategoryPlot()
plot.setDataset(filteredDataset)

#change xaxis 90 degree and size
xAxis = plot.getDomainAxis()
xAxis.setLabel("Date")
xAxis.setCategoryLabelPositions(CategoryAxis.createStandardCategoryLabelPositions().createUpRotationLabelPositions(90))
xAxis.setLabelFont(Font("Arial", Font.BOLD, 10))
xAxis.setTickLabelFont(Font("Arial", Font.PLAIN, 8))
1 Like

Yes, changed the date format and font size, now it's working.

Yes, but there is an option available to change the x-axis label position.

Thanks for the suggestion.

Now, my second question for the bar label position.

When select the bar label option, the data is displayed on each bar, but it is displayed horizontally.

Want to displayed vertically.

Is it possibale to change the bar label position using script?