Use DTO entity as charts datasource

Hi,

I am trying to use DTO entity as a datasource for a chart, i have a normal JPA entity from which i am getting the data and then i programatically create a list of DTO objects then i set them to a DTO collection datasource but its not showing anything on the chart

xml

<data readOnly="true">
        <collection id="tripsDc" class="com.company.dashboardoasis.entity.Trip" fetchPlan="dailyTrip">
            <loader id="tripsDl" readOnly="true">
                <query>
                    <![CDATA[select e from Trip e
                    where e.date = :date
                    order by e.createdDate desc]]>
                </query>
            </loader>
        </collection>
        <collection id="dailyTripsDc" class="com.company.dashboardoasis.entity.DailyTrip">
            <loader id="dailyTripsDl" readOnly="true">
                <query>
                    <![CDATA[select e from DailyTrip e]]>
                </query>
            </loader>
            <fetchPlan extends="_base"/>
        </collection>
    </data>

    <facets>
        <dataLoadCoordinator auto="true"/>

        <timer id="timer"
               delay="4000"
               autostart="true"
               repeating="true"/>

    </facets>

controller

@ViewComponent
    protected Timer timer;
    
    @ViewComponent
    private CollectionLoader<Trip> tripsDl;
    @ViewComponent
    private CollectionContainer<Trip> tripsDc;
    @ViewComponent
    private CollectionContainer<DailyTrip> dailyTripsDc;
    @ViewComponent
    private CollectionLoader<DailyTrip> dailyTripsDl;
    @Autowired
    DashBoardService dashBoardService;

 @Subscribe
    public void onInit(final InitEvent event) {
        tripsDl.setParameter("date", LocalDate.now());
    }

    @Subscribe
    public void onBeforeShow(final BeforeShowEvent event) {

        tripsDl.load();
        refreshTrips();
    }

    @Subscribe("timer")
    protected void onTimerTick(Timer.TimerActionEvent event) {
        refreshTrips();
    }


    public void refreshTrips() {
        tripsDc.setItems(Collections.emptyList());
        tripsDl.load();

        List<DailyTrip> list = new ArrayList<>();
        list.addAll(dashBoardService.generateTrips(tripsDc.getItems()));
        dailyTripsDc.getMutableItems().addAll(list);
    }

And also how do i set the time facet to only run on the mainView’s initialLayout and it should stop when mainView displays other views

Thanks

Hi!

Have you already seen samples of programmatic entity display in out online demo application: UI Samples :: Charts - EntityDataItem?
If the problem is still exist, could you please provide a small test project in which it is not possible to display the DTO entity in a chart.

And also how do i set the time facet to only run on the mainView’s initialLayout and it should stop when mainView displays other views

To do this you can override the showRouterLayoutContent method in the MainView.

    @Override
    public void showRouterLayoutContent(HasElement content) {
        super.showRouterLayoutContent(content);
        
        if (content != null) {
            time.stop();
        }
    }

Regards,
Dmitriy

Hi @d.kremnev,

This worked, thanks