Achartengine display values inside the series areas and horizontal legend - java

I have tried the following with the default renderer
CategorySeries series = new CategorySeries("First test");
int numSlide = portions.length;
for (int i = 0; i < numSlide; i++){
series.add(seriesNames[i]+" ("+portions[i]+" %)", portions[i]);
}
DefaultRenderer defaultRenderer = new DefaultRenderer();
SimpleSeriesRenderer simpleSeriesRenderer = null;
for (int i = 0; i < numSlide; i++){
simpleSeriesRenderer = new SimpleSeriesRenderer();
simpleSeriesRenderer.setColor(colors[i]);
simpleSeriesRenderer.setChartValuesFormat(new DecimalFormat("###,###,##0.0"));
defaultRenderer.addSeriesRenderer(simpleSeriesRenderer);
}
defaultRenderer.setInScroll(true);
defaultRenderer.setZoomButtonsVisible(false);
defaultRenderer.setZoomEnabled(true);
defaultRenderer.setLabelsTextSize(18); //value size
defaultRenderer.setLabelsColor(R.color.primary_dark);
defaultRenderer.setShowLegend(false);
defaultRenderer.setClickEnabled(true);
defaultRenderer.setPanEnabled(true);
defaultRenderer.setShowLabels(true);
defaultRenderer.setShowLegend(true);
//return the pie chart view
return ChartFactory.getPieChartView(context, series, defaultRenderer);
Now the above produces
What am looking forward to get is to have the percentage values eg: 2% inside the chart area something which looks like this
How do i get the percentage values to be displayed inside the charts?

You want to display values and hide labels. Do that like so in your renderer:
defaultRenderer.setShowLabels(false);
defaultRenderer.setDisplayValues(true);
Also, your series names should probably not contain the values, so change the following line
series.add(seriesNames[i]+" ("+portions[i]+" %)", portions[i]);
to
series.add(seriesNames[i], portions[i]);

Related

Applying css to LineChart JavaFx chart

I am filling a LineChat chart with data and sometimes I need to redraw it. The drawGraph() method is attached to the button click event (we press it to redraw the graph). I want the first data series to have transparent markers and the second data series to have transparent lines. For this I use css. On the first call to the drawGraph() method, everything works fine, and I get this picture:
It should be
I try to make it so that every time I redraw the chart, I get the 1st picture
But from the next redrawings, the following picture starts to be displayed every other time:
It should not be
That is, a graph is displayed every other time without applying css to it, and one data series overlaps another
Style sheet:
/* Removes points from a data series */
.default-color0.chart-line-symbol {
-fx-background-color: transparent;
}
/* Removes lines from a data series */
.default-color1.chart-series-line {
-fx-stroke: transparent;
}
Chart redrawing method (from ApplicationController):
double[] X = new double[] {1,2,3,4,5};
double[] Y = new double[] {1.9,5.5,10,15,21};
public void drawGraph() {
//clear the Chart
lineChart.getData().clear();
//Data series #1
ObservableList<XYChart.Data> datas = FXCollections.observableArrayList();
XYChart.Series series = new XYChart.Series();
//Filling series #1
for(int i=0; i< X.length; i++){
datas.add(new XYChart.Data(X[i],Y[i]));
}
series.setData(datas);
//Data series #2
ObservableList<XYChart.Data> datas1 = FXCollections.observableArrayList();
XYChart.Series series1 = new XYChart.Series();
//Filling series #2
for(int i=0; i< X.length; i++){
datas1.add(new XYChart.Data(X[i],Y[i]));
}
series1.setData(datas1);
//lineChart.setStyle();
lineChart.getStylesheets().add(HelloApplication.class.getResource("style.css")
.toExternalForm());
lineChart.getData().add(series);
lineChart.getData().add(series1);
}
It was bad practice to create new data series and place them on the chart instead of the old ones. Also, it was not worth using a style sheet for each new data redrawing.
What I did was to fill in the graph for the first time, and when it was redrawn, I replaced it in the series as follows.
//We get a series:
XYChart.Series<Number, Number> series = lineChart.getData().get(0);
//Let's remove all elements in the series:
series.getData().clear();
//We add elements to the series like this:
series.getData().add(new LineChart.Data<Number,Number>(1, 2));

MPChart xAxis values not aligned

I am trying to implement grouped MPAndroid Bar chart. I have a group of 2 datasets that i want to display. The problem is that the xaxis values are not center aligned with the bar chart (as per the screenshot). I checked other questions as well and implemented the following answers provided.
I want to make the labels center aligned with the grouped bars.
float barSpace = 0.02f;
float groupSpace = 0.3f;
int groupCount = 2;
data.setBarWidth(0.155f);
pvaAmount_chart.getXAxis().setAxisMinimum(0);
pvaAmount_chart.getXAxis().setAxisMaximum(0 + pvaAmount_chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
pvaAmount_chart.groupBars(0, groupSpace, barSpace);
pvaAmount_chart.getXAxis().setCenterAxisLabels(true);
pvaAmount_chart.notifyDataSetChanged();
When entering groupcount=2 as 2 types of bars:
When entering groupcount=4 number of grouped charts:
i'm doing it like this :
String [] values = new String[insert the lenght of your array]
for (int blabla=0;i<values[lenght];blabla++){
String dayOfTheWeek = dateFormat.format(mydate);
vales[blabla] = dayOfTheWeek //in my case
}
xAxis.setGranularity(1.0f); //
xAxis.setCenterAxisLabels(false);
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
List<String> stringList = new ArrayList<String>(Arrays.asList(values));
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues((ArrayList<String>) stringList)));
i'm using : 'com.github.PhilJay:MPAndroidChart:v3.1.0'

