MPAndroidChart specify values on X-axis and scaling axises to the screen - android

I use the Amazing MPChart, however i have trouble setting other than index values
on on the X-Axis.
Do anyone know how to set it, should i modify labels of the axis?
The documentation say that the x-value is set in the data object, bit
X-values is always and index, as far as i can see.
My goal is to have values of time on the x-axis, statistics for 1 week 2 weeks one month etc.

You can do this by using these line of code. You can understand this code if you are familiar with mpchartlibrary android.
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("1 week");
xVals.add("1 Month");
data= new LineData(xVals, setData);
mChart.setData(data);

Related

Formatting Dates on x-axis in MPAndroidChart

I'm using MPAndroidChart and I know this topic is covered quite a bit in the docs, so apologies if I've missed something obvious, but I'm having a bit of trouble formatting timestamp values into dates on the x-axis of my Linechart.
It appears that getFormattedValue() isn't being invoked for my x-axis value for whatever reason. If i debug AxisBase, it only seems to looking to format the y-axis values, which i do not have a custom formatter set on.
Here is the code snippets, taken mostly from the Github docs:
LineChart chart = (LineChart) findViewById(R.id.chart);
XAxis xAxis = chart.getXAxis();
// set min timestamp to Jul 2017
HourAxisValueFormatter(Long.parseLong("1499814559"));
xAxis.setValueFormatter(new IAxisValueFormatter() {
// THIS IS NEVER INVOKED!
#Override
public String getFormattedValue(float value, AxisBase axis)
{
return new Date(Long.parseLong(""+value)).toString();
}
});
// rating is a map of timestamp to numeric value, both stored
// as strings
for (Map.Entry<String, String> rating : ratings.entrySet()) {
String timestamp = rating.getKey();
String ratingValue = rating.getValue();
entries.add(new Entry(Float.parseFloat(timestamp),
Float.parseFloat(ratingValue)));
}
// add entries to dataset
LineDataSet dataSet = new LineDataSet(entries, "Ratings");
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // refresh'
I'm using version 3.0.2 of the library:
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
Thanks in advance, Gary
EDIT per request for desired output/actual output:
Ideally i would have a series of dates along the x-axis , e.g. 10/7, 11/8, 12/8, etc. constructed from the Epoch values I've provided. I know the code above wont format it in that format as it stands, but just want to get it formatting to any date value initially (so ignore that for now), the crux of the issue is that my formatter isn't getting invoked at all.
Below is a screenshot of the current output, it only prints a single value on the x=0 line (note even though there are multiple y values being provided, i wonder if they are all on the same x=0 line does it just draw the last point or something?):
Debugging the timestamp values above, Float.parseFloat(timestamp) is equal to 1.50516885e^12. The actual timestamp value for example is 1505168846751, but after parsing to a Float is rounded, and given an exponent value.
EDIT 2 :
Screen grab of the values supplied as 'entries' to the LineDataSet:
EDIT 3:
The issue appears to be to do with the size/format of the timestamp values for the x-values I'm providing, changing these to small float values in the range 1.0 -> 20.0f, causes the formatter to be invoked as expected. I need to debug the code further to see why the exponential timestamp values are not being formatted.
From looking into this i think this is a limitation at the moment, for this specific use case (timestamps are very close together) since Entry will only accept floating point values, my timestamps are all getting passed as 1.50516885e^12, even though the precise timestamp values are slightly different. When AxisRenderer works out the range for the x-axis then is sees it as 0, and sets the entries to 0 also, in this block here

Android - MPAndroidChart - Overlay 3 bars

