MPAndroidChart how to always show the last point in viewport? - android

I am using MPAndroidChart.
Here https://github.com/PhilJay/MPAndroidChart/wiki/Modifying-the-Viewport i found that we can use moveViewToX(float xIndex) (Moves the left side (edge) of the current viewport to the specified x-index).
But if i need to show the last point always and does not matter how much the points there are?
Now i use moveViewToX(500); with the huge number as argument even if i have just 50 points, and its work.
BUT, it is not right i think!

Why not use
int numberOfYourEntries = ...; // get the number
chart.moveViewTo(numberOfYourEntries);

You could also use
lineChart.moveViewToX(lineChart.getXChartMax());

Related

MPAndroidChart set current visible X axis

Using MPAndroidChart, I'm struggling to figure out how to set the current visible x axis values. The use case is simple and I would have thought very common, so I'm sure I must be missing some function which can do this:
Say you have a chart with x axis values 1-100
A user zooms and pans a chart so that the range 60-80 is visible. I want to store these values, so that tomorrow when the user re-launches the app, I can restore the exact viewing state (60-80)
Storing the values is really easy - you can simply call chart.lowestVisibleX and chart.highestVisibleX to get the x axis values. But how do I set them on a new instance of the chart? Unfortunately there doesn't seem to be a chart.setHighestVisibleX or chart.setLowestVisibleX.
This previous question / answer is nearly, but not quite, what I need. The suggestion is to use a combination of chart.moveViewToX(60) and chart.setVisibleXRangeMaximum(20). However as the docs for setVisibleXRangeMaximum state:
Sets the size of the area (range on the x-axis) that should be maximum
visible at once (no further zooming out allowed)
I don't want to prevent further zooming, which is what this does. There must be a way to set the zoom level without actually restricting further zooming - but I can't figure it out. Any suggestions?
Thanks
Just to answer my own question, I decided to workaround this issue by resetting the X range maximum after calling moveViewToX. This appears to work. So the solution would be:
chart.setVisibleXRangeMaximum(20)
chart.moveViewToX(60)
chart.setVisibleXRangeMaximum(100)

MpAndroidChart: dashed line relating to data count

I need to show chart with dashed line in my application. I am using MpAndroidChart to achieve it. It works, but when data contains too much values, it works incorrectly. For example:
Only 7 values on chart:
Over 500 values:
How to fix it?
The library behaves as expected. The problem is that you are providing too many data points so that the curve overlaps itself (and thus the dashing is displayed as in your image).
Depending on your use case I can suggest the following solutions:
Reduce the line width (probably not sufficient in your case)
Smooth the data points (on this page an overview of possible methods is given)

MPAndroidChart: constant position of Y-axis lines independent of data

I'm using MPAndroid chart to display a line chart.
The chart is updated with different data and of course, when the data changes, the y-axis lines and the zoom level is changed because of the different sets of input data.
Is there any way to maintain the position of the y-axis lines and only change their labels according to the new data?
Thanks!
After a lot of tries and investigations, it seems that I found a solution just after posting the question.
Just in case someone else needs this, I used the setLabelCount function before but never the one with the force parameter.
The solution is:
mChart.getAxisLeft().setLabelCount(visibleYCount, true);
Forcing the label count will assure the number of y-axis lines and thus, their position is stable with different data sets.

App Inventor Slider : Setting it for whole values?

How do I set the slider to increment by a whole number, not by a decimal value of .N? I can get the value to translate to a label, but I don't want it in decimal value.
This was my original reference: https://www.youtube.com/watch?v=P80FkfGvHWw
Edit:
My value needs to be at 5 maximum... but I do not want to receive the decimal values at all. I eliminated most of the possible fractional values, but there are still some options.
Between 1-5, is there a mathematical way that can automatically round fractions to a whole number?
Thank you! (Please ignore the comment, that was for something else)
see the documentation
round
Returns the given number rounded to the closest integer. If the
fractional part is < .5 it will be rounded down. It it is > .5 it will
be rounded up. If it is exactly equal to .5, numbers with an even
whole part will be rounded down, and numbers with an odd whole part
will be rounded up. (This method is called round to even.)
Probably also a good idea is to use Do it, see also the Top 5 Tips how to learn App Inventor. You also can simplify your blocks a bit instead of using 3 times set global wholeInt ...

achartengine time chart set initial range

SOLUTION
Instead of using setInitialRange() I had to set the initial x-values using setXAxisMin() as well as setXAxisMax(). A little bit confusing, I think.
ORIGINAL QUESTION
I'm currently working on a project involving the acharteninge library for drawing time diagrams which works excellent so far. The user is capable of switching between various graphs (one XYMultipleSeriesDataset plus one TimeSeries per graph) via separate buttons. So far, achartengine displays all x values when the graph is shown.
However, I was asked to display only the data captured in the first three hours when the graph is shown, as this information is most relevant. Nevertheless, the user should still be able to scroll to data captured after the first three hours meaning that limiting the x-axis maximum value is not an option.
For example: There are two curves. The first curve is drawn using data captured over a period of 4 hours, while the second curve is created out of data captured over 18 hours.
In the current version my programme initially displays all data of the graph which gets displayed meaning the first curve shows three hours of very relevant data while 1 hour of not so important information is shown too. The "ratio" of the second curve is 3 to 15 (highly significant data vs regular data). As a result, a different amount of high important information is shown when the user compares the two curves making it harder for the user to compare this data, as he has to adjust the zoom manually.
EDIT 01.04.2015 15:46
For setting the initial range I use the XYMultipleSeriesRenderer instance's method setInitialRange(double[] initialRange); which does not affect the graph at all. Below you can see my statement (currentlyProcessedCurve.getRecordStart() is an instance of Joda's DateTime and furhtermore it's the x value of the first displayed point):
public static final int CHART_X_AXIS_INITIAL_MAX = 3;
public static final int CHART_MARGIN_PAN = 45;
...
double[] initialRange = { currentlyProcessedCurve.getRecordStart().minusMinutes(ApplicationSettings.CHART_MARGIN_PAN).toDate().getTime(), currentlyProcessedCurve.getRecordStart().plusHours(ApplicationSettings.CHART_X_AXIS_INITIAL_MAX).toDate().getTime(), 0, 600 };
currentDatasetRenderer.setInitialRange(initialRange);
TIDE (Edit end)
The screenshot below approximately shows how the curve should look initially (Just for making it clear: Please only consider the graphs' shapes, as they should look equal --> What I'm trying to say: I know that the x-axis and the y-axis label of the two screenshots different, but I don't have better pictures yet):
desired initial range
Finally, the following screenshot shows how the curve looks when displayed:
For enabling the user to compare the curves I have locked the y-axis zoom.
Any help is highly appreciated. Thanks in advance!
Have you consider pan option?
With this option you can limit axis maximum, and your chart will show the important data on start, while user can scroll left or right (wherever you have less important data points).
All you need to do is:
set pan enabled for axis you need
set pan limits (so user can't scroll past the last data point)
Hope this helps,
Cheers.
Instead of using XYMultipleSeriesRendererInstance.setInitialRange() I had to set the initial x-values using XYMultipleSeriesRendererInstance.setXAxisMin() as well as XYMultipleSeriesRendererInstance.setXAxisMax().
double xMinValue = ...;
double xMaxValue = ...;
currentDatasetRenderer.setXAxisMin(xMinValue);
currentDatasetRenderer.setXAxisMax(xMaxValue);

Categories

Resources