I create and pass a TimeTableXYDataset to display a StackedBarChart, quite similar to the StackedXYBarChartDemo2 from the demo package. In addition to the values displayed as ItemLabels, I would like to display the sum of all values above every bar. Does anyone know if this is doable, and how ?
You can define a custom XYItemLabelGenerator which will sum the two series.
For example :
DateAxis domainAxis = new DateAxis("Date");
domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
NumberAxis rangeAxis = new NumberAxis("Y");
// Set a margin so that the label above the bar has a place to display
rangeAxis.setUpperMargin(0.15);
StackedXYBarRenderer renderer = new StackedXYBarRenderer(0.10);
renderer.setDrawBarOutline(false);
XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
// Custom LabelGenerator, which displays the sum of the two series.
XYItemLabelGenerator generator = new XYItemLabelGenerator() {
#Override
public String generateLabel(XYDataset dataset, int series, int item) {
// Sum values for the two series of data
double sum = dataset.getYValue(0, item) + dataset.getYValue(1, item);
return "" + sum;
}
};
// The LabelGenerator is linked to series 1 (top part of the bar)
renderer.setSeriesItemLabelGenerator(1, generator);
renderer.setSeriesItemLabelsVisible(1, true);
renderer.setSeriesPositiveItemLabelPosition(1, new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
TextAnchor.BASELINE_CENTER));
renderer.setItemLabelAnchorOffset(10);
JFreeChart chart = new JFreeChart("Stacked XY Bar Chart Demo 2", plot);
return chart;
Related
I am new to JFreeChart. I am trying to create a bubble chart that has a single series with up to 10 bubble, but I want to different color for each bubble.
I tried xyitemrenderer.setSeriesFillPaint(0, Color.GREEN). But it giving only one color for all bubbles. How to setup multiple color for each bubbles in JFreeChart.
I need custom color for each bubble in a series.
As shown here, you can override the renderer's getItemPaint() implementation to return any desired color. The following example prints the default colors totes console.
JFreeChart chart = …;
XYPlot xyplot = (XYPlot) chart.getPlot();
XYItemRenderer xyitemrenderer = new XYBubbleRenderer(){
#Override
public Paint getItemPaint(int row, int col) {
Paint p = super.getItemPaint(row, col);
System.out.println(row + ", " + col + ": " + p);
return p;
}
};
xyplot.setRenderer(xyitemrenderer);
My problem: Using jfreechart, I need to display unevenly spaced percentage thresholds on the y-axis. For example, the only labels on my y-axis should be the thresholds as follows; all other ordinates should be blank:
-
93%
85%
78%
72%
66%
-
50%
-
-
-
-
-
I am currently using this code snippet for percentages display, but this will just create an evenly spaced percentage axis:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
DecimalFormat pctFormat = new DecimalFormat("#.0%");
rangeAxis.setNumberFormatOverride(pctFormat);
Any help would be appreciated!
This is a somewhat hacked-up answer, and i will upvote any answer that gets the same result in a good way. But it works - here is what i did.
First, i created a list of the double values i wanted to be used:
public static List<Double> m_lPercentageValuesForY = Arrays.asList(0.1,0.2,0.3,0.4,0.5,0.58,0.66,0.70,0.74,0.78,0.82,0.95,1.0);
Then i overwrite the refreshTicks method in NumberAxis to first set all default ticks to Ticktype.MINOR, and then add and create my custom ticks with Ticktype.MAJOR:
NumberAxis axisLeft = new NumberAxis(plot.getRangeAxis().getLabel()) {
#Override
public List refreshTicks(
Graphics2D g2,
AxisState state,
Rectangle2D dataArea,
RectangleEdge edge) {
List defaultTicks = super.refreshTicks(g2, state, dataArea, edge);
List customTicks = new ArrayList();
for (Object aTick : defaultTicks) {
NumberTick currenttick = (NumberTick) aTick;
customTicks.add(new NumberTick(
TickType.MINOR,
currenttick.getValue(),
"", //empty
currenttick.getTextAnchor(),
currenttick.getRotationAnchor(),
currenttick.getAngle()));
}
NumberTick aTick = (NumberTick) defaultTicks.get(0);
for (double dTest : m_lPercentageValuesForY) {
customTicks.add(new NumberTick(
TickType.MAJOR,
dTest,
String.format("%.0f%%", dTest * 100), //only wanted values are set to major
aTick.getTextAnchor(),
aTick.getRotationAnchor(),
aTick.getAngle()));
}
return customTicks;
}
};
plot.setRangeAxis(axisLeft);
I am using JFreeChart to plot a line graph. I would like to load arrays as the data set... one array for the x-axis and another array for the y-axis.
I am having a problem when trying to pass the array as the data to use in the dataset.
The following is what I've tried;
public DefaultCategoryDataset createDataset(int[] epochNo, int[] BFacts)
{
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i<epochNo.length(); i++)
{
dataset.addValue(epochNo[i], BFacts[i]);
}
return dataset;
}
Thanks in advance!
There are two solutions; with a normal line chart, you use a DefaultCategoryDataset. The addValue method has three arguments, the second being the name of the dataset. And the third is the column label:
dataset.addValue(BFacts[i], "myline", String.valueOf(epochNo[i]));
There other solution is to see this as an XY line chart (using ChartFactory.createXYLineChart). In that case your dataset is an XYDataset instead of a CategoryDataset:
private XYDataset createDataset(int[] epochNo, int[] bFacts) {
final XYSeries myline = new XYSeries( "myline" );
for (int i = 0; i < epochNo.length; i++) {
myline.add(epochNo[i], bFacts[i]);
}
final XYSeriesCollection dataset = new XYSeriesCollection( );
dataset.addSeries(myline);
return dataset;
}
See more in this example
i have two sets of data
int[] x1 = {1,2,3,4,5,6,7,8,9,10};
int[] y1 = {1,2,3,5,6,8,9,10,14,11};
int[] x2 = {1,2,3,4,5,6,7,8,9,10};
int[] y2 = {0,2,3,5,0,8,9,8,14,11};
int[] z2 = {1,2,3,1,2,3,1,2,3,1};
I want to plot the x1,y1 as an XYLineChart and then plot x2,y2 as a scatter on the same plot without a line.
I also need each scatter point of xy,y2 to be a different color depending on the value of z2 (1=Color.red, 2=Color.green, 3=Color.blue)
How can i do this?
So far i have:
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout(0, 0));
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("series1");
for(int i=0; i<x1.length; i++){
series1.add(x1[i],y1[i]);
}
dataset.add(series1);
JFreeChart chart = ChartFactory.createXYLineChart("Title", "x", "y", dataset, PlotOrientation.VERTICAL, false, false, false);
ChartPanel cp = new ChartPanel(chart);
panel_1.add(cp, BorderLayout.CENTER);
This gets the line graph sorted. I now need to code the scatter plot for x2,y2 (with colors described above) which is where im stuck.
The createXYLineChart() method will create a chart that uses an XYLineAndShapeRenderer. So fetch the renderer from the plot and cast it to XYLineAndShapeRenderer. Then you can call the methods setSeriesLinesVisible() and setSeriesShapesVisible() to control, for each series, whether shapes and/or lines are drawn for the data items. That way you can use a single renderer and dataset, which makes things simpler.
Your requirement to change the colors depending on another data value requires a little more work. You should subclass the XYLineAndShapeRenderer class and override the getItemPaint(int, int) method. Here you can return any color you want for a data item. The default implementation looks at the series index and returns the color for the series. You need to look at the item index as well, then do a lookup in your table of z-values and decide what color to return.
I'm currently trying to use JFreeChart to represent 3D data in a 2D graph.
Essentially, I have a 2d array called data[i][j]. The i and j represent the y and x coordinates where I want to plot. The value of data[i][j] represents a frequency value, which I want to represent in the graph as a colour.
I'm not entirely sure what something like this is called, but it would look something like this:
Now I have been trying to do this using XYBlockRenderer, however I am having issues with defining the dataset. I am trying to use DefaultXYZDataset, but I'm really confused at how to even define the data here.
Can someone explain how to use the DefaultXYZDataset to accomplish such a task?
DefaultXYZDataset dataset = new DefaultXYZDataset();
Concentration.dataoutHeight = Concentration.dataout[0].length;
System.out.println(Concentration.dataoutHeight);
System.out.println(ImageProcessor.MAXCBVINT);
double[][] data = new double[3][ImageProcessor.MAXCBVINT];
for (int i = 0; i < Concentration.dataoutHeight; i++) {
for (int j = 0; j < ImageProcessor.MAXCBVINT; j++) {
data[0][j] = j;//x value
data[1][j] = i;//y value
data[2][j] = Concentration.dataout[j][i][0];//Colour
}
dataset.addSeries(i, data);
}
NumberAxis xAxis = new NumberAxis("Intensity");
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
xAxis.setLowerMargin(0.0);
xAxis.setUpperMargin(0.0);
NumberAxis yAxis = new NumberAxis("Distance to Closest Blood Vessel (um)");
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yAxis.setLowerMargin(0.0);
yAxis.setUpperMargin(0.0);
XYBlockRenderer renderer = new XYBlockRenderer();
PaintScale scale = new GrayPaintScale(0, 10000.0);
renderer.setPaintScale(scale);
renderer.setBlockHeight(1);
renderer.setBlockWidth(1);
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinePaint(Color.white);
JFreeChart chart = new JFreeChart("Surface Plot", plot);
chart.removeLegend();
chart.setBackgroundPaint(Color.white);
ChartFrame frame = new ChartFrame("Surface Map - "
+ (Concentration.testing ? "TESTING using "
+ Concentration.testfile : currentFile.getName()), chart);
frame.pack();
frame.setVisible(true);
You have two options:
Represent them as 3d
3D Lib for JFreeChart
You need to use the class : XYBlockRenderer which does exactly what you are asking. You can download the JFreeChart demo collection where the code for this is given.
(source code of class here)
There is also this full code example with 4D very similar.