use of org.eclipse.swtchart.ISeries in project netxms by netxms.
the class LineChart method getClosestDataPoint.
/**
* Get data point closest to given point in plot area
*
* @param px
* @param py
* @return
*/
public DataPoint getClosestDataPoint(int px, int py) {
IAxis xAxis = getAxisSet().getXAxis(0);
IAxis yAxis = getAxisSet().getYAxis(0);
double x = xAxis.getDataCoordinate(px);
double y = yAxis.getDataCoordinate(py);
double closestX = 0;
double closestY = 0;
double minDist = Double.MAX_VALUE;
ISeries<?> closestSeries = null;
/* over all series */
ISeries<?>[] series = getSeriesSet().getSeries();
for (ISeries<?> s : series) {
double[] xS = s.getXSeries();
double[] yS = s.getYSeries();
/* check all data points */
for (int i = 0; i < xS.length; i++) {
/* compute distance to mouse position */
double newDist = Math.sqrt(Math.pow((x - xS[i]), 2) + Math.pow((y - yS[i]), 2));
/* if closer to mouse, remember */
if (newDist < minDist) {
minDist = newDist;
closestX = xS[i];
closestY = yS[i];
closestSeries = s;
}
}
}
return (closestSeries != null) ? new DataPoint(new Date((long) closestX), closestY, closestSeries) : null;
}
use of org.eclipse.swtchart.ISeries in project org.eclipse.linuxtools by eclipse-linuxtools.
the class PieChartPaintListener method getPieSeriesArray.
private void getPieSeriesArray() {
ISeries[] series = this.chart.getSeriesSet().getSeries();
if (series == null || series.length == 0) {
seriesValues = new double[0][0];
seriesNames = new String[0];
return;
}
String[] names = this.chart.getAxisSet().getXAxis(0).getCategorySeries();
Range range = chart.getAxisSet().getXAxis(0).getRange();
int itemRange = (int) range.upper - (int) range.lower + 1;
int itemOffset = (int) range.lower;
seriesValues = new double[itemRange][series.length];
seriesNames = new String[itemRange];
for (int i = 0; i < seriesValues.length; i++) {
seriesNames[i] = names[i + itemOffset];
for (int j = 0; j < seriesValues[i].length; j++) {
double[] d = series[j].getXSeries();
if (d != null && d.length > 0) {
seriesValues[i][j] = d[i + itemOffset];
} else {
seriesValues[i][j] = 0;
}
}
}
return;
}
use of org.eclipse.swtchart.ISeries in project org.eclipse.linuxtools by eclipse-linuxtools.
the class PieChart method addPieChartSeries.
/**
* Add data to this Pie Chart. We'll build one pie chart for each value in the array provided. The val matrix must
* have an array of an array of values. Ex. labels = {'a', 'b'} val = {{1,2,3}, {4,5,6}} This will create 3 pie
* charts. For the first one, 'a' will be 1 and 'b' will be 4. For the second chart 'a' will be 2 and 'b' will be 5.
* For the third 'a' will be 3 and 'b' will be 6.
* @param labels The titles of each series. (These are not the same as titles given to pies.)
* @param val New values.
*/
public void addPieChartSeries(String[] labels, double[][] val) {
setSeriesNames(val[0].length);
for (ISeries s : this.getSeriesSet().getSeries()) {
this.getSeriesSet().deleteSeries(s.getId());
}
int size = Math.min(labels.length, val.length);
for (int i = 0; i < size; i++) {
IBarSeries s = (IBarSeries) this.getSeriesSet().createSeries(ISeries.SeriesType.BAR, labels[i]);
double[] d = new double[val[i].length];
for (int j = 0; j < val[i].length; j++) {
d[j] = val[i][j];
}
s.setXSeries(d);
if (customColors != null) {
s.setBarColor(customColors[i % customColors.length]);
} else {
s.setBarColor(new Color(this.getDisplay(), sliceColor(i)));
}
}
}
use of org.eclipse.swtchart.ISeries in project org.eclipse.linuxtools by eclipse-linuxtools.
the class ChartWithAxisMouseMoveListener method mouseMove.
@Override
public void mouseMove(MouseEvent e) {
super.mouseMove(e);
double closestDistance = DIST_TOLERANCE;
int closestIndex = -1;
ISeries closestSeries = null;
Point closestPoint = null;
for (ISeries series : chart.getSeriesSet().getSeries()) {
for (int i = 0; i < series.getXSeries().length; i++) {
Point dataPoint = series.getPixelCoordinates(i);
if (dataPoint.x >= 0 && dataPoint.y >= 0) {
double dist = Math.sqrt(Math.pow(dataPoint.x - e.x, 2) + Math.pow(dataPoint.y - e.y, 2));
if (dist < closestDistance) {
closestDistance = dist;
closestIndex = i;
closestSeries = series;
closestPoint = dataPoint;
}
}
}
}
if (closestPoint != null) {
setTextTip(MessageFormat.format(Messages.AbstractChartWithAxisBuilder_ToolTipCoords, closestSeries.getId(), closestSeries.getXSeries()[closestIndex], closestSeries.getYSeries()[closestIndex]));
} else {
tipShell.setVisible(false);
}
}
use of org.eclipse.swtchart.ISeries in project org.eclipse.linuxtools by eclipse-linuxtools.
the class BarChartBuilder method buildXSeries.
@Override
protected void buildXSeries() {
Object[][] data = adapter.getData();
if (data == null || data.length == 0) {
return;
}
int start = 0, len = Math.min(this.maxItems, data.length), leny = data[0].length - 1;
if (this.maxItems < data.length) {
start = data.length - this.maxItems;
}
String[] allValx = new String[len];
Double[][] allValy = new Double[leny][len];
// Want to show x-axis if possible, so default max/min is 0.
double maxY = 0;
double minY = 0;
// If an x-axis category is empty, ignore the entire category.
for (int i = 0; i < len; i++) {
Object label = data[start + i][0];
if (label != null) {
allValx[i] = label.toString();
for (int j = 1; j < leny + 1; j++) {
Double val = getDoubleOrNullValue(data[start + i][j]);
if (val == null) {
val = 0.0;
}
allValy[j - 1][i] = val;
maxY = Math.max(val, maxY);
minY = Math.min(val, minY);
}
}
}
// Now create dense arrays of x/y values that exclude null values,
// and plot those values to the chart.
String[] valx = new String[len];
int lenTrim = 0;
for (int i = 0; i < len; i++) {
if (allValx[i] != null) {
valx[lenTrim] = allValx[i];
lenTrim++;
}
}
String[] valxTrim = new String[lenTrim];
System.arraycopy(valx, 0, valxTrim, 0, lenTrim);
ISeries[] allSeries = chart.getSeriesSet().getSeries();
for (int i = 0; i < leny; i++) {
ISeries series;
if (i >= allSeries.length) {
series = createChartISeries(i);
} else {
series = chart.getSeriesSet().getSeries()[i];
}
double[] valy = new double[len];
int lenyTrim = 0;
for (int j = 0; j < len; j++) {
if (allValy[i][j] != null) {
valy[lenyTrim] = allValy[i][j].doubleValue();
lenyTrim++;
}
}
double[] valyTrim = new double[lenyTrim];
System.arraycopy(valy, 0, valyTrim, 0, lenyTrim);
series.setYSeries(valyTrim);
}
((BarChart) chart).suspendUpdate(true);
((BarChart) chart).setCategorySeries(getUniqueNames(valxTrim));
applyCategoryRange(valxTrim.length);
applyRangeY(minY, maxY);
((BarChart) chart).suspendUpdate(false);
chart.redraw();
}
Aggregations