public class BarChart

  1. Object
  2. AbstractChart
  3. XYChart
  4. BarChart

Known subtypesRangeBarChart

Renders a bar chart based on an XYMultipleSeriesDataset.

Create an instance together with a matching XYMultipleSeriesRenderer and wrap it in a com.codename1.charts.ChartComponent to display it in the UI. A minimal setup looks like this:

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(mySeries);

XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.addSeriesRenderer(new XYSeriesRenderer());

BarChart chart = new BarChart(dataset, renderer, BarChart.Type.DEFAULT);
Form form = new Form(new BorderLayout());
form.add(BorderLayout.CENTER, new ChartComponent(chart));
form.show();

The Type supplied to the constructor controls whether the bars are rendered in their default style, stacked or heaped.

Nested types

enum BarChart.TypeThe bar chart type enum.

Fields

public static final String TYPE = "Bar"The constant to identify this chart type.
protected BarChart.Type mTypeThe chart type.

Constructors

public BarChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)Builds a new bar chart instance.

Methods

protected ClickableArea[] clickableAreasForPoints(List<Float> points, List<Double> values, float yAxisValue, int seriesIndex, int startIndex)Returns the clickable areas for all passed points
public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex)The graphical representation of a series.
protected void drawBar(Canvas canvas, float xMin, float yMin, float xMax, float yMax, float halfDiffX, int seriesNr, int seriesIndex, Paint paint)Draws a bar.
protected void drawBar(Canvas canvas, float xMin, float yMin, float xMax, float yMax, int scale, int seriesIndex, Paint paint)Draws a bar.
protected int getGradientPartialColor(int minColor, int maxColor, float fraction)
protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRenderer renderer, Paint paint, List<Float> points, int seriesIndex, int startIndex)The graphical representation of the series values as text.
public int getLegendShapeWidth(int seriesIndex)Returns the legend shape width.
public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y, int seriesIndex, Paint paint)The graphical representation of the legend shape.
protected float getHalfDiffX(List<Float> points, int length, int seriesNr)Calculates and returns the half-distance in the graphical representation of 2 consecutive points.
protected float getCoeficient()Returns the value of a constant used to calculate the half-distance.
protected boolean isRenderNullValues()Returns if the chart should display the null values.
public double getDefaultMinimum()Returns the default axis minimum.
public String getChartType()Returns the chart type identifier.

Inherited fields

Inherited methods

Field details

TYPE

public static final String TYPE = "Bar"
The constant to identify this chart type.

mType

protected BarChart.Type mType
The chart type.

Constructor details

BarChart

public BarChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)
Builds a new bar chart instance.

Parameters

dataset XYMultipleSeriesDataset
the multiple series dataset
renderer XYMultipleSeriesRenderer
the multiple series renderer
type BarChart.Type
the bar chart type

Method details

clickableAreasForPoints

protected ClickableArea[] clickableAreasForPoints(List<Float> points, List<Double> values, float yAxisValue, int seriesIndex, int startIndex)
Returns the clickable areas for all passed points

Parameters

points List<Float>
the array of points
values List<Double>
the array of values of each point
yAxisValue float
the minimum value of the y axis
seriesIndex int
the index of the series to which the points belong
startIndex int
the start index of the rendering points

Returns

an array of rectangles with the clickable area

drawSeries

public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex)
The graphical representation of a series.

Parameters

canvas Canvas
the canvas to paint to
paint Paint
the paint to be used for drawing
points List<Float>
the array of points to be used for drawing the series
seriesRenderer XYSeriesRenderer
the series renderer
yAxisValue float
the minimum value of the y axis
seriesIndex int
the index of the series currently being drawn
startIndex int
the start index of the rendering points

drawBar

protected void drawBar(Canvas canvas, float xMin, float yMin, float xMax, float yMax, float halfDiffX, int seriesNr, int seriesIndex, Paint paint)
Draws a bar.

Parameters

canvas Canvas
the canvas
xMin float
the X axis minimum
yMin float
the Y axis minimum
xMax float
the X axis maximum
yMax float
the Y axis maximum
halfDiffX float
half the size of a bar
seriesNr int
the total number of series
seriesIndex int
the current series index
paint Paint
the paint

drawBar

protected void drawBar(Canvas canvas, float xMin, float yMin, float xMax, float yMax, int scale, int seriesIndex, Paint paint)
Draws a bar.

Parameters

canvas Canvas
the canvas
xMin float
the X axis minimum
yMin float
the Y axis minimum
xMax float
the X axis maximum
yMax float
the Y axis maximum
scale int
the scale index
seriesIndex int
the current series index
paint Paint
the paint

getGradientPartialColor

protected int getGradientPartialColor(int minColor, int maxColor, float fraction)

drawChartValuesText

protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRenderer renderer, Paint paint, List<Float> points, int seriesIndex, int startIndex)
The graphical representation of the series values as text.

Parameters

canvas Canvas
the canvas to paint to
series XYSeries
the series to be painted
renderer XYSeriesRenderer
the series renderer
paint Paint
the paint to be used for drawing
points List<Float>
the array of points to be used for drawing the series
seriesIndex int
the index of the series currently being drawn
startIndex int
the start index of the rendering points

getLegendShapeWidth

public int getLegendShapeWidth(int seriesIndex)
Returns the legend shape width.

Parameters

seriesIndex int
the series index

Returns

the legend shape width

drawLegendShape

public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y, int seriesIndex, Paint paint)
The graphical representation of the legend shape.

Parameters

canvas Canvas
the canvas to paint to
renderer SimpleSeriesRenderer
the series renderer
x float
the x value of the point the shape should be drawn at
y float
the y value of the point the shape should be drawn at
seriesIndex int
the series index
paint Paint
the paint to be used for drawing

getHalfDiffX

protected float getHalfDiffX(List<Float> points, int length, int seriesNr)
Calculates and returns the half-distance in the graphical representation of 2 consecutive points.

Parameters

points List<Float>
the points
length int
the points length
seriesNr int
the series number

Returns

the calculated half-distance value

getCoeficient

protected float getCoeficient()
Returns the value of a constant used to calculate the half-distance.

Returns

the constant value

isRenderNullValues

protected boolean isRenderNullValues()
Returns if the chart should display the null values.

Returns

if null values should be rendered

getDefaultMinimum

public double getDefaultMinimum()
Returns the default axis minimum.

Returns

the default axis minimum

getChartType

public String getChartType()
Returns the chart type identifier.

Returns

the chart type