Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to customize the line chart in afreechart library in android
    text
    copied!<p>I have downloaded the <em>AFreeChart</em> library and a sample for it. I have downloaded the zip file using <a href="https://code.google.com/p/afreechart/downloads/list" rel="nofollow">this link</a>.</p> <p>I need to customize the application. By default they are displaying the chart in the complete screen. now i want to create a button below that chart. So please suggest me how to customize the code. </p> <p>The code for the chart is below.</p> <pre class="lang-java prettyprint-override"><code>public class AFreeChart implements Drawable, TitleChangeListener, PlotChangeListener, Serializable, Cloneable { /** For serialization. */ private static final long serialVersionUID = -3470703747817429120L; /** The default font for titles. */ public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Typeface.BOLD, 18); /** The default background color. */ public static final PaintType DEFAULT_BACKGROUND_PAINT = new SolidColor( Color.LTGRAY); /** The default background image alignment. */ public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT; /** The default background image alpha. */ public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f; /** A flag that controls whether or not the chart border is drawn. */ private boolean borderVisible; /** The stroke used to draw the chart border (if visible). */ private transient float borderStroke; /** The paint used to draw the chart border (if visible). */ private transient PaintType borderPaintType; /** The padding between the chart border and the chart drawing area. */ private RectangleInsets padding; /** The chart title (optional). */ private TextTitle title; /** The default border effect. */ public static final PathEffect DEFAULT_BORDER_EFFECT = null; /** The effect used to draw the chart border (if visible). */ private transient PathEffect borderEffect; /** * The chart subtitles (zero, one or many). This field should never be * &lt;code&gt;null&lt;/code&gt;. */ private List subtitles; /** Draws the visual representation of the data. */ private Plot plot; /** Paint used to draw the background of the chart. */ private transient PaintType backgroundPaintType; /** Storage for registered change listeners. */ private transient List&lt;ChartChangeListener&gt; changeListeners; /** Storage for registered progress listeners. */ private transient List&lt;ChartProgressListener&gt; progressListeners; private boolean notify; private Button b; public AFreeChart(String title, Font titleFont, Plot plot, boolean createLegend) { if (plot == null) { throw new NullPointerException("Null 'plot' argument."); } // create storage for listeners... this.progressListeners = new CopyOnWriteArrayList&lt;ChartProgressListener&gt;(); this.changeListeners = new CopyOnWriteArrayList&lt;ChartChangeListener&gt;(); this.notify = true; // default is to notify listeners when the this.borderVisible = true; this.borderStroke = 2; this.borderPaintType = new SolidColor(Color.WHITE); this.borderEffect = DEFAULT_BORDER_EFFECT; this.plot = plot; plot.addChangeListener(this); this.subtitles = new ArrayList(); // create a legend, if requested... if (createLegend) { LegendTitle legend = new LegendTitle(this.plot); legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); legend.setFrame(new LineBorder()); PaintType paintType = new SolidColor(Color.WHITE); legend.setBackgroundPaintType(paintType); legend.setPosition(RectangleEdge.BOTTOM); this.subtitles.add(legend); legend.addChangeListener(this); } // add the chart title, if one has been specified... if (title != null) { if (titleFont == null) { titleFont = DEFAULT_TITLE_FONT; } this.title = new TextTitle(title, titleFont); this.title.addChangeListener(this); } this.backgroundPaintType = DEFAULT_BACKGROUND_PAINT; } public AFreeChart(String title, Plot plot) { this(title, AFreeChart.DEFAULT_TITLE_FONT, plot, true); } public boolean isBorderVisible() { return this.borderVisible; } public void setBorderVisible(boolean visible) { this.borderVisible = visible; fireChartChanged(); } public float getBorderStroke() { return this.borderStroke; } public void setBorderStroke(float stroke) { this.borderStroke = stroke; fireChartChanged(); } public PaintType getBorderPaintType() { return this.borderPaintType; } public void setBorderPaintType(PaintType paintType) { this.borderPaintType = paintType; fireChartChanged(); } public RectangleInsets getPadding() { return this.padding; } /** * Sets the padding between the chart border and the chart drawing area, and * sends a {@link ChartChangeEvent} to all registered listeners. * * @param padding * the padding (&lt;code&gt;null&lt;/code&gt; not permitted). * * @see #getPadding() */ public void setPadding(RectangleInsets padding) { if (padding == null) { throw new IllegalArgumentException("Null 'padding' argument."); } this.padding = padding; notifyListeners(new ChartChangeEvent(this)); } public TextTitle getTitle() { return this.title; } public void setTitle(TextTitle title) { if (this.title != null) { this.title.removeChangeListener(this); } this.title = title; if (title != null) { title.addChangeListener(this); } fireChartChanged(); } public void setBorderEffect(PathEffect effect) { this.borderEffect = effect; fireChartChanged(); } public void setTitle(String text) { if (text != null) { if (this.title == null) { setTitle(new TextTitle(text, AFreeChart.DEFAULT_TITLE_FONT)); } else { this.title.setText(text); } } else { setTitle((TextTitle) null); } } public void addLegend(LegendTitle legend) { addSubtitle(legend); } public void addSubtitle(Title subtitle) { if (subtitle == null) { throw new IllegalArgumentException("Null 'subtitle' argument."); } this.subtitles.add(subtitle); subtitle.addChangeListener(this); fireChartChanged(); } public void addSubtitle(int index, Title subtitle) { if (index &lt; 0 || index &gt; getSubtitleCount()) { throw new IllegalArgumentException( "The 'index' argument is out of range."); } if (subtitle == null) { throw new IllegalArgumentException("Null 'subtitle' argument."); } this.subtitles.add(index, subtitle); subtitle.addChangeListener(this); fireChartChanged(); } public LegendTitle getLegend() { return getLegend(0); } public LegendTitle getLegend(int index) { int seen = 0; Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title subtitle = (Title) iterator.next(); if (subtitle instanceof LegendTitle) { if (seen == index) { return (LegendTitle) subtitle; } else { seen++; } } } return null; } public void removeLegend() { removeSubtitle(getLegend()); } public void removeSubtitle(Title title) { this.subtitles.remove(title); fireChartChanged(); } public List getSubtitles() { return new ArrayList(this.subtitles); } public void setSubtitles(List subtitles) { if (subtitles == null) { throw new NullPointerException("Null 'subtitles' argument."); } // setNotify(false); clearSubtitles(); Iterator iterator = subtitles.iterator(); while (iterator.hasNext()) { Title t = (Title) iterator.next(); if (t != null) { addSubtitle(t); } } } public void clearSubtitles() { Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title t = (Title) iterator.next(); t.removeChangeListener(this); } this.subtitles.clear(); fireChartChanged(); } public int getSubtitleCount() { return this.subtitles.size(); } public Title getSubtitle(int index) { if ((index &lt; 0) || (index &gt;= getSubtitleCount())) { throw new IllegalArgumentException("Index out of range."); } return (Title) this.subtitles.get(index); } public Plot getPlot() { return this.plot; } public CategoryPlot getCategoryPlot() { return (CategoryPlot) this.plot; } public XYPlot getXYPlot() { return (XYPlot) this.plot; } public PathEffect getBorderEffect() { return this.borderEffect; } public PaintType getBackgroundPaintType() { return this.backgroundPaintType; } public void setBackgroundPaintType(PaintType paintType) { if (this.backgroundPaintType != null) { if (!this.backgroundPaintType.equals(paintType)) { this.backgroundPaintType = paintType; fireChartChanged(); } } else { if (paintType != null) { this.backgroundPaintType = paintType; fireChartChanged(); } } } public void draw(Canvas canvas, RectShape area) { draw(canvas, area, null, null); } public void draw(Canvas canvas, RectShape area, ChartRenderingInfo info) { draw(canvas, area, null, info); } public void draw(Canvas canvas, RectShape chartArea, PointF anchor, ChartRenderingInfo info) { EntityCollection entities = null; notifyListeners(new ChartProgressEvent(this, this, ChartProgressEvent.DRAWING_STARTED, 0)); // record the chart area, if info is requested... if (info != null) { info.clear(); info.setChartArea(chartArea); entities = info.getEntityCollection(); } if (entities != null) { entities.add(new AFreeChartEntity((RectShape) chartArea.clone(), this)); } Rect savedClip = canvas.getClipBounds(); // ensure no drawing occurs outside chart area... canvas.clipRect((float) chartArea.getMinX(), (float) chartArea.getMinY(), (float) chartArea.getMaxX(), (float) chartArea.getMaxY()); // draw the chart background... if (this.backgroundPaintType != null) { Paint paint = PaintUtility.createPaint(Paint.ANTI_ALIAS_FLAG, this.backgroundPaintType); chartArea.fill(canvas, paint); } if (isBorderVisible()) { PaintType paintType = getBorderPaintType(); if (paintType != null) { RectShape borderArea = new RectShape(chartArea.getX(), chartArea.getY(), chartArea.getWidth() - 1.0, chartArea.getHeight() - 1.0); Paint paint = PaintUtility.createPaint(borderPaintType, borderStroke, borderEffect); borderArea.draw(canvas, paint); } } // draw the title and subtitles... RectShape nonTitleArea = new RectShape(); nonTitleArea.setRect(chartArea); if (this.padding != null) this.padding.trim(nonTitleArea); if (this.title != null) { EntityCollection e = drawTitle(this.title, canvas, nonTitleArea, (entities != null)); if (e != null) { entities.addAll(e); } } Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title currentTitle = (Title) iterator.next(); if (currentTitle.isVisible()) { EntityCollection e = drawTitle(currentTitle, canvas, nonTitleArea, (entities != null)); if (e != null) { entities.addAll(e); } } } RectShape plotArea = nonTitleArea; // draw the plot (axes and data visualisation) PlotRenderingInfo plotInfo = null; if (info != null) { plotInfo = info.getPlotInfo(); } this.plot.draw(canvas, plotArea, anchor, null, plotInfo); canvas.clipRect(savedClip, Op.REPLACE); notifyListeners(new ChartProgressEvent(this, this, ChartProgressEvent.DRAWING_FINISHED, 100)); } private RectShape createAlignedRectShape(Size2D dimensions, RectShape frame, HorizontalAlignment hAlign, VerticalAlignment vAlign) { double x = Double.NaN; double y = Double.NaN; if (hAlign == HorizontalAlignment.LEFT) { x = frame.getX(); } else if (hAlign == HorizontalAlignment.CENTER) { x = frame.getCenterX() - (dimensions.width / 2.0); } else if (hAlign == HorizontalAlignment.RIGHT) { x = frame.getMaxX() - dimensions.width; } if (vAlign == VerticalAlignment.TOP) { y = frame.getY(); } else if (vAlign == VerticalAlignment.CENTER) { y = frame.getCenterY() - (dimensions.height / 2.0); } else if (vAlign == VerticalAlignment.BOTTOM) { y = frame.getMaxY() - dimensions.height; } return new RectShape(x, y, dimensions.width, dimensions.height); } protected EntityCollection drawTitle(Title t, Canvas canvas, RectShape area, boolean entities) { if (t == null) { throw new IllegalArgumentException("Null 't' argument."); } if (area == null) { throw new IllegalArgumentException("Null 'area' argument."); } RectShape titleArea = new RectShape(); RectangleEdge position = t.getPosition(); double ww = area.getWidth(); if (ww &lt;= 0.0) { return null; } double hh = area.getHeight(); if (hh &lt;= 0.0) { return null; } RectangleConstraint constraint = new RectangleConstraint(ww, new Range( 0.0, ww), LengthConstraintType.RANGE, hh, new Range(0.0, hh), LengthConstraintType.RANGE); Object retValue = null; BlockParams p = new BlockParams(); p.setGenerateEntities(entities); if (position == RectangleEdge.TOP) { Size2D size = t.arrange(canvas, constraint); titleArea = createAlignedRectShape(size, area, t.getHorizontalAlignment(), VerticalAlignment.TOP); retValue = t.draw(canvas, titleArea, p); area.setRect(area.getX(), Math.min(area.getY() + size.height, area.getMaxY()), area.getWidth(), Math.max(area.getHeight() - size.height, 0)); } else if (position == RectangleEdge.BOTTOM) { Size2D size = t.arrange(canvas, constraint); titleArea = createAlignedRectShape(size, area, t.getHorizontalAlignment(), VerticalAlignment.BOTTOM); retValue = t.draw(canvas, titleArea, p); area.setRect(area.getX(), area.getY(), area.getWidth(), area.getHeight() - size.height); } else if (position == RectangleEdge.RIGHT) { Size2D size = t.arrange(canvas, constraint); titleArea = createAlignedRectShape(size, area, HorizontalAlignment.RIGHT, t.getVerticalAlignment()); retValue = t.draw(canvas, titleArea, p); area.setRect(area.getX(), area.getY(), area.getWidth() - size.width, area.getHeight()); } else if (position == RectangleEdge.LEFT) { Size2D size = t.arrange(canvas, constraint); titleArea = createAlignedRectShape(size, area, HorizontalAlignment.LEFT, t.getVerticalAlignment()); retValue = t.draw(canvas, titleArea, p); area.setRect(area.getX() + size.width, area.getY(), area.getWidth() - size.width, area.getHeight()); } else { throw new RuntimeException("Unrecognised title position."); } EntityCollection result = null; if (retValue instanceof EntityBlockResult) { EntityBlockResult ebr = (EntityBlockResult) retValue; result = ebr.getEntityCollection(); } return result; } public void handleClick(int x, int y, ChartRenderingInfo info) { // pass the click on to the plot... // rely on the plot to post a plot change event and redraw the chart... this.plot.handleClick(x, y, info.getPlotInfo()); } public void addChangeListener(ChartChangeListener listener) { if (listener == null) { throw new IllegalArgumentException("Null 'listener' argument."); } this.changeListeners.add(listener); } public void removeChangeListener(ChartChangeListener listener) { if (listener == null) { throw new IllegalArgumentException("Null 'listener' argument."); } this.changeListeners.remove(listener); } public void fireChartChanged() { ChartChangeEvent event = new ChartChangeEvent(this); notifyListeners(event); } protected void notifyListeners(ChartChangeEvent event) { if (changeListeners.size() == 0) { return; } if (this.notify) { for (int i = changeListeners.size() - 1; i &gt;= 0; i--) { changeListeners.get(i).chartChanged(event); } } } public void addProgressListener(ChartProgressListener listener) { this.progressListeners.add(listener); } public void removeProgressListener(ChartProgressListener listener) { this.progressListeners.remove(listener); } protected void notifyListeners(ChartProgressEvent event) { if (progressListeners.size() == 0) { return; } for (int i = progressListeners.size() - 1; i &gt;= 0; i--) { progressListeners.get(i).chartProgress(event); } } public void titleChanged(TitleChangeEvent event) { event.setChart(this); notifyListeners(event); } public void plotChanged(PlotChangeEvent event) { event.setChart(this); notifyListeners(event); } public Object clone() throws CloneNotSupportedException { AFreeChart chart = (AFreeChart) super.clone(); // chart.renderingHints = (RenderingHints) this.renderingHints.clone(); // private boolean borderVisible; // private transient Stroke borderStroke; // private transient Paint borderPaint; if (this.title != null) { chart.title = (TextTitle) this.title.clone(); chart.title.addChangeListener(chart); } chart.subtitles = new ArrayList(); for (int i = 0; i &lt; getSubtitleCount(); i++) { Title subtitle = (Title) getSubtitle(i).clone(); chart.subtitles.add(subtitle); subtitle.addChangeListener(chart); } if (this.plot != null) { chart.plot = (Plot) this.plot.clone(); chart.plot.addChangeListener(chart); } chart.progressListeners = new CopyOnWriteArrayList&lt;ChartProgressListener&gt;(); chart.changeListeners = new CopyOnWriteArrayList&lt;ChartChangeListener&gt;(); return chart; } public void setNotify(boolean notify) { this.notify = notify; // if the flag is being set to true, there may be queued up changes... if (notify) { notifyListeners(new ChartChangeEvent(this)); } } public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof AFreeChart)) { return false; } AFreeChart that = (AFreeChart) obj; // if (!this.renderingHints.equals(that.renderingHints)) { // return false; // } if (this.borderVisible != that.borderVisible) { return false; } if (!ObjectUtilities.equal(this.borderStroke, that.borderStroke)) { return false; } if (!PaintTypeUtilities.equal(this.borderPaintType, that.borderPaintType)) { return false; } if (!ObjectUtilities.equal(this.title, that.title)) { return false; } if (!ObjectUtilities.equal(this.subtitles, that.subtitles)) { return false; } if (!ObjectUtilities.equal(this.plot, that.plot)) { return false; } if (!PaintTypeUtilities.equal(this.backgroundPaintType, that.backgroundPaintType)) { return false; } if (this.notify != that.notify) { return false; } return true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload