Creating Dynamic Charts with ShinobiCharts for Android


#1

Hello,

I am having difficulty creating charts without inflating an existing XML refrence.  I need to be able to dynamically create charts and add them to a list, is there a working example or sample available showing how to create a chart which is not defined first in XML?


#2

Hi AEB,

Here’s some code from our internal test app, which is creating charts dynamically, every time we press a button:

@Override
        public void run(TestAreaFragment testAreaFragment, ShinobiChart chart) {
            LinearLayout layout = (LinearLayout) testAreaFragment.getView().findViewById(
                    R.id.test_area_layout);
            ChartView newChartView = new ChartView(layout.getContext());
            newChartView.setLayoutParams(
                    new LinearLayout.LayoutParams(
                            10,
                            LayoutParams.MATCH_PARENT,
                            1
                    ));
            layout.addView(newChartView);

            ShinobiChart shinobiChart = newChartView.getShinobiChart();
            ChartDataBuilder.setUpColumnChart(shinobiChart);

        }

Here we’re adding ChartViews to a linear layout. This may not be the exact scenario you’re looking for, but it’s certainly possible. 

Android does get quite tricky about this sort of thing, particularly in relation to Activity lifecycles (e.g. device rotation causes the the Activity to be torn down and recreated), and this is made harder for you because a Chart is not a simple UI component and contains much internal state, including your data adapters. ChartFragment handles all this for you, but I’d strongly advise you to look at our How To: Manage Chart Lifecycles user guide too.

Best regards,

Robin Sillem

Lead Developer

ShinobiControls


#3

Thanks for your help.  I was attempting to dynamically build a variable number of charts in either a ListView or within a LinearLayout wrapped in a scroll view.  I did intialy hit an issue due to the fact that I was attempting to build my charts via “onActivitiyResult” so all calls to *.getShinobiChart were null.  After i sorted that I was running into it started to work.  I was struggling with the layout parameters, and could never get it to display properly.  This example looks promising, Ill give it another shot.

As a suggestion, I think it wouldbe worthwhile to have a tutorial available in how to use the charts in a listview, using a custom adapter.  It was something i was able to do within 5 minutes using AndroidPlot, but struggled with for hours in Shinobi.  I’m no expert, so Ill consider some of the issue to be between the screen and keys:)