MPAndroidChart - How add put and show legends in a PieChart - java

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);

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

Mouse event when clicking data title in JavaFX

I have created a simple horizontal barchart using JavaFX in Java 1.8. This displays correctly with a bottom legend, list of paths on the left and bars showing data grouped into 3 series. I would like to open a page that is dynamically populated based on the path chosen with the path on the left used as the link but I have not been able to find a way to create a mouse event when clicking on that data text.
In the sample code below the text retrieved by my "path" class's "getpath" method is what I would like to turn into a link.
Is there anyway to do this?
I've reviewed documentation on the BarChart and XYChart classes but I cannot find a way to get the value of a given item in the chart, and even if a did I cannot set an action event because the data value doesn't appear to have a specific node to set the event on.
Doing this with a button and/or drop down works as expected but i'd like to use the text built into the chart as a link.
Most of the other similar questions I have found referenced using a label for the title but that won't work in this case as the number of items in my chart will vary on the dataset passed into it.
//method for building chart
protected BarChart getChart(ObservableList<myClass> chartData) {
//Setup a horizontal barchart for displaying heat stats.
final NumberAxis xAxis = new NumberAxis();
final CategoryAxis yAxis = new CategoryAxis();
final BarChart<Number,String> bc =
new BarChart<Number,String>(xAxis,yAxis);
bc.setTitle("");
xAxis.setLabel("Ops");
xAxis.setTickLabelRotation(90);
yAxis.setLabel("NFS Exports with lock contention");
//Create series to hold data for each event type to show
XYChart.Series series1= new XYChart.Series();
series1.setName("Series1");
XYChart.Series series2= new XYChart.Series();
series2.setName("Series2");
XYChart.Series series3= new XYChart.Series();
series3.setName("Series3");
for (myClass path: chartData)
{
series1.getData().add(new XYChart.Data(path.getData1(), path.getPath()));
series2.getData().add(new XYChart.Data(path.getData2(), path.getPath()));
series3.getData().add(new XYChart.Data(path.getData3(), path.getPath()));
}
bc.getData().addAll(series1, series2, series3);
return bc;
}
//method for getting sample data
public ObservableList<myClass> getPathData()
{
ObservableList<myClass> exports = FXCollections.observableArrayList();
paths.add(new myClass("/path1", 12.0, 30.0, 62.0));
paths.add(new myClass("/path2", 20.0, 43.0, 82.0));
paths.add(new myClass("/path3", 0.0, 18.0, 23.0));
paths.add(new myClass("/path4", 40.0, 26.0, 75.0));
paths.add(new myClass("/path5", 8.0, 4.0, 5.0));
return paths;
}
What I would like is a way to make clicking on the path run a function passing in the path that was selected.

setDescriptionTextSize(float size) cannot resolve method MPAndroidChart

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.

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.

Display values in JFreeChart spider chart

The spider web plot of JFreeChart does not display values close to the marked points, as default:
what must be done to display the numbers?
That's what I have now, in a method returning StreamedContent (for PrimeFaces' p:graphicImage:
CategoryDataset categorydataset = createCategoryDataset(displayBy, configuration, null);
SpiderWebPlot plot = new SpiderWebPlot(categorydataset);
plot.setInteriorGap(0.40);
plot.setNoDataMessage("No data available");
plot.setStartAngle(0);
plot.setLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getInstance()));
plot.setWebFilled(true);
JFreeChart chart = new JFreeChart(title, TextTitle.DEFAULT_FONT, plot, true);
File chartFile = new File(String.valueOf(new Random().nextLong()));
ChartUtilities.saveChartAsPNG(chartFile, chart, 375, 300);
return new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
EDIT: After #trashgod's suggestion, some values appeared but no in the expected place. I want the columnKey values, not the `rowKey' values (addValue method in DefaultCategoryDataset class):
What must be done to display the numbers?
You can label points on the plot using a CategoryItemLabelGenerator, as shown here.
What matters are the three other [points].
It looks like you might be able to add a second series, one with a second rowKey, in order to get labels associated with those points.

Categories

Resources