All Packages Class Hierarchy This Package Previous Next Index
Class com.objectplanet.chart.LineChart
java.lang.Object
|
+----java.awt.Component
|
+----com.objectplanet.chart.Chart
|
+----com.objectplanet.chart.LineChart
- public class LineChart
- extends Chart
This class implements a line chart component. The line chart can have
several series of data, and draws a line for each serie. One serie
consists of a set of values which are connected by a line. The user
can select a serie by clicking the serie label on the legend.
The following attributes can be set:
- the chart title
- the chart title font
- the chart legend position
- the legend label color
- the chart background color
- the chart foreground color
- the upper range of the chart.
- the lower range of the chart.
- the number of decimals used for the range value labels
- the number of series in the chart
- the label for each series
- the color of each series
- the sample labels
- target value lines can be set with specified colors
You can turn on or off the following features:
- the chart title
- the legend
- the value lines
- the sample labels
- the range adjuster
The following code creates a line chart with 4 series with 10 samples each:
// create the chart and the samples
LineChart chart = new LineChart(4, 10, 1000);
long[] samples1 = new long[] {200, 300, 100, 400, 600, 500, 1000, 800, 700, 900};
long[] samples2 = new long[] {500, 200, 500, 300, 700, 300, 800, 900, 700, 600};
long[] samples3 = new long[] {600, 700, 900, 800, 400, 700, 400, 500, 300, 500};
long[] samples4 = new long[] {600, 700, 900, 800, 400, 700, 400, 500, 300, 500};
// set the samples for each of the series
chart.setSampleValues(0, samples1);
chart.setSampleValues(1, samples2);
chart.setSampleValues(2, samples3);
chart.setSampleValues(3, samples4);
// set the label for each of the series
chart.setSeriesLabel(0, "series 1");
chart.setSeriesLabel(1, "series 2");
chart.setSeriesLabel(2, "series 3");
chart.setSeriesLabel(3, "series 4");
// set the sample labels for the x-axis
for (int i = 0; i < chart.samples.length; i++) {
chart.setSampleLabel(i, "T"+i);
}
// set the chart title and turn on decired features
chart.setTitle("Line Chart");
chart.setTitleOn(true);
chart.setLegendOn(true);
chart.setValueLinesOn(true);
chart.setRangeAdjusterOn(true);
chart.setSampleLabelsOn(true);

