Displaying only a part of graph using Achartengine - android

I am using Achartengine to generate TimeChart graph. The data set consists of dates form 1/15/2003 to 12/4/2040 (x-axis) with respective random values for Y-axis. I am displaying the graph dynamically where I keeps reading the values on background thread (AsyncTask) and repaints the graphview. I have 2 questions:
The view starts from Jan, 2, 1970(I dono why) and I have to scroll to 1/15/2003 to see the graph. What should I change to make it start from 1/15/2003 ?
Also I take 2 date values FROM and TO (Eg: FROM:2/17/2004 TO:6/23/2006) and I want to display the graph only in this range. Is there any way to do this?

I could solve the 1st one using mRenderer.setYAxisMin(new Date("1/15/2003 11:16:00 AM").getTime()) Although this is a deprecated method, but it did the work for me. Now when I display the graph its starts from given data not Jan, 2, 1970.

You can dynamically set Y axis min and max with values desired values, just before repainting.
And for the 1. question, maybe better option is to set pan limits, so you can't scroll to empty parts of your chart.
You can do it like this
mRenderer.setPanLimits(new double[]{xMin, xMax, yMin, yMax});
where you calculate limits like this
double xMin = minDate.getTime();
double xMax = maxDate.getTime();

Related

MPAndroidChart gradient line depending on dataset values

