Closed Venkataramana0801 closed 1 month ago
Hi @Venkataramana0801!
Do you see any errors in logcat when running this code? Is the chart completely blank or are the axes visible?
You don't need to use reload
and redraw
in this case; just chartView.setOptions(options);
is sufficient.
I changed your loop, where you fill the lists, to use random floats, because I don't have access to your specifica data. I also used a placeholder string for dateFormatList
:
for (int i = 0; i < 100; i++) {
dateFormatList.add("dateFormat");
usageList.add(random.nextFloat() * 100);
tempList.add(random.nextFloat() * 100);
humidityList.add(random.nextFloat() * 100);
heatIndexList.add(random.nextFloat() * 100);
windSpeedList.add(random.nextFloat() * 100);
}
The code is working fine, so it means that your data list could be invalid at some point. Please try to debug its values or share a small sample if you can, so I can test it on my side with your data.
@MikolajMichalczak Thanks for you response, as you said I did sample graph and the sample graph code added in the method, the below is the method
private void setupSampleGraph() {
Log.e("calling ", "@@@ setupSampleGraph");
HIOptions optionsTest = new HIOptions();
HIChart chartTest = new HIChart();
chartTest.setType("column");
optionsTest.setChart(chartTest);
HITitle titleTest = new HITitle();
titleTest.setText("Demo chart");
optionsTest.setTitle(titleTest);
HIColumn seriesTest = new HIColumn();
seriesTest.setData(new ArrayList<>(Arrays.asList(49.9, 71.5, 106.4, 129.2, 144, 176, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4)));
optionsTest.setSeries(new ArrayList<HISeries>(Collections.singletonList(seriesTest)));
chartView.setOptions(optionsTest);
}
And if I run this method in onCreateView method in the fragment, the graph is loading fine, the following is that method
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layout_view = inflater.inflate(R.layout.fragment_usage_test_interval, container, false);
chartView = layout_view.findViewById(R.id.hc);
// new UsagegraphServ(getActivity(), new String[]{"07/29/2024"}, "Get60minIntervalUsageData").execute();
setupSampleGraph();
return layout_view;
}
But if I run the same method in onPostExecute of AsyncTask, the graph is not loading, it is displaying blank only, the following is the onPostExecute of AsyncTask.
protected void onPostExecute(Integer i) {
if (pgrsDialog.isShowing())
pgrsDialog.dismiss();
setupSampleGraph();
}
I have verified in the logcat, there is no error displaying, here what is the issue with AsyncTask class. And also I am using the latest gradle dependencies
implementation 'com.github.highcharts:highcharts-android:11.4.6'
@Venkataramana0801 First of all, according to the documentation AsyncTask has been deprecated due to several issues, some of which may align with the problems you encountered:
AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions from doInBackground, and does not provide much utility over using Executors directly.
Given these issues, we cannot guarantee that Highcharts will always function correctly with AsyncTask
.
However, I implemented a chart with AsyncTask
and saw a console message:
HIOptions not attached. Chart won't render without options.
.
Since Highcharts is a JavaScript-based framework, synchronization issues can arise. The best approach is to load all chart options before starting the async task with an empty series. Then, in the callback, update the options and series by creating a new list with your data and setting the series again.
Alternatively, another workaround that worked for me was to call chartView.setWillNotDraw(true)
in the onCreateView
method (before executing async task) and then call chartView.setWillNotDraw(false)
after setupSampleGraph()
in the onPostExecute()
method (or at the end of setupSampleGraph()
function).
Thank you for your quick response. It is working fine as per your these two lines, chartView.setWillNotDraw(true)
and chartView.setWillNotDraw(false)
.
In My application, to get the data I am using AsyncTask.
new UsagegraphServ(getActivity(), new String[]{"07/29/2024"}, "Get60minIntervalUsageData").execute();
After successful data, I am calling setupGraph(GraphType.INIT); in the onPostExecute method of AsyncTask the following is the setupGraph method, and also I tried with sample value 100, but not displaying the graph and also tried with debugging all the values are setting the graph, but no luck