- the range is set to 1000
- all the 4 samples range from 100 to 1000
- the series are named "series 1", "series 2", "series 3", and "series 4"
- the x axis sample labels are set to "T0", "T1", "T2", ..., "T9"
- the chart title is set to "Line Chart"
- chart title is turned on
- series legend is turned on
- value lines turned on
- sample labels turned on
- range adjuster turned on
- serie 2 is selected by the user
- Author:
- Bjorn J. Kvande.
-
TARGET_LINE_ID_AND_VALUE_LABEL
- Use both the ID and value label with the target lines.
-
TARGET_LINE_ID_LABEL
- Use the ID label with the target lines.
-
TARGET_LINE_NO_LABEL
- Use no label with the target lines.
-
TARGET_LINE_VALUE_LABEL
- Use the value label with the target lines.
-
LineChart()
- Creates a line chart with one series, 5 samples, and a range of 0-100.
-
LineChart(int, int, double)
- Creates a line chart with the specified number of series,
the number of samples per serie, and the chart range.
-
LineChart(int, int, double, double)
- Creates a line chart with the specified number of series,
the number of samples per serie, and the chart range.
-
appendSampleValue(int, double, boolean)
- Appends a sample value after the current last value in the given series.
-
checkSelection(Point)
- Checks if the given point is inside a sample.
-
getFloatLowerRange()
- Gets the lower range of the chart as a floating point value.
-
getFloatMaxValue()
- Gets the largest value in the chart as a floating point value.
-
getFloatMinValue()
- Gets the lowest value in the chart as a floating point value.
-
getFloatRange()
- Gets the upper range of the chart as a floating point value.
-
getFloatSampleValue(int, int)
- Gets the specified sample in the given series as floating point.
-
getFloatSampleValues(int)
- Gets the sample values for the specified series as floating point numbers.
-
getGraphBounds()
- Calculates the bounding rectangle of the graph itself,
based on which features are on and how they are configured.
-
getLineColor()
- Gets the color of the line in use when only one series is in use.
Deprecated.
-
getLowerRange()
- Gets the lower range of the chart.
-
getRange()
- Gets the upper range of the chart.
-
getRangeDecimalCount()
- Gets the number of decimals used in the y-axis range values.
-
getSample(int, int)
- Gets the specified sample from the given series.
-
getSamples(int)
- Gets the samples from one series.
-
getSampleValue(int, int)
- Gets the specified sample in the given series as an integer number.
-
getSampleValues(int)
- Gets the sample values for the specified series.
-
getSeriesCount()
- Returns the number of series in the chart.
-
getSeriesLabel(int)
- Gets the label of the given series.
-
getSeriesLabels()
- Gets the series labels as an array of strings.
-
getTargetValueLine(String)
- Gets the value of a target value line.
-
getValueLinesColor()
- Gets the color of the value lines.
-
getValuePosition(double)
- Gets the position in the chart of the specified value.
-
isRangeAdjusterOn()
- Checks if the range adjuster is turned on.
-
isRangeLabelsOn()
- Checks if the range labels are on or off.
-
isSampleLabelsOn()
- Checks if the sample labels are turned on.
-
isValueLinesOn()
- Checks if the value lines are turned on.
-
render(Graphics)
- Renders the line chart according to the set attributes
and the current features that are turned on.
-
setLineColor(Color)
- Sets the line color when only one series is in use.
-
setLowerRange(double)
- Sets the lower range of the chart.
-
setLowerRelativeRange(double)
- Sets the lower range of the chart relative to the current minimum value.
-
setRange(double)
- Sets the upper range of the chart.
-
setRangeAdjusterOn(boolean)
- Turns on or off the range adjuster.
-
setRangeDecimalCount(int)
- Sets the number of decimals to be used in the y-axis range values.
-
setRangeLabelsOn(boolean)
- Turns on or off the range value labels.
-
setRelativeRange(double)
- Sets the upper range of the chart relative to the maximum value of the lines
in the chart.
-
setSampleColors(Color[])
- Sets the sample colors.
-
setSampleCount(int)
- Sets the number of samples for the chart.
-
setSampleLabelsOn(boolean)
- Turns on the sample labels on the x-axis.
-
setSampleValue(int, int, double)
- Sets on sample in the given serie.
-
setSampleValues(int, double[])
- Sets the sample values of the given series as floating point values.
-
setSampleValues(int, long[])
- Sets the sample values of the given series.
-
setSeriesCount(int)
- Sets the number of series in the chart.
-
setSeriesLabel(int, String)
- Sets the label of the given series.
-
setSeriesLabels(String[])
- Sets the labels for the series.
-
setTargetValueLine(String, double, Color, int)
- Sets a target value line with the specified ID, value, and color.
-
setValueLinesColor(Color)
- Sets the color of the value lines in the chart.
-
setValueLinesOn(boolean)
- Turns on or off the horizontal value lines.
TARGET_LINE_NO_LABEL
public static final int TARGET_LINE_NO_LABEL
- Use no label with the target lines.
TARGET_LINE_ID_LABEL
public static final int TARGET_LINE_ID_LABEL
- Use the ID label with the target lines.
TARGET_LINE_VALUE_LABEL
public static final int TARGET_LINE_VALUE_LABEL
- Use the value label with the target lines.
TARGET_LINE_ID_AND_VALUE_LABEL
public static final int TARGET_LINE_ID_AND_VALUE_LABEL
- Use both the ID and value label with the target lines.
LineChart
public LineChart()
- Creates a line chart with one series, 5 samples, and a range of 0-100.
LineChart
public LineChart(int seriesCount,
int sampleCount,
double range)
- Creates a line chart with the specified number of series,
the number of samples per serie, and the chart range.
- Parameters:
- seriesCount - The number of sample series.
- sampleCount - The number of samples per series.
- range - The upper range of the chart.
LineChart
public LineChart(int seriesCount,
int sampleCount,
double range,
double lowerRange)
- Creates a line chart with the specified number of series,
the number of samples per serie, and the chart range.
- Parameters:
- seriesCount - The number of sample series.
- sampleCount - The number of samples per series.
- range - The upper range of the chart.
- lowerRange - The lower range of the chart.
getSamples
public ChartSample[] getSamples(int serie)
- Gets the samples from one series.
- Parameters:
- serie - The index of the serie to get the samples from (0-based).
- Returns:
- An array of the chart samples.
getSample
public ChartSample getSample(int serie,
int index)
- Gets the specified sample from the given series.
- Parameters:
- serie - The index of the series to get the sample from (0-based).
- index - The index of the sample to get (0-based).
setSampleValues
public synchronized void setSampleValues(int serie,
long values[])
- Sets the sample values of the given series. This will be the
sample values for one whole line in the chart.
- Parameters:
- serie - The index of the series to set (0-based).
- values - The values to set as an array of longs.
setSampleValues
public synchronized void setSampleValues(int serie,
double values[])
- Sets the sample values of the given series as floating point values.
This will be the sample values for one whole line in the chart.
If the chart contains more samples than the number of values put in,
any trailing sample values will be set to null.
- Parameters:
- serie - The index of the series to set (0-based).
- values - The values to set as an array of longs.
getSampleValues
public long[] getSampleValues(int serie)
- Gets the sample values for the specified series.
- Parameters:
- serie - The index of the serie to get the samples for.
- Returns:
- An array containing the integer values.
getFloatSampleValues
public double[] getFloatSampleValues(int serie)
- Gets the sample values for the specified series as floating point numbers.
- Parameters:
- serie - The index of the serie to get the samples for.
- Returns:
- A double array containing the floating point values.
setSampleValue
public synchronized void setSampleValue(int serie,
int index,
double value)
- Sets on sample in the given serie. This sets one point on a line.
- Parameters:
- serie - The index of the series to set (0-based).
- index - The index of the sample to set (0-based).
- value - The value to set.
getSampleValue
public long getSampleValue(int serie,
int index)
- Gets the specified sample in the given series as an integer number.
- Parameters:
- serie - The index of the series to get (0-based).
- index - The index of the sample to get (0-based).
getFloatSampleValue
public double getFloatSampleValue(int serie,
int index)
- Gets the specified sample in the given series as floating point.
- Parameters:
- serie - The index of the series to get (0-based).
- index - The index of the sample to get (0-based).
appendSampleValue
public synchronized void appendSampleValue(int serie,
double value,
boolean makeSpace)
- Appends a sample value after the current last value in the given series.
If the current number of values in the series is less than the sample
count of the chart, the new value will be appended after the last value
set. If all the values are set in the chart, the behavior depends on the
makeSpace parameter.
- Parameters:
- serie - The series to append the value to.
- value - The value to append.
- makeSpace - True if the the sample count should be increased,
false if the rest of the samples should scroll.
setSeriesLabels
public void setSeriesLabels(String labels[])
- Sets the labels for the series. The labels are given as an array
where the first label is the label of the first serie and so on.
- Parameters:
- labels - The labels as a string array.
getSeriesLabels
public String[] getSeriesLabels()
- Gets the series labels as an array of strings.
setSeriesLabel
public void setSeriesLabel(int serie,
String label)
- Sets the label of the given series. This is the label in the legend.
- Parameters:
- serie - The index of the series (0-based).
- label - The label of the series.
getSeriesLabel
public String getSeriesLabel(int serie)
- Gets the label of the given series.
- Parameters:
- serie - The index of the series (0-based).
setRange
public synchronized void setRange(double range)
- Sets the upper range of the chart. The upper range is the maximum
value on the value axis, and the lines are adjusted according to this
value. The range can be set to lower than the largest value of a line,
but the top of the line will be cut off at the top of the chart.
- Parameters:
- range - The range to set.
getRange
public synchronized long getRange()
- Gets the upper range of the chart.
getFloatRange
public synchronized double getFloatRange()
- Gets the upper range of the chart as a floating point value.
setLowerRange
public synchronized void setLowerRange(double range)
- Sets the lower range of the chart. The lower range is the minimum
value on the value axis, and the lines are adjusted according to this
value. The range can be set to larger than the lowest value of a line,
but the bottom of the line will be cut off at the bottom of the chart.
- Parameters:
- range - The range to set.
getLowerRange
public synchronized long getLowerRange()
- Gets the lower range of the chart.
getFloatLowerRange
public synchronized double getFloatLowerRange()
- Gets the lower range of the chart as a floating point value.
setRelativeRange
public synchronized void setRelativeRange(double range)
- Sets the upper range of the chart relative to the maximum value of the lines
in the chart. If the maximum value is 1000, and the upper range is set to
1.5, the new range will be 1500, and the lines will be adjusted according
to the new range. If all the values are negative, the upper range will
be set to 0.
- Parameters:
- range - The relative range (1.0->);
setLowerRelativeRange
public synchronized void setLowerRelativeRange(double range)
- Sets the lower range of the chart relative to the current minimum value.
If the lowest value in one line is -1000, and the lower range is set to
1.5, the new lower range will then be -1500. If all the values are
positive, the lower range will be set to 0.
- Parameters:
- range - The relative range;
setRangeDecimalCount
public void setRangeDecimalCount(int count)
- Sets the number of decimals to be used in the y-axis range values.
- Parameters:
- count - The number of decimals to use.
getRangeDecimalCount
public int getRangeDecimalCount()
- Gets the number of decimals used in the y-axis range values.
getFloatMaxValue
public double getFloatMaxValue()
- Gets the largest value in the chart as a floating point value.
This will be the largest value of all the series.
- Returns:
- The maximum value, if no samples are present, 0 is returned.
- Overrides:
- getFloatMaxValue in class Chart
getFloatMinValue
public double getFloatMinValue()
- Gets the lowest value in the chart as a floating point value.
This will be the lowest value of all the series.
- Returns:
- The smalles value found, or 0 if no samples were found.
- Overrides:
- getFloatMinValue in class Chart
checkSelection
protected ChartSample checkSelection(Point point)
- Checks if the given point is inside a sample. This is called
by the selection code when the user clicks the mouse button.
- Parameters:
- point - The point to check for.
- Returns:
- The sample selected, or null if none was selected.
- Overrides:
- checkSelection in class Chart
setSampleCount
public synchronized void setSampleCount(int count)
- Sets the number of samples for the chart. If the number of samples are
less than the current count, the samples with index larger than the new
count is discarded. If the new count is larger then the current, new
samples are created and initialized to zero.
- Parameters:
- count - The number of samples.
- Overrides:
- setSampleCount in class Chart
setSeriesCount
public void setSeriesCount(int count)
- Sets the number of series in the chart. The number of series is the
number of lines in the chart. If the current number of series is
larger than the new one, the above series will be discarted. If the
new series count is larger than the current one, new series is
created with zero values and no labels.
- Parameters:
- count - The number of series.
getSeriesCount
public int getSeriesCount()
- Returns the number of series in the chart. This is the number of lines.
setSampleColors
public void setSampleColors(Color colors[])
- Sets the sample colors. If the number of samples is larger than the
number of colors, the colors of the samples will cycle through the array.
To use the default colors, use NULL as a parameter.
- Parameters:
- colors - An array with the colors to be used, null for default colors.
- Overrides:
- setSampleColors in class Chart
getValuePosition
public int getValuePosition(double value)
- Gets the position in the chart of the specified value. If the alignment
of the bars is VERTICAL the position represents the y-axis. If the
alignment of the bars is HORIZONTAL the position represents the x-axis.
- Parameters:
- value - The value to find the position for.
- Returns:
- The pixel position.
setTargetValueLine
public void setTargetValueLine(String id,
double value,
Color color,
int style)
- Sets a target value line with the specified ID, value, and color.
- Parameters:
- id - The id of the target line.
- value - The value where the line should appear.
- color - The color of the line, if null, the line is removed.
- style - TARGET_LINE_NO_LABEL, TARGET_LINE_ID_LABEL,
TARGET_LINE_VALUE_LABEL, or TARGET_LINE_ID_AND_VALUE_LABEL.
getTargetValueLine
public double getTargetValueLine(String id)
- Gets the value of a target value line.
- Parameters:
- id - The id of the target line.
- Returns:
- The value of the line.
setValueLinesOn
public void setValueLinesOn(boolean on)
- Turns on or off the horizontal value lines. There are initially 20 lines
in the chart, but the number of lines are halved if there is not enough
room for the lines, and then halved again until there is enough room.
- Parameters:
- on - True for on, false for off.
isValueLinesOn
public boolean isValueLinesOn()
- Checks if the value lines are turned on.
- Returns:
- True if they are turned on, false otherwise.
setRangeLabelsOn
public void setRangeLabelsOn(boolean on)
- Turns on or off the range value labels. They are on by default.
- Parameters:
- on - True for on, false for off.
isRangeLabelsOn
public boolean isRangeLabelsOn()
- Checks if the range labels are on or off.
- Returns:
- True for on, false for off.
setSampleLabelsOn
public void setSampleLabelsOn(boolean on)
- Turns on the sample labels on the x-axis. The sample labels are
for the first series only, and will be the same for all the series.
These labels are not the same as the ones in the legend.
- Parameters:
- on - True for on, false for off.
isSampleLabelsOn
public boolean isSampleLabelsOn()
- Checks if the sample labels are turned on.
- Returns:
- True if they are turned on, false otherwise.
setRangeAdjusterOn
public void setRangeAdjusterOn(boolean on)
- Turns on or off the range adjuster. When this is on, the user
can adjust the range to see any low-value lines more clearly.
- Parameters:
- on - True for on, false for off.
- See Also:
- setRange
isRangeAdjusterOn
public boolean isRangeAdjusterOn()
- Checks if the range adjuster is turned on.
- Returns:
- True if turned on, false otherwise.
setLineColor
public void setLineColor(Color color)
- Sets the line color when only one series is in use.
- Parameters:
- color - The color to set.
- deprecated - Use setSampleColor(0, color) instead.
getLineColor
public Color getLineColor()
- Note: getLineColor() is deprecated.
Use getSampleColor(0) instead.
- Gets the color of the line in use when only one series is in use.
setValueLinesColor
public void setValueLinesColor(Color color)
- Sets the color of the value lines in the chart.
- Parameters:
- color - The color to use.
getValueLinesColor
public Color getValueLinesColor()
- Gets the color of the value lines.
render
public void render(Graphics g)
- Renders the line chart according to the set attributes
and the current features that are turned on.
and GUI state values.
- Overrides:
- render in class Chart
getGraphBounds
public Rectangle getGraphBounds()
- Calculates the bounding rectangle of the graph itself,
based on which features are on and how they are configured.
All Packages Class Hierarchy This Package Previous Next Index