Display values in JFreeChart spider chart - java

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.

Related

JFreeChart: Bar chart X axis labels overlapping with large datasets

I'm facing the below issue while using JFreeChart for creating bar chart with relatively large dataset:
Bar chart generated with overlapping X-axis labels. I have tried positioning the labels vertical, still no help. Please provide the best possible way to solve this issue. Code snipped below:
CategoryDataset categoryDataSet = getBarDataset();
JFreeChart barChart = ChartFactory.createBarChart("TITLE", "X-LABEL","Y-LABEL", categoryDataSet, PlotOrientation.VERTICAL, true, true, false);
barChart.getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
barChart.getCategoryPlot().getDomainAxis().setMaximumCategoryLabelLines(4);
jpg = Image.getInstance(im, null);
document.add(jpg);
Update:
As per the suggestion from #trashgod, I've used SlidingCategoryDataset indexing from 0 to the column count. When the column count is large(50 here), the X-Label's are overlapping. When the column count is set to a lower number, it's working fine. I want to find a solution for large column size. Importantly, I need to export the chart image to pdf. Please help me in arriving to a feasible solution. Thanks!
private static void createBarChart() throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_LOCN));
writer.setStrictImageSequence(true);
document.open();
CategoryDataset categoryDataset = createDataset();
int colCount = categoryDataset.getColumnCount();
SlidingCategoryDataset slidingCategoryDataSet = new SlidingCategoryDataset(categoryDataset, 0, colCount);
JFreeChart barChart = ChartFactory.createBarChart("TITLE", "X-LABEL","Y-LABEL", slidingCategoryDataSet,
PlotOrientation.VERTICAL, true, true, false);
barChart.getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
barChart.getCategoryPlot().getDomainAxis().setMaximumCategoryLabelLines(4);
java.awt.Image im = barChart.createBufferedImage(400, 400);
Image jpg = Image.getInstance(im, null);
jpg.scaleToFit(400, 400);
document.add(jpg);
document.close();
writer.close();
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i <= 50; i++) {
dataset.addValue(i, "R", "C" + i);
}
return dataset;
}
With a large number of categories, specifying vertical labels via setCategoryLabelPositions() is a good choice for maximizing legibility. Arbitrary CategoryLabelPositions are supported.
In an interactive chart, SlidingCategoryDataset, mentioned here, allows your application to set the count and starting index of visible categories as desired, perhaps using a slider or spinner, as shown here.
How can I achieve this in a PDF?…More pixels seems a good idea, but how can we implement that here?
In a fixed size context with a given number of categories, you can optimize the size of the chart image via createBufferedImage(), as shown here, or Image.scaleToFit(). For yet larger datasets, it may be possible to create multiple linked charts in a domain specific way, e.g. by year or subsidiary.

How to count the number of series of a bar chart in JFreeChart?

I am facing the some problems when creating bar chart using JFreeChart.
I have to write a condition according to a number of series available on the bar chart, but I don't know how to get it.
Bar charts typically use a CategoryDataset, all of which implement the KeyedValues2D interface. Use dataset.getRowCount() to get the number of series. Use dataset.getColumnCount() to get the number of distinct categories. Their product is the total number of bars.
As a concrete example, I added a spurious new series to BarChartDemo1 to get three series (rows) and two categories (columns).
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
…
dataset.addValue(8000, "Meretricious", "Warm-up");
dataset.addValue(24000, "Meretricious", "Test");
return 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.

Box plot using JFreeChart

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.

Y tags in XYSeriesCollection (JFreeChart)

I have an XY chart where I want to represent X values along some dates. The creation of the dataset is simple:
XYSeries serie = new XYSeries("valor");
for(int i=0;i<lista.size();i++){
serie.add(i,lista.get(i).getValue());
}
dataset.addSeries(serie);
where serie.add uses as arguments (y.value, x.value). I am representing the x value along time, but it appears as just the index of the arraylist (obvious, I use i as the first parameter). My question is, how could I show dates (or Strings) as the Y values?
I know that this possible to be done with a BarChart, for instance:
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.addValue(51, "series", "Colonel Forbin");
dataSet.addValue(92, "series", "The Lizards");
Something like this is what I need to do, hope you could help, thanks
As shown here, if the range values in your series represent milliseconds from the Java epoch, a DateAxis should display the values correctly.
NumberAxis domain = new NumberAxis("X");
DateAxis range = new DateAxis("Y");
domain.setAutoRangeIncludesZero(false);
XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
XYPlot plot = new XYPlot(dataset, domain, range, renderer);
JFreeChart chart = new JFreeChart(
"Title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
Addendum: I just want to set … tags that change dynamically.
It looks like you want SymbolAxis, seen here.

Categories

Resources