Class BarChart

Direct Known Subclasses:
RangeBarChart

public class BarChart extends XYChart

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.

  • Field Details

  • Constructor Details

  • Method Details

    • clickableAreasForPoints

      protected ClickableArea[] clickableAreasForPoints(List<Float> points, List<Double> values, float yAxisValue, int seriesIndex, int startIndex)
      Description copied from class: XYChart

      Returns the clickable areas for all passed points

      Parameters
      • points: the array of points

      • values: the array of values of each point

      • yAxisValue: the minimum value of the y axis

      • seriesIndex: the index of the series to which the points belong

      • startIndex: the start index of the rendering points

      Returns

      an array of rectangles with the clickable area

      Specified by:
      clickableAreasForPoints in class XYChart
    • 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: the canvas to paint to

      • paint: the paint to be used for drawing

      • points: the array of points to be used for drawing the series

      • seriesRenderer: the series renderer

      • yAxisValue: the minimum value of the y axis

      • seriesIndex: the index of the series currently being drawn

      • startIndex: the start index of the rendering points

      Specified by:
      drawSeries in class XYChart
    • 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: the canvas

      • xMin: the X axis minimum

      • yMin: the Y axis minimum

      • xMax: the X axis maximum

      • yMax: the Y axis maximum

      • halfDiffX: half the size of a bar

      • seriesNr: the total number of series

      • seriesIndex: the current series index

      • 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: the canvas

      • xMin: the X axis minimum

      • yMin: the Y axis minimum

      • xMax: the X axis maximum

      • yMax: the Y axis maximum

      • scale: the scale index

      • seriesIndex: the current series index

      • 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: the canvas to paint to

      • series: the series to be painted

      • renderer: the series renderer

      • paint: the paint to be used for drawing

      • points: the array of points to be used for drawing the series

      • seriesIndex: the index of the series currently being drawn

      • startIndex: the start index of the rendering points

      Overrides:
      drawChartValuesText in class XYChart
    • getLegendShapeWidth

      public int getLegendShapeWidth(int seriesIndex)

      Returns the legend shape width.

      Parameters
      • seriesIndex: the series index
      Returns

      the legend shape width

      Specified by:
      getLegendShapeWidth in class AbstractChart
    • 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: the canvas to paint to

      • renderer: the series renderer

      • x: the x value of the point the shape should be drawn at

      • y: the y value of the point the shape should be drawn at

      • seriesIndex: the series index

      • paint: the paint to be used for drawing

      Specified by:
      drawLegendShape in class AbstractChart
    • 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: the points

      • length: the points length

      • seriesNr: 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

      Overrides:
      isRenderNullValues in class XYChart
    • getDefaultMinimum

      public double getDefaultMinimum()

      Returns the default axis minimum.

      Returns

      the default axis minimum

      Overrides:
      getDefaultMinimum in class XYChart
    • getChartType

      public String getChartType()

      Returns the chart type identifier.

      Returns

      the chart type

      Specified by:
      getChartType in class XYChart