I am using aChart Engine, Zoom in and out functionality in it moves the chart away from the screen.I want to keep the Chart fit to screen. Is there any way to keep the chart visible on screen i.e. always fit it to visible portion ??
I am using this method :
private XYMultipleSeriesRenderer getLineRenderer(int[] lineColor, PointStyle[] styles) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxesColor(Color.BLUE);
renderer.setAxisTitleTextSize(16);
renderer.setChartTitle("Time");
renderer.setChartTitleTextSize(15);
renderer.setFitLegend(true);
renderer.setPanEnabled(true, true);
renderer.setYLabels(6);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
renderer.setZoomButtonsVisible(true);
renderer.setBarSpacing(10);
renderer.setShowGrid(true);
renderer.setYAxisMin(Common.min - 1);
renderer.setYAxisMax(Common.max + 1);
XYSeriesRenderer rendererSeries = new XYSeriesRenderer();
rendererSeries.setColor(Color.RED);
renderer.addSeriesRenderer(rendererSeries);
rendererSeries.setFillPoints(true);
rendererSeries.setLineWidth(3);
return renderer;
}
You can use two things,
Either desable the Zoom functionality
renderer.setZoomEnabled(false, false);
or, Set Limits of for the Zoom
renderer.setZoomLimits(arg0); // arg0 = new double[]{-20,20,-20,20}; anything you want
I find out the solution to my query
renderer.setPanEnabled(false);
renderer.setPanEnabled(false, false);
It works fine
You can do either:
renderer.setPanEnabled(false);
or
renderer.setPanLimits(limits);
Related
I'm creating a dynamic bar charts using achartengine in my application. Bar charts are used to display weekly work data in hours and they are scrollable horizontally (each bar chart have its own view with width of screen). I've placed bar charts in HorizontallScrollView with infinity scroll and sticking enabled (by swapping views with each other) and when I'm scrolling to either left or right the bar charts shrinks to the side of screen instead of being scrolled out of screen like rest of views. I've found that I should enable setInScroll on renderer of my chart to get rid of this behaviour but sadly this doesn't work (chart shrinks with setInScroll set to true). I'm out of ideas how to fix that. Here's my renderer setup:
private static XYMultipleSeriesRenderer getRenderer(List<Integer> list, int[] weekdays)
{
float maxValue = 0;
for (Integer value : list){
if (value > maxValue){
maxValue = value;
}
}
NumberFormat formatter = new DecimalFormat("#' h'");
formatter.setMaximumFractionDigits(0);
maxValue = maxValue/60;
maxValue = maxValue + (float)(maxValue*0.2);
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
seriesRenderer.setColor(mContext.getResources().getColor(R.color.ActionBarBlue));
seriesRenderer.setChartValuesFormat(formatter);
seriesRenderer.setFillPoints(true);
seriesRenderer.setLineWidth(2);
seriesRenderer.setDisplayChartValues(true);
seriesRenderer.setChartValuesTextSize(12);
seriesRenderer.setChartValuesTextAlign(Paint.Align.RIGHT);
renderer.addSeriesRenderer(seriesRenderer);
for (int i=0; i < 7; i++){
renderer.addXTextLabel(i+1, mDays[i] + " " + weekdays[i]);
}
renderer.setBackgroundColor(mContext.getResources().getColor((R.color.White)));
renderer.setApplyBackgroundColor(true);
renderer.setPanEnabled(false);
renderer.setInScroll(true);
renderer.setMargins(new int[] {0, 0, 0, 0});
renderer.setAntialiasing(true);
renderer.setMarginsColor(mContext.getResources().getColor(R.color.White));
renderer.setShowLegend(false);
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(7.5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(maxValue);
renderer.setBarSpacing(0.5);
renderer.setShowGrid(true);
renderer.setGridColor(mContext.getResources().getColor(R.color.Gray));
renderer.setXLabelsColor(mContext.getResources().getColor((R.color.Black)));
renderer.setYLabelsColor(0, mContext.getResources().getColor((R.color.White)));
renderer.setLabelsTextSize(12);
renderer.setXLabels(0);
renderer.setYLabels(0);
return renderer;
}
Appears it was some kind of problem with Android Studio and java, not responding to any changes in code i made, rebooting PC worked.
I am using achartengine to display charts in an Android app.
The app allows the user to display different datasets and different charts types depending on the selections they make from spinners.
My problem is this. The original chart displays correctly and all zoom, pan, and onClick events preform as expected. But when I remove the first chart and add another, the charts will not zoom or pan from touch and onClick will not return selected point data. The achartengine zoom and reset buttons still display and work as expected. To remove a chart and add another I have used different variations of the following code with the same results.
private void repaint() {
mchart1_LL_pie = (LinearLayout) this.findViewById(R.id.chart1_LL_pie);
if (mChart1_ChartView_pie != null) {
mchart1_LL_pie.removeView(mChart1_ChartView_pie);
}
mChart1_ChartView_pie = ChartFactory.getPieChartView(getApplicationContext(), mChart1_Pie_Series, mChart1_Pie_Renderer);
mchart1_LL_pie.addView(mChart1_ChartView_pie, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
I have also tried to remove all the renderers and clear the data series with the following code.
mChart1_Pie_Renderer.removeAllRenderers();
int size = mChart1_Pie_Series.getItemCount();
for (int i = 0; i < size; i++) {
mChart1_Pie_Series.remove(0);
And then display the chart with a rebuilt dataset and renderer using the following.
mChart1_ChartView_pie = ChartFactory.getPieChartView(getApplicationContext(), mChart1_Pie_Series, mChart1_Pie_Renderer);
The problem with this approach is that the next chart is not displayed until the display is touched. After the display is touched, the correct chart is displayed and all zoom, pan and onClick events work properly.
Has anyone experienced this problem? If so, how did you solve it?
Any help or suggestions would be greatly appreciated. Thank you
Don
I am doing an application with charts. I have an evolution chart with TimeSeries. I paste my function to draw the chart. It can help you :)
public void drawChart() {
mChartView = null;
mDataset = null;
mRenderer = null;
mDataset = new XYMultipleSeriesDataset();
mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setAxisTitleTextSize(15);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.BLUE);
r.setFillPoints(true);
mRenderer.addSeriesRenderer(r);
mRenderer.setZoomEnabled(true);
mRenderer.setShowGrid(true);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setShowLegend(true);
mRenderer.setShowLabels(true);
mRenderer.setLabelsColor(Color.BLACK);
mRenderer.setBackgroundColor(Color.TRANSPARENT);
mRenderer.setYLabelsPadding(20);
mRenderer.setXLabelsPadding(7);
mRenderer.setYLabelsAlign(Align.LEFT);
mRenderer.setMarginsColor(Color.rgb(220, 220, 220));
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setLabelsColor(Color.BLACK);
mRenderer.setXLabelsColor(Color.BLACK);
mRenderer.setYLabelsColor(0, Color.BLACK);
// mRenderer.setAxisTitleTextSize(30);
mRenderer.setGridColor(Color.BLACK);
mRenderer.setXRoundedLabels(false);
// mRenderer.setLabelsTextSize(25);
mRenderer.setYTitle(Config.getResource("PRICE") + "("
+ dto.getCurrencyCode() + ")");
mRenderer.setMargins(new int[] { 0, 60, 50, 0 });
mRenderer.setMarginsColor(Color.WHITE);
// mRenderer.setXTitle("Date");
mRenderer.setShowLegend(false);
mRenderer.setInScroll(true);
evolutionSecurity = new TimeSeries("");
mDataset.addSeries(evolutionSecurity);
fillDataEvolution(); //Get data into serie
mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
"dd-MM-yy");
LinearLayout layout = (LinearLayout) findViewById(R.id.graphe_evolution_rate_currency);
layout.addView(mChartView);
if (layout != null)
layout.removeAllViews();
layout.addView(mChartView);
mChartView.repaint();
mChartView = null;
}
Then when i select an another value in my spinner i call this method and the chart is redrawn correctly.
I'm trying to make a BarChart using achartengine, but so far, I haven't been able to get rid of the following effect : bar width is only correctly displayed when values are on the edge of the screen, but at startup, bars are much bigger and sometimes even hide the bar's value. Could this have something to do with my data set not being a continued serie of x values ? I have one point at x=17 then then next value is at x=24, and it seems the changing bar width happens between these 2 values. Can someone assist in making this graph look good ?
Thanks in advance for your help !
[The graph being shown on fragment's start]
[The graph being shown when scrolling to the left]
CODE TO MAKE THE GRAPH :
XYSeries xySerie = new XYSeries("Climb Level Statistics");
XYSeriesRenderer serieRenderer;
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
//Adding data to XYSerie and adding XYSerie to mDataset
//stats is an int[] containing level id, value for the level, level id, ...
//example of dataset used for image shown above: {17, 4, 24, 3, 25, 7, 26, 10, 27, 4}
int statsSize = stats.size();
double routeLevel;
double climbsQuantity;
for (int counter = 0; counter<statsSize; counter++){
routeLevel = stats.get(counter);
counter++;
climbsQuantity = stats.get(counter);
xySerie.add(routeLevel, climbsQuantity);
}
mDataset.addSeries(xySerie);
//Setting series renderer properties and adding it to the mRenderer
serieRenderer = new XYSeriesRenderer();
serieRenderer.setChartValuesTextSize(28);
serieRenderer.setDisplayChartValues(true);
mRenderer.addSeriesRenderer(serieRenderer);
//Setting main renderer properties
mRenderer.setApplyBackgroundColor(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
mRenderer.setBackgroundColor(Color.TRANSPARENT);
mRenderer.setLabelsTextSize(15);
mRenderer.setMargins(new int[] { 30, 30, 30, 30 });
mRenderer.setZoomButtonsVisible(false);
mRenderer.setPanLimits(new double[]{0,48, 0, 5000});
mRenderer.setBarSpacing(1.5);
mRenderer.setLabelsTextSize(28);
mRenderer.setLabelsColor(Color.BLACK);
mRenderer.setXLabels(0);
mRenderer.setXLabelsColor(Color.BLACK);
String[] routeLevels = context.getResources().getStringArray(R.array.route_levels);
int routeLevelsSize = routeLevels.length;
for (int x=1;x<routeLevelsSize+1;x++){
mRenderer.addXTextLabel(x, routeLevels[x-1]);
}
mRenderer.setYLabels(0);
mRenderer.setYAxisMin(0);
mRenderer.setShowLegend(false);
mRenderer.setZoomEnabled(false);
mRenderer.setZoomEnabled(false, false);
mRenderer.setPanEnabled(true, false);
GraphicalView graphView = ChartFactory.getBarChartView(context, mDataset, mRenderer, BarChart.Type.STACKED);
graphView.setBackgroundColor(Color.argb(255, 247, 247, 247));
I was able to resolve the changing bar width issue by ensuring the serie was a "perfect" suite for x values, changing
this : {17, 4, 24, 3, 25, 7, 26, 10, 27, 4}
to this : {17, 4, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 3, 25, 7, 26, 10, 27, 4}
To achieve that, I modified my first for loop to check for missing x values :
for (int x = 0; x<statsSize; x++){
routeLevel = currentLevel;
x++;
currentLevel++;
climbsQuantity = stats.get(x);
xySerie.add(routeLevel, climbsQuantity);
//if not at the last x/y couple of the array, fill the blanks in the serie
if (x<statsSize-1){
while (stats.get(x+1)>currentLevel){
routeLevel = currentLevel;
climbsQuantity = 0;
xySerie.add(routeLevel, climbsQuantity);
currentLevel++;
}
}
}
Now the bar width doesn't change at all when I scroll. This is more a workaround, though, because now the "filling" couples I added are all showing 0 as value, which I find a bit odd. If I find a way to hide these 0 values, I'll edit my answer...
CURRENT APPEARANCE OF THE GRAPH
First of all, you can set the visible area by changing the X axis min and max:
renderer.setXAxisMin(min);
renderer.setXAxisMax(max);
For changing the bars width, you have a few options.
If your dataset contain more than one element per series then I suggest you use renderer.setBarSpacing().
If there is only one element in your series then you can use renderer.setBarWidth().
First i give my View show below. and give some explanation:
In my ViewGroup i have two GraphicalView, each of them share the same size space. Let's just call the chart above chartA and below one chartB.
Then i have some questions:
If i make a move a movement on chartA ,how could i make the chartB move just as my finger moving on chartB?
If i pinch on chartA can also chartB change auto?
I want to add some callback function to some points? Does ACE supports this?
I do not want to show numbers negative while user pinching.How can I make this point?
Last is my chart code:
mDataset.addSeries(series);
PointStyle style = PointStyle.CIRCLE;
renderer = buildRenderer(lineColor, style, true);
setChartSettings(renderer, "X", "Y", 0, 50, yMin, yMax, Color.WHITE,
Color.WHITE, title,chartColor);
GraphicalView chart = ChartFactory.getLineChartView(context, mDataset, renderer);
layout.addView(chart, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
HashMap map = new HashMap();
and the renderer:
protected XYMultipleSeriesRenderer buildRenderer(int color,
PointStyle style,
boolean fill) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(color);
r.setPointStyle(style);
r.setFillPoints(fill);
r.setLineWidth(3);
renderer.addSeriesRenderer(r);
return renderer;
}
I random all the data i want.
You have to register a PanListener on your chartA and on every panApplied event, just do: rendererB.setXAxisMin(rendererA.getXAxisMin()); and the same for max X and for the Y axis and then call chartB.repaint();
Same as the above, just that you need to add a ZoomListener.
You can set a click listener: chartA.setOnClickListener();
Please reformulate into a separate question.
Also, see this code showing you how to use the above APIs.
I want to create XY plot with positive and negative numbers on both axys. By default achartnegine shows negative values of series but the start popint is always zero^ graph start from zero and next nubmer is something like -50, -40 etc. I have tried to avoid this but haven;t succed yet. Could you please suggest the right approach?
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesXY);
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
//renderer.setXAxisMin(-10f);
//renderer.setYAxisMin(-250f);
//renderer.setBarSpacing(SPACING);
//renderer.setRange(new double[] { -250d, 250d});
XYSeriesRenderer sRenderer = new XYSeriesRenderer();
sRenderer.setColor(Color.BLUE);
sRenderer.setFillBelowLine(false);
sRenderer.setChartValuesSpacing(SPACING);
sRenderer.setFillBelowLineColor(Color.WHITE);
sRenderer.setFillPoints(false);
renderer.addSeriesRenderer(sRenderer);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);
renderer.setMarginsColor(Color.WHITE);
renderer.setAxesColor(Color.BLACK);
renderer.setXLabelsColor(Color.BLUE);
renderer.setYLabelsColor(0, Color.BLUE);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
GraphicalView chartView = ChartFactory.getLineChartView(
getActivity(),
dataset, renderer);
plot.removeAllViews();
plot.addView(chartView, new LayoutParams(
LayoutParams.MATCH_PARENT, 200));
chartView.repaint();
I use LineChartView
The answer is that you cannot put the axis in the center of the chart with AChartEngine as it is out of the box.