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

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.

Related

MP Android Chart, custom xAxis labels only on entries

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 ""
}

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.

Libgdx Label multiline text height

i have done a small test on LibGdx, on Multi-line Label, it seems that i cant get the wrapped line's height. Following is the code. Theoretically, height for aLebel should be > bLabel. But the result appear the same.
code:
aLabel.setText("this is a super long long long text that need wrapping."); // line wrapped into 3 lines
aLabel.setWrap(true);
aLabel.setWidth(470);
doLog("aLabel.getHeight(): " + aLabel.getHeight());
bLabel.setText("this is short."); // unwrapped line
bLabel.setWrap(true);
bLabel.setWidth(470);
doLog("bLabel.getHeight(): " + bLabel.getHeight());
result:
aLabel.getHeight(): 45.0
bLabel.getHeight(): 45.0
Do anyone have any idea how to get the actual multi-line height in LibGdx? Thanks in advance.
I had this issue for years and accidentally solved it by setting the width and packing the label twice. Note that multiline labels were never intended to figure out their own width, so you have to set them externally, preferably from it's parent.
public Label createLabel() {
// Create label and set wrap
Label label = new Label("Some long string here...", skin);
label.setWrap(true);
// Pack label
label.pack(); // This might not be necessary, unless you're changing other attributes such as font scale.
// Manual sizing
label.setWidth(textWidth); // Set the width directly
label.pack(); // Label calculates it's height here, but resets width to 0 (bug?)
label.setWidth(textWidth); // Set width again
return label;
}
LibGDX version used: 1.6.4
Pack sizes the widget to its pref size, nothing more. Pref width of a label with wrapping is 0.
Label label = new Label(...);
label.setWrap(true);
label.setWidth(123);
label.setHeight(label.getPrefHeight());
I had the same issue and it seems there doesn't exist a method in Label class to solve this. Also, I agree with you, the getHeight() method should return the real height of the Actor, so I don't know if that's a bug or there is a reasoning behind that behaviour.
Anyways, how I solved the issue is by using BitmapFont's getWrappedBounds method. It's not short, but for your example it would be the following:
doLog("aLabel.getHeight(): " + aLabel.getStyle().font.getWrappedBounds(aLabel.getText(), aLabel.getWidth()).height);
This could be done by adding a restriction to the cell that contains the Label in the Table:
Label label = new Label("Example", new Label.LabelStyle(font, Color.WHITE));
label.setWrap(true);
Table table = new Table();
table.add(label).width(WITH);
For more information about how to use Table go to: https://github.com/libgdx/libgdx/wiki/Table

JProgress Bar with Values

How can you display text inside a JProgressBar? i.e. "000/100"
progPanel.setBorder(BorderFactory. createEmptyBorder(10,10,10,10));
timerBar = new JProgressBar(0,100);
timerBar.setOrientation(JProgressBar.VERTICAL);
timerBarLabel = new JLabel("Play");
timerBarLabel.setForeground(Color.white);
progPanel.add(timerBarLabel);
progPanel.add(timerBar);
This is my code for my progress bar.
As noted the documentation for JProgressBar (I'm assuming you are using Java 6), you can use the getValue() method to retrieve the progress bar's current value from the BoundedRangeModel.
So,
int maximum = timerBar.getMaximum();
int value = timerBar.getValue(); // This will be the value from 0 to 100 inclusive
String text = String.format("%d/%d", value, maximum);
The above would result in text containing the string "x/y", where x is the current value of the JProgressBar and y is the maximum value of the same.
If you want to draw this inside the progress bar, you may be interested in setString(String s) and setStringPainted(boolean b),
timerBar.setStringPainted(true);
timerBar.setString(text);
And since the value of the progress bar will be changing, you will want to update the text each time the value changes.
If I understand your question correctly, I would think something as simple this should do the job:
progressBar.setString("Text Here!");

Change line spacing in JTextArea (not JTextPane)

I'm trying to find a way to change the line spacing in a JTextArea component.
A little bit of searching always seems to reveal the same answer: "Use a JTextPane instead and then call setParagraphAttributes".
But I'm wondering whether it's possible to achieve this with just a JTextArea by, for example, messing with the font.
With the deriveFont(...) method, it's possible to change the tracking and the kerning of the font, i.e. the horizontal spacing between characters, but I haven't been able to find a way to change the vertical spacing (maybe ascent, descent, leading). Am I missing something there?
As camickr pointed out, JTextArea does not provide a way to change the line height directly. It simply uses the font height provided by the corresponding FontMetrics.
But, this leaves a point of attack open using the following helper-class:
public class FontMetricsWrapper extends FontMetrics {
private final FontMetrics target;
public FontMetricsWrapper(FontMetrics target) {
super(target.getFont());
this.target = target;
}
#Override
public int bytesWidth(byte[] data, int off, int len) {
return target.bytesWidth(data, off, len);
}
#Override
public int charWidth(char ch) {
return target.charWidth(ch);
}
#Override
public int charWidth(int codePoint) {
return target.charWidth(codePoint);
}
// ... tons more #Override's, all of the same form:
//
// #Override
// public baz foo(bar, ...) {
// return target.foo(bar, ...);
// }
}
Then, it becomes possible to create the JTextArea like this:
JTextArea myTextArea = new JTextArea("Some text") {
#Override
public FontMetrics getFontMetrics(Font font) {
return new FontMetricsWrapper(super.getFontMetrics(font)) {
#Override
public int getHeight() {
return 10; // Gives line height in pixels
}
};
}
};
This is definitely not the cleanest solution and is merely meant as proof of concept. For example, one issue is that getFontMetrics(...) is called quite often, and, in the given example, creates a new instance of the wrapper class each time. So, at the very least, a HashMap that caches the created FontMetricsWrapper for each given font would be in order...
But, what I was really hoping for was a way to play with the Font or maybe the associated FontRenderContext passed into the JTextArea to modify the line height. For example, is there some way to influence font's reported ascent, descent, and leading values? Seems strange that you can change pretty much any other aspect of the font's appearance, except this one...
What's wrong with light-weight coding?
Nothing, you should use the simplest component for the job. But if the simple component doesn't support a requirement it is usually because the requirement is more complex and you need a more complex component to implement the functionality. Rarely would it be as simple at setting a property of a class.
Sometimes I'm simply interested in learning something new and figuring out what's possible.
Swing text components use a View to paint the text. It is the view's responsibility to format and position the text. So each view determines when to wrap and where to position the next line.
In the case of a JTextArea it uses either a Plainview or a WrappedPlanView. For the Plainview the painting code is:
drawLine(line, g, x, y);
y += fontHeight;
where the fontHeight is determined by using the FontMetrics.getHeight() method.
So the value is basically hard coded in the View. You could always provide a custom View for your text area, but overriding a View is generally not an easy task.

Categories

Resources