MPAndroidChart homescreen widget - android

is there any example, howto place any of charts in MPAndroidChart library to homescreen widget?
//i tried to use getChartbitmap(), but
1)bitmap is not created immediately after calling invalidate(), so it return null
2)i dont see way, howto initialize chart class without placing resources - which i cant do
for widget.
anybody have some successful example ?

The chart needs some milliseconds to draw the content onto the Bitmap. That is the reason why calling getChartBitmap() right after invalidate() will not return a valid Bitmap.
Try using a Handler and delay the time before retrieving the Bitmap by abound 100ms.

Try this code
BarData chartData =...
BarChart chart = new BarChart(mContext);
chart.setData(chartData);
chart.measure(View.MeasureSpec.makeMeasureSpec(300,View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(500,View.MeasureSpec.EXACTLY));
chart.layout(0, 0, chart.getMeasuredWidth(), chart.getMeasuredHeight());
Bitmap chartBitmap = chart.getChartBitmap();

In Activity with chart, function getChartBitmap() working fine! It's return bitmap without any errors. IMHO that's the problem, in widget we don't have an activity, placing created chart in layout is not take effect :( So, we can't have a widget with this chart at all, and it's very sad - this chart so really cool.

Related

android kotlin click listener loses its "click methods" after some time

Hi I'm implementing click listeners in the following way but after some time the methods and variables inside the listener's closure get the wrong values or something. Let me explain the implementation of the listener a little better a for loop creates the listener for a set of image views then later in the program the for loop is called a second time and it resets the listener methods and variables to different values. Everything works great for about 30 minutes but then for some reason, the listener's methods and variables start having the wrong values. Has anybody ever heard of this behavior or can tell me where I've gone wrong with the code? Keep in mind that the listener I'm about to paste here is just a small piece of a 1014 line class. I'm hoping somebody can spot How I'm implementing the listener wrongly and can give me some advice on how to "reset" the listener so that it's variables and values stay over time. Hopefully you can read the code without putting it in an editor but feel free to copy it for readability's sake Here is the code for the image view listener with comments.
//image views are held in an array
//set an image view in its imageview container
imgArr0[slotId1].invalidate()
imgArr0[slotId1].setImageDrawable(null)
//drw is not defined in this example
imgArr0[slotId1].setImageDrawable(drw)
/*if video or image id is set to image then set a listener for the image
*/
/*slotId1 is not defined in this example but it is simply a counter to iterate over the ImageView array
*/
if (videoOrImageId0[slotId1] == "image") {
//null any listeners that might be attached to the image view
imgArr0[slotId1].setOnClickListener(null)
//set or reset the listener
imgArr0[slotId1].setOnClickListener() {
`enter code here`//if the current config is portrait then set a new image image
if (currentConfig0 == "portrait0") {
act0.lrgImage0.invalidate()
act0.lrgImage0.setImageDrawable(null)
/*drw is not defined in this example but works fine in the production script
*/
act0.lrgImage0.setImageDrawable(drw)
}
--calmchess
ccc tv application with problem.
(https://i.stack.imgur.com/PjdbN.jpg)![enter image description here](https://i.stack.imgur.com/FaMnc.
I was able to partially solve this question by destroying all the image views and their associated click listeners then rebuilding those... However I don't consider this issue completely solved so if anybody can provide a better solution I'd love to hear it because rebuilding the images every few minutes has to be using a lot of unnecessary hardware resources.
--calmchess

onChartFling never called in MPAndroidChart

I have problem that onChartFling method is never called in my LineChart or BarChart. All other methods from OnChartGestureListener are called correctly, but these not. I put a breakpoint alo int BarLineChartTouchListener. Not called also.
Or is they any other way how to detect end of scrollin on fling ?
onChartTranslate is called multiple times, is it possible to find out if onChartTranslate was called when fling stopped ?
The author says, the way to solve this is to modify the library:
"The reason therefore is, that I do not want to permanently recognize a fling-gesture or single tap when the chart is zoomed in and panned / dragged.
Therefore, gestures do not work in zoomed mode.
If you want to disable this, you will have to modify the library."
These lines should be modified:
if(mTouchMode == NONE) { // remove the if
mGestureDetector.onTouchEvent(event);
}
Code for changing is here: https://github.com/PhilJay/MPAndroidChart/issues/405

Alternative for android.graphics.Path

I've been struck hard with an issue with the path class that is used to draw smooth lines on canvas with the canvas.drawPath(path,paint) function . Path class is useful for smoothing out lines with the path.quadTo() and cubeTo() function . But they do not let you draw a smoothed out line with varying thickness . I want to draw a Path on the canvas with increasing thickness up to a certain threshold width and then slim out at the end . Also i tried using a number of paths at every touch point of user but that fails when the user moves his finger really fast , because at that time a single path of a long length is obtained . Please help me i am in big trouble with this . Is there any other way of smoothing out lines .
Thank You
I think you are asking how to vary the width of the Path that is drawn using the canvas.drawPath() method. The following code snippet should help you regarding that:
private Paint myPaint;
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeJoin(Paint.Join.ROUND);
myPaint.setStrokeCap(Paint.Cap.ROUND);
if(someFlag != thresholdValue)
myPaint.setStrokeWidth(20);
else
{
myPaint.setStrokeWidth(someReducedValue); // or have a counter updated in your thread to regularly decrement the value
}
//..
..//
canvas.drawPath(path, myPaint); // inside onDraw() where path corresponds to your Path variable
The correct way to do this is to use the PathMeausre class , where we can easily get subPaths from a prent path and manuplate them accordingly . When i ll be done with the code ,i ll post the snippet soon.

Implementing a chart with AFreeChart into a View

Here is my problem: I am using AFreeChart to display a chart in my activity. The reason why I used AFreeChart was because I first finished this chart with JFreeChart, and, realized after that, it wasn't compatible with Android.
So, with AFreeChart, I could create the same chart with exactly the same code, but I don't know how to display it on a View.
Here I am creating the chart:
private void creerGraphique(){
//Here I have the creation of the DateSet
AFreeChart chart = ChartFactory.createXYLineChart(
"Mois", // Title
"Jours", // x-axis Label
"Repetitions", // y-axis Label
graph, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
}
Here I want to use it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graphique);
this.stockTableau();
this.creerGraphique();
//HERE: How can I display it since it's already created
}
I downloaded the AFreeChart demo code, but a function which wasn't on the package was used, and so, I couldn't use it too.
I thank you for your help.
PS: I'm not an english, so I hope my problem is clear, do not hesitate to ask me more details.
Have you looked at the sample in AFreeChart? It's quite straight forward, look at what they did for this chart for instance :
http://code.google.com/p/afreechart/source/browse/trunk/afreechart_sample/src/org/afree/chart/demo/view/PieChartDemo01View.java
They extend a DemoView which is basically an Android View with a setChart method, and pass the chart to the View.
So either extend DemoView or create you own equivalent if you don't need everything that's in it and follow the sample !
Good luck.
It is also worth noting that using the code you've pasted above, it would be helpful to call the chart's draw() method when you want it to draw.
As a simple example, if you were using a SurfaceView, you could create a method something like the following:
private void drawChart(AFreeChart chart, ChartRenderingInfo info) {
getHolder.lockCanvas();
chart.draw(canvas, area, info);
getHolder().unlockCanvasAndPost(canvas);
}
Where 'canvas' and 'area' have been set.
This is useful if you are looking to do a very simple implementation where you don't want to use the DemoView discussed above.

How a view is created in android?

I want to know how a View is created in Android. On a View object creation which functions are called?Initially constructors will be called.Next what? And so on.
If anybody know the sequence of functions called after object creation,Please reply me.
Thanks
This might help you better I guess.
1) Constructor : to initialize variables
2) onMeansure : to prepare canvas , set Width and height
3)OnDraw() : to draw actual view .
now onDraw() will continuously be called after a particular interval which depends upon display/processor and UI eventsenter code here or on calling invalidate()

Categories

Resources