How to customize legend of a BoxAndWhisker plot - java

I've created a BoxAndWhiskerRenderer plot using JFreeChart and it seems that it is automatically creating a sort of legend (see attached picutre).
Is there a way to remove the outside border of this legend and customize the font of the labels in the legend items?
Here is an example of my code:
//Get the desired BoxAndWhiskerCategoryDataset from a LinkedHashMap
BoxAndWhiskerCategoryDataset dataset = values.get(b);
//Create X axis
CategoryAxis xAxis = new CategoryAxis();
xAxis.setAxisLineVisible(false);
//Create Y axis
NumberAxis yAxis = new NumberAxis(b.getLabel());
yAxis.setAxisLineVisible(false);
yAxis.setTickLabelFont(FDFont.getFont(12f));
yAxis.setLabelFont(FDFont.getFont());
yAxis.setLabelPaint(FDPalette.secondaryText);
yAxis.setTickLabelPaint(FDPalette.secondaryText);
yAxis.setAutoRangeIncludesZero(false);
//Create renderer
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
int count = 0;
for(Map.Entry<Integer,Color> map : clusterColor.entrySet()){
//Set color for the series (I have a previously created map which links colors and series)
renderer.setSeriesPaint(count,map.getValue());
count++;
}
renderer.setFillBox(true);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(plot);
chart.setBackgroundPaint(white);
chart.setBorderVisible(false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(300, 270));

As shown here, the simplest JFreeChart constructor adds a legend by default. The code for doing so is seen here; simply substitute your desired frame, color and position.
Starting from this example, the following changes produce the chart shown below:
private void createChartPanel() {
…
JFreeChart chart = new JFreeChart("BoxAndWhiskerDemo", plot);
LegendTitle legend = chart.getLegend();
legend.setFrame(new LineBorder(Color.white, new BasicStroke(1.0f),
new RectangleInsets(1.0, 1.0, 1.0, 1.0)));
legend.setItemFont(legend.getItemFont().deriveFont(16f));
chartPanel = new ChartPanel(chart);
}
Compare to this default legend:

Related

Show stock split in candle chart jfreechart

Currently I'm working on charts and want to achieve an output.
I wanted to achieve two things.
To display candle stick chart of stocks data. [done]
To display the stock split log above or below the candle. [pending]
I'm using JFreeChart SDK to build the charts.
I'm using OHLCDataItem[] to build data for daily high/low etc of stock. unfortunately jfreechart does not have any extension of OHLCDataItem to show additional information (logo + text metadata) on the candle.
Sample code:
TimeSeries timeSeriesDailyMovingAverage = new TimeSeries("day moving average");
List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>(); //to collect High low close prices
OHLCDataItem item = new OHLCDataItem(date, open, high, low, adjClose, volume);//this code is in loop with do that more data can be added for each day
dataItems.add(item);
OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);
OHLCDataset dataset = new DefaultOHLCDataset(symbol, data);
//now initializing the candleStickRanderer and setting its properties series strock + paint
CandlestickRenderer CandleStickRenderer = new CandlestickRenderer();
XYLineAndShapeRenderer LineRenderer = new XYLineAndShapeRenderer(true, false);
HighLowRenderer OHLCRenderer = new HighLowRenderer();
DateAxis domainAxis = new DateAxis();
NumberAxis rangeAxis = new NumberAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, LineRenderer);//set other properties
JFreeChart chart = new JFreeChart(info, new Font("SansSerif", Font.PLAIN, 15), plot, false);
ChartPanel chartPanel = new ChartPanel(chart);
JPanel panel = new JPanel(new BorderLayout());
panel.add(chartPanel, BorderLayout.CENTER);
getContentPane().add(panel);
XYPlot xyplot = (XYPlot) chart.getPlot();
xyplot.setRenderer(CandleStickRenderer);
This is some rough code which is working fine. I'm getting the desired out of Candle chart.
current output of candle stick
But I cannot figure out how can I add text/logo on XYPlot based on dataset.
What I want to achieve is that, add text/or shape containing text information at the bottom/top of a particular candle.
It would be a great help if someone can guide me how I can do it based on the dataset.
The desired output is like this chart.
desired output
Or
desired output

Wrap Plot label in a Combined Plot JFreeChart

Is there a way to wrap plot label for Category axis?
I'm facing a problem where these titles merge between the plots. It would be best if they would wrap at least once. Is it possible?
The titles are dynamic.
Here's the classes I use to construct it.
var renderer = new LineAndShapeRenderer();
...
var yAxis = new NumberAxis();
...
var xAxis = new CategoryAxis(title);
...
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
...
var combinedPlot = new CombinedRangeCategoryPlot();
combinedPlot.add(plot, weight);
...
I'm using version 1.0.19 of JFreeChart since newer versions produce some visual artifacts.

Issues creating a jfreechart score bar/chart