I'm currently working on a project using MPAndroidChart and i want to make a LineChart to represent data per hour. I need this chart to have gradient line depending on values on yAxis.
eg. In the graph above i want the line to change the color when yAxis values are >50.
I didn't find any solution to my problem, so any suggestions or examples are welcome
I've recently had to face the exact same problem, and the solution was to define a linear gradient and apply it as a shader, like this:
(Warning: You'll notice that the code looks funny, and that is because it is xamarin code (i.e: C#, not java nor kotlin), but you'll be able to translate it easily, mostly you have to change the PascalCase for camelCase, and change some properties into setters and getter or vice versa, just use your IDE's intellisense)
[set your line chart, add the dataset, etc]
...
var gradient = new LinearGradient(0f, 0f, 0f, 100f,
[your first color], [your second color],
Shader.TileMode.Clamp);
var paint = vh.Chart.Renderer.PaintRender;
paint.SetShader(gradient);
...
[set axis, labels, grid, etc]
...
lineChart.Invalidate(); //this is to refresh the chart on the screen, you may need it or not depending on your code
In the code above you just have to add your colors, and you may want to change the gradient direction (I'm using a vertical gradient here) by changing the 4 first floats when creating the linearGradient. One typical change is to set the y0 = 0 and y1 = the height of your chart, for example. You have to play with the values according to your layout.
The result for this code is something like:
Of course, I'm showing it with a repeated sample dataset, that's why you are seeing two cards with the same line.
UPDATE:
I understand what you want to achieve, and with the code above you can do it. You will need some calculations, of course, but you can set the "trigger" where the color starts to change at any Y coordinate you want.
For example, in this first picture, I've used the coordinates 0,0,0,100
(Please keep in mind that I'm showing you the last value in dp, but in the real code I am translating it to the equivalent in pixels according to the device's resolution. It is roughly the height of my chart)
And in the following image, I've changed it to 0,0,0,50:
As you can see, you have full control over how the gradient is shown just by changing the 4 values. (or, in my case, just one of them)
(You may notice that I've changed the colors as per my design, it has nothing to do with the threshold)
Lets say you are using for-loop to populate your data. In list you need to populate a list of colors for every value and then use lineDataSet.setColors(listOfColors).
List<Integer> listOfColors = new List<>();
for(i= 0; i<dataEntries.size(); i++){
//Logic for colors
if(i<= dataEntries.size()-1){ //Otherwise app will crash on last index
if(dataEntries.get(i+1).y > 50)
listOfColors.add(Color.GREEN);
else
listOfColors.add(Color.RED);
}
Then in the last
lineDataSet.setColors(listOfColors)
Hope, this will help you.

MPAndroidChart centre view on middle of chart

I am using the MPAndroidChart library to graph live sensor data in a line graph. I'm hoping to give the data a sort of 'buffer' on the right-hand end so that new data is in the centre of the screen. I've tried to illustrate what I mean below:
Basically, I have got it so the ViewPort scrolls and shows the most recent item in the right-most end of the screen, but I would like it in the centre. Thanks!
you need to set Xaxis maximum, set visibleXrange and last move the viewport
double range = 5; // how many data you want to show in view port
double maxX = 100; // your highest X value
chartView.getXAxis().setAxisMaximum(maxX + range/2);
chartView.setVisibleXRange(range, range);
chartView.moveViewToX(maxX);
chartView.notifyDataSetChanged();

MPAndroidChart - how to draw grid line for only max and min value in y axis(left axis)?

I use MPAndroidChart to check the graph of the polonomial equation.
I want to get the maximum and minimum values ​​in the graph obtained, but grid line of y axis(left axis) like this.Current Chart
Unlike the above figure, I would like to draw the y grid line of the graph as follows.The chart I want to get
Is there any way to display grid line of max and min value?
I tried to solve it using the setValueFormatter function, but I do not know how to approach it.
https://weeklycoding.com/mpandroidchart-documentation/axis-general/
You can use setShowOnlyMinMax(boolean enabled)
That is used to showing min / max label

How to set x-axis label for every y-value in MPAndroidChart?

I want to set a label in the x-axis for every y-value I have in my LineChart. I want to obtain this result:
Can someone help me in finding the correct way of doing this in MPAndroidChart?
You may be looking for granularity - see the wiki page for the axis and the javadoc.
Calling like this:
mChart.getXAxis().setGranularity(0.3f);
mChart.getXAxis().setGranularityEnabled(true);
will ensure that 0.3 is the minimum interval on the axis when you are zoomed in. Apart from that you may have to tweak the the scale of the x-axis by calling:
mChart.getXAxis().setAxisMinValue(10.01f); //experiment with these values
mChart.getXAxis().setAxisMaxValue(10.14f);
mChart.getXAxis().setLabelCount(5);

MPAndroidChart how to set y-axis min and max value or set default zoom?

I use MPAndroidChart to display the stock chart, but the Y line value is so close,the chart is like this
I can use finger zoom like this way
Which is I want the default looks like
Is there any way to set Y line max and min value or set default zoom.
I tried the lib function , but it does not work
Take a look at the documentation of the YAxis. There are various methods that allow you to control the axis range.
setStartAtZero(boolean enabled): If this is enabled, this axis will always have it's minimum value at zero (0), no matter which kind of data the chart displays.
setAxisMaxValue(float max): Set a custom maximum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
setAxisMinValue(float min): Set a custom minimum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
setSpaceTop(float percent): Sets the top spacing (in percent of the total axis-range) of the highest value in the chart in comparison to the highest value on the axis.
setSpaceBottom(float percent): Sets the bottom spacing (in percent of the total axis-range) of the lowest value in the chart in comparison to the lowest value on the axis.
There is a kind of dependency between left and right axises. I had only right axis enabled and set
chart.getAxisRight().setStartAtZero(false);
but that didn't work till I set the same parameter to my leftYAxis, which was disabled. So I have now
chart.getAxisLeft().setStartAtZero(false);
chart.getAxisRight().setStartAtZero(false);
edit:
setStartAtZero is deprecated, the code recommends using setAxisMinimum instead.
* This method is deprecated.
* Use setAxisMinimum(...) / setAxisMaximum(...) instead.
You can find more on that in the documentation.
You can also use the
mChart.setVisibleYRangeMaximum(150, AxisDependency.LEFT);
method to set highest visible value on y axis.
use setScaleMinima()
and chart.moveViewToY(max), YAxis.AxisDependency.LEFT);
is the best solution.
please try this , i am not sure that what your want.when i face minim zoom level in MPAndroidChart i used this method like this,
mChart.setPinchZoom(true);
mChart.setScaleMinima(200,200); //for min and max value for zoom
i
Hope to be ok for your problem
None of the other answers worked for me (I'm not sure why). However, I made it work using:
ratingTracker.getAxisLeft().mAxisMaximum = max;
ratingTracker.getAxisLeft().mAxisMinimum = min;
Hope this works for you!
Mp Android Line chart in this fix the Y-Axis Minimum and maximum value so your graph line value start middle of the graph chart.
use this function so your problem will solve
yAxis.setAxisMinimum();
yAxis.setAxisMaximum();
in this code you can put any value you want and your graph Y-axis line
Minimum and Maximum value
Try below line of code :
YAxis yAxis = chart.getAxisLeft();
yAxis.setEnabled(false);
yAxis.setAxisMinimum(-400);
yAxis.setAxisMaximum(400);

Categories

Resources