setDescriptionTextSize(float size) cannot resolve method MPAndroidChart - java

I am making a PieChart in android studio but am running into some issues with styling. The size of description (and legend and values are really small).
I read the docs about this and it said there i had to use the setDescriptionTextSize() method and that it is available for every chart type.
However that method is not resolved for some reason.
extra info about project:
-the problem occurs in a fragment
-my pieChart is built in the "onChildAdded" method because it needs to be created when that function runs.
-the MPAndroidChart version is: v3.0.3
Here is my code:
pieChart = (PieChart) myView.findViewById(R.id.idPieChart);
pieChart.setCenterTextSize(25f);
pieChart.setUsePercentValues(true);
pieChart.setExtraOffsets(5, 10, 5, 5);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleRadius(61f);
pieChart.setEntryLabelTextSize(25f);
pieChart.setUsePercentValues(true);
pieChart.setDescriptionTextSize(25f); //THIS IS NOT RESOLVED
List<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(likeCount.getLikes(), "Leuk"));
entries.add(new PieEntry(likeCount.getDislikes(), "Niet leuk"));
PieDataSet set = new PieDataSet(entries, "likes/dislikes");
PieData data = new PieData(set);
set.setColors(getResources().getColor(R.color.chartgreen)
,getResources().getColor(R.color.chartred));
pieChart.setData(data);
pieChart.invalidate(); // refresh
Does anyone know a possible solution to this problem?

you can use
pieChart.getDescription().setTextSize(25f);
instead of
pieChart.setDescriptionTextSize(25f);

The value has to be between 6f and 16f. Other values aren't accepted.
setDescriptionTextSize(float size): Sets the size of the description text in pixels, min 6f, max 16f.

Related

How to draw grouped bar chart in Android with different number of columns in each group?

This is the chart that I want to create (click on the link to view image):
Desired chart
I have researched a lot about Grouped Bar Chart in MPAndroidChart but that library seems to restrict. Each group must have the same number of columns and the column order must be consistent. Does anyone know how to create a grouped bar chart like in my image using MPAndroidChart (or any other libraries)?
Please help me. Thank you in advance.
Could you try this code?
List<String> xValues = ...; // "Shop1", "Shop2", "Shop3"
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyValueFormatter(xValues));
// create 2 datasets
BarDataSet set1 = new BarDataSet(valuesProduct1, "Product1"); //valuesProduct1 are the values corresponding to the shops,
set1.setColor(Color.BLUE);
BarDataSet set2 = new BarDataSet(valuesProduct2, "Product2");
set2.setColor(Color.RED);
BarData data = new BarData(set1, set2);
chart.setData(data);
chart.groupBars(...); // available since release v3.0.0
chart.invalidate(); // refresh
If you have some troubles where the shop has no products, just put zero on the concrete position

MPAndroidChart - How add put and show legends in a PieChart

I´m read various tutoriais but most of then show the same example. The legend array is put in a constructor of:
new PieData(legendValues, IPieDataSet dataSet)
But PieData doesn't have this constructor.
Does have anyone a sample code for this issue?
I'm assuming you are using the latest 3.0.0-beta1 version of the library. For that version a lot of (breaking) changes were made to support float x-values.
Specifically, the PieData constructor has changed and now only accepts an IPieDataSet. The labels are now set with the PieEntry which has a PieEntry(float value, String label) constructor. Here's a small example illustrating how you set labels in the latest version of the library:
ArrayList<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(25, "Android 5.0"));
entries.add(new PieEntry(25, "Android 5.1"));
entries.add(new PieEntry(25, "Android 6.0"));
PieDataSet dataSet = new PieDataSet(entries, "Android versions");
PieData data = new PieData(dataSet);

Graphing scientific data in LineChart from MPAndroidChart

I am using MPAndroidChart library to draw graphs (especially LineCharts) in my App.
To draw a LineChart with the mentioned library first we need to create entries and labels as follow:
// Getting LineChart
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
// Creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
// Creating labels
ArrayList<String> labels = new ArrayList<String>();
// Fill entries and lables
entries.add(new Entry(326.422f, 0));
entries.add(new Entry(8.36f, 1));
entries.add(new Entry(6.5f, 2));
entries.add(new Entry(2.37f, 3));
entries.add(new Entry(18.13f, 4));
entries.add(new Entry(9f, 5));
labels.add("0");
labels.add("1");
labels.add("2");
labels.add("3");
labels.add("4");
labels.add("5");
// Create dataset
final LineDataSet dataset = new LineDataSet(entries, "Legend description");
// Create LineData with labels and dataset prepared previously
LineData data = new LineData(labels, dataset);
// Set the data and list of labels into chart
lineChart.setData(data);
Ok this is working, but the point is what if I want to graph a set of coordinates like this: X = {(35.3, 22.9), (69.39, 27.36), (66.37, 31.697), (58.36, 36.32), (45.336, 38.296), (25.39, 40), (67.396, 43.633)}.
The constructor of Entry accepts a float number as first parameter and an integer as second parameter, so how can I give the above X set to the LineChart?
Someone could say that I can set the labels accordingly, for example the first label could be labeled as "22.9", the second as "27.36", and so on... But this is mathematically wrong as the graph is not scaled properly.
In the documentation I found classes like Entry, BarEntry, BubbleEntry, CandleEntry but there is nothing something like LineEntry.
Can anyone point me to the right direction on how to achieve this goal?
Thank you,
HSB
Currently only integers are supported for the x-axis. The reason therefore is that each string on the x-axis should correspond to a value on the y-axis.
This will change in the next release of the library, where both values will be changed to double.
The new version should be released in April.

Java - Xeiam's XChart Marker With Values

I've been trying to figure out if it is possible to put the exact value for each Bar/Line Marker.
Current Result :
Desired Result :
In Xchart library, to get the count of each bar use annotations.
CategoryChart chart = new CategoryChartBuilder()
.yAxisTitle("ABC Title")
.theme(Styler.ChartTheme.GGPlot2).build();
//each stack count and total count of each bar
chart.getStyler().setHasAnnotations(true);
chart.getStyler().setShowTotalAnnotations(true);
chart.getStyler().setAnnotationsPosition(1);

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.

Categories

Resources