Vaadin Charts: Pie chart won't update dynamically - java

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();

Related

Sencha GXT4 : How to display text inside a pie chart?

I'm developing an application with the GWT framework and Sencha GXT on the front-end.
I managed to display some data in a pie chart but I would also like to have a label inside the donut pie chart like . I looked at the demo provided by sencha as well as the "Chart" class but I don't see any cases where there is text inside a pie chart. Is it feasible ?
I'm using "Sencha GXT 4.0.0 - Sencha for GWT"
I finally found a solution, I just need to use the setX method in my textConfig so my code looks something like this :
TextSprite textConfig = new TextSprite();
textConfig.setFont("Arial");
textConfig.setTextBaseline(TextBaseline.MIDDLE);
textConfig.setFontSize(18);
textConfig.setTextAnchor(TextAnchor.MIDDLE);
textConfig.setZIndex(15);
textConfig.setX(-150);
150 worked for me but maybe it won't for you.

Implementing custom barchart in android

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.

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.

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.

JfreeChart background image not showing up

I'm creating a chart in a servlet, and it works great.
chart = ChartFactory.createPieChart("Smart Chart", ds, true, true,
true);
PiePlot plot = (PiePlot) chart.getPlot();
ImageIcon icon = new ImageIcon(bgImageStr);
plot.setBackgroundPaint(Color.CYAN);
plot.setBackgroundAlpha(0.15f);
chart.setBackgroundPaint(Color.WHITE);
chart.getTitle().setBackgroundPaint(Color.PINK);
chart.setBackgroundImage(icon.getImage());
Problem is, the background image is not showing up. I've tried the plot and chart bg, and all kinds of other stuff. It must be simple, anyone see what is wrong? I'm just using the write to PNG to dump it to the browser. It shows up fine, with all the color changes, just no image.
OK, OP here, I fixed it.
I was calling the servlet from itself, and it did not like that. I used a resource (as suggested) and it worked fine without the infinite recursion (imagine that).
apply
try{
Thread.sleep(100);
}
catch(InterruptedException IntExp)
{
//your implementation
}
before chart.setBackgroundImage(icon.getImage());

Categories

Resources