Search in sources :

Example 36 with IAxis

use of org.eclipse.swtchart.IAxis 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;
}
Also used : DataPoint(org.netxms.nxmc.modules.charts.api.DataPoint) ISeries(org.eclipse.swtchart.ISeries) IAxis(org.eclipse.swtchart.IAxis) DataPoint(org.netxms.nxmc.modules.charts.api.DataPoint) Point(org.eclipse.swt.graphics.Point) Date(java.util.Date)

Example 37 with IAxis

use of org.eclipse.swtchart.IAxis in project org.eclipse.linuxtools by eclipse-linuxtools.

the class ChartFactory method produceBarChart.

/**
 * Produces a 2D bar chart from the input objects.
 *
 * @param objects
 *            the input data
 * @param nameField
 *            the field used to get the labels of the objects (the labels of the series groups).
 * @param valFields
 *            the fields providing the values for the different bars in a series group.
 * @param title Title of the chart.
 * @param horizontal
 *            if true the bars are displayed horizontally, else vertically.
 * @return a new 2D bar chart
 */
public static InteractiveChart produceBarChart(Object[] objects, final ISTDataViewersField nameField, List<IChartField> valFields, String title, boolean horizontal) {
    ChartView view;
    try {
        final Color WHITE = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_WHITE);
        final Color BLACK = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_BLACK);
        final Color GRAD = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
        view = (ChartView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ChartView.VIEW_ID, String.valueOf(ChartView.getSecId()), IWorkbenchPage.VIEW_ACTIVATE);
        InteractiveChart chart = new InteractiveChart(view.getParent(), SWT.NONE);
        chart.setBackground(WHITE);
        chart.getPlotArea().setBackground(GRAD);
        chart.getTitle().setText(title);
        chart.getTitle().setForeground(BLACK);
        chart.setProposedSaveAsFilename(title.replace(' ', '_'));
        // this is correct (refers to orientation of x-axis, not bars)
        if (horizontal) {
            chart.setOrientation(SWT.VERTICAL);
        } else {
            chart.setOrientation(SWT.HORIZONTAL);
        }
        chart.getLegend().setPosition(SWT.RIGHT);
        String[] textLabels = new String[objects.length];
        for (int i = 0; i < objects.length; i++) {
            textLabels[i] = nameField.getValue(objects[i]);
        }
        // x-axis
        IAxis xAxis = chart.getAxisSet().getXAxis(0);
        xAxis.getGrid().setStyle(LineStyle.NONE);
        xAxis.getTick().setForeground(BLACK);
        ITitle xTitle = xAxis.getTitle();
        xTitle.setForeground(BLACK);
        xTitle.setText(nameField.getColumnHeaderText());
        xAxis.setCategorySeries(textLabels);
        xAxis.enableCategory(true);
        // y-axis
        IAxis yAxis = chart.getAxisSet().getYAxis(0);
        yAxis.getGrid().setStyle(LineStyle.NONE);
        yAxis.getTick().setForeground(BLACK);
        yAxis.getTitle().setVisible(false);
        // data
        for (IChartField field : valFields) {
            final IBarSeries bs = (IBarSeries) chart.getSeriesSet().createSeries(SeriesType.BAR, field.getColumnHeaderText());
            bs.setBarColor(new Color(Display.getDefault(), getRC(), getRC(), getRC()));
            double[] doubleValues = new double[objects.length];
            for (int i = 0; i < objects.length; i++) {
                Number num = field.getNumber(objects[i]);
                double longVal = num.doubleValue();
                doubleValues[i] = longVal;
            }
            bs.setYSeries(doubleValues);
        }
        chart.getAxisSet().adjustRange();
        return chart;
    } catch (PartInitException e) {
        Activator.getDefault().getLog().log(e.getStatus());
    }
    return null;
}
Also used : Color(org.eclipse.swt.graphics.Color) ITitle(org.eclipse.swtchart.ITitle) InteractiveChart(org.eclipse.swtchart.extensions.charts.InteractiveChart) IAxis(org.eclipse.swtchart.IAxis) IBarSeries(org.eclipse.swtchart.IBarSeries) IChartField(org.eclipse.linuxtools.dataviewers.charts.provider.IChartField) PartInitException(org.eclipse.ui.PartInitException)