I am trying to create a score/percent/status bar using jfreechart.
The image show what it's suppose to look like.Good ScoreChart
It looks like one bar from a stack chart, so I try to adjust from there. (Do let me know if there is a better way to do this other than the stack chart)
I am seeing multiple issues doing the bar this way. I can't make the start line and end line longer. I can't put text on top of both start/end line.(I think they get chopped off) I cannot put center text in the middle of the bar either...
Here is what I got now. Bad ScoreChart
Here is the code for the bad Chart.
public JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedBarChart(null, null, null, dataset,
PlotOrientation.HORIZONTAL, false, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
plot.getRenderer().setSeriesPaint(0, DONUT_CHART_ORANGE);
plot.getRenderer().setSeriesPaint(1, DONUT_CHART_BLUE);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setVisible(false);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
plot.setRangeGridlinesVisible(false);
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlineVisible(false);
CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer();
//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelGenerator(new CustomItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER);
renderer.setBasePositiveItemLabelPosition(position);
Marker startMarker = new ValueMarker(0);
startMarker.setPaint(Color.black);
startMarker.setLabel("000000000");
startMarker.setStroke(new BasicStroke(1.0f));
startMarker.setLabelOffset( new RectangleInsets(UnitType.ABSOLUTE, 10,0,0,0));
startMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT) ;
startMarker.setLabelTextAnchor(TextAnchor.BASELINE_CENTER);
plot.addRangeMarker(startMarker);
Marker endMarker = new ValueMarker(1000);
endMarker.setPaint(Color.black);
endMarker.setLabel("1000");
endMarker.setStroke(new BasicStroke(1.0f));
endMarker.setLabelAnchor(RectangleAnchor.TOP);
endMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
plot.addRangeMarker(endMarker);
RectangleInsets chartRectangle = new RectangleInsets(35, 0, 35, 0);
chart.setPadding(chartRectangle);
return chart;
}
public CategoryDataset createDataset() {
double inValue = 775;
double[][] data = new double[][] {
{ inValue },
{ 1000- inValue },
};
return DatasetUtilities.createCategoryDataset("A", "B", data);
}
Any Idea how to move the text so they show up on top of the lines and at the center of the bar?
Can we adjust the line width at the beginning and the end?
Thanks for any idea/info.

JFreechart XYAreaChart changing color

I have an area chart , I want to color the area of the chart which is >0 in green and the area which is <0 in red. I tried the SetSeriesPaint() method but it makes all the area chart colored by one color. How to do this?
Here's my code:
final XYSeries series = new XYSeries("Data");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYAreaChart("Fun Meter", "", "",
dataset, PlotOrientation.VERTICAL, false, false, false);
// jframe=new ChartFrame("Fun Meter", chart);
ChartPanel CP = new ChartPanel(chart);
XYPlot xyPlot = (XYPlot) chart.getPlot();
xyPlot.getRenderer().setSeriesPaint(0, Color.red);
xyPlot.setForegroundAlpha(0.75f);
Use a XYDifferenceRenderer:
XYDifferenceRenderer r = new XYDifferenceRenderer(Color.green,Color.red, false);
plot.setRenderer(r);

In jfreechart TimeSeriesCollection how to show dates which has only values?

public class Example {
public static void main(String args[]){
/*creating timeSeriesCollection*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries timeSeries1 = new TimeSeries("Sample 1");
timeSeries1.add(new Day(8,4, 2012), 7.0);
timeSeries1.add(new Day(19,4, 2012), 5.0);
dataset.addSeries(timeSeries1);
/*Creating the chart*/
JFreeChart chart = ChartFactory.createTimeSeriesChart("Population","Date","Population",dataset,true,true,false);
/*Altering the graph */
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(5.0, 10.0, 10.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));
axis.setAutoTickUnitSelection(false);
axis.setVerticalTickLabels(true);
/* Displaying the chart*/
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);
}
}
I have written the above code which works perfectly fine but when rendering the graph it displays date which does not have values. It displays dates like 9/4/12,10/4/2012 to 18/4/2012 in y-axis which does not have a value. How can I remove the date which does not have value. And why it is behaving so?
Can any one help me please?
Try this:
public class Example1 {
public static void main(String args[]){
DefaultKeyedValues data = new DefaultKeyedValues();
data.addValue("8/4/2012" ,7.0);
data.addValue("19/04/2012",5.0);
CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
ChartFrame frame = new ChartFrame("Test", chart);
//Switch from a Bar Rendered to a LineAndShapeRenderer so the chart looks like an XYChart
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseLinesVisible(false); //TUrn of the lines
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRenderer(0, renderer);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
frame.pack();
frame.setVisible(true);
}
}
Rather than using a TimeSeriesChart use a CategoryDataSet as #Dirk sugested and then switch to a LineAndShapeRenderer.
The TimeSeries is displayed continuously, which means that the dates are equally distributed along the axis, even if there is no actual value for this date. If you want only certain dates displayed, you should use some kind of CategoryDataSet.

Categories

Resources