I have multiple DataSet in line chart and i want to highlight all the Y points with custom overlay image. do we have any function for it ?
If not then
is there any function which can return corresponding pixel values for an entry object ?
Any direction would be appreciated.
Thanks you for reading.
The method is given by getPixelForValues(x,y) in Transform class
val viewPortHandler = ViewPortHandler()
viewPortHandler.setChartDimens(canvas.width.toFloat(),canvas.height.toFloat())
val transformer = Transformer(viewPortHandler)
var mpPointD = transformer.getPixelForValues(x, y)
But the transformation is not working for me with the points I pass but I know that the lib uses it with similar data that I set
Related
MPAndroidChart seems to force you to have data points in sorted for x.
I have a plot that returns to x = zero when the y values are negative the data set would look like this:
val dataVals = ArrayList<Entry>()
dataVals.add(Entry(0f, 0f))
dataVals.add(Entry(1f, 5f))
dataVals.add(Entry(2f, 5f))
dataVals.add(Entry(3f, 2f))
dataVals.add(Entry(4f, 0f))
dataVals.add(Entry(3f, -3f))
dataVals.add(Entry(2f, -3f))
dataVals.add(Entry(1f, -3f))
dataVals.add(Entry(0f, 0f))
val dataSet = LineDataSet(dataVals, "datatest")
Getting an error code java.lang.NegativeArraySizeException: which is fixed when the x values are sorted.
The desired plot would look similar too:
Does anyone know of a way this is possible in MPAndroidChart? If not if there are alternatives I am happy to change charting libraries?
I have started using HelloCharts which I believe is no longer updated but seems to do the trick for this type of data.
It is not as good for plotting livedata though.
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 have intention on looking for a specific point in a line chart of MPAndroidChart and then display the the marker to highlight the point after a button is hit. The example given is where the marker is only displayed after touching event which is different in my case. I tried code below but to no avail, can some one please teach me I would appreciate.
Highlight h = new Highlight((int) valIndex, linechart2.getData().getDataSetCount());
linechart2.highlightValue(h, true);
mv2.refreshContent2(valueYAxis.get((int) valIndex), h);
linechart2.getMarkerView();
linechart2.setDrawMarkerViews(true);
linechart2.getData().setHighlightEnabled(true);
// RefreshChart();
linechart2.invalidate();
You can easily highlight values programmatically by using one of the following methods on your Chart object:
highlightValues(Highlight[] highs): Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.
highlightValue(int xIndex, int dataSetIndex): Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.
It's all in the wiki.
the makerview will show when the point is highlighted, so you can try this
Highlight h = new Highlight((int) valIndex, 0);
mv2.refreshContent2(valueYAxis.get((int) valIndex), h);
linechart2.setMarker(mv2);
linechart2.highlightValue(h);
I'm using jjoe64's awesome GraphView for Android. At the moment I'm trying to determine 2 things:
Is it possible, once plotted, to get a DataPoint's x,y location values?
Also, is it possible to get that location in the onTap call?
I'm trying to show a custom view just above the data point (tapped or otherwise); hence the reason I need it's coordinates. I know how to get the x,y position of a user touch, but the issue is I also need the value from the datapoint.
From what I can tell the DataPoint and DataPointInterface do not have an accessible x,y location value - only x,y double values (non-location related).
I was hoping for something like:
DataPoint p = series.getPoint(n);
int x = p.getXLocation();
int y = p.getYLocation();
Where n is either the exact position in the series (like getting something from an ArrayList) or n represents the non-location x value given to the DataPoint upon creation (new DataPoint(double x, double y)).
Has anyone else using this library solved this? Any help would be much appreciated. Thank you!
I ended up subclassing BaseSeries and basically doing a complete replication of LineGraphSeries with the exception of making some of those protected functions public. This gave me the ability to keep track of datapoints and their PointF's in a Map, then look up PointF's based on their corresponding datapoint.
views.setOnDataPointTapListener(new OnDataPointTapListener() {
#Override
public void onTap(Series series, DataPointInterface dataPoint) {
Toast.makeText(graph.getContext(), "Data : On Data Point clicked: "+dataPoint, Toast.LENGTH_SHORT).show();
}
});
I found this to show data-points.Its quite easy as you can see.
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.