Example 38 with IAxis

use of org.eclipse.swtchart.IAxis in project org.eclipse.linuxtools by eclipse-linuxtools.

the class PieChart method setSeriesNames.

/**
 * Sets this chart's category names such that the number of names
 * is equal to the number of pies. This method will only make changes
 * to category series if they are not already properly set.
 * @param numExpected The number of pies / the expected number of category names.
 */
private void setSeriesNames(int numExpected) {
    IAxis xAxis = getAxisSet().getXAxis(0);
    if (xAxis.getCategorySeries().length != numExpected) {
        String[] seriesNames = new String[numExpected];
        for (int i = 0, n = Math.min(xAxis.getCategorySeries().length, numExpected); i < n; i++) {
            seriesNames[i] = xAxis.getCategorySeries()[i];
        }
        for (int i = xAxis.getCategorySeries().length; i < numExpected; i++) {
            // $NON-NLS-1$
            seriesNames[i] = "";
        }
        xAxis.setCategorySeries(seriesNames);
    }
}
Also used : IAxis(org.eclipse.swtchart.IAxis)

Example 39 with IAxis

use of org.eclipse.swtchart.IAxis in project org.eclipse.linuxtools by eclipse-linuxtools.

the class AbstractChartWithAxisBuilder method buildXAxis.

/**
 * Builds X axis.
 */
@Override
protected void buildXAxis() {
    String[] labels = adapter.getLabels();
    IAxis xAxis = this.chart.getAxisSet().getXAxis(0);
    if (xLineGrid) {
        xAxis.getGrid().setStyle(LineStyle.SOLID);
    } else {
        xAxis.getGrid().setStyle(LineStyle.NONE);
    }
    xAxis.getTick().setForeground(BLACK);
    xAxis.getTick().setTickMarkStepHint(xSeriesTicks);
    ITitle xTitle = xAxis.getTitle();
    xTitle.setForeground(BLACK);
    if (labels.length > 0) {
        xTitle.setText(labels[0]);
    } else {
        // $NON-NLS-1$
        xTitle.setText("");
    }
}
Also used : ITitle(org.eclipse.swtchart.ITitle) IAxis(org.eclipse.swtchart.IAxis)

Example 40 with IAxis

use of org.eclipse.swtchart.IAxis in project org.eclipse.linuxtools by eclipse-linuxtools.

the class PieChartBuilder method buildXAxis.

@Override
protected void buildXAxis() {
    String[] labels = adapter.getLabels();
    String[] seriesLabels = new String[labels.length - 1];
    for (int i = 0; i < seriesLabels.length; i++) {
        seriesLabels[i] = labels[i + 1];
    }
    IAxis xAxis = this.chart.getAxisSet().getXAxis(0);
    xAxis.getTitle().setText(labels[0]);
    xAxis.setCategorySeries(seriesLabels);
}
Also used : IAxis(org.eclipse.swtchart.IAxis)

Aggregations

IAxis (org.eclipse.swtchart.IAxis)64 Point (org.eclipse.swt.graphics.Point)27 Range (org.eclipse.swtchart.Range)19 IAxisSet (org.eclipse.swtchart.IAxisSet)10 Chart (org.eclipse.swtchart.Chart)7 ISeries (org.eclipse.swtchart.ISeries)7 MouseEvent (org.eclipse.swt.events.MouseEvent)5 ILineSeries (org.eclipse.swtchart.ILineSeries)5 Color (org.eclipse.swt.graphics.Color)4 GridData (org.eclipse.swt.layout.GridData)4 DecimalFormat (java.text.DecimalFormat)3 PaintEvent (org.eclipse.swt.events.PaintEvent)3 PaintListener (org.eclipse.swt.events.PaintListener)3 GC (org.eclipse.swt.graphics.GC)3 Rectangle (org.eclipse.swt.graphics.Rectangle)3 Composite (org.eclipse.swt.widgets.Composite)3 IBarSeries (org.eclipse.swtchart.IBarSeries)3 ITitle (org.eclipse.swtchart.ITitle)3 Axis (org.eclipse.swtchart.internal.axis.Axis)3 StyleRange (org.eclipse.swt.custom.StyleRange)2