MP Android Chart, custom xAxis labels only on entries - java

1) I'm practicing stuff with graphs in order to add that feture to my app, I want the upper labels ( the xAxis base ) to be shown only where entries occur.
I haven't found a suitable solution online yet, and currently it appears on every xAxis from first entry to last entry as in the picture below:
I want it to be without the one sI deleted, as shown in the picture below:
2) and the second question I'm struggling with it is that I want to be able to draw for example in (x=5, y=7) and after it to draw at (x=1, y =3), but it wont let me add an entry with a smaller x that any other entry that already in the graph.

You have to extend from ValueFormatter class.
for more detail take a look at link
You can pick your desired logic to make the label disappear with returning "".
for example:
public String getFormattedValue(float value) {
if ((int)value <= 0) //your logic to evaluate correctness
return ""; // make lable go away
//...
}
UPDATE 2 (in Kotlin):
There is another overload for getFormattedValue which have a AxisBase parameter and you can use mEntryCount or mEntries.
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
if (axis?.mEntryCount!! <= 0)
return ""
}

Related

Clearing textbox from StdDraw to write new input

I'm not sure if this would be a simple question to answer on here, just because I'm using the Standard Draw class written by Princeton University, and I'm not sure if it's a globally known class.
But I'd much appreciate any feedback from those familiar with the StdDraw library.
What I'm trying to do is fairly straight-forward; check to see if the mouse of the user clicks onto an input box I drew, and if it is clicked, clear the existing text (which simply says "input") to make an empty String.
This is what it looks like so far:
public boolean handleClick(double x, double y) {
if(!super.handleClick(x,y)){
value = false;}
else {
if(highlighted){
value = true;
StdDraw.textLeft(xCentre+0.005,yCentre," ");} //Add the label
else{
value = false;}
}
return value; //I handled it. Nobody else should.}
}//handleClick
super.handleClick(x,y) is simply a method in the super class that draws the dimensions of the box:
public void draw(){
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(xCentre,yCentre,halfWidth,halfHeight);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(); //Default thin line.
StdDraw.rectangle(xCentre,yCentre,halfWidth,halfHeight);
}
value is simply an instance variable of this class that will return true if all conditions are satisfied:
private boolean value;
highlighted is a boolean instance variable from the super class that simply states if the box is an input or output box.
My main question would be, is the line
StdDraw.textLeft(xCentre+0.005,yCentre," ");
the right way to clear the existing text and create an empty String with StdDraw? As it's not clearing the line, but maybe there's a bug elsewhere in my code that I'm missing and this line should work?

Add space between graph and tick labels in GraphView?

I am trying out the GraphView Library for creating charts on Android. It looks quite decent, but I am wondering if there is a way to add some space between the tick labels and the graph itself. As you can see, there is basically none:
I use the following code to set up the graph (very similar to the example):
GraphView graph = (GraphView)view.findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3)
});
graph.addSeries(series);
I tried using graph.getGridLabelRenderer().setPadding(), but that just added padding around the whole graph.
So, is there a way to put some padding around those labels?
yes it is possible in the current version in github (will be released in 4.0.1).
There is the method:
graph.getGridLabelRenderer().setLabelsSpace(x)
Follow this example to give your graph a custom label formatter. By doing so, you can at least add space padding to your y-axis labels (if not newline spacing to your x-axis labels).
// GraphView 4.x
graph.getGridLabelRenderer().setLabelFormatter(
new DefaultLabelFormatter() {
#Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// show normal x values
return super.formatLabel(value, isValueX);
} else {
// show currency for y values
return super.formatLabel(value, isValueX) + " €";
}
}
}
);
I pulled this example from the GraphView documentation.
Otherwise, I found it interesting that someone chose this answer as the best response for a similar question.

GWT infinite scroll with discarding start-of-list results

Looking for a GWT DataGrid component which implements infinite scroll, BUT also makes sure to discard the results no longer visible on the screen : such as the previously loaded results that are not shown anymore.
This is to avoid a memory hog.
I've been trying to find this on Google, but no luck so far.
Please note : I could take a JS library and adapt it to what I need, but I don't think it would work good with GWT's DataGrid component.
Edit: I am interested specifically in an infinite scroll which ALSO discards/releases the topmost results that are not visible (and loads them up as appropriate).
Any ideas ?
As a matter of fact the showcase example has an infinite scrolling CellList. (you can find the code there).
Although this was done with a CellList the same principles should also apply to a DataGrid.
Check out the ShowMorePagerPanel.java file.
Update:
The onScroll function of ShowMorePagerPanel.java will add the new records at the bottom. However you can easily change the behavior:
Something along the lines (not tested tough):
HasRows display = getDisplay();
if (display == null) {
return;
}
boolean loadData = false;
// If scrolling up, change newStart
int oldScrollPos = lastScrollPos;
lastScrollPos = scrollable.getVerticalScrollPosition();
// get the current visible Range
Range currentRange = display.getVisibleRange();
if (oldScrollPos >= lastScrollPos) {
int newStart = Math.max(
currentRange.getStart() - incrementSize,0);
loadData = true;
}
int maxScrollTop = scrollable.getWidget().getOffsetHeight()
- scrollable.getOffsetHeight();
if (lastScrollPos >= maxScrollTop) {
// We are near the end, so increase the page size.
int newPageSize = Math.min(
display.getVisibleRange().getLength() + incrementSize,
display.getRowCount());
loadData = true;
}
if (loadData) {
display.setVisibleRange(newStart, newPageSize);
}

