I want my application to be able to display frequency response graphs but I cannot find any graphing library that has this functionality. I am currently using MPAndroidChart for some other charts (and it is great !) but sadly I could not find any way to use it to do log plots. I have also tried using numAndroidCharts (numcharts logplot example), but that library seems broken/outdated since i couldn't even get the example code to work properly. Is there anyway that you know of to achieve this ?
I use MPAndroidChart with a log scale with converted values.
For example if your value is 1E-5:
value = 1E-5;
Entry entry = new Entry();
entry.setX(Math.log10(value)); // entry.getX() will return -5
Because your value now is -5 you need to create an AxisFormatter to show that it really represents an logarithmic value:
public class Log10AxisValueFormatter implements IAxisValueFormatter {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return String.format(Locale.ENGLISH, "%.2E", Math.pow(10,value));
}
}
You need to set an instance of this to your axis when you create your chart:
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new Log10AxisValueFormatter());
Related
I'm developing fitness application where I would like to display chart of user running activities over months, so something like this:
Graph how I want it
I'm using MPAndroidChart for it, but I'm getting some weird values on the x axis (the y axis is alright).
I have a map (monthMileage in the code sample), where the key is the month and the year in string, so for example Jul 2020 as "072020" and the value is the total distance of km which user ran that month.
profileViewModel.monthOverview.observe(viewLifecycleOwner, Observer { monthMileage ->
val entries = ArrayList<BarEntry>()
for ((k, v) in monthMileage) {
entries.add(BarEntry(k, v.toFloat()))
}
val dataSet = BarDataSet(entries, "Monthly mileage")
val barData = BarData(dataSet)
activity_graph.xAxis.labelCount = entries.size
activity_graph.xAxis.valueFormatter = ActivityGraphFormatter()
activity_graph.axisRight.axisMinimum = 0.toFloat()
activity_graph.axisLeft.axisMinimum = 0.toFloat()
activity_graph.data = barData
activity_graph.invalidate()
})
This is how I'm trying to set data to the Bar Chart. The x value is the month-year string to Float and y is the total distance. I use custom value formatter, which looks weird, but should be working with the right values (and the values are still wrong even when I don't use the formatter so there's error probably somewhere else). In the whole process the values in the entries, data set and data are still the values I'm expecting, but when it's set to the graph it's just wrong.
For example for 72020 and 62020 I'm getting 65000 and 70000.
There's probably some correlation I don't see so I welcome all advicess or if anybody knows about some nicer chart library for android mobile app, I've been searching, but all the time everybody is just praising 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
I am trying to add a String Label in order to label both the domain (x-axis) and the range (y-axis) of my LineChart, as shown in the picture below.
Any suggestions on how to do it using MPAndroidChart?
(Turning #Ironman's comment into an answer:)
As of MPAndroidChart 3.0.1 this is currently not possible using the API exposed by the library. You will need to add additional TextView outside the chart, or modify the library to your purpose.
If you want to modify the library to your own purpose, you will need to study the source code XAxisRenderer and subclass it to add functionality to draw the axis label you want.
You can apply this code
mChart.getAxisLeft().setEnabled(true); //show y-axis at left
mChart.getAxisRight().setEnabled(false); //hide y-axis at right
mChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return "string_" + (int) value; // yVal is a string array
}
});
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
I use actually the AndroidPlot library to use a simple chart in my android project but I don't know how i can change the values of domain zone.
More specific, this line:
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));
In the web site said that i can use another formats and its true, but if I use for example:
SimpleDateFormat("dd-MM-yyyy")
In the chart appears "31-12-1969" in all domain values.
Somebody know how I can change that date? or use another format (like String)?
Late answer, but maybe will be useful to other people.
I also had a hard time with this issue, especially since I am Java beginner.
I implemented a custom formatter. Seems a bit ugly solution but it works. Idea is the following:
take only Y axis for values, and leave X axis as indexes into an array (as Androidplot tutorial suggests, use using ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value)
each time your data changes, you need to pass to the Plot a new formatter with your new data for X axis
Here are some code snippets.
First is the class which transforms array index to a custom label String:
public class MyIndexFormat extends Format {
public String[] Labels = null;
#Override
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos) {
// try turning value to index because it comes from indexes
// but if is too far from index, ignore it - it is a tick between indexes
float fl = ((Number)obj).floatValue();
int index = Math.round(fl);
if(Labels == null || Labels.length <= index ||
Math.abs(fl - index) > 0.1)
return new StringBuffer("");
return new StringBuffer(Labels[index]);
}
And here is how I attach it to the Plot:
MyIndexFormat mif = new MyIndexFormat ();
mif.Labels = // TODO: fill the array with your custom labels
// attach index->string formatter to the plot instance
pricesPlot.getGraphWidget().setDomainValueFormat(mif);
This trick works also for dynamic updates.
NOTICE: Androidplot seems to have problems with drawing horizontal lines, so if your data has the same Y values, you might get strange results, I already asked for help on this issue.