Search in sources :

Example 1 with InteractiveChart

use of org.eclipse.swtchart.extensions.charts.InteractiveChart in project org.eclipse.linuxtools by eclipse-linuxtools.

the class ChartEditor method createPartControl.

@Override
public void createPartControl(Composite parent) {
    final ChartEditorInput input = (ChartEditorInput) getEditorInput();
    final HeapChart heapChart = input.getChart();
    control = new InteractiveChart(parent, SWT.FILL);
    heapChart.setChartControl(control);
    final Color LIGHTYELLOW = new Color(Display.getDefault(), 255, 255, 225);
    final Color WHITE = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
    final Color BLACK = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
    final Color RED = Display.getDefault().getSystemColor(SWT.COLOR_RED);
    final Color ORANGE = new Color(Display.getDefault(), 255, 165, 0);
    final Color GREEN = Display.getDefault().getSystemColor(SWT.COLOR_GREEN);
    final Color DARK_BLUE = new Color(Display.getDefault(), 64, 128, 128);
    final int TICK_GAP = 40;
    control.setBackground(WHITE);
    control.getPlotArea().setBackground(LIGHTYELLOW);
    control.setProposedSaveAsFilename(heapChart.title.substring(0, heapChart.title.indexOf(' ')));
    FontData fd = JFaceResources.getDialogFont().getFontData()[0];
    fd.setStyle(SWT.BOLD);
    Font font = new Font(Display.getDefault(), fd);
    fd.setHeight(fd.getHeight() + 2);
    Font titleFont = new Font(Display.getDefault(), fd);
    ITitle title = control.getTitle();
    title.setFont(titleFont);
    title.setForeground(BLACK);
    title.setText(heapChart.title);
    IAxis xAxis = control.getAxisSet().getXAxis(0);
    xAxis.getGrid().setStyle(LineStyle.NONE);
    xAxis.getTick().setForeground(BLACK);
    ITitle xTitle = xAxis.getTitle();
    xTitle.setFont(font);
    xTitle.setForeground(BLACK);
    xTitle.setText(heapChart.xUnits);
    IAxis yAxis = control.getAxisSet().getYAxis(0);
    yAxis.getGrid().setStyle(LineStyle.SOLID);
    yAxis.getTick().setForeground(BLACK);
    yAxis.getTick().setTickMarkStepHint(TICK_GAP);
    ITitle yTitle = yAxis.getTitle();
    yTitle.setFont(font);
    yTitle.setText(heapChart.yUnits);
    yTitle.setForeground(BLACK);
    control.getLegend().setPosition(SWT.BOTTOM);
    // data
    final ILineSeries lsUseful = (ILineSeries) control.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$;
    Messages.getString("HeapChart.Useful_Heap"));
    lsUseful.setXSeries(heapChart.time);
    lsUseful.setYSeries(heapChart.dataUseful);
    lsUseful.setSymbolType(PlotSymbolType.DIAMOND);
    lsUseful.setSymbolColor(RED);
    lsUseful.setLineColor(RED);
    final ILineSeries lsExtra = (ILineSeries) control.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$;
    Messages.getString("HeapChart.Extra_Heap"));
    lsExtra.setXSeries(heapChart.time);
    lsExtra.setYSeries(heapChart.dataExtra);
    lsExtra.setSymbolType(PlotSymbolType.DIAMOND);
    lsExtra.setSymbolColor(ORANGE);
    lsExtra.setLineColor(ORANGE);
    if (heapChart.dataStacks != null) {
        final ILineSeries lsStack = (ILineSeries) control.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$;
        Messages.getString("HeapChart.Stacks"));
        lsStack.setXSeries(heapChart.time);
        lsStack.setYSeries(heapChart.dataStacks);
        lsStack.setSymbolType(PlotSymbolType.DIAMOND);
        lsStack.setSymbolColor(DARK_BLUE);
        lsStack.setLineColor(DARK_BLUE);
    }
    final ILineSeries lsTotal = (ILineSeries) control.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$;
    Messages.getString("HeapChart.Total_Heap"));
    lsTotal.setXSeries(heapChart.time);
    lsTotal.setYSeries(heapChart.dataTotal);
    lsTotal.setSymbolType(PlotSymbolType.DIAMOND);
    lsTotal.setSymbolColor(GREEN);
    lsTotal.setLineColor(GREEN);
    // adjust axes
    control.getAxisSet().adjustRange();
    IAxisSet axisSet = control.getAxisSet();
    Range xRange = axisSet.getXAxis(0).getRange();
    Range yRange = axisSet.getYAxis(0).getRange();
    double xExtra = 0.05 * (xRange.upper - xRange.lower);
    double yExtra = 0.05 * (yRange.upper - yRange.lower);
    axisSet.getXAxis(0).setRange(new Range(xRange.lower, xRange.upper + xExtra));
    axisSet.getYAxis(0).setRange(new Range(yRange.lower, yRange.upper + yExtra));
    // listeners
    control.getPlotArea().getControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent e) {
            showView();
            TableViewer viewer = input.getView().getTableViewer();
            input.getView().setTopControl(viewer.getControl());
            Point p = new Point(e.x, e.y);
            int closest = 0;
            double d1, d2, d3, currMin;
            double globalMin = Double.MAX_VALUE;
            for (int i = 0; i < heapChart.time.length; i++) {
                // get distance from click event to data points for the given index
                d1 = distance(lsUseful.getPixelCoordinates(i), p);
                d2 = distance(lsExtra.getPixelCoordinates(i), p);
                d3 = distance(lsTotal.getPixelCoordinates(i), p);
                // find the closest data point to the click event
                currMin = Math.min(Math.min(d1, d2), d3);
                if (currMin < globalMin) {
                    closest = i;
                    globalMin = currMin;
                }
            }
            MassifSnapshot snapshot = (MassifSnapshot) viewer.getElementAt(closest);
            viewer.setSelection(new StructuredSelection(snapshot), true);
            if (e.count == 2 && snapshot.isDetailed()) {
                ChartLocationsDialog dialog = new ChartLocationsDialog(Display.getCurrent().getActiveShell());
                dialog.setInput(snapshot);
                if (dialog.open() == Window.OK) {
                    dialog.openEditorForResult();
                }
            }
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) Color(org.eclipse.swt.graphics.Color) FontData(org.eclipse.swt.graphics.FontData) ILineSeries(org.eclipse.swtchart.ILineSeries) MouseAdapter(org.eclipse.swt.events.MouseAdapter) MassifSnapshot(org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Point(org.eclipse.swt.graphics.Point) Range(org.eclipse.swtchart.Range) ITitle(org.eclipse.swtchart.ITitle) InteractiveChart(org.eclipse.swtchart.extensions.charts.InteractiveChart) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) IAxis(org.eclipse.swtchart.IAxis) IAxisSet(org.eclipse.swtchart.IAxisSet) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 2 with InteractiveChart

