I got 78 labels in a pie chart like this picture.
And I want to show only top 3 labels in descending order.
How can I do this?
Create a custom PieSectionLabelGenerator and return null when you do not like to display the label.
Example
public class PieMaximumLabelsGenerator extends StandardPieSectionLabelGenerator {
private static final long serialVersionUID = 1385777973353453096L;
private int nrLabels;
private boolean showFirst;
/**
* A custom label generator to show only limited numbers of labels
* #param nrLabels, number of labels to show
* #param showFirst, if true, show first labels otherwise show the last
*/
public PieMaximumLabelsGenerator(int nrLabels, boolean showFirst){
this.nrLabels = nrLabels;
this.showFirst = showFirst;
}
#Override
public String generateSectionLabel(PieDataset dataset, Comparable key) {
int index = dataset.getIndex(key);
if (showFirst){
if (index>=nrLabels){
return null; //no more lables if index is above
}
}else{
if (index<dataset.getItemCount()-nrLabels){
return null; //no labels if index is not enough
}
}
return super.generateSectionLabel(dataset, key);
}
}
Then set this to your plot
((PiePlot) chart.getPlot()).setLabelGenerator(new PieMaximumLabelsGenerator(3, false));
Output, similar example but displaying first 5 values instead of last 3, hence ((PiePlot) chart.getPlot()).setLabelGenerator(new PieMaximumLabelsGenerator(5, true));
My preference however is to display label if the arc angle of slice is large enough. This can be done by collecting totale values of items in the chart and then calculating the angle using Number value = dataset.getValue(key); in generateSectionLabel to get the current angle (dimension) of slice.
Related
I work for my school project on MPAndroidChart especially a realtime graph, i would like to display Time of the value.
I set a IndexAxisValueFormatter with a "getFormattedValue", it work but refresh every label and not just the last, I try to have for each entry in my graph a XLabel who show the time, I really don't know how to do that you're help would be welcome.
Code for create an entry and display it :
void creaGraph() {
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
if (boxCO2.isChecked()) {
A_CO2.add(new Entry(indice, listData.recup_data(indice - 1).getCO2()));
LineDataSet setCO2 = new LineDataSet(A_CO2, "CO2");
setCO2.setAxisDependency(YAxis.AxisDependency.LEFT);
paramSet(setCO2);
setCO2.setColor(Color.RED);
setCO2.setCircleColor(Color.RED);
dataSets.add(setCO2);
}
LineData data = new LineData(dataSets);
graph.setData(data);
data.notifyDataChanged();
graph.notifyDataSetChanged();
graph.invalidate();
}
The override of getFormattedValue
#Override
public String getFormattedValue(float value) {
return listData.recup_data(GraphPage.indice - 1).getTemps();
}
And a picture of my issue
Every label are refresh when a new entry come
Also, I see after the 7th values entry no longer have a time values
You never use value in getFormattedValue. The string you construct there should be based on value or it will show the same thing for every axis entry.
Something like this:
#Override
public String getFormattedValue(float value) {
return makeDateStringAt(value);
}
For example, if you have a chart with axis values at 0, 1, 2, 3 then getFormattedValue will be called 4 times with 0f, 1f, 1f, and 3f as its arguments and you should use those inputs to create the appropriate string to show at those positions on the axis.
I am working on a barchart for a school assignement. However I got stuck when trying to position the bar label at the left of the bar. I assume I have to use a method to get the width of the label text and then substract the value from the x position of the bar.
public class Bar {
// the numeric value of the bar
private int value;
// the rectangle representing the bar
private Rectangle view;
// The text label for this bar
private Text label;
// The value as a text object
private Text valueText;
/**
* Constructor for a bar object. The rectangle for the bar is
* also constructed here and its size is set to the given height.
* The value of the bar is initialised to 0.
* If there is a label for the bar, a Text object is created using the
* text of the label and the colour of the label set to black.
* The bar and the label are not visible until the display method
* is called.
*
* #param label The label for the bar.
* #param width The height of the bar.
*/
public Bar(String label, int height) {
view = new Rectangle();
view.changeSize (0,height);
this.label = new Text(label);
display();
}
public void display() {
label.makeVisible();
view.makeVisible();
}
public void setValue (int Newvalue){
value= Newvalue;
}
public void setPosition (int x, int y){
view.setPosition(x,y);
label.setPosition(x,y);
}
public int getTextWidth() {
return (int) ((Text)label).getWidth();
}
}
Edit:
I have attached a screenshot of of my BlueJ program so far and also the assignment description.
bar chart
assignment
I have created in the Public calss bar, a bar as a Rectangle named "view". The rectangle's (bar) width is initially set to zero, that is why on canvas appears as a line (see picture). I have created a label as Text, which must appear to the left of the bar( my rectangle) everytime I edit is width. The point is I don't know how to edit is width in the method Public void set value. When I call the method and I enter 100 as the parameter value, the value of the bar should change to 100 and the width of the view (rectangle) should change to 100 but it dosen't. And the label is in the wrong position. In the setPosition method I have to make the label appear to the left of the rectangle(bar). But how? And then when I call the display method I must see on the cavas the value representing the bar width. How? Many thanks.
Just so you know, I am an absolut beginner, only 3 weeks into my course.
So basically I want the value returned by getTextWidth() to substract it from the x coordinate of the bar (the text must appear at the left of the bar). Any help is much appreciated.
I need to move the label (Text) of all tick marks in an Axis such that the text is right in the middle of its own tick mark and the next tick mark.
I am using Roland's GanttChart control (with some modifications) and Christian Schudt's DateAxis for my X-axis. My objective is to plot a gantt chart based on Date values (ignoring time; all time values are truncated).
My gantt chart has a "start date" and an "end date" for every single task (i.e. visually on the chart it is represented by a single bar).
Consider this:
I have a task starting on 1st Feb, and it ends on the same day (1st Feb). I have two ways to render this on the chart:
Render it starting from 1st Feb, and ends at 1st Feb. This bar is effectively hidden, because its width is 0.
Render it starting from 1st Feb, with the right-edge of the bar touching 2nd Feb. This can potentially confuse the users because it would look like it starts from 1st Feb and ends on 2nd Feb.
To solve the problem caused by method 2, I need to shift the text labels by half a tick mark width to the right. Doing this would make it very clear to the user.
I have tried doing this:
final DateAxis xAxis = (DateAxis) this.ganttchart.getXAxis();
xAxis.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>()
{
#Override
public void onChanged(javafx.collections.ListChangeListener.Change<? extends Node> c)
{
final List<Node> labels = xAxis.getChildrenUnmodifiable().filtered(node -> node instanceof Text);
for (Node label : labels)
{
label.setTranslateX(xAxis.getWidth() / (labels.size() - 1) / 2);
}
}
});
However, doing so does not seem to shift the Text labels to the right at all.
The method I had tried actually works, but I was having problem with race condition. If I used Platform.runLater(), it would shift correctly. However, the position flickers whenever I resize the chart (it would jump between original position and shifted position).
This is the complete working version, which I need to override layoutChildren() in DateAxis.
#Override
protected void layoutChildren()
{
if (!isAutoRanging())
{
currentLowerBound.set(getLowerBound().getTime());
currentUpperBound.set(getUpperBound().getTime());
}
super.layoutChildren();
/*
* Newly added codes.
*/
final List<Node> labels = this.getChildrenUnmodifiable().filtered(node -> node instanceof Text);
for (Node label : labels)
{
if (this.getSide() == Side.LEFT || this.getSide() == Side.RIGHT)
label.setTranslateY(this.getHeight() / (labels.size() - 1) / 2);
else if (this.getSide() == Side.TOP || this.getSide() == Side.BOTTOM)
label.setTranslateX(this.getWidth() / (labels.size() - 1) / 2);
}
/*
* End of new codes.
*/
}
Update
There was still some layout bugs. It has to do with the fact that Axis.positionTextNode() positions the texts using getBoundsInParent(), which takes into consider the translation.
This is the new working version, hopefully someday it would be helpful to someone.
#Override
protected void layoutChildren()
{
if (!isAutoRanging())
{
currentLowerBound.set(getLowerBound().getTime());
currentUpperBound.set(getUpperBound().getTime());
}
super.layoutChildren();
/*
* Newly added codes.
*/
final List<Node> labels = this.getChildrenUnmodifiable().filtered(node -> node instanceof Text);
for (Node label : labels)
{
if (this.getSide().isHorizontal())
{
if (label.getTranslateX() == 0)
label.setTranslateX(this.getWidth() / (labels.size() - 1) / 2);
else
{
label.setLayoutX(label.getLayoutX() + label.getTranslateX());
}
}
else if (this.getSide().isVertical())
{
if (label.getTranslateY() == 0)
label.setTranslateY(this.getHeight() / (labels.size() - 1) / 2);
else
{
label.setLayoutY(label.getLayoutY() + label.getTranslateY());
}
}
}
/*
* End of new codes.
*/
}
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.
I have a JFreeChart instance that displays process memory status, initialized as follows:
m_data = new TimeSeriesCollection();
TimeSeries vmsize = new TimeSeries("VMSize");
TimeSeries resident = new TimeSeries("Resisdent");
TimeSeries shared = new TimeSeries("Shared memory");
TimeSeries code = new TimeSeries("Code");
TimeSeries data = new TimeSeries("Data");
m_data.addSeries(vmsize);
m_data.addSeries(resident);
m_data.addSeries(shared);
m_data.addSeries(code);
m_data.addSeries(data);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Memory usage", "Time", "Size", m_data, true, true, false);
m_chart = new ChartPanel(chart);
Later I add values to each TimeSeries in the TimeSeriesCollection. I would like to somehow know - when the user clicks on the Chart - either what time associated with that columm, or even better - what is the index of the value.
I looked at the JFreeChart and ChartMouseListener classes, but I could not figure out how to do that (also the documentation of JFreeChart is annoyingly scarce, I guess they are trying to get people to buy their developer's guide).
if you click dead on the item, the event.getEntity() function returns XYItem and then from there onwards
XYItemEntity xyitem=(XYItemEntity) event.getEntity(); // get clicked entity
XYDataset dataset = (XYDataset)xyitem.getDataset(); // get data set
System.out.println(xyitem.getItem()+" item of "+xyitem.getSeriesIndex()+"series");
System.out.println(dataset.getXValue(xyitem.getSeriesIndex(), xyitem.getItem()));
System.out.println(dataset.getYValue(xyitem.getSeriesIndex(), xyitem.getItem()));
Comparable comparable=dataset.getSeriesKey(0);
XYPlot xyplot = (XYPlot) event.getChart().getPlot();
System.out.println(xyplot.getRangeCrosshairValue());
however incase you do not click on the item itself but your crosshair is set to auto lock on data, in such case the crosshair will move to nearest item but since the item has not been clicked, you will not be able to get the XYItem and hence you cannot know the series and item index, to solve this problem there is this code below, it should be put in the catch clause while the above mentioned code should be in try clause
first define a function which will take crosshair value at domain and range and also Xydataset, this functions returns an inner class object that groups item index and series index
public static SeriesAndItemIndex getItemIndex(double domainVal,double rangeVal,XYDataset xydataset){
Comparable comparable;
int indexOf;
for(int i=0;i<xydataset.getSeriesCount();i++){
comparable = xydataset.getSeriesKey(i);
indexOf=xydataset.indexOf(comparable);
for(int j=0 ; j<xydataset.getItemCount(indexOf);j++){
double x=xydataset.getXValue(indexOf, j);
double y=xydataset.getYValue(indexOf, j);
if(x == domainVal && y==rangeVal){
return new SeriesAndItemIndex(j,indexOf);//return item index and series index
}
}
}
return null;
}
private static class SeriesAndItemIndex{ ///inner CLASS to group series and item clicked index
public int itemIndex;
public int seriesIndex;
public SeriesAndItemIndex(int i,int s){
itemIndex=i;
seriesIndex=s;
}
#Override
public String toString(){
return "itemIndex="+itemIndex+",seriesIndex="+seriesIndex;
}
}
how to use it?
try{......code block from the top
}catch(Exception e){
Object source=event.getSource();
JFreeChart chartpanel=(JFreeChart)source;
XYPlot xyplot = (XYPlot) chartpanel.getPlot();
XYDataset xydataset= xyplot.getDataset();
double d=xyplot.getDomainCrosshairValue(); //get crosshair X value
double r =xyplot.getRangeCrosshairValue(); //get crosshair y value
SeriesAndItemIndex index=getItemIndex(d,r,xydataset);
if(index != null){
System.out.println(index.toString());
}
}
hmm should work, if you replace the last two lines by something like this:
ChartPanel panel=new ChartPanel(ChartFactory.createTimeSeriesChart("Memory usage", "Time", "Size", m_data, true, true, false)));
panel.addChartMouseListener(new ChartMouseListener(){
void chartMouseClicked(ChartMouseEvent e){
[...do something on click...]
}
void chartMouseMoved(ChartMouseEvent e){
[...do something on move...]
}
});
return panel;