Adding % sign to Y-Axis Labels in Bar Chart AChartEngine

I am working on an android app and using AChartEngine for Charting. The Bar Chart is drawn on the basis of the dynamic data coming from a server.
Th Y-Axis labels are set to be shown from 0 to 100 and no of labels are 11 s it shows
0..10..20..30..40..60..70..80..90..100 as Y-Axis Labels. Is it possible to set custom Y-Axis labels such that it adds '%' sign after the Y-Axis title value so that it shows,
0%..10%..20%..30%..40%..60%..70%..80%..90%..100% as Y-Axis label values.
How to do it??
All what you need to do is: Enjoy ;)
renderer.addXTextLabel(0, "0");
renderer.addYTextLabel(10, "10%");
renderer.addYTextLabel(20, "20%");
renderer.addYTextLabel(30, "30%");
renderer.addYTextLabel(40, "40%");
renderer.addYTextLabel(50, "50%");
renderer.addYTextLabel(60, "60%");
renderer.addYTextLabel(70, "70%");
renderer.addYTextLabel(80, "80%");
renderer.addYTextLabel(90, "90%");
renderer.addYTextLabel(100, "100%");
renderer.setYLabels(0);
In order to set custom labels on the Y axis, you just need to use the following method:
mRenderer.addYTextLabel(10, "10%");
mRenderer.addYTextLabel(20, "20%");
...
Also, if you want to hide the default labels, do this:
mRenderer.setYLabels(0);
I haven't actually done a lot with AChartEngine, but a quick glance at the source code suggests you could extend BarChart to accomplish what you're after.
Have a look at the getLabel(double label) method located in AbstractChart (BarChart extends XYChart, which on its turn extends AbstractChart).
/**
* Makes sure the fraction digit is not displayed, if not needed.
*
* #param label the input label value
* #return the label without the useless fraction digit
*/
protected String getLabel(double label) {
String text = "";
if (label == Math.round(label)) {
text = Math.round(label) + "";
} else {
text = label + "";
}
return text;
}
I would start with something naive to see how that works out; e.g. simply append "%" on the result of above:
#Override protected String getLabel(double label) {
return super.getLabel(label) + "%";
}
Only thing I'm not too sure about is whether the same method is used to generate labels for the X-axis. If that's the case, you'll probably need to do something slightly smarter to enable it just for the axis you're interested in.

GWT - ListBox - pre-selecting an item

I got a doubt regarding pre-selecting(setSelectedIndex(index)) an item in a ListBox, Im using Spring + GWT.
I got a dialog that contains a panel, this panel has a FlexPanel, in which I've put a couple ListBox, this are filled up with data from my database.
But this Panel is for updates of an entity in my database, thus I wanted it to pre-select the current properties for this items, allowing the user to change at will.
I do the filling up in the update method of the widget.
I tried setting the selectedItem in the update method, but it gives me an null error.
I've searched a few places and it seems that the ListBox are only filled at the exact moment of the display. Thus pre-selecting would be impossible.
I thought about some event, that is fired when the page is displayed.
onLoad() doesnt work..
Anyone have something to help me out in here?
I really think you can set the selection before it's attached and displayed, but you have to have added the data before you can select an index. If this is a single select box you could write something like this:
void updateListContent(MyDataObject selected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
theListBox.setSelectedIndex(list.indexOf(selected));
}
If this is a multiple select box something like this may work:
void updateListContent(List<MyDataObject> allSelected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theMultipleListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
for (MyDataObject selected : allSelected) {
theMultipleListBox.setItemSelected(list.indexOf(selected), true);
}
}
(Note I haven't actually compiled this, so there might be typos. And this assumes that the selected element(s) is really present in the list of possible values, so if you cant be sure of this you'll need to add some bounds checking.)
I've been happily setting both the values and the selection index prior to attachment so as far as I'm aware it should work. There's a bug however when setting the selected index to -1 on IE, see http://code.google.com/p/google-web-toolkit/issues/detail?id=2689.
private void setSelectedValue(ListBox lBox, String str) {
String text = str;
int indexToFind = -1;
for (int i = 0; i < lBox.getItemCount(); i++) {
if (lBox.getValue(i).equals(text)) {
indexToFind = i;
break;
}
}
lBox.setSelectedIndex(indexToFind);
}
Pre-selection should work also with setValue()-function. Thus, no complicated code is needed.

Categories

Resources