use of org.eclipse.swtchart.extensions.charts.InteractiveChart in project org.eclipse.linuxtools by eclipse-linuxtools.

the class ChartExportTest method testChartExportPNG.

@Test
public void testChartExportPNG() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testDefaults");
    IEditorInput input = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput();
    assertTrue("input must be ChartEditorInput", input instanceof ChartEditorInput);
    Composite control = ((ChartEditorInput) input).getChart().getChartControl();
    if (control.getSize().x == 0 || control.getSize().y == 0) {
        // Manually resize the composite to non-zero width/height so it can be saved
        control.setSize(10, 10);
    }
    InteractiveChart intChart = (InteractiveChart) control;
    for (IPath path : paths) {
        saveAsPath(intChart, path);
    }
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) Composite(org.eclipse.swt.widgets.Composite) IPath(org.eclipse.core.runtime.IPath) ChartEditorInput(org.eclipse.linuxtools.internal.valgrind.massif.charting.ChartEditorInput) IEditorInput(org.eclipse.ui.IEditorInput) InteractiveChart(org.eclipse.swtchart.extensions.charts.InteractiveChart) Test(org.junit.Test)

Example 3 with InteractiveChart

use of org.eclipse.swtchart.extensions.charts.InteractiveChart 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 4 with InteractiveChart

use of org.eclipse.swtchart.extensions.charts.InteractiveChart in project swtchart by eclipse.

the class InteractiveChartExample method createPartControl.

/*
	 * @see WorkbenchPart#createPartControl(Composite)
	 */
@Override
public void createPartControl(Composite parent) {
    parent.setLayout(new FillLayout());
    // create an interactive chart
    chart = new InteractiveChart(parent, SWT.NONE);
    // set title
    chart.getTitle().setText("Sample Interactive Chart");
    // set category series
    chart.getAxisSet().getXAxis(0).enableCategory(true);
    chart.getAxisSet().getXAxis(0).setCategorySeries(categorySeries);
    // create line series 1
    ILineSeries lineSeries1 = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "line series 1");
    lineSeries1.setYSeries(yLineSeries1);
    // create line series 2
    ILineSeries lineSeries2 = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "line series 2");
    lineSeries2.setYSeries(yLineSeries2);
    lineSeries2.setLineColor(Display.getDefault().getSystemColor(SWT.COLOR_RED));
    // create bar series 1
    IBarSeries barSeries1 = (IBarSeries) chart.getSeriesSet().createSeries(SeriesType.BAR, "bar series 1");
    barSeries1.setYSeries(yBarSeries1);
    // create bar series 2
    IBarSeries barSeries2 = (IBarSeries) chart.getSeriesSet().createSeries(SeriesType.BAR, "bar series 2");
    barSeries2.setYSeries(yBarSeries2);
    barSeries2.setBarColor(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
    // adjust the axis range
    chart.getAxisSet().adjustRange();
}
Also used : IBarSeries(org.eclipse.swtchart.IBarSeries) ILineSeries(org.eclipse.swtchart.ILineSeries) FillLayout(org.eclipse.swt.layout.FillLayout) InteractiveChart(org.eclipse.swtchart.extensions.charts.InteractiveChart)

Aggregations

InteractiveChart (org.eclipse.swtchart.extensions.charts.InteractiveChart)4 Color (org.eclipse.swt.graphics.Color)2 IAxis (org.eclipse.swtchart.IAxis)2 IBarSeries (org.eclipse.swtchart.IBarSeries)2 ILineSeries (org.eclipse.swtchart.ILineSeries)2 ITitle (org.eclipse.swtchart.ITitle)2 IPath (org.eclipse.core.runtime.IPath)1 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 IChartField (org.eclipse.linuxtools.dataviewers.charts.provider.IChartField)1 MassifSnapshot (org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot)1 ChartEditorInput (org.eclipse.linuxtools.internal.valgrind.massif.charting.ChartEditorInput)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 Font (org.eclipse.swt.graphics.Font)1 FontData (org.eclipse.swt.graphics.FontData)1 Point (org.eclipse.swt.graphics.Point)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 Composite (org.eclipse.swt.widgets.Composite)1