Hi im trying to create a chart that is a combination of a bar chart and a line chart in JFree chart. The bar chart is vs time and for each hour it will compare two (or more) different values.
The line chart uses the same scale as the bar chart and shows the overall trend of the data set.
You can plot each dataset on the same Plot, and use a different renderer for each dataset (for instance a BarRenderer and LineAndShapeRenderer). Below is a simplified example that generates some mock data values (1-9) and renders the same data as both bars and lines on the same ChartPanel.
//Mock data
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int[] times = new int[]{1,2,3,4,5,6,7,8,9};
for ( int i = 0; i < times.length; i++ ){
dataset.addValue(times[i], "Time", "Hour" + String.valueOf(i+1));
}
//create the plot
CategoryPlot plot = new CategoryPlot();
//add the first dataset, and render as bar values
CategoryItemRenderer renderer = new BarRenderer();
plot.setDataset(0,dataset);
plot.setRenderer(0,renderer);
//add the second dataset, render as lines
CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
plot.setDataset(1, dataset);
plot.setRenderer(1, renderer2);
//set axis
plot.setDomainAxis(new CategoryAxis("Time"));
plot.setRangeAxis(new NumberAxis("Value"));
And the resulting Chart:
Related
I would like to align the labels of my x-axis with the bars in my bar chart.
When launching the graph, only 3 of the 5 labels appear, as can be seen in this image .
As soon as I zoom in slightly, the rest of the labels appear, as can be seen in this image
The following is the relevant code relating to the building of the Bar Graph and its x-axis:
for (int c = 0; c < numberOfStoriesPlayed; c++) {
barEntries.add(new BarEntry(chartData [0] [c], chartData [1] [c]));
}
BarDataSet barDataSet = new BarDataSet(barEntries, "Social Stories");
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));
BarData data;
data = new BarData(barDataSet);
barChart.setData(data);
barChart.getAxisRight().setDrawLabels(false);
barChart.getAxisLeft().setDrawLabels(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getAxisRight().setDrawGridLines(false);
barChart.setTouchEnabled(true);
barChart.setDragEnabled(true);
barChart.setScaleEnabled(true);
barChart.setDrawGridBackground(false);
barChart.notifyDataSetChanged();
barChart.invalidate();
barChart.setAutoScaleMinMaxEnabled(true);
I would like to find a way for all labels to show up immediately, and not just when I start zooming, and also for them to be properly aligned with the bars in the graph. Thanks in advance!
I have to implement an histogram using JFreeChart API. This histogram has to represent the datas of this JTable:
So I have a JTable with three columns: "thea", "type", "Number of occurrences". My histogram has two targets: the first is to count the number of occurrences of each thea field; the second is to mark with different colors the bars corresponding to JTable records with different types.
To implement my histogram I used a DefaultCategoryDataset:
private DefaultCategoryDataset createDataset(ArrayList<String>fieldsOccs) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i = 0; i<this.fieldsOccs.size() && i<end; i++) {
String thea = fieldsOccs.get(i).getFieldName();
String type = fieldsOccs.get(i).getType();
int occurrences = fieldsOccs.get(i).getOccurrences();
dataset.setValue(occurrences, type, thea);
}
return dataset;
}
Anf then I create my chart using a createChart method:
private JFreeChart createChart(DefaultCategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart(
"",
"", //X-axis title
"", //Y-axis title
dataset, //dataset
PlotOrientation.HORIZONTAL, //plot orientation
true, //show legends
true, //use tooltips
false //generate URLs
);
return chart;
}
This is what I get:
As you can see in the picture it is not nice to see. The values on x axes are not formatted correctly.
How can I solve this rendering problem?
--edit
I have this problem just in case of more types in the JTable. For example if my JTable is:
and there is just String, the correspondig histogram is nice:
--edit1
What dou you think about StackedBarChart3D? I get this output:
My histogram has two targets:
You may get a more appealing result with ChartFactory.createHistogram() and a SimpleHistogramDataset, seen here.
To get diverse colors, override the getItemPaint() method in a custom XYBarRenderer, as suggested here.
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.
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.
I'm a noob with jfreechart, and I have an app that creates a simple bar chart that is working well. The issue is, I want all the charts to show a range of 1 to 10. When the highest val in the chart is lower than that, that lower value will be the upper bound of the chart, and it will display in different increments. It wasn't obvious to me from the online notes where to change that....
DefaultCategoryDataset barDataSet = new DefaultCategoryDataset();
System.out.println("Setting values:");
barDataSet.setValue(rating.getFlavor(), "category", "Flavor");
barDataSet.setValue(rating.getBody(), "category", "Body");
barDataSet.setValue(rating.getAftertaste(), "category", "Aftertaste");
barDataSet.setValue(rating.getSweetness(), "category", "Sweetness");
barDataSet.setValue(rating.getFloral(), "category", "Floral");
barDataSet.setValue(rating.getSpice(), "category", "Spice");
JFreeChart chart = ChartFactory.createBarChart(null, // title
"category", // left heading
"score", // top heading
barDataSet, // dataset
PlotOrientation.HORIZONTAL,
false, // no idea what this is
true, // or this
false); // or this
System.out.println("setting bg color");
Color bgcolor = new Color(237, 232, 228);
chart.setBackgroundPaint(bgcolor);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(112,112,112) );
thanks,
bp
You can change the displayed range via the axes. If i recall right, you can ask the JFreeChart instance for the XYPlot, with has a domain (x) and a range (y) axis. Both (when instances of NumberAxis) will have a setLowerBound(double) and setUpperBound(double) method.
chart.getXYPlot().getRangeAxis().setLowerBound(30.0);
chart.getXYPlot().getRangeAxis().setUpperBound(300.0);