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.
Related
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
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);
I am using MPAndroid Chart Library for plotting Line Chart and I am setting dynamic data on LineChart but sometimes I am getting data as value 0.0 for some indexes and I don't want show 0.0 values on any index.
How can I skip indexes having 0.0 value.
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(23.00f, 0));
entries.add(new Entry(40.00f, 1));
entries.add(new Entry(00.00f, 2)); // want to skip this index 2(Mar)
entries.add(new Entry(00.00f, 3)); // want to skip this index 3 (Apr)
entries.add(new Entry(94.00f, 4));
entries.add(new Entry(20.00f, 5));
Now i am getting like this
But i would like to get some thing like this
Any Idea about this ?
Thanks
Finally after a lot of Internet search , i found solution.
I tried many solutions but in my case and best fit with question is as well.
Get the axis suppose we are planning to hide useless sequence value from the xAxix
xAxis.setLabelCount(originalValueArray.size, true)
where originalValueArray is the array of original data source.
Above solution will only draw required label and it will remove unnecessary sequence data.
What about adding multiple dataset, one for each contiguous part of your graph ?
You can try Override drawData method from LineChartRender and do this:
int index = lineData.getDataSets().size();
for (ILineDataSet set : lineData.getDataSets()) {
if (set.getEntryForIndex(index).getY() != 0) {
if (set.isVisible()) {
drawDataSet(c, set);
}
}
c.drawBitmap(mDrawBitmap.get(), 0, 0, mRenderPaint);
}
}
Your code should be looking as:
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(23.00f, 0));
entries.add(new Entry(40.00f, 1));
// entries.add(new Entry(00.00f, 2)); // want to skip this index 2(Mar)
// entries.add(new Entry(00.00f, 3)); // want to skip this index 3 (Apr)
entries.add(new Entry(94.00f, 4));
entries.add(new Entry(20.00f, 5));
The lib will draw it automatically as on screenshot 2.
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.
First off, I am new to Java and to Stackoverflow. So I hope I can supply enough clarity in my question.
My goal is to create a box plot using jfreechart to keep track of measurement values from every day use. I want to do this by storing minimal amount of data ie. by storing statists of mean, standard deviation, median, 1Q,3Q, min and maximum. This should then be visualized by a box plot for each day measured.
I have looked at the box plot demo here
http://www.java2s.com/Code/Java/Chart/JFreeChartBoxAndWhiskerDemo.htm
In this demo they create the dataset and add all the values to the dataset, then adds it to the plot. The dataset itself contains methods to return the mean, median etc. of the dataset to be able to create the plot. See the code below for a snip from the demo in the link above.
DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
//some type of algorithm to add values to the dataset
dataset.add(//values, series and type here);
// Return the finished dataset
CategoryAxis xAxis = new CategoryAxis("Type");
NumberAxis yAxis = new NumberAxis("Value");
yAxis.setAutoRangeIncludesZero(false);
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
renderer.setFillBox(false);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis,
renderer);
JFreeChart chart = new JFreeChart("Box-and-Whisker Demo",
new Font("SansSerif", Font.BOLD, 14), plot, true);
So my question is, how should I do to just add the median, Q1,Q3, mean, minimum and maximum values to create the box plot? Because in the demo above they base the plot of a complete sample set.
You can create your own dataset class and use it to create the chart.
Create your own implementation of BoxAndWhiskerCategoryDataset and use it in place of DefaultBoxAndWhiskerCategoryDataset.