Implementing custom barchart in android - java

I'm trying to implement a custom barchart
which should look exactly like this. I tried mpChart but could only get this far
barDataSet = new BarDataSet(barEntries, "");
barData = new BarData(barDataSet);
barData.setBarWidth(.5f);
barChart.setData(barData);
barDataSet.setColors(getResources().getColor(R.color.colorPrimary));
barDataSet.setValueTextColor(Color.BLACK);
barDataSet.setDrawValues(false);
barChart.getXAxis().setDrawAxisLine(false);
barChart.setDrawGridBackground(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getAxisRight().setDrawGridLines(false);
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.getLegend().setEnabled(false);
barChart.setPinchZoom(false);
barChart.setDescription(null);
barChart.setTouchEnabled(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.setExtraOffsets(20, 0, 0, 30);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(14f);
xAxis.setTextColor(Color.BLACK);
xAxis.setYOffset(5);
xAxis.setDrawLabels(true);
xAxis.setGranularityEnabled(true);
xAxis.setGranularity(1f);
xAxis.setXOffset(30);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));
xAxis.setCenterAxisLabels(true);
barChart.invalidate();
I tried almost all possible examples and questions here, couldn't get the label to centre of the bars. Is it possible to put the labels exactly below the bars in non grouped bars? all the solutions I saw is only for grouped bars or combined charts.

I was plotting the x axis values from 1. when changed it to 0 and removed
xAxis.setCenterAxisLabels(true);
and i was able to centre the axis labels

You need to create a custom renderer for it.
Subclass BarChartRenderer and then call:
barChart.setRenderer(new MyBarChartRenderer(barChart, barChart.getAnimator(), barChart.getViewPortHandler()))
It will even allow you to alter the shape of it as described here.
But I think what you're looking for comes really close to this.

Download the MPAndroidChart Source from here. Then, you can customize the code according to your need and add it your project by
Clicking on File>New>Import Module.

Related

Android Chart how do i remove VERTICAL SIDE LINES ? tried everything

I'm using MPAndroid Chart , I'm trying to remove these 2 lines and I don't know how.
In my code I already have these:
chart.animateY(500);
chart.invalidate();
chart.setDrawGridBackground(false);
chart.setDrawBorders(false);
chart.setDoubleTapToZoomEnabled(true);
chart.setScaleEnabled(false);
chart.setTouchEnabled(false);
chart.getXAxis().setEnabled(false);
chart.getXAxis().setDrawLabels(false);
chart.getAxisLeft().setDrawLabels(false);
chart.getAxisRight().setDrawLabels(false);
chart.getLegend().setEnabled(false);
chart.getDescription().setEnabled(false);
chart.getXAxis().setDrawGridLines(false);
chart.getAxisRight().setDrawGridLines(false);
chart.getAxisLeft().setDrawGridLines(false);
chart.getXAxis().setDrawGridLines(false);
What is it that I am missing?
From the docs, setDrawAxisLine(boolean enabled): Set this to true if the line alongside the axis (axis-line) should be drawn or not.
In your case you could try with
chart.getAxisLet().setDrawAxisLine(false)
chart.getAxisRight().setDrawAxisLine(false)
Hope it helps!

Aspose slides data labels are overlapping in 'Area Chart'

I am using aspose-slides-17.3-jdk16.jar for java. I have created the Area Chart using IChartDataWorkbook. I have used multiple series to plot area chart and following image is the chart output, where the data labels are overlapped.
Is there any way to organize or fit the data labels properly?
#Chandra Shekar,
I have observed your requirements and like to mention that you can try using different options label.getDataLabelFormat().setLabelPosition() and label.getDataLabelFormat().setShowLabelAsDataCallout(true) methods on your end to set the individual label positions for chart data points. You can please try using following sample code on your end and may alter this as per your requirements on your end.
public static void TestAreaChart()
{
Presentation pres = new Presentation();
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 50, 50, 500, 400);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat()
.setShowLabelAsDataCallout(true);
chart.getChartData().getSeries().get_Item(0).getLabels().get_Item(2).getDataLabelFormat()
.setShowLabelAsDataCallout(true);
chart.getChartData().getSeries().get_Item(0).getLabels().get_Item(2).getDataLabelFormat()
.setPosition(LegendDataLabelPosition.OutsideEnd);
pres.save("C:\\Aspose Data\\AreaChart.pptx", SaveFormat.Pptx);
}
I am working as Support developer/ Evangelist at Aspose.

Vaadin Charts: Pie chart won't update dynamically

