Search in sources :

Example 6 with ITitle

use of org.swtchart.ITitle in project linuxtools by eclipse.

the class AbstractChartWithAxisBuilder method applyTitleBoundsListener.

/**
 * After this method is called, the chart's title will (from then on) be centered with the plot area.
 * @since 3.0
 */
protected void applyTitleBoundsListener() {
    ITitle title = chart.getTitle();
    // once the title has been altered.
    if (title instanceof Control) {
        titleBoundsPaintListener = e -> {
            Rectangle bounds = chart.getPlotArea().getBounds();
            Control title1 = (Control) chart.getTitle();
            Rectangle titleBounds = title1.getBounds();
            title1.setLocation(new Point(bounds.x + (bounds.width - titleBounds.width) / 2, title1.getLocation().y));
        };
        chart.addPaintListener(titleBoundsPaintListener);
    } else {
        // move title paint listener to end
        chart.removePaintListener((PaintListener) title);
        titleBoundsPaintListener = e -> {
            ITitle title1 = chart.getTitle();
            Font font = title1.getFont();
            Font oldFont = e.gc.getFont();
            e.gc.setFont(font);
            Control legend = (Control) chart.getLegend();
            Rectangle legendBounds = legend.getBounds();
            int adjustment = legendBounds.width - 15;
            // $NON-NLS-1$
            Point blankSize = e.gc.textExtent(" ");
            int numBlanks = ((adjustment / blankSize.x) >> 1) << 1;
            String text = title1.getText().trim();
            for (int i = 0; i < numBlanks; ++i) {
                // $NON-NLS-1$
                text += " ";
            }
            e.gc.setFont(oldFont);
            title1.setText(text);
        };
        chart.addPaintListener(titleBoundsPaintListener);
        chart.addPaintListener((PaintListener) title);
    }
}
Also used : Control(org.eclipse.swt.widgets.Control) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) ITitle(org.swtchart.ITitle) Font(org.eclipse.swt.graphics.Font) Point(org.eclipse.swt.graphics.Point)

Example 7 with ITitle

use of org.swtchart.ITitle in project linuxtools by eclipse.

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 Chart 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);
        Chart chart = new Chart(view.getParent(), SWT.NONE);
        chart.setBackground(WHITE);
        chart.setBackgroundInPlotArea(GRAD);
        chart.getTitle().setText(title);
        chart.getTitle().setForeground(BLACK);
        // 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.swtchart.ITitle) IAxis(org.swtchart.IAxis) ChartView(org.eclipse.linuxtools.internal.dataviewers.charts.view.ChartView) IBarSeries(org.swtchart.IBarSeries) PartInitException(org.eclipse.ui.PartInitException) PieChart(org.eclipse.linuxtools.dataviewers.piechart.PieChart) Chart(org.swtchart.Chart)

Aggregations

ITitle (org.swtchart.ITitle)7 IAxis (org.swtchart.IAxis)4 Color (org.eclipse.swt.graphics.Color)3 Font (org.eclipse.swt.graphics.Font)3 Point (org.eclipse.swt.graphics.Point)3 Chart (org.swtchart.Chart)3 Rectangle (org.eclipse.swt.graphics.Rectangle)2 Control (org.eclipse.swt.widgets.Control)2 IAxisSet (org.swtchart.IAxisSet)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 PieChart (org.eclipse.linuxtools.dataviewers.piechart.PieChart)1 ChartView (org.eclipse.linuxtools.internal.dataviewers.charts.view.ChartView)1 MassifSnapshot (org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 FontData (org.eclipse.swt.graphics.FontData)1 PartInitException (org.eclipse.ui.PartInitException)1