I am creating a Bar Chart:
final JFreeChart result = ChartFactory.createBarChart(TITLE, // chart title
X_TITLE, // domain axis label
Y_TITLE, // range axis label
dataset, // data
PlotOrientation.HORIZONTAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
I then added a ChartMouseListener as described here.
When I click on a legend item, "LegendItemEntity: seriesKey=null, dataset=null" is displayed.
How do I access the legend text for the corresponding item?
Starting from org.jfree.chart.demo.BarChartDemo1 and adding the same ChartMouseListener to the source yields results like this:
LegendItemEntity: seriesKey=First, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
LegendItemEntity: seriesKey=Second, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
LegendItemEntity: seriesKey=Third, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
If you're geting a different result, an sscce may help.
Related
What I have done: I have created a pie chart using MPAndroidChart library and ended up having the legend in the left corner. I tried changing the pie chart width but then when the items increase legend tends to wrap to a second line.
What I expect: I want the legend to be in the center every time when it updates. Even legend wrap to the second line, I want it to be in the center. I would appreciate any suggestions on this.
Thank you!
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
>
</com.github.mikephil.charting.charts.PieChart>
Activity Code :
private void setupPieChart(){
//pupulating list of PieEntires
List<PieEntry> pieEntires = new ArrayList<>();
for( int i = 0 ; i<time.length;i++){
pieEntires.add(new PieEntry(time[i],activity[i]));
}
PieDataSet dataSet = new PieDataSet(pieEntires,"");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(dataSet);
//pie slices text value
data.setValueTextSize(10);
//Get the chart
pieChart.setData(data);
pieChart.invalidate();
pieChart.setCenterText("50%");
pieChart.setDrawEntryLabels(false);
pieChart.setContentDescription("");
//pieChart.setDrawMarkers(true);
//pieChart.setMaxHighlightDistance(34);
pieChart.setEntryLabelTextSize(12);
pieChart.setHoleRadius(75);
//to remove bottom left description
pieChart.getDescription().setEnabled(false);
//legend attributes
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setTextSize(12);
legend.setFormSize(20);
legend.setFormToTextSpace(2);
//to wrap legend text
legend.setWordWrapEnabled(true);
Log.d("legend " ,legend.getEntries().toString());
}
I was able to center the legend by adding the below line in setupPieChart() mentioned in the question above.
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
Is there any way, to change Category Axis Label Expression's Alignment of a Chart in Jasper Report through Java Code.
I want Left alignment of Category Axis Label Expression. As shown the picture below, I want "hello" to be left aligned.
Starting with BarChartDemo1, included in the distribution, the changes below create a bar chart with an axis label having its location set to the LOW_END. For PlotOrientation.VERTICAL, that means aligned on the left.
JFreeChart chart = ChartFactory.createBarChart(
"Performance: JFreeSVG vs Batik",
"$P{hello}" /* x-axis label*/,
"Milliseconds" /* y-axis label */,
dataset, PlotOrientation.VERTICAL, false, false, false);
…
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelLocation(AxisLabelLocation.LOW_END);
Try doing the same for the range axis to see the effect:
rangeAxis.setLabelLocation(AxisLabelLocation.LOW_END);
Assume that I have a JFreeChart 's line chart with points (0,0) and (3,3). So the line from (0,0) to (3,3) is shown. However when I zoom X-Axis so that to see the data between x=1 and x=2 no data (no line) is shown.
The problem occurs for me in two cases:
JFreeChart chart = org.jfree.chart.ChartFactory.createTimeSeriesChart("Example",
"", "test", null, false, true, false);
and for:
JFreeChart chart = org.jfree.chart.ChartFactory.createXYStepChart
("Example", "", "test", null,PlotOrientation.VERTICAL, true, true, false);
However in the second additionally I am getting the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Requires xLow < xHigh.
at org.jfree.chart.renderer.RendererUtilities.findLiveItemsLowerBound(RendererUtilities.java:76)
at org.jfree.chart.renderer.RendererUtilities.findLiveItems(RendererUtilities.java:261)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3845)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3389)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1237)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1677)
The main question is:
How can I set the renderer up to see the data (line) even if I zoom between 'passed data points'?
Can someone tell me how to change samples of series color in legend in jfreechart. What I have now is small line of series color eg: I would like to have square sample of those colors. Here is an example
Can someone help me?
Ok I found the solution. At least I think. Of course there is no simple way to do this. There is now, you know, setShape(square) method, that will do the trick, at least i haven't found one.
Basicly XY chart and time chart have "line style" legend by default in contrary to bar chart for example (if has square legend by default). So I had to remove current legend and create new one with square samples of color and this new legend add to my time chart.
LegendItemCollection legend = new LegendItemCollection();
for (int i = 0; i < seriecCount; ++i) {
chart.getXYPlot().getRenderer().setSeriesPaint(i, colorPalette.get(i));
LegendItem li = new LegendItem(data.getSeriesName(i), "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, colorPalette.get(i));
legend.add(li);
}
chart.getXYPlot().setFixedLegendItems(legend);
Thanks for attention. I hope it will help someone.
Generating your own legend, as you do above, is a perfectly acceptable way of doing things in JFreeChart. If you didn't want to do it, you can also define your own renderer with the lookupLegendShape() method overridden.
thePlot.setRenderer(new XYLineAndShapeRenderer()
{
public Shape lookupLegendShape(int series)
{
return new Rectangle(15, 15);
}
});
If you use a XYBarRenderer Class XYBarRenderer
(Subclasses: ClusteredXYBarRenderer, StackedXYBarRenderer)
You can use XYBarRenderer.setLegendBar(java.awt.Shape bar);
See: Javadoc
to get nice squares.
Example:
JFreeChart chart = ChartFactory.createXYBarChart(/*...*/);
XYPlot plot = (XYPlot) chart.getPlot();
ClusteredXYBarRenderer renderer = new ClusteredXYBarRenderer();
renderer.setLegendBar(new Rectangle(17, 17));
plot.setRenderer(renderer);
Is it possible to draw a 3D chart using JfreeChart like in the following link.If possible can anyone give some hints and some snippets of code on what parameters of Plot can be used to do this.
link text
It's possible though it won't look exactly the same. The easiest way is to create a dataset (descendant of org.jfree.data.general.PieDataset) and use one of org.jfree.chart.ChartFactory methods:
PieDataset data = new DefaultPieDataset();
data.setValue("Section1", 30);
data.setValue("Section2", 60);
data.setValue("Section3", 120);
JFreeChart pieChart = ChartFactory.createPieChart3D(
"My Pie Chart", // title
data, // data set
true, // draw a legend
true, // show tooltips over sections
false); // do not generate image map with URLs
You can then further customize your chart through pieChart methods. For example, here's how to explode one pie section:
PiePlot plot = (PiePlot) pieChart.getPlot();
plot.setExplodePercent("Section2", 0.25);