I created a Vaadin pie chart with following code.
Chart chart = new Chart(ChartType.PIE);
DataSeries dataSeries = new DataSeries("Logins");
chart.getConfiguration().setSeries(dataSeries);
I need to update my chart dynamically. I tried following (this code gets executed whenever new data is available).
adding new item:
dataSeries.add(new DataSeriesItem("New item", value), true, false);
updating existing item:
DataSeriesItem dataSeriesItem = dataSeries.get(0);
dataSeriesItem.setY(newValue);
dataSeries.update(dataSeriesItem);
But none of the above worked.
The only solution I could find is clearing the chart (chart.clear()), re, populating the data series and re drawing the chart (chart.drawChart()).
This method is not optimal since it re-draws the chart, and also the selection in the chart gets lost.
Does pie chart support dynamic updating? Can anyone suggest a way to fix this?
Try to call chart.getConfiguration().addSeries(dataSeries) before updating items. If that doesn't work, I think you should re-draw your chart.
I had the same problem and fixed it with chart.drawChart() after updating the data series in configurations (chart.getConfiguration.setSeries(newSeries)).
In my case (vaadin version 7.6.3), chart.clear() was not required.
There are other more complicated ways like vaadin-push plugin as well which I didn't find necessary for my case.
I know this question is old, but I will share how I solved this problem to Chart Pie in DChart 1.7.0.
DataSeries dataSeries = new DataSeries();
dataSeries.newSeries();
dataSeries.add( "Food", 500);
dataSeries.add( "Clothes", 1000);
chart.setDataSeries(dataSeries);
chart.show(); //this will update the chart in the browser;
Late to the question, but I encountered similar problem in 2020, I solved it using below code:
dataSeries.clear();
dataSeries.add(new DataSeriesItem("Expense", 120));
dataSeries.add(new DataSeriesItem("Saving", 20));
dataSeries.add(new DataSeriesItem("Balance", 100));
chart.getConfiguration().setSeries(dataSeries);
chart.drawChart();

How to get the rectangular coordinates of the annotation using ice pdf viewer

I need to get the rectangular coordinates of the annotation using ice pdf viewer. Is there anyway to achieve this?
You'll need to go into the Viewer RI source code make a few changes and then rebuild the icepdf-viewer.jar.
The class org.icepdf.ri.common.tools.SquareAnnotationHandler handles all the mouse events and drawing for creating a Square annotation. This is a good place to start for extending or using it as a reference.
Add a message dialog to under mouseReleased
JOptionPane.showMessageDialog(null, "Rectangle coordinates x ---> "+rectToDraw.x+" y --> "+ rectToDraw.y+" Width --> "+ rectToDraw.width+" height --> "+ rectToDraw.height);
If anyone is still looking for answer.
Icepdf's Annotation class holds these coordinates. They can be retrieved like this:
Rectangle2D.Float box = annotation.getUserSpaceRectangle();
Map<String, Double> bounds = new HashMap<String, Double>();
bounds.put("height", box.getHeight());
bounds.put("width", box.getWidth());
bounds.put("x", box.getX());
bounds.put("y", box.getY());

Chart Wont Show When I Run It?

I'm trying to make a simple pie chart appear on a jpanel in netbeans with JFreeChart and got this:
public void createPieChart()
{
DefaultPieDataset myPie = new DefaultPieDataset();
myPie.setValue("Apples",new Integer(12));
myPie.setValue("Oranges",new Integer(23));
myPie.setValue("Mangos",new Integer(7));
myPie.setValue("Pears",new Integer(22));
JFreeChart myChart = ChartFactory.createPieChart3D("Damo's Fruit Sales", myPie,true,true,true);
PiePlot3D pie3D = (PiePlot3D)myChart.getPlot();
ChartPanel myPanel = new ChartPanel(myChart);
lowerMain_PNL.removeAll();
lowerMain_PNL.add(myPanel,BorderLayout.CENTER);
lowerMain_PNL.revalidate();
}
I get no compiler errors and when it runs the window appears with the button, but when I press the button my pie chart doesn't appear. Anyone know what I could be missing?
Check the layout manager of lowerMain_PNL. Netbeans form designer uses GroupLayout by default, so unless you changed it, that's what you got. Adding to a container using GroupLayout at run time is tricky, especially if the component contains more than one subcomponent (And requires adding components to the layout, instead of using the usual add() methods).
Change it to BorderLayout instead, since you are using BorderLayout constraints.

Categories

Resources