JFreeChart getSeriesStroke/Paint/Shape returns null

After creating a line chart by passing to JFreeChart constructor a XYSeriesCollection dataset, I'm trying to get either series Stroke/Paint/Shape as:
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < dataset.getSeriesCount(); i++) {
renderer.getSeriesStroke(i);
renderer.getSeriesPaint(i);
renderer.getSeriesShape(i);
}
but all return null.
Why is that? How can I get the non-null objects?
An XYLineAndShapeRenderer is an XYItemRenderer, which supports "rendering the visual representation of a single (x, y) item on an XYPlot." Although the corresponding series properties are null, the item properties are accessible. Starting from this example, The following changes produce the output shown:
Code:
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < xyPlot.getDataset().getSeriesCount(); i++) {
System.out.println(renderer.getItemStroke(i, 0));
System.out.println(renderer.getItemPaint(i, 0));
System.out.println(renderer.getItemShape(i, 0));
System.out.println(renderer.getItemShape(i, N));
}
Console:
$ java -cp .:$JFREE_LIB/* ScatterShape
java.awt.BasicStroke#d1a007c0
java.awt.Color[r=255,g=85,b=85]
java.awt.geom.Rectangle2D$Double[x=-3.0,y=-3.0,w=6.0,h=6.0]
java.awt.geom.GeneralPath#7ef51f0f

Bar chart in aChartEngine in Android

I am working on graph chart using aChartEngine, the problem faced is that the although graph are generating, problem is graph is not coming as per need inside the view, how to fix it ??
When selecting Bar from drop down
After Zooming Out and panning(by interacting with graph)
Here is my code
private static XYMultipleSeriesDataset getBarDataset(int rightAnswers, int wrongAnswers)
{
String[] labels={"right","wrong"};
List<Double> values = new ArrayList<Double>();
values.add((double) rightAnswers);
values.add((double) wrongAnswers);
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
for (int i = 0; i < titles.length; i++)
{
XYSeries series = new XYSeries(titles[i]);
series.add(i,values.get(i));
dataset.addSeries(i,series);
}
return dataset;
}
2)Renderer
protected static XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.addXTextLabel(0, "right");
renderer.addXTextLabel(1, "wrong");
renderer.setChartTitle("Results");
int length = colors.length;
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[0]);
renderer.addSeriesRenderer(r);
r = new SimpleSeriesRenderer();
r.setColor(colors[1]);
renderer.addSeriesRenderer(r);
renderer.setScale(10);
renderer.setXLabels(0);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);
renderer.setAxesColor(Color.CYAN);
renderer.setBarWidth(30);
return renderer;
}
3) the view
ChartFactory.getBarChartView(context, getBarDataset(rightAnswers, wrongAnswers), buildBarRenderer(new int[]{Color.GREEN,Color.RED}), Type.DEFAULT);
Query
1) How to fix the position and exact panned graph
2) Can we have 3d effect on bars??How??
3) Is there any other library simpler compared to aChartEngine??
To solve the problem of having bar inside the graph we need to specify X-axis and Y-axis min and max based on your minimum and maximum values,eg; for the renderer, you need
renderer.setXAxisMin(-1);
renderer.setXAxisMax(wrongAnswers);
renderer.setYAxisMax(10);
renderer.setYAxisMin(0);
so the final result comes out to be

JfreeChart - How to hide item from legend - Problem of colors

I would like to hide items from legend in Jfreechart and I've tried this code
jFreeChart: How to hide items from legend?
It works but something strange happened : the colors of the legend items do not match to the right data anymore. In other words, in the graph, a piece of data appears in yellow for example but the legend corresponding to this item appears in another color. In fact, the colors in the legend have been mixed.
Besides, when I try to display both old and new legends, there is no problem of colors but when I make the old legend invisible, the problem of mixed color appears. Obviously, I would like not to display the old legend.
Working code =>
LegendItemCollection legendItemsOld = localCombinedDomainXYPlot.getLegendItems();
final LegendItemCollection legendItemsNew = new LegendItemCollection();
for(int i = 0; i<4; i++){
legendItemsNew.add(legendItemsOld.get(i));
}
LegendItemSource source = new LegendItemSource() {
LegendItemCollection lic = new LegendItemCollection();
{lic.addAll(legendItemsNew);}
public LegendItemCollection getLegendItems() {
return lic;
}
};
localJFreeChart.addLegend(new LegendTitle(source));
ChartUtilities.applyCurrentTheme(localJFreeChart);
localJFreeChart.getLegend().setVisible(true); ///////////////////
Not working code =>
LegendItemCollection legendItemsOld = localCombinedDomainXYPlot.getLegendItems();
final LegendItemCollection legendItemsNew = new LegendItemCollection();
for(int i = 0; i<4; i++){
legendItemsNew.add(legendItemsOld.get(i));
}
LegendItemSource source = new LegendItemSource() {
LegendItemCollection lic = new LegendItemCollection();
{lic.addAll(legendItemsNew);}
public LegendItemCollection getLegendItems() {
return lic;
}
};
localJFreeChart.addLegend(new LegendTitle(source));
ChartUtilities.applyCurrentTheme(localJFreeChart);
localJFreeChart.getLegend().setVisible(false); ///////////////////
Based on this thread, you might try adding a null element to replace the unwanted legend item. The other approach appears to elide unwanted items, but I'm not sure if you're doing the same. To clarify, consider posting an sscce that demonstrates the problem. One of the org.jfree.chart.demo classes may be a suitable starting point.

Categories

Resources