This question has been made here, but still has got no answer...
Using StackedBarActivity example from MPAndroidChart library to draw a 3 merge bars, and setting the values as [1,2,3]... the chart is showed with a total of all numbers, 1+2+3=6.. but what I want is to merge all values in order to value 2 overlay value 3 and value 1 overlay value 2, as showed on the image bellow:
Basically, I want to use the StackedBarActivity activity, but not stack all the bars, instead, I want to put one behind another.
Visually I know I can subtract the bigger value with the smaller, making a sum until reaching 3 (1+1+1), but then the value 3 will retain value 1 and not value 3.
Is there a way I can do this using this class? Is there a better class to do this?
I know I am like 6 months late with answer, but I think that is working solution:
List<String> xVals = getXVals(); //some function to get values
List<BarEntry> yVals = new ArrayList();
int[] barColors = new int[bars.size() * numberOfValues];
int index=0;
for (Bar bar : bars) {
List<BarEntry> yValsForBar = new ArrayList();
for (int i=0; i<numberOfValues; i++) {
yValsForBar.add(new BarEntry(bar.value(i), i));
barColors[i+index*numberOfValues] = bar.color();
}
yVals.addAll(yValsForBar);
index++;
}
BarDataSet dataSet = new BarDataSet(yVals, "data set");
BarData data = new BarData(xVals);
data.addDataSet(dataSet);
And here, you should have overlying bars in different colors (note: of course you might not see some bars if they have bigger values than next ones, if you wan't to sort them you will have to do some modifications to this). At least works with me.
Expanding on n4yArh's answer.
With the latest version of the library, the constructor for BarEntry has changed to BarEntry(x,y).
So the line
yValsForBar.add(new BarEntry(bar.value(i),i));
should be changed to
yValsForBar.add(new BarEntry(i,bar.value(i));
and of course to use the color array, the following line is missing
dataSet.setColors(barColors);

How to format values inside MPAndroidChart?

2 Questions about the MPAndroidChart library.
All my yvalues are integers, but displayed as Decimals.
How can I get them displayed as integers (without the digits)?
How can I prevent that the ylabels are also shows as decimals?
I know there is a setFormatter for the yvalues, just don't understand how to use...
Have a look at the IValueFormatter interface provided by the library. With that interface, you can completely customize what gets displayed on the chart based on your own logic.
Usage:
chart.setValueFormatter(new YourValueFormatter());
YLabels yl = chart.getYLabels();
yl.setFormatter(new YourValueFormatter());
UPDATE (for versions 2.0.0+ of this [library][2]):
Now, a ValueFormatter can be set for each DataSet separately, or the same ValueFormatter can be set for the whole data object containig all DataSets. Furthermore, the YLabels class is now called YAxis.
Example:
// set for whole data object (individual DataSets also possible)
LineData data = new LineData(...);
data.setValueFormatter(new YourValueFormatter());
// YLabels are now called YAxis
YAxis yAxis = mChart.getAxisLeft(); // get the left or right axis
yAxis.setValueFormatter(new YourAxisValueFormatter());
UPDATE (for versions 3.0.0+ of this [library][2]):
The interfaces for formatting have been renamed and extended in their functionality. Now, the IAxisValueFormatter can be used to format values of both XAxis and YAxis. The IValueFormatter interface is used to customize chart values.
Link to the ValueFormatter documentation.
If all you want is to change the number of decimal places on the values, the
DefaultValueFormatter will suffice.
pieDataSet.setDefaultValueFormatter(new DefaultValueFormatter(digits = 1))
//where digits is the number of decimal places
The example above will format 88.65 as 88.7

Displaying all x axis labels; not just ones where a point value exists

I'ms setting custom text labels on my line chart, using this on the renderer:
String[] xLabelsForTimeScale = getXLabelsForCurrentTimeScale(); //weekday names
for (int i = 0; i < xLabelsForTimeScale.length; i++)
{
mRenderer.addXTextLabel(i, xLabelsForTimeScale[i]);
}
mRenderer.setXLabels(0);
mRenderer.setShowCustomTextGridX(true);
when the chart renders, it's only displaying x labels for points where I have a non null series value. For example, assume that the xLabelsForTimeScale array is an array of weekday abbreviations. If I have only two values, say for Mon, Tue, it only shows labels for Mon and Tue. I'd like to display all the x axis labels, whether there's a series point or not. Sun - Sat, basically. How can I do that?
ADDITIONAL INFO ON REQUIREMENT: I don't want to add zero, dummy points within the series if I can at all help it. What I expect to happen is the line just renders between actual points, connecting each actual, valid point, in a linear fashion. If I add zero, dummy points, there will be a lot of pointy tops (lack of a better term) as the line draws between valid point data, and placeholder, dummy data. Hope that makes sense. Thanks.
You can avoid this for time charts by calling:
renderer.setXRoundedLabels(false);

Achartengine : reverse plot

I'm using achartengine in my Android Application, it works well but i have a question.
Is it possible to reverse plot?
I mean in my graph, new values which are added to my XY series with the function add, are added at end of the series, and then the graph is redrawn, so new values appears at right of the graph.
But i want to add my new value at index 0, in order to show on the graph only the last X values.
Sorry if it's not very clear
Thanks
There is no support for inserting data to XYSeries at a given index. However you could get to this behavior by clearing the current data and adding it back, with the new value included. I know this would not be a very optimal way, but it helps you get to your needed behavior.
Edit: I added an add(index, x, y) method in XYSeries. You can download a version including this feature here.
So, i've found a trick:
Dataset is my XYseries and mRenderer my multipleSeriesRenderer
I've changed alignment of the Y axis:
mRenderer.setYAxisAlign(Align.RIGHT, 0);
I've erased automatic labels:
mRenderer.setXLabels(0);
The 0 on the axe X is now a custom label, so every time a new value is added to the graph, I have to change is position:
mRenderer.setXAxisMax(dataset.getItemCount() - 1); mRenderer.addXTextLabel(dataset.getItemCount() - 1, "0");
And to finish, i change the X axis min everytime a new value is added, in order to get a fix scale:
mRenderer.setXAxisMin(dataset.getItemCount() - scale - 1);

Categories

Resources